@@ -1003,10 +1003,19 @@ mrb_method_search(mrb_state *mrb, struct RClass* c, mrb_sym mid)
10031003 return m ;
10041004}
10051005
1006- void
1007- mrb_obj_call_init (mrb_state * mrb , mrb_value obj , int argc , mrb_value * argv )
1006+ static mrb_value
1007+ mrb_instance_alloc (mrb_state * mrb , mrb_value cv )
10081008{
1009- mrb_funcall_argv (mrb , obj , mrb -> init_sym , argc , argv );
1009+ struct RClass * c = mrb_class_ptr (cv );
1010+ struct RObject * o ;
1011+ enum mrb_vtype ttype = MRB_INSTANCE_TT (c );
1012+
1013+ if (c -> tt == MRB_TT_SCLASS )
1014+ mrb_raise (mrb , E_TYPE_ERROR , "can't create instance of singleton class" );
1015+
1016+ if (ttype == 0 ) ttype = MRB_TT_OBJECT ;
1017+ o = (struct RObject * )mrb_obj_alloc (mrb , ttype , c );
1018+ return mrb_obj_value (o );
10101019}
10111020
10121021/*
@@ -1020,70 +1029,30 @@ mrb_obj_call_init(mrb_state *mrb, mrb_value obj, int argc, mrb_value *argv)
10201029 * an object is constructed using .new.
10211030 *
10221031 */
1023- mrb_value
1024- mrb_class_new_instance (mrb_state * mrb , int argc , mrb_value * argv , struct RClass * klass )
1025- {
1026- mrb_value obj ;
1027- struct RClass * c = (struct RClass * )mrb_obj_alloc (mrb , klass -> tt , klass );
1028- c -> super = klass ;
1029- obj = mrb_obj_value (c );
1030- mrb_obj_call_init (mrb , obj , argc , argv );
1031- return obj ;
1032- }
1033-
1034- mrb_value
1035- mrb_class_new_instance_m (mrb_state * mrb , mrb_value klass )
1036- {
1037- mrb_value * argv ;
1038- mrb_value blk ;
1039- struct RClass * k = mrb_class_ptr (klass );
1040- struct RClass * c ;
1041- int argc ;
1042- mrb_value obj ;
1043-
1044- mrb_get_args (mrb , "*&" , & argv , & argc , & blk );
1045- c = (struct RClass * )mrb_obj_alloc (mrb , k -> tt , k );
1046- c -> super = k ;
1047- obj = mrb_obj_value (c );
1048- mrb_funcall_with_block (mrb , obj , mrb -> init_sym , argc , argv , blk );
1049-
1050- return obj ;
1051- }
10521032
10531033mrb_value
10541034mrb_instance_new (mrb_state * mrb , mrb_value cv )
10551035{
1056- struct RClass * c = mrb_class_ptr (cv );
1057- struct RObject * o ;
1058- enum mrb_vtype ttype = MRB_INSTANCE_TT (c );
10591036 mrb_value obj , blk ;
10601037 mrb_value * argv ;
10611038 int argc ;
10621039
1063- if (c -> tt == MRB_TT_SCLASS )
1064- mrb_raise (mrb , E_TYPE_ERROR , "can't create instance of singleton class" );
1065-
1066- if (ttype == 0 ) ttype = MRB_TT_OBJECT ;
1067- o = (struct RObject * )mrb_obj_alloc (mrb , ttype , c );
1068- obj = mrb_obj_value (o );
1040+ obj = mrb_instance_alloc (mrb , cv );
10691041 mrb_get_args (mrb , "*&" , & argv , & argc , & blk );
1070- mrb_funcall_with_block (mrb , obj , mrb -> init_sym , argc , argv , blk );
1042+ mrb_funcall_with_block (mrb , obj , mrb_intern ( mrb , "initialize" ) , argc , argv , blk );
10711043
10721044 return obj ;
10731045}
10741046
10751047mrb_value
1076- mrb_class_new_class (mrb_state * mrb , mrb_value cv )
1048+ mrb_obj_new (mrb_state * mrb , struct RClass * c , int argc , mrb_value * argv )
10771049{
1078- mrb_value super ;
1079- struct RClass * new_class ;
1050+ mrb_value obj ;
10801051
1081- if (mrb_get_args (mrb , "|o" , & super ) == 0 ) {
1082- super = mrb_obj_value (mrb -> object_class );
1083- }
1084- new_class = mrb_class_new (mrb , mrb_class_ptr (super ));
1085- mrb_funcall (mrb , super , "inherited" , 1 , mrb_obj_value (new_class ));
1086- return mrb_obj_value (new_class );
1052+ obj = mrb_instance_alloc (mrb , mrb_obj_value (c ));
1053+ mrb_funcall_argv (mrb , obj , mrb_intern (mrb , "initialize" ), argc , argv );
1054+
1055+ return obj ;
10871056}
10881057
10891058mrb_value
@@ -1902,8 +1871,8 @@ mrb_init_class(mrb_state *mrb)
19021871 mrb_define_method (mrb , bob , "!" , mrb_bob_not , MRB_ARGS_NONE ());
19031872 mrb_define_method (mrb , bob , "method_missing" , mrb_bob_missing , MRB_ARGS_ANY ()); /* 15.3.1.3.30 */
19041873
1905- mrb_define_class_method (mrb , cls , "new" , mrb_class_new_class , MRB_ARGS_ANY ());
19061874 mrb_define_method (mrb , cls , "superclass" , mrb_class_superclass , MRB_ARGS_NONE ()); /* 15.2.3.3.4 */
1875+ mrb_define_method (mrb , cls , "alloc" , mrb_instance_alloc , MRB_ARGS_NONE ());
19071876 mrb_define_method (mrb , cls , "new" , mrb_instance_new , MRB_ARGS_ANY ()); /* 15.2.3.3.3 */
19081877 mrb_define_method (mrb , cls , "inherited" , mrb_bob_init , MRB_ARGS_REQ (1 ));
19091878
0 commit comments