]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Regular expressions cannot have alphabetic follow.
[cycript.git] / Execute.cpp
index 1c7b833ab560cc69e37c08b9a665ef2a7edc00d3..70751a4b2555a331c9d05a98c53f032bb4cf8b83 100644 (file)
@@ -1,5 +1,5 @@
 /* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2014  Jay Freeman (saurik)
+ * Copyright (C) 2009-2015  Jay Freeman (saurik)
 */
 
 /* GNU Affero General Public License, Version 3 {{{ */
@@ -261,6 +261,10 @@ JSObjectRef CYMakeStruct(JSContextRef context, void *data, sig::Type *type, ffi_
 }
 
 static void *CYCastSymbol(const char *name) {
+    for (CYHook *hook : GetHooks())
+        if (hook->CastSymbol != NULL)
+            if (void *value = (*hook->CastSymbol)(name))
+                return value;
     return dlsym(RTLD_DEFAULT, name);
 }
 
@@ -421,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);
@@ -748,7 +752,7 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void
     }
 }
 
-void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) {
+void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg) {
     Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
 
     JSContextRef context(internal->context_);
@@ -759,7 +763,7 @@ void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, J
     for (size_t index(0); index != count; ++index)
         values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
 
-    JSValueRef value(adapter(context, count, values, internal->function_));
+    JSValueRef value(internal->adapter_(context, count, values, internal->function_));
     CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
 }
 
@@ -767,20 +771,16 @@ static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRe
     return CYCallAsFunction(context, function, NULL, count, values);
 }
 
-static void FunctionClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
-    CYExecuteClosure(cif, result, arguments, arg, &FunctionAdapter_);
-}
-
-Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, void (*callback)(ffi_cif *, void *, void **, void *)) {
+Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)) {
     // XXX: in case of exceptions this will leak
     // XXX: in point of fact, this may /need/ to leak :(
-    Closure_privateData *internal(new Closure_privateData(context, function, signature));
+    Closure_privateData *internal(new Closure_privateData(context, function, adapter, signature));
 
 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
     void *executable;
     ffi_closure *writable(reinterpret_cast<ffi_closure *>(ffi_closure_alloc(sizeof(ffi_closure), &executable)));
 
-    ffi_status status(ffi_prep_closure_loc(writable, &internal->cif_, callback, internal, executable));
+    ffi_status status(ffi_prep_closure_loc(writable, &internal->cif_, &CYExecuteClosure, internal, executable));
     _assert(status == FFI_OK);
 
     internal->value_ = executable;
@@ -791,7 +791,7 @@ Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function,
         -1, 0
     )));
 
-    ffi_status status(ffi_prep_closure(closure, &internal->cif_, callback, internal));
+    ffi_status status(ffi_prep_closure(closure, &internal->cif_, &CYExecuteClosure, internal));
     _assert(status == FFI_OK);
 
     _syscall(mprotect(closure, sizeof(*closure), PROT_READ | PROT_EXEC));
@@ -803,7 +803,7 @@ Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function,
 }
 
 static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, const sig::Signature &signature) {
-    Closure_privateData *internal(CYMakeFunctor_(context, function, signature, &FunctionClosure_));
+    Closure_privateData *internal(CYMakeFunctor_(context, function, signature, &FunctionAdapter_));
     JSObjectRef object(JSObjectMake(context, Functor_, internal));
     // XXX: see above notes about needing to leak
     JSValueProtect(CYGetJSContext(context), object);
@@ -990,6 +990,10 @@ static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JS
     }
 }
 
+void CYCallFunction(CYPool &pool, JSContextRef context, ffi_cif *cif, void (*function)(), void *value, void **values) {
+    ffi_call(cif, function, value, values);
+}
+
 JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, sig::Signature *signature, ffi_cif *cif, void (*function)()) {
     if (setups + count != signature->count - 1)
         throw CYJSError(context, "incorrect number of arguments to ffi function");
@@ -1008,15 +1012,13 @@ JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, voi
 
     uint8_t value[cif->rtype->size];
 
+    void (*call)(CYPool &, JSContextRef, ffi_cif *, void (*)(), void *, void **) = &CYCallFunction;
+    // XXX: this only supports one hook, but it is a bad idea anyway
     for (CYHook *hook : GetHooks())
-        if (hook->CallFunction != NULL) {
-            // XXX: this only supports one hook, but it is a bad idea anyway
-            (*hook->CallFunction)(context, cif, function, value, values);
-            goto from;
-        }
-    ffi_call(cif, function, value, values);
+        if (hook->CallFunction != NULL)
+            call = hook->CallFunction;
 
-  from:
+    call(pool, context, cif, function, value, values);
     return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize);
 }
 
@@ -1588,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;
 
@@ -1620,6 +1624,10 @@ const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
     return json;
 }
 
+void CYCancel() {
+    cancel_ = true;
+}
+
 static bool initialized_ = false;
 
 void CYInitializeDynamic() {
@@ -1831,6 +1839,9 @@ static JSValueRef require(JSContextRef context, JSObjectRef object, JSObjectRef
     return CYGetProperty(context, module, property);
 } CYCatch(NULL) }
 
+extern "C" void CYDestroyWeak(JSWeakObjectMapRef weak, void *data) {
+}
+
 extern "C" void CYSetupContext(JSGlobalContextRef context) {
     CYInitializeDynamic();
 
@@ -1932,7 +1943,7 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) {
 
 #ifdef __APPLE__
     if (&JSWeakObjectMapCreate != NULL) {
-        JSWeakObjectMapRef weak(JSWeakObjectMapCreate(context, NULL, NULL));
+        JSWeakObjectMapRef weak(JSWeakObjectMapCreate(context, NULL, &CYDestroyWeak));
         CYSetProperty(context, cy, weak_s, CYCastJSValue(context, reinterpret_cast<uintptr_t>(weak)));
     }
 #endif