How to stop JavaScriptCore JSContext evaluating by force on iOS?

南笙酒味 提交于 2020-05-17 08:48:51

问题


Sometime the script being evaluated should be stopped by force, but I can't find a way to achieve this. Someone pointed out JSContextGroupSetExecutionTimeLimit might work, but It doesn't in my testing, can anyone help?

Another reference: https://github.com/phoboslab/JavaScriptCore-iOS/issues/14

My code:

int extendTerminateCallbackCalled = 0;
static bool extendTerminateCallback(JSContextRef ctx, void *context)
{
    extendTerminateCallbackCalled++;
    if (extendTerminateCallbackCalled == 2) {
        JSContextGroupRef contextGroup = JSContextGetGroup(ctx);
        JSContextGroupSetExecutionTimeLimit(contextGroup, .200f, extendTerminateCallback, 0);
        return false;
    }
    return true;
}

+ (void)stop
{
    JSGlobalContextRef ref = [_context JSGlobalContextRef];
    JSContextGroupRef contextGroup = JSContextGetGroup(ref);
    JSContextGroupSetExecutionTimeLimit(contextGroup, .200f, extendTerminateCallback, 0);
}

来源:https://stackoverflow.com/questions/61727497/how-to-stop-javascriptcore-jscontext-evaluating-by-force-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!