]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Implement internal multiply (for the compiler).
[cycript.git] / Execute.cpp
index b2e0e0a467a674de79b9e14f2e8d444f8a7f9337..898cd14e29d93f7c31b57a4dee0fd8dcb14d4754 100644 (file)
@@ -342,8 +342,14 @@ static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _th
     return CYCastJSValue(context, name);
 } CYCatch(NULL) }
 
+static void (*JSSynchronousGarbageCollectForDebugging$)(JSContextRef);
+
+void CYGarbageCollect(JSContextRef context) {
+    (JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context);
+}
+
 static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    JSGarbageCollect(context);
+    CYGarbageCollect(context);
     return CYJSUndefined(context);
 } CYCatch(NULL) }
 
@@ -1387,19 +1393,33 @@ JSObjectRef CYGetGlobalObject(JSContextRef context) {
     return JSContextGetGlobalObject(context);
 }
 
+class ExecutionHandle {
+  private:
+    JSContextRef context_;
+    void *handle_;
+
+  public:
+    ExecutionHandle(JSContextRef context) :
+        context_(context)
+    {
+        if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
+            handle_ = (*hooks_->ExecuteStart)(context_);
+        else
+            handle_ = NULL;
+    }
+
+    ~ExecutionHandle() {
+        if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
+            (*hooks_->ExecuteEnd)(context_, handle_);
+    }
+};
+
 const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     JSValueRef exception(NULL);
 
-    void *handle;
-    if (hooks_ != NULL && hooks_->ExecuteStart != NULL)
-        handle = (*hooks_->ExecuteStart)(context);
-    else
-        handle = NULL;
+    ExecutionHandle handle(context);
 
-    try {
-
-    JSValueRef result;
-    try {
+    JSValueRef result; try {
         result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception);
     } catch (const char *error) {
         return error;
@@ -1411,8 +1431,7 @@ const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     if (JSValueIsUndefined(context, result))
         return NULL;
 
-    const char *json;
-    try {
+    const char *json; try {
         json = CYPoolCCYON(pool, context, result, &exception);
     } catch (const char *error) {
         return error;
@@ -1424,12 +1443,6 @@ const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     CYSetProperty(context, CYGetGlobalObject(context), Result_, result);
 
     return json;
-
-    } catch (...) {
-        if (hooks_ != NULL && hooks_->ExecuteEnd != NULL)
-            (*hooks_->ExecuteEnd)(context, handle);
-        throw;
-    }
 }
 
 static bool initialized_ = false;
@@ -1440,6 +1453,7 @@ void CYInitializeDynamic() {
     else return;
 
     JSObjectMakeArray$ = reinterpret_cast<JSObjectRef (*)(JSContextRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(RTLD_DEFAULT, "JSObjectMakeArray"));
+    JSSynchronousGarbageCollectForDebugging$ = reinterpret_cast<void (*)(JSContextRef)>(dlsym(RTLD_DEFAULT, "JSSynchronousGarbageCollectForDebugging"));
 
     JSClassDefinition definition;