[macruby-changes] [3934] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed Apr 14 19:00:45 PDT 2010


Revision: 3934
          http://trac.macosforge.org/projects/ruby/changeset/3934
Author:   lsansonetti at apple.com
Date:     2010-04-14 19:00:40 -0700 (Wed, 14 Apr 2010)
Log Message:
-----------
always auto-convert NSNumbers

Modified Paths:
--------------
    MacRuby/trunk/objc.h
    MacRuby/trunk/objc.m

Modified: MacRuby/trunk/objc.h
===================================================================
--- MacRuby/trunk/objc.h	2010-04-14 20:04:13 UTC (rev 3933)
+++ MacRuby/trunk/objc.h	2010-04-15 02:00:40 UTC (rev 3934)
@@ -174,6 +174,8 @@
             return (id)kCFNull;
         }
 	if (FIXNUM_P(obj)) {
+	    // TODO: this could be optimized in case we can fit the fixnum
+	    // into an immediate NSNumber directly.
 	    long val = FIX2LONG(obj);
 	    CFNumberRef number = CFNumberCreate(NULL, kCFNumberLongType, &val);
 	    CFMakeCollectable(number);
@@ -190,6 +192,8 @@
     return (id)obj;
 }
 
+extern CFTypeID __CFNumberTypeID;
+
 static inline VALUE
 rb_ocid_to_rval(id obj)
 {
@@ -202,35 +206,25 @@
     if (obj == (id)kCFNull || obj == nil) {
 	return Qnil;
     }
-#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
-    if (true) {
-#else
-    if (((unsigned long)obj & 0x1) == 0x1) {
-#endif
-	// An Objective-C immediate! We only recognize NSNumbers for now.
-	Class k = object_getClass(obj);
-	while (k != NULL) {
-	    if (k == (Class)rb_cNSNumber) {
-		if (CFNumberIsFloatType((CFNumberRef)obj)) {
-		    // Will likely not happen, but just in case...	
-		    double v;
-		    assert(CFNumberGetValue((CFNumberRef)obj,
-				kCFNumberDoubleType, &v));
-		    return DOUBLE2NUM(v);
-		}
-		else {
-		    long v;
-		    assert(CFNumberGetValue((CFNumberRef)obj,
-				kCFNumberLongType, &v));
-		    return LONG2FIX(v);
-		}
-	    }
-	    k = class_getSuperclass(k);
+
+    if (CFGetTypeID(obj) == __CFNumberTypeID) {
+	// TODO: this could be optimized in case the object is an immediate.
+	if (CFNumberIsFloatType((CFNumberRef)obj)) {
+	    double v = 0;
+	    assert(CFNumberGetValue((CFNumberRef)obj, kCFNumberDoubleType, &v));
+	    return DOUBLE2NUM(v);
 	}
-#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
+	else {
+	    long v = 0;
+	    assert(CFNumberGetValue((CFNumberRef)obj, kCFNumberLongType, &v));
+	    return LONG2FIX(v);
+	}
+    }
+
+    if (((unsigned long)obj & 0x1) == 0x1) {
 	rb_bug("unknown Objective-C immediate: %p\n", obj);
-#endif
     }
+
     return (VALUE)obj;
 }
 

Modified: MacRuby/trunk/objc.m
===================================================================
--- MacRuby/trunk/objc.m	2010-04-14 20:04:13 UTC (rev 3933)
+++ MacRuby/trunk/objc.m	2010-04-15 02:00:40 UTC (rev 3934)
@@ -25,6 +25,8 @@
 # include "bs.h"
 #endif
 
+CFTypeID __CFNumberTypeID = 0;
+
 static inline const char *
 rb_get_bs_method_type(bs_element_method_t *bs_method, int arg)
 {
@@ -663,6 +665,9 @@
 void
 Init_ObjC(void)
 {
+    __CFNumberTypeID = CFNumberGetTypeID();
+    assert(__CFNumberTypeID > 0);
+
     rb_objc_define_method(rb_mKernel, "load_bridge_support_file",
 	    rb_objc_load_bs, 1);
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100414/748a8209/attachment.html>


More information about the macruby-changes mailing list