]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Error instances that weren't thrown have no stack.
[cycript.git] / Execute.cpp
index 2cd5765e343207138c55c78f3d07c2510cb74b46..1c83e26e9252cec2b189c249321e96cf82faca90 100644 (file)
@@ -1137,12 +1137,13 @@ JSObjectRef CYMakeType(JSContextRef context, sig::Signature *signature) {
     return CYMakeType(context, &type);
 }
 
-extern "C" const char *CYBridgeHash(CYPool &pool, CYUTF8String name) {
+extern "C" bool CYBridgeHash(CYPool &pool, CYUTF8String name, const char *&code, unsigned &flags) {
     sqlite3_stmt *statement;
 
     _sqlcall(sqlite3_prepare(database_,
         "select "
-            "\"cache\".\"value\" "
+            "\"cache\".\"code\", "
+            "\"cache\".\"flags\" "
         "from \"cache\" "
         "where"
             " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM " and"
@@ -1152,14 +1153,17 @@ extern "C" const char *CYBridgeHash(CYPool &pool, CYUTF8String name) {
 
     _sqlcall(sqlite3_bind_text(statement, 1, name.data, name.size, SQLITE_STATIC));
 
-    const char *value;
+    bool success;
     if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE)
-        value = NULL;
-    else
-        value = sqlite3_column_pooled(pool, statement, 0);
+        success = false;
+    else {
+        success = true;
+        code = sqlite3_column_pooled(pool, statement, 0);
+        flags = sqlite3_column_int(statement, 1);
+    }
 
     _sqlcall(sqlite3_finalize(statement));
-    return value;
+    return success;
 }
 
 static bool All_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
@@ -1176,7 +1180,9 @@ static bool All_hasProperty(JSContextRef context, JSObjectRef object, JSStringRe
                 return true;
 
     CYPool pool;
-    if (CYBridgeHash(pool, CYPoolUTF8String(pool, context, property)) != NULL)
+    const char *code;
+    unsigned flags;
+    if (CYBridgeHash(pool, CYPoolUTF8String(pool, context, property), code, flags))
         return true;
 
     return false;
@@ -1197,10 +1203,24 @@ static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSSt
                     return value;
 
     CYPool pool;
-    if (const char *code = CYBridgeHash(pool, CYPoolUTF8String(pool, context, property))) {
-        JSValueRef result(_jsccall(JSEvaluateScript, context, CYJSString(CYPoolCode(pool, code)), NULL, NULL, 0));
-        JSObjectRef cache(CYGetCachedObject(context, CYJSString("cache")));
-        CYSetProperty(context, cache, property, result);
+    const char *code;
+    unsigned flags;
+    if (CYBridgeHash(pool, CYPoolUTF8String(pool, context, property), code, flags)) {
+        CYUTF8String parsed;
+
+        try {
+            parsed = CYPoolCode(pool, code);
+        } catch (const CYException &error) {
+            CYThrow("%s", pool.strcat("error caching ", CYPoolCString(pool, context, property), ": ", error.PoolCString(pool), NULL));
+        }
+
+        JSValueRef result(_jsccall(JSEvaluateScript, context, CYJSString(parsed), NULL, NULL, 0));
+
+        if (flags == 0) {
+            JSObjectRef cache(CYGetCachedObject(context, CYJSString("cache")));
+            CYSetProperty(context, cache, property, result);
+        }
+
         return result;
     }
 
@@ -1216,16 +1236,30 @@ static JSValueRef All_complete_callAsFunction(JSContextRef context, JSObjectRef
 
     sqlite3_stmt *statement;
 
-    _sqlcall(sqlite3_prepare(database_,
-        "select "
-            "\"cache\".\"name\" "
-        "from \"cache\" "
-        "where"
-            " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM " and"
-            " \"cache\".\"name\" like ? || '%'"
-    , -1, &statement, NULL));
-
-    _sqlcall(sqlite3_bind_text(statement, 1, prefix.data, prefix.size, SQLITE_STATIC));
+    if (prefix.size == 0)
+        _sqlcall(sqlite3_prepare(database_,
+            "select "
+                "\"cache\".\"name\" "
+            "from \"cache\" "
+            "where"
+                " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM
+        , -1, &statement, NULL));
+    else {
+        _sqlcall(sqlite3_prepare(database_,
+            "select "
+                "\"cache\".\"name\" "
+            "from \"cache\" "
+            "where"
+                " \"cache\".\"name\" >= ? and \"cache\".\"name\" < ? and "
+                " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM
+        , -1, &statement, NULL));
+
+        _sqlcall(sqlite3_bind_text(statement, 1, prefix.data, prefix.size, SQLITE_STATIC));
+
+        char *after(pool.strndup(prefix.data, prefix.size));
+        ++after[prefix.size - 1];
+        _sqlcall(sqlite3_bind_text(statement, 2, after, prefix.size, SQLITE_STATIC));
+    }
 
     while (_sqlcall(sqlite3_step(statement)) != SQLITE_DONE)
         values.push_back(CYCastJSValue(context, CYJSString(sqlite3_column_string(statement, 0))));
@@ -2035,6 +2069,10 @@ static const char *CYPoolLibraryPath(CYPool &pool) {
     _assert(slash != NULL);
     *slash = '\0';
 
+    slash = strrchr(lib, '/');
+    if (slash != NULL && strcmp(slash, "/.libs") == 0)
+        *slash = '\0';
+
     return lib;
 }
 
@@ -2098,7 +2136,7 @@ static JSValueRef require_callAsFunction(JSContextRef context, JSObjectRef objec
 
 static bool CYRunScript(JSGlobalContextRef context, const char *path) {
     CYPool pool;
-    CYUTF8String code(CYPoolFileUTF8String(pool, path));
+    CYUTF8String code(CYPoolFileUTF8String(pool, pool.strcat(CYPoolLibraryPath(pool), path, NULL)));
     if (code.data == NULL)
         return false;
 
@@ -2250,7 +2288,7 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) {
 
     CYArrayPush(context, alls, cycript);
 
-    CYRunScript(context, "libcycript.cy");
+    CYRunScript(context, "/libcycript.cy");
 }
 
 static JSGlobalContextRef context_;