Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/jsc/JSValue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2295,12 +2295,7 @@ impl JSValue {
}
/// `JSValue.toObject` — ECMA `ToObject`; throws on null/undefined.
pub fn to_object(self, global: &JSGlobalObject) -> JsResult<*mut JSObject> {
let p = JSC__JSValue__toObject(self, global);
if p.is_null() {
Err(JsError::Thrown)
} else {
Ok(p)
}
crate::cpp::JSC__JSValue__toObject(self, global).map(core::ptr::NonNull::as_ptr)
}
/// `JSValue.unwrapBoxedPrimitive` — unwraps Number,
/// Boolean, String, and BigInt objects to their primitive forms.
Expand Down Expand Up @@ -2687,7 +2682,6 @@ unsafe extern "C" {
global: &JSGlobalObject,
) -> bool;
safe fn Bun__JSValue__toNumber(this: JSValue, global: &JSGlobalObject) -> f64;
safe fn JSC__JSValue__toObject(this: JSValue, global: &JSGlobalObject) -> *mut JSObject;
safe fn JSC__JSValue__unwrapBoxedPrimitive(global: &JSGlobalObject, this: JSValue) -> JSValue;
safe fn JSC__JSValue__getPrototype(this: JSValue, global: &JSGlobalObject) -> JSValue;
safe fn JSC__JSValue__getName(
Expand Down
34 changes: 18 additions & 16 deletions src/jsc/bindings/BunProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,12 @@

globalObject->m_pendingNapiModuleAndExports[0].set(vm, globalObject, moduleObject);
globalObject->m_pendingNapiModuleAndExports[1].set(vm, globalObject, exports);
// The slots are how registration (which may run under dlopen()) talks to this call; nothing may be left in them
// for the next — possibly nested — process.dlopen to misread.
auto clearModuleSlots = WTF::makeScopeExit([&] {
globalObject->m_pendingNapiModuleAndExports[0].clear();
globalObject->m_pendingNapiModuleAndExports[1].clear();
});

Strong<JSC::Unknown> strongExports;

Expand Down Expand Up @@ -590,8 +596,6 @@
globalObject->m_pendingV8Modules.clear();
globalObject->m_pendingNapiModules.clear();
globalObject->napiModuleRegisterCallCount = 0;
globalObject->m_pendingNapiModuleAndExports[0].clear();
globalObject->m_pendingNapiModuleAndExports[1].clear();
});

if (handle) {
Expand All @@ -607,9 +611,13 @@
}
}

// A V8-style module's nm_register_func already ran inside dlopen() (node_module_register)
// and may have thrown.
// A V8-style module's nm_register_func already ran inside dlopen() (node_module_register) and may have
// thrown, or reported its failure through slot 0.
RETURN_IF_EXCEPTION(scope, {});
if (JSValue thrown = globalObject->m_pendingNapiModuleAndExports[0].get(); thrown && thrown != strongModule.get()) {
JSC::throwException(globalObject, scope, thrown);
return {};
}

// Execute all NAPI modules. If an nm_register_func registers more
// modules re-entrantly, they accumulate back in m_pendingNapiModules;
Expand All @@ -629,12 +637,11 @@
pendingNapiModules = std::exchange(globalObject->m_pendingNapiModules, {});
}

// Slot 0 holds either the module object or what registration threw.
JSValue resultValue = globalObject->m_pendingNapiModuleAndExports[0].get();
if (resultValue && resultValue != strongModule.get()) {
if (resultValue.isCell() && resultValue.getObject()->isErrorInstance()) {
JSC::throwException(globalObject, scope, resultValue);
return {};
}
JSC::throwException(globalObject, scope, resultValue);
return {};
}

return JSValue::encode(jsUndefined());
Expand All @@ -644,14 +651,12 @@
if (auto cachedModules = Bun::DLHandleMap::singleton().get(handle)) {
// (The V8 registrations are already in DLHandleMap; nothing here re-saves them.)
auto resetPendingRegistrations = WTF::makeScopeExit([&] {
globalObject->m_pendingV8Modules.clear();
globalObject->m_pendingNapiModules.clear();
globalObject->napiModuleRegisterCallCount = 0;
globalObject->m_pendingNapiModuleAndExports[0].clear();
globalObject->m_pendingNapiModuleAndExports[1].clear();
});

// Replay all registrations from this handle. napi ones only queue into

Check warning on line 659 in src/jsc/bindings/BunProcess.cpp

View check run for this annotation

Claude / Claude Code Review

Cached-replay branch missing the pre-NAPI-loop slot-0 throw check

The new slot-0 check at lines 617-620 (throw whatever V8 registration left in slot 0 before running queued NAPI modules) is only added to the first-load branch; the parallel cached-replay branch calls `node::node_module_register(*nodeModule)` at line 663 with no equivalent check before its NAPI loop at line 673. Pre-PR, `RETURN_IF_EXCEPTION` at line 664 covered a V8 register func that called `Isolate::ThrowException`; post-PR that path writes to slot 0 instead, so on cached replay the Error is r
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
// m_pendingNapiModules; a V8 one runs its nm_register_func right here.
for (auto& registration : *cachedModules) {
if (auto* const* nodeModule = std::get_if<node::node_module*>(&registration)) {
Expand All @@ -678,12 +683,11 @@
}
}

// Slot 0 holds either the module object or what registration threw.
JSValue resultValue = globalObject->m_pendingNapiModuleAndExports[0].get();
if (resultValue && resultValue != strongModule.get()) {
if (resultValue.isCell() && resultValue.getObject()->isErrorInstance()) {
JSC::throwException(globalObject, scope, resultValue);
return {};
}
JSC::throwException(globalObject, scope, resultValue);
return {};
}

return JSValue::encode(jsUndefined());
Expand Down Expand Up @@ -767,8 +771,6 @@
}
}

globalObject->m_pendingNapiModuleAndExports[0].clear();
globalObject->m_pendingNapiModuleAndExports[1].clear();
globalObject->m_pendingNapiModuleDlopenHandle = nullptr;

// https://github.com/nodejs/node/blob/2eff28fb7a93d3f672f80b582f664a7c701569fb/src/node_api.cc#L734-L742
Expand Down
2 changes: 1 addition & 1 deletion src/jsc/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4813,7 +4813,7 @@ CPP_DECL double Bun__JSValue__toNumber(JSC::EncodedJSValue JSValue0, JSC::JSGlob
return JSC::JSValue::encode(JSC::jsTDZValue());
};

JSC::JSObject* JSC__JSValue__toObject(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1)
[[ZIG_EXPORT(null_is_throw)]] JSC::JSObject* JSC__JSValue__toObject(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1)
{
JSC::JSValue value = JSC::JSValue::decode(JSValue0);
return value.toObject(arg1);
Expand Down
Loading
Loading