@@ -287,6 +287,7 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> scrip
287287 // TODO t15069155: reduce the number of overrides here
288288#ifdef WITH_FBJSCEXTENSIONS
289289 if (auto fileStr = dynamic_cast <const JSBigFileString *>(script.get ())) {
290+ JSContextLock lock (m_context);
290291 JSLoadSourceStatus jsStatus;
291292 auto bcSourceCode = JSCreateSourceCodeFromFile (fileStr->fd (), jsSourceURL, nullptr , &jsStatus);
292293
@@ -295,7 +296,6 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> scrip
295296 if (!bcSourceCode) {
296297 throw std::runtime_error (" Unexpected error opening compiled bundle" );
297298 }
298-
299299 evaluateSourceCode (m_context, bcSourceCode, jsSourceURL);
300300
301301 flush ();
@@ -332,6 +332,7 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> scrip
332332#endif
333333 {
334334 String jsScript;
335+ JSContextLock lock (m_context);
335336 {
336337 SystraceSection s_ (" JSCExecutor::loadApplicationScript-createExpectingAscii" );
337338 ReactMarker::logMarker (ReactMarker::JS_BUNDLE_STRING_CONVERT_START);
@@ -433,10 +434,10 @@ void JSCExecutor::flush() {
433434
434435void JSCExecutor::callFunction (const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) {
435436 SystraceSection s (" JSCExecutor::callFunction" );
436-
437437 // This weird pattern is because Value is not default constructible.
438438 // The lambda is inlined, so there's no overhead.
439439 auto result = [&] {
440+ JSContextLock lock (m_context);
440441 try {
441442 if (!m_callFunctionReturnResultAndFlushedQueueJS) {
442443 bindBridge ();
@@ -451,13 +452,13 @@ void JSCExecutor::callFunction(const std::string& moduleId, const std::string& m
451452 std::runtime_error (" Error calling " + moduleId + " ." + methodId));
452453 }
453454 }();
454-
455455 callNativeModules (std::move (result));
456456}
457457
458458void JSCExecutor::invokeCallback (const double callbackId, const folly::dynamic& arguments) {
459459 SystraceSection s (" JSCExecutor::invokeCallback" );
460460 auto result = [&] {
461+ JSContextLock lock (m_context);
461462 try {
462463 if (!m_invokeCallbackAndReturnFlushedQueueJS) {
463464 bindBridge ();
@@ -471,29 +472,29 @@ void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic&
471472 std::runtime_error (folly::to<std::string>(" Error invoking callback " , callbackId)));
472473 }
473474 }();
474-
475475 callNativeModules (std::move (result));
476476}
477477
478478Value JSCExecutor::callFunctionSyncWithValue (
479479 const std::string& module , const std::string& method, Value args) {
480480 SystraceSection s (" JSCExecutor::callFunction" );
481-
482- if (!m_callFunctionReturnResultAndFlushedQueueJS) {
483- bindBridge ();
484- }
485- Object result = m_callFunctionReturnResultAndFlushedQueueJS->callAsFunction ({
486- Value (m_context, String::createExpectingAscii (m_context, module )),
487- Value (m_context, String::createExpectingAscii (m_context, method)),
488- std::move (args),
489- }).asObject ();
481+ Object result = [&] {
482+ JSContextLock lock (m_context);
483+ if (!m_callFunctionReturnResultAndFlushedQueueJS) {
484+ bindBridge ();
485+ }
486+ return m_callFunctionReturnResultAndFlushedQueueJS->callAsFunction ({
487+ Value (m_context, String::createExpectingAscii (m_context, module )),
488+ Value (m_context, String::createExpectingAscii (m_context, method)),
489+ std::move (args),
490+ }).asObject ();
491+ }();
490492
491493 Value length = result.getProperty (" length" );
492494
493495 if (!length.isNumber () || length.asInteger () != 2 ) {
494496 std::runtime_error (" Return value of a callFunction must be an array of size 2" );
495497 }
496-
497498 callNativeModules (result.getPropertyAtIndex (1 ));
498499 return result.getPropertyAtIndex (0 );
499500}
0 commit comments