Browse files

remove #new from immediate classes

  • Loading branch information...
1 parent 0e5958d commit 3e8d330c2cca7b67edbfa2a03331c03499dbdbf5 @matz matz committed May 31, 2012
Showing with 21 additions and 9 deletions.
  1. +1 −0 include/mruby.h
  2. +10 −4 src/class.c
  3. +2 −0 src/numeric.c
  4. +3 −0 src/object.c
  5. +5 −5 test/t/nil.rb
View
1 include/mruby.h
@@ -294,6 +294,7 @@ void mrb_define_class_method(mrb_state *, struct RClass *, const char *, mrb_fun
void mrb_define_singleton_method(mrb_state*, struct RObject*, const char*, mrb_func_t,int);
void mrb_define_module_function(mrb_state*, struct RClass*, const char*, mrb_func_t,int);
void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value);
+void mrb_undef_method(mrb_state*, struct RClass*, const char*);
mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super);
struct RClass * mrb_module_new(mrb_state *mrb);
View
14 src/class.c
@@ -1100,8 +1100,8 @@ mrb_mod_alias(mrb_state *mrb, mrb_value mod)
}
-void
-mrb_undef_method(mrb_state *mrb, struct RClass *c, mrb_sym a)
+static void
+undef_method(mrb_state *mrb, struct RClass *c, mrb_sym a)
{
mrb_value m;
@@ -1110,6 +1110,12 @@ mrb_undef_method(mrb_state *mrb, struct RClass *c, mrb_sym a)
mrb_define_method_vm(mrb, c, a, m);
}
+void
+mrb_undef_method(mrb_state *mrb, struct RClass *c, const char *name)
+{
+ undef_method(mrb, c, mrb_intern(mrb, name));
+}
+
mrb_value
mrb_mod_undef(mrb_state *mrb, mrb_value mod)
{
@@ -1119,7 +1125,7 @@ mrb_mod_undef(mrb_state *mrb, mrb_value mod)
mrb_get_args(mrb, "*", &argv, &argc);
while (argc--) {
- mrb_undef_method(mrb, c, mrb_symbol(*argv));
+ undef_method(mrb, c, mrb_symbol(*argv));
argv++;
}
return mrb_nil_value();
@@ -1170,7 +1176,7 @@ mrb_init_class(mrb_state *mrb)
mrb_name_class(mrb, mod, mrb_intern(mrb, "Module"));
mrb_name_class(mrb, cls, mrb_intern(mrb, "Class"));
- mrb_undef_method(mrb, mod, mrb_intern(mrb, "new"));
+ mrb_undef_method(mrb, mod, "new");
MRB_SET_INSTANCE_TT(cls, MRB_TT_CLASS);
mrb_define_method(mrb, bob, "initialize", mrb_bob_init, ARGS_NONE());
mrb_define_method(mrb, bob, "!", mrb_bob_not, ARGS_NONE());
View
2 src/numeric.c
@@ -1266,6 +1266,7 @@ mrb_init_numeric(mrb_state *mrb)
integer = mrb_define_class(mrb, "Integer", numeric);
fixnum = mrb->fixnum_class = mrb_define_class(mrb, "Fixnum", integer);
+ mrb_undef_method(mrb, fixnum, "new");
mrb_define_method(mrb, fixnum, "+", mrb_fixnum_plus, ARGS_REQ(1)); /* 15.2.8.3.1 */
mrb_define_method(mrb, fixnum, "-", mrb_fixnum_minus, ARGS_REQ(1)); /* 15.2.8.3.2 */
mrb_define_method(mrb, fixnum, "-@", fix_uminus, ARGS_REQ(1)); /* 15.2.7.4.2 */
@@ -1293,6 +1294,7 @@ mrb_init_numeric(mrb_state *mrb)
/* Float Class */
fl = mrb->float_class = mrb_define_class(mrb, "Float", numeric);
+ mrb_undef_method(mrb, fl, "new");
mrb_define_method(mrb, fl, "+", mrb_float_plus, ARGS_REQ(1)); /* 15.2.9.3.1 */
mrb_define_method(mrb, fl, "-", flo_minus, ARGS_REQ(1)); /* 15.2.9.3.2 */
mrb_define_method(mrb, fl, "*", flo_mul, ARGS_REQ(1)); /* 15.2.9.3.3 */
View
3 src/object.c
@@ -290,19 +290,22 @@ mrb_init_object(mrb_state *mrb)
struct RClass *f;
n = mrb->nil_class = mrb_define_class(mrb, "NilClass", mrb->object_class);
+ mrb_undef_method(mrb, n, "new");
mrb_define_method(mrb, n, "&", false_and, ARGS_REQ(1)); /* 15.2.4.3.1 */
mrb_define_method(mrb, n, "^", false_xor, ARGS_REQ(1)); /* 15.2.4.3.2 */
mrb_define_method(mrb, n, "|", false_or, ARGS_REQ(1)); /* 15.2.4.3.3 */
mrb_define_method(mrb, n, "nil?", mrb_true, ARGS_NONE()); /* 15.2.4.3.4 */
mrb_define_method(mrb, n, "to_s", nil_to_s, ARGS_NONE()); /* 15.2.4.3.5 */
t = mrb->true_class = mrb_define_class(mrb, "TrueClass", mrb->object_class);
+ mrb_undef_method(mrb, n, "new");
mrb_define_method(mrb, t, "&", true_and, ARGS_REQ(1)); /* 15.2.5.3.1 */
mrb_define_method(mrb, t, "^", true_xor, ARGS_REQ(1)); /* 15.2.5.3.2 */
mrb_define_method(mrb, t, "to_s", true_to_s, ARGS_NONE()); /* 15.2.5.3.3 */
mrb_define_method(mrb, t, "|", true_or, ARGS_REQ(1)); /* 15.2.5.3.4 */
f = mrb->false_class = mrb_define_class(mrb, "FalseClass", mrb->object_class);
+ mrb_undef_method(mrb, n, "new");
mrb_define_method(mrb, f, "&", false_and, ARGS_REQ(1)); /* 15.2.6.3.1 */
mrb_define_method(mrb, f, "^", false_xor, ARGS_REQ(1)); /* 15.2.6.3.2 */
mrb_define_method(mrb, f, "to_s", false_to_s, ARGS_NONE()); /* 15.2.6.3.3 */
View
10 test/t/nil.rb
@@ -6,21 +6,21 @@
end
assert('NilClass#&', '15.2.4.3.1') do
- not NilClass.new.& and not NilClass.new.&(nil)
+ not nil.&(true) and not nil.&(nil)
end
assert('NilClass#^', '15.2.4.3.2') do
- NilClass.new.^(true) and not NilClass.new.^(false)
+ nil.^(true) and not nil.^(false)
end
assert('NilClass#|', '15.2.4.3.3') do
- NilClass.new.|(true) and not NilClass.new.|(false)
+ nil.|(true) and not nil.|(false)
end
assert('NilClass#nil?', '15.2.4.3.4') do
- NilClass.new.nil?
+ nil.nil?
end
assert('NilClass#to_s', '15.2.4.3.5') do
- NilClass.new.to_s == ''
+ nil.to_s == ''
end

0 comments on commit 3e8d330

Please sign in to comment.