]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Block lambda functions should output |parameters|.
[cycript.git] / Execute.cpp
index 8c50dcd3b0bc5354c9820c51054436d09fe75652..c871ef51b1a7efe3cf092f28d45dcc3ec50980ad 100644 (file)
@@ -425,7 +425,7 @@ const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object,
     JSValueRef toCYON(CYGetProperty(context, object, toCYON_s));
     if (CYIsCallable(context, toCYON)) {
         // XXX: this needs to be abstracted behind some kind of function
-        JSValueRef arguments[1] = {CYCastJSValue(context, static_cast<double>(reinterpret_cast<uintptr_t>(&objects)))};
+        JSValueRef arguments[1] = {CYCastJSValue(context, reinterpret_cast<uintptr_t>(&objects))};
         JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 1, arguments));
         _assert(value != NULL);
         return CYPoolCString(pool, context, value);
@@ -1464,7 +1464,7 @@ static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef
 static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
     CYLocalPool pool;
-    std::ostringstream out;
+    std::stringbuf out;
     CYOptions options;
     CYOutput output(out, options);
     (new(pool) CYEncodedType(Decode(pool, internal->type_)))->Output(output, CYNoFlags);
@@ -1590,30 +1590,32 @@ class ExecutionHandle {
     }
 };
 
+static volatile bool cancel_;
+
+static bool CYShouldTerminate(JSContextRef context, void *arg) {
+    return cancel_;
+}
+
 const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     JSValueRef exception(NULL);
+    if (false) error:
+        return CYPoolCString(pool, context, CYJSString(context, exception));
 
     ExecutionHandle handle(context);
 
-    JSValueRef result; try {
-        result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
-    } catch (const char *error) {
-        return error;
-    }
+    cancel_ = false;
+    if (&JSContextGroupSetExecutionTimeLimit != NULL)
+        JSContextGroupSetExecutionTimeLimit(JSContextGetGroup(context), 0.5, &CYShouldTerminate, NULL);
 
-    if (exception != NULL) error:
-        return CYPoolCString(pool, context, CYJSString(context, exception));
+    JSValueRef result(JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception));
+    if (exception != NULL)
+        goto error;
 
     if (JSValueIsUndefined(context, result))
         return NULL;
 
-    const char *json; try {
-        std::set<void *> objects;
-        json = CYPoolCCYON(pool, context, result, objects, &exception);
-    } catch (const char *error) {
-        return error;
-    }
-
+    std::set<void *> objects;
+    const char *json(CYPoolCCYON(pool, context, result, objects, &exception));
     if (exception != NULL)
         goto error;
 
@@ -1622,6 +1624,10 @@ const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     return json;
 }
 
+void CYCancel() {
+    cancel_ = true;
+}
+
 static bool initialized_ = false;
 
 void CYInitializeDynamic() {