mfikes / ambly

ClojureScript REPL into embedded JavaScriptCore
http://ambly.fikesfarm.com
Eclipse Public License 1.0
541 stars 21 forks source link

Support JSContext being on another thread #24

Closed mfikes closed 9 years ago

mfikes commented 9 years ago

Currently Ambly simply creates a context on the main thread, and all TCP messaging with the REPL is also done on the main thread.

Frameworks like React Native want the JSContext to be on a background thread, and if you look at RCTContextExecutor you can see that it sets up an event dispatch thread loop. This could perhaps be supported by abstracting over how Ambly sends stuff into the JSContext to be evaluated (so that the app can cause REPL calls to simply be redirected to React Native's RCTContextExecutor, for example).

mfikes commented 9 years ago

Had a discussion on IRC with the resulting sentiment: Perhaps it need not be on another thread for development purposes.

mfikes commented 9 years ago

JSContext is thread safe. Its hosting JSVirtualMachine will serialize calls from different threads using locking/mutexes as needed.

So the concept of JSContext being on another thread is ill defined. It is really where the calls are coming from. ReactNative sets things up so that calls originate from the dedicated thread it sets up. But, there is really nothing wrong with REPL calls also coming in on other threads (even the main thread). If a long-running REPL evaluation is done from the main thread, it will simply block the UI for a bit.

Looked at another way, we are actually already in good shape and can eliminate some of the thread-affinity assertions sprinkled in the Ambly code.