Revision: 1197 http://trac.macosforge.org/projects/ruby/changeset/1197 Author: lsansonetti@apple.com Date: 2009-03-26 21:21:39 -0700 (Thu, 26 Mar 2009) Log Message: ----------- fixed +[NSObject new:] + minor things Modified Paths: -------------- MacRuby/branches/experimental/class.c MacRuby/branches/experimental/include/ruby/ruby.h MacRuby/branches/experimental/objc.m MacRuby/branches/experimental/object.c MacRuby/branches/experimental/roxor.cpp MacRuby/branches/experimental/roxor.h MacRuby/branches/experimental/string.c MacRuby/branches/experimental/vm_method.c Modified: MacRuby/branches/experimental/class.c =================================================================== --- MacRuby/branches/experimental/class.c 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/class.c 2009-03-27 04:21:39 UTC (rev 1197) @@ -99,12 +99,13 @@ VALUE rb_obj_init_copy(VALUE, SEL, VALUE); +VALUE rb_class_new_instance_imp(VALUE klass, SEL sel, int argc, VALUE *argv); + void rb_define_object_special_methods(VALUE klass) { rb_objc_define_method(*(VALUE *)klass, "alloc", rb_class_allocate_instance, 0); RCLASS_SET_VERSION(*(VALUE *)klass, (RCLASS_VERSION(*(VALUE *)klass) | RCLASS_HAS_ROBJECT_ALLOC)); - VALUE rb_class_new_instance_imp(VALUE klass, SEL sel, int argc, VALUE *argv); rb_objc_define_method(*(VALUE *)klass, "new", rb_class_new_instance_imp, -1); rb_objc_define_method(*(VALUE *)klass, "__new__", rb_class_new_instance_imp, -1); rb_objc_define_method(klass, "dup", rb_obj_dup, 0); @@ -166,8 +167,9 @@ DLOG("DEFC", "%s < %s (version=%d)", ocname, class_getName(class_getSuperclass((Class)ocklass)), version_flag); - if (klass != 0) + if (klass != 0) { rb_objc_install_primitives(ocklass, (Class)super); + } return (VALUE)ocklass; } @@ -1050,28 +1052,39 @@ return sel_registerName(name); } -void -rb_objc_add_method(VALUE klass, const char *name, void *imp, const int arity, const int noex) +static void +rb_objc_add_method(VALUE klass, const char *name, void *imp, const int arity, + const int noex, bool direct) { - assert(name[strlen(name) - 1] != ':'); + if (!direct) { + assert(name[strlen(name) - 1] != ':'); + } NODE *node = NEW_CFUNC(imp, arity); NODE *body = NEW_FBODY(NEW_METHOD(node, klass, noex), 0); rb_objc_retain(body); - rb_vm_define_method((Class)klass, name_to_sel(name, arity), (IMP)imp, body); + rb_vm_define_method((Class)klass, name_to_sel(name, arity), (IMP)imp, body, direct); } void +rb_objc_define_direct_method(VALUE klass, const char *name, void *imp, + const int arity) +{ + rb_objc_add_method(klass, name, imp, arity, NOEX_PUBLIC, true); +} + +void rb_objc_define_method(VALUE klass, const char *name, void *imp, const int arity) { - rb_objc_add_method(klass, name, imp, arity, NOEX_PUBLIC); + rb_objc_add_method(klass, name, imp, arity, NOEX_PUBLIC, false); } void -rb_objc_define_private_method(VALUE klass, const char *name, void *imp, const int arity) +rb_objc_define_private_method(VALUE klass, const char *name, void *imp, + const int arity) { - rb_objc_add_method(klass, name, imp, arity, NOEX_PRIVATE); + rb_objc_add_method(klass, name, imp, arity, NOEX_PRIVATE, false); } void Modified: MacRuby/branches/experimental/include/ruby/ruby.h =================================================================== --- MacRuby/branches/experimental/include/ruby/ruby.h 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/include/ruby/ruby.h 2009-03-27 04:21:39 UTC (rev 1197) @@ -873,6 +873,7 @@ void rb_define_attr(VALUE,const char*,int,int); void rb_objc_define_method(VALUE klass, const char *name, void *imp, const int arity); +void rb_objc_define_direct_method(VALUE klass, const char *name, void *imp, const int arity); void rb_objc_define_private_method(VALUE klass, const char *name, void *imp, const int arity); void rb_objc_undef_method(VALUE klass, const char *name); Modified: MacRuby/branches/experimental/objc.m =================================================================== --- MacRuby/branches/experimental/objc.m 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/objc.m 2009-03-27 04:21:39 UTC (rev 1197) @@ -3595,6 +3595,7 @@ old_imp_isaForAutonotifying = method_getImplementation(m); method_setImplementation(m, (IMP)rb_obj_imp_isaForAutonotifying); +#if 0 { VALUE klass; NODE *node, *body; @@ -3606,6 +3607,7 @@ closure = rb_ruby_to_objc_closure("@@:@", 1, body->nd_body); assert(class_addMethod((Class)klass, @selector(new:), (IMP)closure, "@@:@")); } +#endif _objc_msgForward_addr = &_objc_msgForward; Modified: MacRuby/branches/experimental/object.c =================================================================== --- MacRuby/branches/experimental/object.c 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/object.c 2009-03-27 04:21:39 UTC (rev 1197) @@ -2679,9 +2679,13 @@ void rb_include_module2(VALUE klass, VALUE module, int check, int add_methods); + // At this point, methods defined on Class or Module will be automatically + // added to NSObject's metaclass. rb_include_module2(*(VALUE *)rb_cNSObject, rb_cClass, 0, 0); rb_include_module2(*(VALUE *)rb_cNSObject, rb_cModule, 0, 0); + rb_objc_define_direct_method(*(VALUE *)rb_cNSObject, "new:", rb_class_new_instance_imp, -1); + rb_objc_define_private_method(rb_cNSObject, "initialize", rb_obj_dummy, 0); rb_objc_define_method(rb_cNSObject, "==", rb_obj_equal, 1); rb_objc_define_method(rb_cNSObject, "equal?", rb_obj_equal, 1); Modified: MacRuby/branches/experimental/roxor.cpp =================================================================== --- MacRuby/branches/experimental/roxor.cpp 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/roxor.cpp 2009-03-27 04:21:39 UTC (rev 1197) @@ -4603,7 +4603,7 @@ IMP imp = GET_VM()->compile(func); - rb_vm_define_method(klass, sel, imp, node); + rb_vm_define_method(klass, sel, imp, node, false); } extern "C" @@ -4667,7 +4667,7 @@ NODE *body = NEW_FBODY(NEW_METHOD(node, klass, noex), 0); rb_objc_retain(body); - rb_vm_define_method(klass, sel_registerName(name), imp, body); + rb_vm_define_method(klass, sel_registerName(name), imp, body, false); } if (write) { @@ -4679,7 +4679,7 @@ rb_objc_retain(body); snprintf(buf, sizeof buf, "%s=:", name); - rb_vm_define_method(klass, sel_registerName(buf), imp, body); + rb_vm_define_method(klass, sel_registerName(buf), imp, body, false); } delete compiler; @@ -4687,20 +4687,19 @@ extern "C" void -rb_vm_define_method(Class klass, SEL sel, IMP imp, NODE *node) +rb_vm_define_method(Class klass, SEL sel, IMP imp, NODE *node, bool direct) { + assert(klass != NULL); assert(node != NULL); const rb_vm_arity_t arity = rb_vm_node_arity(node); assert(arity.real < MAX_ARITY); - assert(klass != NULL); - const char *sel_name = sel_getName(sel); const bool genuine_selector = sel_name[strlen(sel_name) - 1] == ':'; int oc_arity = genuine_selector ? arity.real : 0; - bool redefined = false; + bool redefined = direct; define_method: char *types; @@ -5110,11 +5109,12 @@ } #if ROXOR_VM_DEBUG - printf("objc dispatch %c[<%s %p> %s] (cached=%s)\n", + printf("objc dispatch %c[<%s %p> %s] imp=%p (cached=%s)\n", class_isMetaClass(klass) ? '+' : '-', class_getName(klass), (void *)self, sel_getName(sel), + ocache.imp, cached ? "true" : "false"); #endif Modified: MacRuby/branches/experimental/roxor.h =================================================================== --- MacRuby/branches/experimental/roxor.h 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/roxor.h 2009-03-27 04:21:39 UTC (rev 1197) @@ -21,7 +21,7 @@ void rb_vm_const_is_defined(ID path); bool rb_vm_lookup_method(Class klass, SEL sel, IMP *pimp, NODE **pnode); bool rb_vm_lookup_method2(Class klass, ID mid, SEL *psel, IMP *pimp, NODE **pnode); -void rb_vm_define_method(Class klass, SEL sel, IMP imp, NODE *node); +void rb_vm_define_method(Class klass, SEL sel, IMP imp, NODE *node, bool direct); void rb_vm_define_attr(Class klass, const char *name, bool read, bool write, int noex); void rb_vm_alias(VALUE klass, ID name, ID def); VALUE rb_vm_call(VALUE self, SEL sel, int argc, const VALUE *args, bool super); Modified: MacRuby/branches/experimental/string.c =================================================================== --- MacRuby/branches/experimental/string.c 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/string.c 2009-03-27 04:21:39 UTC (rev 1197) @@ -5156,7 +5156,7 @@ (IMP)imp_rb_str_fastestEncodingInCFStringEncoding); rb_objc_install_method2(klass, "isEqual:", (IMP)imp_rb_str_isEqual); - rb_define_alloc_func((VALUE)klass, str_alloc); + rb_objc_define_method(*(VALUE *)klass, "alloc", str_alloc, 0); } static CFIndex Modified: MacRuby/branches/experimental/vm_method.c =================================================================== --- MacRuby/branches/experimental/vm_method.c 2009-03-27 01:35:40 UTC (rev 1196) +++ MacRuby/branches/experimental/vm_method.c 2009-03-27 04:21:39 UTC (rev 1197) @@ -789,7 +789,7 @@ rb_bug("undefined method `%s'; can't happen", rb_id2name(id)); } - rb_vm_define_method(*(Class *)module, sel, imp, node); + rb_vm_define_method(*(Class *)module, sel, imp, node, false); } return module;