X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/012c7408a0ace1ba4bdac9318bae201bc25b1f19..a5662a5364816f852acb056d5c2b2fc94e0c0a5c:/Execute.cpp?ds=sidebyside diff --git a/Execute.cpp b/Execute.cpp index 8c50dcd..9f45354 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -368,7 +368,7 @@ static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _th static void (*JSSynchronousGarbageCollectForDebugging$)(JSContextRef); -void CYGarbageCollect(JSContextRef context) { +_visible void CYGarbageCollect(JSContextRef context) { (JSSynchronousGarbageCollectForDebugging$ ?: &JSGarbageCollect)(context); } @@ -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(reinterpret_cast(&objects)))}; + JSValueRef arguments[1] = {CYCastJSValue(context, reinterpret_cast(&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(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); @@ -1538,7 +1538,7 @@ static JSStaticFunction Type_staticFunctions[14] = { static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *); -void CYSetArgs(int argc, const char *argv[]) { +_visible void CYSetArgs(int argc, const char *argv[]) { JSContextRef context(CYGetJSContext()); JSValueRef args[argc]; for (int i(0); i != argc; ++i) @@ -1590,30 +1590,32 @@ class ExecutionHandle { } }; -const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) { +static volatile bool cancel_; + +static bool CYShouldTerminate(JSContextRef context, void *arg) { + return cancel_; +} + +_visible 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 objects; - json = CYPoolCCYON(pool, context, result, objects, &exception); - } catch (const char *error) { - return error; - } - + std::set 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; } +_visible void CYCancel() { + cancel_ = true; +} + static bool initialized_ = false; void CYInitializeDynamic() { @@ -1753,8 +1759,6 @@ JSGlobalContextRef CYGetJSContext(JSContextRef context) { return reinterpret_cast(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_; } -extern "C" bool CydgetMemoryParse(const uint16_t **data, size_t *size); - void *CYMapFile(const char *path, size_t *psize) { int fd(_syscall_(open(path, O_RDONLY), 1, ENOENT)); if (fd == -1) @@ -1954,7 +1958,7 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { static JSGlobalContextRef context_; -JSGlobalContextRef CYGetJSContext() { +_visible JSGlobalContextRef CYGetJSContext() { CYInitializeDynamic(); if (context_ == NULL) { @@ -1965,7 +1969,7 @@ JSGlobalContextRef CYGetJSContext() { return context_; } -void CYDestroyContext() { +_visible void CYDestroyContext() { if (context_ == NULL) return; JSGlobalContextRelease(context_);