[macruby-changes] [2301] MacRuby/trunk/variable.c

source_changes at macosforge.org source_changes at macosforge.org
Wed Aug 12 20:10:56 PDT 2009


Revision: 2301
          http://trac.macosforge.org/projects/ruby/changeset/2301
Author:   lsansonetti at apple.com
Date:     2009-08-12 20:10:54 -0700 (Wed, 12 Aug 2009)
Log Message:
-----------
a better lazy objc class loader that is also called upon #const_defined?

Modified Paths:
--------------
    MacRuby/trunk/variable.c

Modified: MacRuby/trunk/variable.c
===================================================================
--- MacRuby/trunk/variable.c	2009-08-13 01:01:49 UTC (rev 2300)
+++ MacRuby/trunk/variable.c	2009-08-13 03:10:54 UTC (rev 2301)
@@ -1538,6 +1538,25 @@
 }
 
 static VALUE
+retrieve_dynamic_objc_class(VALUE klass, ID name)
+{
+    // Classes are typically pre-loaded by Kernel#framework but it is still
+    // useful to keep the dynamic import facility, because someone in the
+    // Objective-C world may dynamically define classes at runtime (like
+    // ScriptingBridge.framework).
+    if (klass == rb_cObject) {
+	VALUE k = (VALUE)objc_getClass(rb_id2name(name));
+	if (k != 0 && !RCLASS_RUBY(k)) {
+	     // Set the constant. Only if the returned class is a pure
+	     // Objective-C class, to avoid namespace conflicts in Ruby land.
+	    rb_const_set(klass, name, k);
+	    return k;
+	}
+    }
+    return Qnil;
+}
+
+static VALUE
 rb_const_get_0(VALUE klass, ID id, int exclude, int recurse)
 {
     VALUE value, tmp;
@@ -1577,25 +1596,9 @@
 	tmp = rb_cObject;
 	goto retry;
     }
-
-    /* Classes are typically pre-loaded by Kernel#framework but it is still
-     * useful to keep the dynamic import facility, because someone in the
-     * Objective-C world may dynamically define classes at runtime (like
-     * ScriptingBridge.framework).
-     *
-     * Note that objc_getClass does _not_ honor namespaces. Consider:
-     *
-     *  module Namespace
-     *    class RubyClass; end
-     *  end
-     *
-     * In this case objc_getClass will happily return the Namespace::RubyClass
-     * object, which is ok but _not_ when trying to find a Ruby class. So we
-     * test whether or not the found class is a pure Ruby class/module or not.
-     */
-    Class k = (Class)objc_getClass(rb_id2name(id));
-    if (k != NULL && !RCLASS_RUBY(k)) {
-	return (VALUE)k;
+    VALUE k = retrieve_dynamic_objc_class(klass, id);
+    if (k != Qnil) {
+	return k;
     }
 
     return const_missing(klass, id);
@@ -1790,6 +1793,10 @@
 	tmp = rb_cObject;
 	goto retry;
     }
+    VALUE k = retrieve_dynamic_objc_class(klass, id);
+    if (k != Qnil) {
+	return Qtrue;
+    }
     return Qfalse;
 }
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090812/a7b09e38/attachment.html>


More information about the macruby-changes mailing list