diff options
author | 2023-07-17 23:07:09 -0700 | |
---|---|---|
committer | 2023-07-17 23:07:09 -0700 | |
commit | eaff66b098074a32d3d145eb1e56a3bdfaea37b3 (patch) | |
tree | 0c75a1c66d9b9b87c10153bd16000f431a296454 | |
parent | b760d1da30c343a98600f8693b5455e00e3f47c5 (diff) | |
download | bun-eaff66b098074a32d3d145eb1e56a3bdfaea37b3.tar.gz bun-eaff66b098074a32d3d145eb1e56a3bdfaea37b3.tar.zst bun-eaff66b098074a32d3d145eb1e56a3bdfaea37b3.zip |
Emit writeBarrier in `napi_module_register`
-rw-r--r-- | src/bun.js/bindings/napi.cpp | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp index c4bf7fc32..6e47db8c4 100644 --- a/src/bun.js/bindings/napi.cpp +++ b/src/bun.js/bindings/napi.cpp @@ -496,7 +496,10 @@ extern "C" void napi_module_register(napi_module* mod) auto* globalObject = Bun__getDefaultGlobal(); JSC::VM& vm = globalObject->vm(); globalObject->napiModuleRegisterCallCount++; - JSC::JSObject* object = globalObject->pendingNapiModule.getObject(); + JSValue pendingNapiModule = globalObject->pendingNapiModule; + JSObject* object = (pendingNapiModule && pendingNapiModule.isObject()) ? pendingNapiModule.getObject() + : nullptr; + if (!object) { object = JSC::constructEmptyObject(globalObject); } else { @@ -504,20 +507,23 @@ extern "C" void napi_module_register(napi_module* mod) } EnsureStillAliveScope ensureAlive(object); - auto result = reinterpret_cast<JSC::EncodedJSValue>( - mod->nm_register_func(reinterpret_cast<napi_env>(globalObject), reinterpret_cast<napi_value>(JSC::JSValue::encode(JSC::JSValue(object))))); + auto resultValue = toJS( + mod->nm_register_func(toNapi(globalObject), toNapi(object))); auto keyStr = WTF::String::fromUTF8(mod->nm_modname); - JSC::JSValue resultValue = JSC::JSValue::decode(result); EnsureStillAliveScope ensureAlive2(resultValue); if (resultValue.isEmpty()) { - globalObject->pendingNapiModule = createError(globalObject, makeString("Node-API module \""_s, keyStr, "\" returned an error"_s)); + JSValue errorInstance = createError(globalObject, makeString("Node-API module \""_s, keyStr, "\" returned an error"_s)); + globalObject->pendingNapiModule = errorInstance; + vm.writeBarrier(globalObject, errorInstance); EnsureStillAliveScope ensureAlive(globalObject->pendingNapiModule); return; } if (!resultValue.isObject()) { - globalObject->pendingNapiModule = createError(globalObject, makeString("Expected Node-API module \""_s, keyStr, "\" to return an exports object"_s)); + JSValue errorInstance = createError(globalObject, makeString("Expected Node-API module \""_s, keyStr, "\" to return an exports object"_s)); + globalObject->pendingNapiModule = errorInstance; + vm.writeBarrier(globalObject, errorInstance); EnsureStillAliveScope ensureAlive(globalObject->pendingNapiModule); return; } @@ -532,7 +538,9 @@ extern "C" void napi_module_register(napi_module* mod) // Add it to the ESM registry globalObject->moduleLoader()->provideFetch(globalObject, JSC::jsString(vm, WTFMove(keyStr)), WTFMove(source)); + globalObject->pendingNapiModule = object; + vm.writeBarrier(globalObject, object); } extern "C" napi_status napi_wrap(napi_env env, |