call Class#initialize from Class.new; ref ISO 15.2.3.3.1

parent 00f21446
...@@ -1094,19 +1094,31 @@ mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *arg ...@@ -1094,19 +1094,31 @@ mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *arg
return obj; return obj;
} }
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 static mrb_value
mrb_class_new_class(mrb_state *mrb, mrb_value cv) mrb_class_new_class(mrb_state *mrb, mrb_value cv)
{ {
mrb_int n;
mrb_value super, blk; mrb_value super, blk;
mrb_value new_class; 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); super = mrb_obj_value(mrb->object_class);
} }
new_class = mrb_obj_value(mrb_class_new(mrb, mrb_class_ptr(super))); new_class = mrb_obj_value(mrb_class_new(mrb, mrb_class_ptr(super)));
if (!mrb_nil_p(blk)) { mrb_funcall_with_block(mrb, new_class, mrb_intern_lit(mrb, "initialize"), n, &super, blk);
mrb_yield_with_class(mrb, blk, 1, &new_class, new_class, mrb_class_ptr(new_class));
}
mrb_funcall(mrb, super, "inherited", 1, new_class); mrb_funcall(mrb, super, "inherited", 1, new_class);
return new_class; return new_class;
} }
...@@ -1963,9 +1975,10 @@ mrb_init_class(mrb_state *mrb) ...@@ -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, "!", 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_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, "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, "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_define_method(mrb, cls, "inherited", mrb_bob_init, MRB_ARGS_REQ(1));
MRB_SET_INSTANCE_TT(mod, MRB_TT_MODULE); MRB_SET_INSTANCE_TT(mod, MRB_TT_MODULE);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment