Reputation: 2897
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);
}
Upvotes: 2
Views: 448
Reputation: 257789
Here is an idea of possible approach
+ (void)stop
{
JSContext *context = [JSContext currentContext]; // or whichever your
// set property of JS global object to specific know value
// that should be checked inside JS code regularly during
// long operations and interrupt self execution
context[@"externallyCancelled"] = @(YES);
}
Upvotes: 2