diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-05-22 14:44:59 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-05-22 14:44:59 +0900 |
| commit | 9be81e74291a22272b4261c806063bb3187588a6 (patch) | |
| tree | 5e70218fde0b41426de1c559c6daa3668e8ea5a1 /src/class.c | |
| parent | 00f2144692cbad6ba8190235867f3964d2727246 (diff) | |
| download | mruby-9be81e74291a22272b4261c806063bb3187588a6.tar.gz mruby-9be81e74291a22272b4261c806063bb3187588a6.zip | |
call Class#initialize from Class.new; ref ISO 15.2.3.3.1
Diffstat (limited to 'src/class.c')
| -rw-r--r-- | src/class.c | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/class.c b/src/class.c index 0a1327089..a5fe36c41 100644 --- a/src/class.c +++ b/src/class.c @@ -1095,18 +1095,30 @@ mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *arg } static mrb_value +mrb_class_initialize(mrb_state *mrb, mrb_value c) +{ + mrb_value a, b; + + mrb_get_args(mrb, "|C&", &a, &b); + if (!mrb_nil_p(b)) { + mrb_yield_with_class(mrb, b, 1, &c, c, mrb_class_ptr(c)); + } + return c; +} + +static mrb_value mrb_class_new_class(mrb_state *mrb, mrb_value cv) { + mrb_int n; mrb_value super, blk; mrb_value new_class; - if (mrb_get_args(mrb, "|C&", &super, &blk) == 0) { + n = mrb_get_args(mrb, "|C&", &super, &blk); + if (n == 0) { super = mrb_obj_value(mrb->object_class); } new_class = mrb_obj_value(mrb_class_new(mrb, mrb_class_ptr(super))); - if (!mrb_nil_p(blk)) { - mrb_yield_with_class(mrb, blk, 1, &new_class, new_class, mrb_class_ptr(new_class)); - } + mrb_funcall_with_block(mrb, new_class, mrb_intern_lit(mrb, "initialize"), n, &super, blk); mrb_funcall(mrb, super, "inherited", 1, new_class); return new_class; } @@ -1963,9 +1975,10 @@ mrb_init_class(mrb_state *mrb) mrb_define_method(mrb, bob, "!", mrb_bob_not, MRB_ARGS_NONE()); mrb_define_method(mrb, bob, "method_missing", mrb_bob_missing, MRB_ARGS_ANY()); /* 15.3.1.3.30 */ - mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, MRB_ARGS_ANY()); + mrb_define_class_method(mrb, cls, "new", mrb_class_new_class, MRB_ARGS_OPT(1)); mrb_define_method(mrb, cls, "superclass", mrb_class_superclass, MRB_ARGS_NONE()); /* 15.2.3.3.4 */ mrb_define_method(mrb, cls, "new", mrb_instance_new, MRB_ARGS_ANY()); /* 15.2.3.3.3 */ + mrb_define_method(mrb, cls, "initialize", mrb_class_initialize, MRB_ARGS_OPT(1)); /* 15.2.3.3.1 */ mrb_define_method(mrb, cls, "inherited", mrb_bob_init, MRB_ARGS_REQ(1)); MRB_SET_INSTANCE_TT(mod, MRB_TT_MODULE); |
