[macruby-changes] [4830] MacRuby/trunk/bridgesupport.cpp

source_changes at macosforge.org source_changes at macosforge.org
Tue Oct 26 15:40:19 PDT 2010


Revision: 4830
          http://trac.macosforge.org/projects/ruby/changeset/4830
Author:   lsansonetti at apple.com
Date:     2010-10-26 15:40:17 -0700 (Tue, 26 Oct 2010)
Log Message:
-----------
Pointer.new: type shortcuts must now always be symbols (strings will not be interpreted as shortcuts but as real types) + add shortcuts for :selector/:sel for :

Modified Paths:
--------------
    MacRuby/trunk/bridgesupport.cpp

Modified: MacRuby/trunk/bridgesupport.cpp
===================================================================
--- MacRuby/trunk/bridgesupport.cpp	2010-10-26 01:48:07 UTC (rev 4829)
+++ MacRuby/trunk/bridgesupport.cpp	2010-10-26 22:40:17 UTC (rev 4830)
@@ -1301,74 +1301,59 @@
 
 // FFI
 
-static const char *
-convert_ffi_type(VALUE type, bool raise_exception_if_unknown)
+static std::map<ID, std::string> ffi_type_shortcuts;
+
+static void
+init_ffi_type_shortcuts(void)
 {
-    const char *typestr = TYPE(type) == T_SYMBOL
-	? rb_sym2name(type)
-	: StringValueCStr(type);
+#define SHORTCUT(name, type) \
+    ffi_type_shortcuts.insert(std::make_pair(rb_intern(name), type))
 
-    assert(typestr != NULL);
-
     // Ruby-FFI types.
+    SHORTCUT("char", "c");
+    SHORTCUT("uchar", "C");
+    SHORTCUT("short", "s");
+    SHORTCUT("ushort", "S");
+    SHORTCUT("int", "i");
+    SHORTCUT("uint", "I");
+    SHORTCUT("long", "l");
+    SHORTCUT("ulong", "L");
+    SHORTCUT("long_long", "q");
+    SHORTCUT("ulong_long", "Q");
+    SHORTCUT("float", "f");
+    SHORTCUT("double", "d");
+    SHORTCUT("string", "*");
+    SHORTCUT("pointer", "^");
 
-    if (strcmp(typestr, "char") == 0) {
-	return "c";
-    }
-    if (strcmp(typestr, "uchar") == 0) {
-	return "C";
-    }
-    if (strcmp(typestr, "short") == 0) {
-	return "s";
-    }
-    if (strcmp(typestr, "ushort") == 0) {
-	return "S";
-    }
-    if (strcmp(typestr, "int") == 0) {
-	return "i";
-    }
-    if (strcmp(typestr, "uint") == 0) {
-	return "I";
-    }
-    if (strcmp(typestr, "long") == 0) {
-	return "l";
-    }
-    if (strcmp(typestr, "ulong") == 0) {
-	return "L";
-    }
-    if (strcmp(typestr, "long_long") == 0) {
-	return "q";
-    }
-    if (strcmp(typestr, "ulong_long") == 0) {
-	return "Q";
-    }
-    if (strcmp(typestr, "float") == 0) {
-	return "f";
-    }
-    if (strcmp(typestr, "double") == 0) {
-	return "d";
-    }
-    if (strcmp(typestr, "string") == 0) {
-	return "*";
-    }
-    if (strcmp(typestr, "pointer") == 0) {
-	return "^";
-    }
-
     // MacRuby extensions.
+    SHORTCUT("object", "@");
+    SHORTCUT("id", "@");
+    SHORTCUT("class", "#");
+    SHORTCUT("boolean", "B");
+    SHORTCUT("bool", "B");
+    SHORTCUT("selector", ":");
+    SHORTCUT("sel", ":");
 
-    if (strcmp(typestr, "object") == 0 || strcmp(typestr, "id") == 0) {
-	return "@";
+#undef SHORTCUT
+}
+
+static const char *
+convert_ffi_type(VALUE type, bool raise_exception_if_unknown)
+{
+    // Only accept symbols as type shortcuts.
+    if (TYPE(type) != T_SYMBOL) {
+	return StringValueCStr(type);
     }
-    if (strcmp(typestr, "boolean") == 0 || strcmp(typestr, "bool") == 0) {
-	return "B";
-    }
 
-    if (raise_exception_if_unknown) {
+    std::map<ID, std::string>::iterator iter =
+	ffi_type_shortcuts.find(SYM2ID(type));
+
+    if (iter == ffi_type_shortcuts.end()) {
 	rb_raise(rb_eTypeError, "unrecognized string `%s' given as FFI type",
-		typestr);
+		rb_id2name(SYM2ID(type)));
     }
-    return typestr;
+
+    return iter->second.c_str();
 }
 
 Function *
@@ -1534,6 +1519,8 @@
     VALUE mFFILib = rb_define_module_under(mFFI, "Library");
     rb_objc_define_method(mFFILib, "attach_function",
 	    (void *)rb_ffi_attach_function, 3);
+
+    init_ffi_type_shortcuts();
 #endif
 }
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101026/0d5e6830/attachment.html>


More information about the macruby-changes mailing list