Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way stopping or pausing the execution of Javascript in a JSContext object?

I'm developing an iOS 7 app that has scripting capabilities using JavascriptCore.

I'd like to have a way to pause or completely stop the code currently running on the JSContext. JavascriptCore isn't really well documented so I couldn't find an answer.

I have a few ideas:

  • Remove from the context the bridge object used to interact with my app and just let any code still running fail.
  • Getting the JSGlobalContextRef for my JSContext and releasing it using JSGlobalContextRelease and recreating a JSContext to use.

but hopefully there are better ways.

Any help would be appreciated. Thanks!

like image 465
Jorge Cohen Avatar asked Feb 13 '14 09:02

Jorge Cohen


1 Answers

There actually is an API that allows to stop execution of Javascript code inside of JavascriptCore. Unfortunately though, it is private and thus cannot be used within apps that are to be distributed through the App Store.

The API in question is the JSContextGroupSetExecutionTimeLimit function defined in JSContextRefPrivate. Not only does it allow to specify a timeout for your JS code, but also a callback that will be called regularly to determine whether the execution should be stopped.

You can find more information in my answer to this SO question.

like image 66
FD_ Avatar answered Oct 29 '22 01:10

FD_