]> git.saurik.com Git - cycript.git/blobdiff - Library.cpp
Got true JS booleans working in GNUstep.
[cycript.git] / Library.cpp
index a981b2bcb8281322f68e62c41d591641a13cb9d3..7bc867bd43f9c7c5009094ceb3b1a2ad2a9852b8 100644 (file)
@@ -186,19 +186,25 @@ const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef val
 /* Index Offsets {{{ */
 size_t CYGetIndex(const CYUTF8String &value) {
     if (value.data[0] != '0') {
-        char *end;
-        size_t index(strtoul(value.data, &end, 10));
-        if (value.data + value.size == end)
-            return index;
-    } else if (value.data[1] == '\0')
+        size_t index(0);
+        for (size_t i(0); i != value.size; ++i) {
+            if (!DigitRange_[value.data[i]])
+                return _not(size_t);
+            index *= 10;
+            index += value.data[i] - '0';
+        }
+        return index;
+    } else if (value.size == 1)
         return 0;
-    return _not(size_t);
+    else
+        return _not(size_t);
 }
 
 size_t CYGetIndex(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
     return CYGetIndex(CYPoolUTF8String(pool, context, value));
 }
 
+// XXX: this isn't actually right
 bool CYGetOffset(const char *value, ssize_t &index) {
     if (value[0] != '0') {
         char *end;
@@ -1021,7 +1027,7 @@ static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JS
         name = elements[index].name;
 
         if (name == NULL) {
-            sprintf(number, "%lu", index);
+            sprintf(number, "%zu", index);
             name = number;
         }
 
@@ -1118,17 +1124,21 @@ static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object,
 
         case 0:
             return JSEvaluateScript(CYGetJSContext(), CYJSString(value), NULL, NULL, 0, NULL);
-        case 1:
-            return CYMakeFunctor(context, reinterpret_cast<void (*)()>(CYCastSymbol(name.data)), value);
 
-        case 2: {
-            // XXX: this is horrendously inefficient
-            sig::Signature signature;
-            sig::Parse(pool, &signature, value, &Structor_);
-            ffi_cif cif;
-            sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
-            return CYFromFFI(context, signature.elements[0].type, cif.rtype, CYCastSymbol(name.data));
-        }
+        case 1:
+            if (void (*symbol)() = reinterpret_cast<void (*)()>(CYCastSymbol(name.data)))
+                return CYMakeFunctor(context, symbol, value);
+            else return NULL;
+
+        case 2:
+            if (void *symbol = CYCastSymbol(name.data)) {
+                // XXX: this is horrendously inefficient
+                sig::Signature signature;
+                sig::Parse(pool, &signature, value, &Structor_);
+                ffi_cif cif;
+                sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
+                return CYFromFFI(context, signature.elements[0].type, cif.rtype, symbol);
+            } else return NULL;
 
         // XXX: implement case 3
         case 4: