]> git.saurik.com Git - cycript.git/blobdiff - Library.cpp
Forgot to add lexer to clean.
[cycript.git] / Library.cpp
index 555cd8a2e95ad7b35c31218d680932b3fee03275..d80978e35694949710702e3577e5ebc374226b9e 100644 (file)
@@ -140,20 +140,29 @@ static CYUTF16String CYCastUTF16String(JSStringRef value) {
     return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value));
 }
 
+template <typename Type_>
+_finline size_t iconv_(size_t (*iconv)(iconv_t, Type_, size_t *, char **, size_t *), iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) {
+    return iconv(cd, const_cast<Type_>(inbuf), inbytesleft, outbuf, outbytesleft);
+}
+
 static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
     _assert(pool != NULL);
 
     CYUTF16String utf16(CYCastUTF16String(value));
     const char *in(reinterpret_cast<const char *>(utf16.data));
 
+#ifdef __GLIBC__
+    iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2")));
+#else
     iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL")));
+#endif
 
     size_t size(JSStringGetMaximumUTF8CStringSize(value));
     char *out(new(pool) char[size]);
     CYUTF8String utf8(out, size);
 
     size = utf16.size * 2;
-    _syscall(iconv(conversion, const_cast<char **>(&in), &size, &out, &utf8.size));
+    _syscall(iconv_(&iconv, conversion, const_cast<char **>(&in), &size, &out, &utf8.size));
 
     *out = '\0';
     utf8.size = out - utf8.data;
@@ -241,8 +250,9 @@ void CYStringify(std::ostringstream &str, const char *data, size_t size) {
             break;
 
             default:
+                // this test is designed to be "awewsome", generating neither warnings nor incorrect results
                 if (*value < 0x20 || *value >= 0x7f)
-                    str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
+                    str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value));
                 else simple:
                     str << *value;
         }
@@ -1036,7 +1046,11 @@ JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups,
     }
 
     uint8_t value[cif->rtype->size];
-    ffi_call(cif, function, value, values);
+
+    if (hooks_ != NULL && hooks_->CallFunction != NULL)
+        (*hooks_->CallFunction)(context, cif, function, value, values);
+    else
+        ffi_call(cif, function, value, values);
 
     return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize);
 } CYCatch }
@@ -1288,14 +1302,24 @@ static JSStaticFunction Type_staticFunctions[4] = {
     {NULL, NULL, 0}
 };
 
+static JSObjectRef (*$JSObjectMakeArray)(JSContextRef, size_t, const JSValueRef[], JSValueRef *);
+
 void CYSetArgs(int argc, const char *argv[]) {
     JSContextRef context(CYGetJSContext());
     JSValueRef args[argc];
     for (int i(0); i != argc; ++i)
         args[i] = CYCastJSValue(context, argv[i]);
-    JSValueRef exception(NULL);
-    JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception));
-    CYThrow(context, exception);
+
+    JSObjectRef array;
+    if ($JSObjectMakeArray != NULL) {
+        JSValueRef exception(NULL);
+        array = (*$JSObjectMakeArray)(context, argc, args, &exception);
+        CYThrow(context, exception);
+    } else {
+        JSValueRef value(CYCallAsFunction(context, Array_, NULL, argc, args));
+        array = CYCastJSObject(context, value);
+    }
+
     CYSetProperty(context, System_, CYJSString("args"), array);
 }
 
@@ -1309,7 +1333,7 @@ const char *CYExecute(apr_pool_t *pool, const char *code) {
 
     void *handle;
     if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
-        handle = (*hooks_->ExecuteStart)();
+        handle = (*hooks_->ExecuteStart)(context);
     else
         handle = NULL;
 
@@ -1341,7 +1365,7 @@ const char *CYExecute(apr_pool_t *pool, const char *code) {
     CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
 
     if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
-        (*hooks_->ExecuteEnd)(handle);
+        (*hooks_->ExecuteEnd)(context, handle);
     return json;
 }
 
@@ -1357,6 +1381,8 @@ void CYInitialize() {
     _aprcall(apr_initialize());
     _aprcall(apr_pool_create(&Pool_, NULL));
     _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_));
+
+    $JSObjectMakeArray = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray"));
 }
 
 apr_pool_t *CYGetGlobalPool() {
@@ -1543,7 +1569,10 @@ JSGlobalContextRef CYGetJSContext() {
 
         Result_ = JSStringCreateWithUTF8CString("_");
 
+// XXX: this is very wrong and sad
+#ifdef __APPLE__
         CYObjectiveC(context, global);
+#endif
     }
 
     return Context_;