From a8854f8198779c8b0c7b66f755d1503c60b6faaf Mon Sep 17 00:00:00 2001 From: Kai Tamkun Date: Wed, 7 May 2025 15:48:31 -0700 Subject: [PATCH] Fix contextual store detection --- src/bun.js/bindings/NodeVM.cpp | 13 ++--- .../bindings/NodeVMSourceTextModule.cpp | 47 +++++++++++-------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/bun.js/bindings/NodeVM.cpp b/src/bun.js/bindings/NodeVM.cpp index dfbef183e0..9791c1583c 100644 --- a/src/bun.js/bindings/NodeVM.cpp +++ b/src/bun.js/bindings/NodeVM.cpp @@ -479,8 +479,6 @@ void NodeVMGlobalObject::sigintReceived() bool NodeVMGlobalObject::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot) { - // if (!propertyName.isSymbol()) - // printf("put called for %s\n", propertyName.publicName()->utf8().data()); auto* thisObject = jsCast(cell); if (!thisObject->m_sandbox) { @@ -492,7 +490,9 @@ bool NodeVMGlobalObject::put(JSCell* cell, JSGlobalObject* globalObject, Propert auto& vm = JSC::getVM(globalObject); JSValue thisValue = slot.thisValue(); bool isContextualStore = thisValue != JSValue(globalObject); - (void)isContextualStore; + if (auto* proxy = jsDynamicCast(thisValue); proxy && proxy->target() == globalObject) { + isContextualStore = false; + } bool isDeclaredOnGlobalObject = slot.type() == JSC::PutPropertySlot::NewProperty; auto scope = DECLARE_THROW_SCOPE(vm); PropertySlot getter(sandbox, PropertySlot::InternalMethodType::Get, nullptr); @@ -531,10 +531,7 @@ static const ASCIILiteral s_proxyAlreadyRevokedErrorMessage { "Proxy has already bool NodeVMGlobalObject::getOwnPropertySlot(JSObject* cell, JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot) { - // if (!propertyName.isSymbol()) - // printf("getOwnPropertySlot called for %s\n", propertyName.publicName()->utf8().data()); - - auto& vm = JSC::getVM(globalObject); + VM& vm = JSC::getVM(globalObject); auto scope = DECLARE_THROW_SCOPE(vm); auto* thisObject = jsCast(cell); @@ -975,9 +972,9 @@ JSC_DEFINE_HOST_FUNCTION(vmModule_isContext, (JSGlobalObject * globalObject, Cal // // visitor.append(thisObject->m_proxyTarget); // } const ClassInfo NodeVMGlobalObject::s_info = { "NodeVMGlobalObject"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(NodeVMGlobalObject) }; + bool NodeVMGlobalObject::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSC::DeletePropertySlot& slot) { - auto* thisObject = jsCast(cell); if (UNLIKELY(!thisObject->m_sandbox)) { return Base::deleteProperty(cell, globalObject, propertyName, slot); diff --git a/src/bun.js/bindings/NodeVMSourceTextModule.cpp b/src/bun.js/bindings/NodeVMSourceTextModule.cpp index efea4cbde5..2c1d29f8fa 100644 --- a/src/bun.js/bindings/NodeVMSourceTextModule.cpp +++ b/src/bun.js/bindings/NodeVMSourceTextModule.cpp @@ -193,29 +193,34 @@ JSValue NodeVMSourceTextModule::link(JSGlobalObject* globalObject, JSArray* spec const unsigned length = specifiers->getArrayLength(); ASSERT(length == moduleNatives->getArrayLength()); - if (length == 0) { - status(Status::Linked); - return JSC::jsUndefined(); + if (length != 0) { + VM& vm = globalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + for (unsigned i = 0; i < length; i++) { + JSValue specifierValue = specifiers->getDirectIndex(globalObject, i); + JSValue moduleNativeValue = moduleNatives->getDirectIndex(globalObject, i); + + ASSERT(specifierValue.isString()); + ASSERT(moduleNativeValue.isObject()); + + WTF::String specifier = specifierValue.toWTFString(globalObject); + JSObject* moduleNative = moduleNativeValue.getObject(); + + m_resolveCache.set(WTFMove(specifier), WriteBarrier { vm, this, moduleNative }); + } } - VM& vm = globalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); - - for (unsigned i = 0; i < length; i++) { - JSValue specifierValue = specifiers->getDirectIndex(globalObject, i); - JSValue moduleNativeValue = moduleNatives->getDirectIndex(globalObject, i); - - ASSERT(specifierValue.isString()); - ASSERT(moduleNativeValue.isObject()); - - WTF::String specifier = specifierValue.toWTFString(globalObject); - JSObject* moduleNative = moduleNativeValue.getObject(); - - m_resolveCache.set(WTFMove(specifier), WriteBarrier { vm, this, moduleNative }); + if (NodeVMGlobalObject* nodeVmGlobalObject = getGlobalObjectFromContext(globalObject, m_context.get(), false)) { + globalObject = nodeVmGlobalObject; } - // JSModuleRecord* record = m_moduleRecord.get(); - // record->link(globalObject, jsUndefined()); + JSModuleRecord* record = m_moduleRecord.get(); + Synchronousness sync = record->link(globalObject, jsUndefined()); + + if (sync == Synchronousness::Async) { + ASSERT_NOT_REACHED_WITH_MESSAGE("TODO(@heimskr): async module linking"); + } status(Status::Linked); return JSC::jsUndefined(); @@ -236,6 +241,10 @@ JSValue NodeVMSourceTextModule::evaluate(JSGlobalObject* globalObject, uint32_t NodeVMGlobalObject* nodeVmGlobalObject = getGlobalObjectFromContext(globalObject, m_context.get(), false); + if (nodeVmGlobalObject) { + globalObject = nodeVmGlobalObject; + } + auto run = [&] { // TODO(@heimskr): top-level await support result = record->evaluate(globalObject, jsUndefined(), jsNumber(static_cast(JSGenerator::ResumeMode::NormalMode)));