JSContextLock::JSContextLock(JSGlobalContextRef ctx) noexcept #if WITH_FBJSCEXTENSIONS : ctx_(ctx), globalLock_(PTHREAD_MUTEX_INITIALIZER) { WTFThreadData& threadData = wtfThreadData(); // Code below is responsible for acquiring locks. It should execute // atomically, thus none of the functions invoked from now on are allowed to // throw an exception try { if (!threadData.isDebuggerThread()) { CHECK(0 == pthread_mutex_lock(&globalLock_)); } JSLock(ctx_); } catch (...) { abort(); } }
Value Value::fromDynamic(JSContextRef ctx, const folly::dynamic& value) { // JavaScriptCore's iOS APIs have their own version of this direct conversion. // In addition, using this requires exposing some of JSC's private APIs, // so it's limited to non-apple platforms and to builds that use the custom JSC. // Otherwise, we use the old way of converting through JSON. #if USE_FAST_FOLLY_DYNAMIC_CONVERSION // Defer GC during the creation of the JSValue, as we don't want // intermediate objects to be collected. // We could use JSValueProtect(), but it will make the process much slower. JSDeferredGCRef deferGC = JSDeferGarbageCollection(ctx); // Set a global lock for the whole process, // instead of re-acquiring the lock for each operation. JSLock(ctx); JSValueRef jsVal = Value::fromDynamicInner(ctx, value); JSUnlock(ctx); JSResumeGarbageCollection(ctx, deferGC); return Value(ctx, jsVal); #else auto json = folly::toJson(value); return fromJSON(String(ctx, json.c_str())); #endif }