[macruby-changes] [383] MacRuby/branches/lrz_unstable

source_changes at macosforge.org source_changes at macosforge.org
Tue Jul 29 23:17:41 PDT 2008


Revision: 383
          http://trac.macosforge.org/projects/ruby/changeset/383
Author:   lsansonetti at apple.com
Date:     2008-07-29 23:17:41 -0700 (Tue, 29 Jul 2008)
Log Message:
-----------
wip

Modified Paths:
--------------
    MacRuby/branches/lrz_unstable/class.c
    MacRuby/branches/lrz_unstable/include/ruby/intern.h
    MacRuby/branches/lrz_unstable/include/ruby/ruby.h
    MacRuby/branches/lrz_unstable/numeric.c
    MacRuby/branches/lrz_unstable/objc.m
    MacRuby/branches/lrz_unstable/object.c
    MacRuby/branches/lrz_unstable/variable.c
    MacRuby/branches/lrz_unstable/vm.c
    MacRuby/branches/lrz_unstable/vm_insnhelper.c

Modified: MacRuby/branches/lrz_unstable/class.c
===================================================================
--- MacRuby/branches/lrz_unstable/class.c	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/class.c	2008-07-30 06:17:41 UTC (rev 383)
@@ -123,7 +123,7 @@
     	snprintf(ocname, sizeof ocname, "RBAnonymous%ld", ++anon_count);
     }
     else {
-	if (super == rb_cBasicObject && strcmp(name, "Object") != 0) {
+	if (super == rb_cNSObject && strcmp(name, "Object") != 0) {
 	    rb_warn("Do not subclass NSObject directly, please subclass " \
 		    "Object instead.");
 	    super = rb_cObject; 

Modified: MacRuby/branches/lrz_unstable/include/ruby/intern.h
===================================================================
--- MacRuby/branches/lrz_unstable/include/ruby/intern.h	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/include/ruby/intern.h	2008-07-30 06:17:41 UTC (rev 383)
@@ -703,6 +703,7 @@
 long rb_objc_remove_flags(const void *obj);
 void rb_objc_methods(VALUE, Class);
 bool rb_objc_is_immutable(VALUE);
+VALUE rb_objc_call(VALUE, SEL, int, VALUE *);
 #endif
 /* version.c */
 void ruby_show_version(void);

Modified: MacRuby/branches/lrz_unstable/include/ruby/ruby.h
===================================================================
--- MacRuby/branches/lrz_unstable/include/ruby/ruby.h	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/include/ruby/ruby.h	2008-07-30 06:17:41 UTC (rev 383)
@@ -955,6 +955,9 @@
 
 RUBY_EXTERN VALUE rb_cBasicObject;
 RUBY_EXTERN VALUE rb_cObject;
+#if WITH_OBJC
+RUBY_EXTERN VALUE rb_cNSObject;
+#endif
 RUBY_EXTERN VALUE rb_cArray;
 RUBY_EXTERN VALUE rb_cBignum;
 RUBY_EXTERN VALUE rb_cBinding;

Modified: MacRuby/branches/lrz_unstable/numeric.c
===================================================================
--- MacRuby/branches/lrz_unstable/numeric.c	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/numeric.c	2008-07-30 06:17:41 UTC (rev 383)
@@ -3192,6 +3192,8 @@
 #if WITH_OBJC
     rb_cNumeric = rb_define_class("Numeric",
         rb_objc_import_class((void*)objc_getClass("NSNumber")));
+    // We need to redefine #class because otherwise NSObject#class will return NSCFNumber for all numeric types.
+    rb_define_method(rb_cNumeric, "class", rb_obj_class, 0);
 #else
     rb_cNumeric = rb_define_class("Numeric", rb_cObject);
 #endif

Modified: MacRuby/branches/lrz_unstable/objc.m
===================================================================
--- MacRuby/branches/lrz_unstable/objc.m	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/objc.m	2008-07-30 06:17:41 UTC (rev 383)
@@ -1101,7 +1101,7 @@
     char buf[128];
     void *imp;
 
-    DLOG("OCALL", "[%p %s] argc=%d", ocrcv, (char *)ctx->selector, argc);
+    DLOG("OCALL", "[<%s %p> %s]", class_getName((Class)klass), (void *)ocrcv, (char *)ctx->selector);
 
     count = method_getNumberOfArguments(ctx->method);
     assert(count >= 2);
@@ -1120,7 +1120,7 @@
 
     if (count == 2) {
 	method_getReturnType(ctx->method, buf, sizeof buf);
-	if (buf[0] == '@' || buf[0] == 'v') {
+	if (buf[0] == '@' || buf[0] == '#' || buf[0] == 'v') {
 	    /* Easy case! */
 	    @try {
 		if (super_call) {
@@ -1136,7 +1136,7 @@
 	    @catch (id e) {
 		rb_objc_exc_raise(e);
 	    }
-	    return buf[0] == '@' ? (VALUE)ffi_ret : Qnil;
+	    return buf[0] == '@' || buf[0] == '#' ? (VALUE)ffi_ret : Qnil;
 	}
     } 
 
@@ -1234,119 +1234,25 @@
     }
 }
 
-#if 0
-static VALUE
-rb_objc_to_ruby_closure(int argc, VALUE *argv, VALUE rcv)
-{
-    id ocrcv;
-    bool super_call;
-    Class klass;
-    struct objc_ruby_closure_context *ctx;
-
-    rb_objc_rval_to_ocid(rcv, (void **)&ocrcv, true);
-    super_call = (ruby_current_thread->cfp->flag >> FRAME_MAGIC_MASK_BITS) 
-	& VM_CALL_SUPER_BIT;
-    klass = *(Class *)ocrcv;
-    
-    assert(rb_current_cfunc_node != NULL);
-
-    if (rb_current_cfunc_node->u3.value == 0) {
-	const char *selname;
-	size_t selnamelen;
-
-	ctx = (struct objc_ruby_closure_context *)xmalloc(sizeof(
-	    struct objc_ruby_closure_context));
-
-	selname = rb_id2name(rb_frame_this_func());
-	selnamelen = strlen(selname);
-	if (argc == 1 && selname[selnamelen - 1] != ':') {
-	    char *tmp = alloca(selnamelen + 2);
-	    snprintf(tmp, selnamelen + 2, "%s:", selname);
-	    selname = (const char *)tmp;
-	}
-	ctx->selector = sel_registerName(selname);
-
-	ctx->bs_method = rb_bs_find_method(*(Class *)ocrcv, ctx->selector);
-	GC_WB(&rb_current_cfunc_node->u3.value, ctx);
-    }
-    else {
-	ctx = (struct objc_ruby_closure_context *)
-	    rb_current_cfunc_node->u3.value;
-    }
-    ctx->method = class_getInstanceMethod(*(Class *)ocrcv, ctx->selector); 
-    assert(ctx->method != NULL);
-    ctx->cif = NULL;
-    ctx->imp = NULL;
-    ctx->klass = klass;
-
-    if (super_call) {
-	Class sklass = klass;
-	Method orig_method = ctx->klass == klass 
-	    ? ctx->method : class_getInstanceMethod(klass, ctx->selector);
-	for (;;) {
-	    Method smethod;
-	    sklass = class_getSuperclass(sklass);
-	    if (sklass == NULL)
-		break;
-	    smethod = class_getInstanceMethod(sklass, ctx->selector);
-	    if (smethod != orig_method) {
-		klass = sklass;
-	        break;
-	    }
-	}
-    }
-    
-//NSLog(@"Ruby -> ObjC [%@ klass=%@ sel=%s argc=%d super_call=%d", ocrcv, (id)klass, (char *)ctx->selector, argc, super_call);
-
-    return rb_objc_call_objc(argc, argv, ocrcv, klass, super_call, ctx);
-}
-#endif
-
 VALUE
-rb_objc_call(VALUE recv, ID mid, int argc, VALUE *argv)
+rb_objc_call(VALUE recv, SEL sel, int argc, VALUE *argv)
 {
+    struct objc_ruby_closure_context fake_ctx;
     VALUE klass;
-    SEL sel;
-    IMP imp;
-    NODE *node;
+    id ocrcv;
 
-    klass = CLASS_OF(recv);
-    node = rb_objc_method_node(klass, mid, &imp, &sel);
+    rb_objc_rval_to_ocid(recv, (void **)&ocrcv, true);
+    klass = *(VALUE *)ocrcv;
 
-    if (imp == NULL) {
-	// TODO
-	printf("METHOD_MISSING!");
-	assert(1 == 0);
-	return Qnil;
-    }
-    else if (node != NULL) {
-	assert(node->nd_body != NULL);
+    fake_ctx.selector = sel;
+    fake_ctx.method = class_getInstanceMethod((Class)klass, fake_ctx.selector); 
+    assert(fake_ctx.method != NULL);
+    fake_ctx.bs_method = NULL;
+    fake_ctx.cif = NULL;
+    fake_ctx.imp = NULL;
+    fake_ctx.klass = NULL;
 
-	DLOG("RCALL", "[<%s %p> %s] node=%p argc=%d", class_getName((Class)klass), (void *)recv, (char *)sel, node->nd_body, argc);
-
-	VALUE rb_vm_call(rb_thread_t * th, VALUE klass, VALUE recv, VALUE id, 
-		ID oid, int argc, const VALUE *argv, const NODE *body, 
-		int nosuper);
-
-	return rb_vm_call(GET_THREAD(), klass, recv, mid, Qnil,
-		argc, argv, node->nd_body, 0);
-    }
-    else {
-	id ocrcv;
-	struct objc_ruby_closure_context fake_ctx;
-
-	rb_objc_rval_to_ocid(recv, (void **)&ocrcv, true);
-
-	fake_ctx.selector = sel;
-	fake_ctx.method = class_getInstanceMethod((Class)klass, fake_ctx.selector); 
-	assert(fake_ctx.method != NULL);
-	fake_ctx.bs_method = NULL;
-	fake_ctx.cif = NULL;
-	fake_ctx.imp = imp;
-	fake_ctx.klass = NULL;
-
-	return rb_objc_call_objc(argc, argv, ocrcv, (Class)klass, true, &fake_ctx);
-    }
+    return rb_objc_call_objc(argc, argv, ocrcv, (Class)klass, false, &fake_ctx);
 }
 
 void

Modified: MacRuby/branches/lrz_unstable/object.c
===================================================================
--- MacRuby/branches/lrz_unstable/object.c	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/object.c	2008-07-30 06:17:41 UTC (rev 383)
@@ -23,6 +23,9 @@
 
 VALUE rb_cBasicObject;
 VALUE rb_mKernel;
+#if WITH_OBJC
+VALUE rb_cNSObject;
+#endif
 VALUE rb_cObject;
 VALUE rb_cModule;
 VALUE rb_cClass;
@@ -2538,23 +2541,24 @@
 void
 Init_Object(void)
 {
+#if WITH_OBJC
+    rb_cNSObject = (VALUE)objc_getClass("NSObject");
+    rb_cObject = boot_defclass("Object", rb_cNSObject);
+    rb_cBasicObject = rb_cObject; // TODO
+    RCLASS_SET_VERSION_FLAG(rb_cObject, RCLASS_IS_OBJECT_SUBCLASS);
+    rb_cModule = boot_defclass("Module", rb_cObject);
+    rb_cClass =  boot_defclass("Class",  rb_cModule);
+#else
     VALUE metaclass;
 
-#if WITH_OBJC
-    rb_cBasicObject = boot_defclass("BasicObject", rb_objc_import_class((Class)objc_getClass("NSObject")));
-    RCLASS_SET_VERSION_FLAG(rb_cBasicObject, RCLASS_IS_OBJECT_SUBCLASS);
-#else
     rb_cBasicObject = boot_defclass("BasicObject", 0);
-#endif
     rb_cObject = boot_defclass("Object", rb_cBasicObject);
     rb_cModule = boot_defclass("Module", rb_cObject);
     rb_cClass =  boot_defclass("Class",  rb_cModule);
+#endif
 
 #if WITH_OBJC
-    metaclass = 0; // eliminate warning
-    RCLASS_SUPER(*(Class *)RCLASS_SUPER(rb_cBasicObject)) = rb_cClass;
-    //metaclass = boot_defclass("MetaClass", rb_cClass);
-    //RCLASS_SUPER(*(Class *)rb_cBasicObject) = rb_cClass;//metaclass;
+    RCLASS_SUPER(*(Class *)rb_cNSObject) = rb_cClass;
 #else
     metaclass = rb_make_metaclass(rb_cBasicObject, rb_cClass);
     metaclass = rb_make_metaclass(rb_cObject, metaclass);
@@ -2578,7 +2582,11 @@
     rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
 
     rb_mKernel = rb_define_module("Kernel");
+#if WITH_OBJC
+    rb_include_module(rb_cNSObject, rb_mKernel);
+#else
     rb_include_module(rb_cObject, rb_mKernel);
+#endif
     rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
     rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
     rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);

Modified: MacRuby/branches/lrz_unstable/variable.c
===================================================================
--- MacRuby/branches/lrz_unstable/variable.c	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/variable.c	2008-07-30 06:17:41 UTC (rev 383)
@@ -162,6 +162,15 @@
 #endif
 	return arg.path;
     }
+#if WITH_OBJC
+    if (!RCLASS_RUBY(klass)) {
+	VALUE name = rb_str_new2(class_getName((Class)klass));
+	iv_dict = rb_class_ivar_dict_or_create(klass);
+	CFDictionarySetValue(iv_dict, (const void *)classpath, (const void *)name);
+	CFDictionaryRemoveValue(iv_dict, (const void *)tmp_classpath);
+	return name;
+    }
+#endif
     return Qnil;
 }
 

Modified: MacRuby/branches/lrz_unstable/vm.c
===================================================================
--- MacRuby/branches/lrz_unstable/vm.c	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/vm.c	2008-07-30 06:17:41 UTC (rev 383)
@@ -848,42 +848,41 @@
 /* optimization: redefine management */
 
 VALUE ruby_vm_redefined_flag = 0;
-#if WITH_OBJC
-static void 
-rb_vm_check_redefinition_opt_method(const NODE *node) 
-{
-}
-#else
 static st_table *vm_opt_method_table = 0;
 
 static void
 rb_vm_check_redefinition_opt_method(const NODE *node)
 {
+#if !WITH_OBJC
     VALUE bop;
 
-    if (st_lookup(vm_opt_method_table, (st_data_t)node, &bop)) {
+    if (vm_opt_method_table != NULL 
+	&& st_lookup(vm_opt_method_table, (st_data_t)node, &bop)) {
 	ruby_vm_redefined_flag |= bop;
     }
+#endif
 }
 
 static void
 add_opt_method(VALUE klass, ID mid, VALUE bop)
 {
     NODE *node;
+#if WITH_OBJC
+    if ((node = rb_method_node(klass, mid)) != NULL) {
+#else
     if (st_lookup(RCLASS_M_TBL(klass), mid, (void *)&node) &&
 	nd_type(node->nd_body->nd_body) == NODE_CFUNC) {
+#endif
 	st_insert(vm_opt_method_table, (st_data_t)node, (st_data_t)bop);
     }
     else {
 	rb_bug("undefined optimized method: %s", rb_id2name(mid));
     }
 }
-#endif
 
 static void
 vm_init_redefined_flag(void)
 {
-#if !WITH_OBJC
     ID mid;
     VALUE bop;
 
@@ -903,13 +902,14 @@
     OP(LTLT, LTLT), (C(String), C(Array));
     OP(AREF, AREF), (C(Array), C(Hash));
     OP(ASET, ASET), (C(Array), C(Hash));
+#if !WITH_OBJC
     OP(Length, LENGTH), (C(Array), C(String), C(Hash));
+#endif
     OP(Succ, SUCC), (C(Fixnum), C(String), C(Time));
     OP(GT, GT), (C(Fixnum));
     OP(GE, GE), (C(Fixnum));
 #undef C
 #undef OP
-#endif
 }
 
 /* evaluator body */

Modified: MacRuby/branches/lrz_unstable/vm_insnhelper.c
===================================================================
--- MacRuby/branches/lrz_unstable/vm_insnhelper.c	2008-07-30 02:27:07 UTC (rev 382)
+++ MacRuby/branches/lrz_unstable/vm_insnhelper.c	2008-07-30 06:17:41 UTC (rev 383)
@@ -489,14 +489,15 @@
 #if WITH_OBJC
     IMP imp;
 
-#define STUPID_CACHE 0
+#define STUPID_CACHE 1
 
 #if STUPID_CACHE
 static VALUE c_klass = 0;
+static SEL c_sel = 0;
 static IMP c_imp = 0;
 static NODE *c_mn = NULL;
 
-if (c_klass == klass) {
+if (c_klass == klass && c_sel == sel && sel != 0) {
 imp = c_imp;
 mn = c_mn;
 }
@@ -512,15 +513,31 @@
 
 #if STUPID_CACHE
 c_klass = klass;
+c_sel = sel;
 c_imp = imp;
 c_mn = (NODE*)mn;
 }
 #endif
 
     if (mn == NULL && imp != NULL) {
-	printf("OBJC_CALL %p %s %p %p\n",(void*)klass,(char*)sel,mn,imp);
-	assert(1==0);
-	return Qnil;
+	rb_control_frame_t *reg_cfp = cfp;
+	rb_control_frame_t *cfp =
+	    vm_push_frame(th, 0, FRAME_MAGIC_CFUNC | (flag << FRAME_MAGIC_MASK_BITS),
+		    recv, (VALUE) blockptr, 0, reg_cfp->sp, 0, 1);
+
+	cfp->method_id = id;
+	cfp->method_class = klass;
+
+	reg_cfp->sp -= num + 1;
+
+   	val = rb_objc_call(recv, sel, num, reg_cfp->sp + 1);
+
+	if (reg_cfp != th->cfp + 1)
+	    rb_bug("cfp consistency error - send");
+
+	vm_pop_frame(th);
+
+	return val;
     }
 
     DLOG("RCALL", "[<%s %p> %s] node=%p", class_getName((Class)klass), (void *)recv, (char *)sel, mn);
@@ -628,13 +645,6 @@
 	}
 	else {
 	    int stat = 0;
-#if 0//WITH_OBJC
-	    mn = rb_objc_define_objc_mid_closure(recv, id, 0);
-	    if (mn != NULL) {
-		return vm_call_method(th, cfp, num, blockptr, flag, id,
-				      mn, recv, klass);
-	    }
-#endif
 	    if (flag & VM_CALL_VCALL_BIT) {
 		stat |= NOEX_VCALL;
 	    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080729/66a0fb94/attachment-0001.html 


More information about the macruby-changes mailing list