diff options
author | 2022-09-17 21:56:30 -0700 | |
---|---|---|
committer | 2022-09-17 21:56:30 -0700 | |
commit | a08b323e617c6e2db6c6bc2e1731f447b43ccb50 (patch) | |
tree | 58068b571282a1d194a716a59c819ac04cfca714 | |
parent | 745cc5c65502f1c1bcd69392e526d63f04653e11 (diff) | |
download | bun-a08b323e617c6e2db6c6bc2e1731f447b43ccb50.tar.gz bun-a08b323e617c6e2db6c6bc2e1731f447b43ccb50.tar.zst bun-a08b323e617c6e2db6c6bc2e1731f447b43ccb50.zip |
Fix napi module registration
Fixes https://github.com/oven-sh/bun/issues/1240
-rw-r--r-- | src/bun.js/bindings/Process.cpp | 17 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.h | 4 | ||||
-rw-r--r-- | src/bun.js/bindings/napi.cpp | 2 |
3 files changed, 17 insertions, 6 deletions
diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp index c3a98d876..b4297edab 100644 --- a/src/bun.js/bindings/Process.cpp +++ b/src/bun.js/bindings/Process.cpp @@ -114,6 +114,7 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen, (JSC::JSGlobalObject * globalObject_, JSC::CallFrame* callFrame)) { Zig::GlobalObject* globalObject = reinterpret_cast<Zig::GlobalObject*>(globalObject_); + auto callCountAtStart = globalObject->napiModuleRegisterCallCount; auto scope = DECLARE_THROW_SCOPE(globalObject->vm()); JSC::VM& vm = globalObject->vm(); @@ -144,15 +145,19 @@ static JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen, return JSC::JSValue::encode(JSC::JSValue {}); } - if (JSValue pendingModule = globalObject->pendingNapiModule) { + if (callCountAtStart != globalObject->napiModuleRegisterCallCount) { + JSValue pendingModule = globalObject->pendingNapiModule; globalObject->pendingNapiModule = JSValue {}; - if (pendingModule.isCell() && pendingModule.getObject()->isErrorInstance()) { - JSC::throwException(globalObject, scope, pendingModule); - return JSC::JSValue::encode(JSC::JSValue {}); + globalObject->napiModuleRegisterCallCount = 0; + + if (pendingModule) { + if (pendingModule.isCell() && pendingModule.getObject()->isErrorInstance()) { + JSC::throwException(globalObject, scope, pendingModule); + return JSC::JSValue::encode(JSC::JSValue {}); + } + return JSC::JSValue::encode(pendingModule); } - return JSC::JSValue::encode(pendingModule); } - globalObject->pendingNapiModule = JSValue {}; JSC::EncodedJSValue (*napi_register_module_v1)(JSC::JSGlobalObject * globalObject, JSC::EncodedJSValue exports); diff --git a/src/bun.js/bindings/ZigGlobalObject.h b/src/bun.js/bindings/ZigGlobalObject.h index bd065ee36..41c304e10 100644 --- a/src/bun.js/bindings/ZigGlobalObject.h +++ b/src/bun.js/bindings/ZigGlobalObject.h @@ -353,7 +353,11 @@ public: JSC::Structure* pendingVirtualModuleResultStructure() { return m_pendingVirtualModuleResultStructure.get(this); } // When a napi module initializes on dlopen, we need to know what the value is + // This value is not observed by GC. It should be extremely ephemeral. JSValue pendingNapiModule = JSValue {}; + // We need to know if the napi module registered itself or we registered it. + // To do that, we count the number of times we register a module. + int napiModuleRegisterCallCount = 0; #include "ZigGeneratedClasses+lazyStructureHeader.h" diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp index b6deb3a56..009a11f61 100644 --- a/src/bun.js/bindings/napi.cpp +++ b/src/bun.js/bindings/napi.cpp @@ -478,6 +478,7 @@ 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(); if (!object) { object = JSC::constructEmptyObject(globalObject); @@ -789,6 +790,7 @@ extern "C" napi_status napi_get_reference_value(napi_env env, napi_ref ref, { NapiRef* napiRef = toJS(ref); *result = toNapi(napiRef->value()); + return napi_ok; } |