Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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 @@ -2337,12 +2337,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 @@ -2742,7 +2737,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 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__((

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 @@ -589,8 +595,6 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__((
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 @@ -606,9 +610,13 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__((
}
}

// 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 @@ -628,12 +636,11 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__((
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 @@ -646,8 +653,6 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__((
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
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -677,12 +682,11 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__((
}
}

// 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 @@ -766,8 +770,6 @@ JSC_DEFINE_HOST_FUNCTION_WITH_ATTRIBUTES(Process_functionDlopen, __attribute__((
}
}

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 @@ -4956,7 +4956,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