aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/bindings/Process.cpp17
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.h4
-rw-r--r--src/bun.js/bindings/napi.cpp2
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;
}