diff options
author | 2023-05-18 18:45:13 -0700 | |
---|---|---|
committer | 2023-05-18 18:45:13 -0700 | |
commit | f862ae405036acb35ed2134b66f056031e1367fe (patch) | |
tree | 65c103ba17015b4f9927f417d66ca95f688a6ef9 /src | |
parent | 958f5f8b9998a0e5140f7275946750e76f02bd73 (diff) | |
download | bun-f862ae405036acb35ed2134b66f056031e1367fe.tar.gz bun-f862ae405036acb35ed2134b66f056031e1367fe.tar.zst bun-f862ae405036acb35ed2134b66f056031e1367fe.zip |
[node:vm] Propagate global properties defined on the new context object
Diffstat (limited to 'src')
-rw-r--r-- | src/bun.js/bindings/NodeVMScript.cpp | 38 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 4 |
2 files changed, 26 insertions, 16 deletions
diff --git a/src/bun.js/bindings/NodeVMScript.cpp b/src/bun.js/bindings/NodeVMScript.cpp index 2484fdb58..bba9d2b79 100644 --- a/src/bun.js/bindings/NodeVMScript.cpp +++ b/src/bun.js/bindings/NodeVMScript.cpp @@ -95,11 +95,11 @@ static EncodedJSValue runInContext(JSGlobalObject* globalObject, NodeVMScript* s auto& vm = globalObject->vm(); if (!optionsArg.isUndefined()) { - if (!optionsArg.isObject()) { - auto scope = DECLARE_THROW_SCOPE(vm); - return throwVMTypeError(globalObject, scope, "options must be an object"_s); - } - JSObject* options = asObject(optionsArg); + // if (!optionsArg.isObject()) { + // auto scope = DECLARE_THROW_SCOPE(vm); + // return throwVMTypeError(globalObject, scope, "options must be an object"_s); + // } + // JSObject* options = asObject(optionsArg); // TODO: displayErrors - Not really sure what this option even does or why it's useful // TODO: timeout - I can't figure out how to make Watchdog work so leaving this for now @@ -164,11 +164,19 @@ JSC_DEFINE_HOST_FUNCTION(scriptRunInContext, (JSGlobalObject * globalObject, Cal return throwVMTypeError(globalObject, scope, "context parameter must be a contextified object"_s); } JSScope* scope = jsDynamicCast<JSScope*>(scopeVal); - ASSERT(scope); + if (UNLIKELY(!scope)) { + auto scope = DECLARE_THROW_SCOPE(vm); + return throwVMTypeError(globalObject, scope, "context parameter must be a contextified object"_s); + } + + JSGlobalProxy* globalProxy = jsDynamicCast<JSGlobalProxy*>(context->getPrototypeDirect()); + if (!globalProxy) { + auto scope = DECLARE_THROW_SCOPE(vm); + return throwVMTypeError(globalObject, scope, "context parameter must be a contextified object"_s); + } - return runInContext(globalObject, script, context, scope, args.at(1)); + return runInContext(globalProxy->target(), script, context, scope, args.at(1)); } -JSC_DEFINE_HOST_FUNCTION(scriptRunInThisContext, (JSGlobalObject* globalObject, CallFrame* callFrame)) JSC_DEFINE_HOST_FUNCTION(scriptRunInThisContext, (JSGlobalObject * globalObject, CallFrame* callFrame)) { auto& vm = globalObject->vm(); @@ -209,13 +217,15 @@ JSC_DEFINE_HOST_FUNCTION(vmModule_createContext, (JSGlobalObject * globalObject, return throwVMTypeError(globalObject, scope, "parameter to createContext must be an object"_s); } JSObject* context = asObject(contextArg); + auto* targetContext = JSC::JSGlobalObject::create( + vm, JSC::JSGlobalObject::createStructure(vm, JSC::jsNull())); + + auto proxyStructure = JSGlobalProxy::createStructure(vm, globalObject, JSC::jsNull()); + auto proxy = JSGlobalProxy::create(vm, proxyStructure); + proxy->setTarget(vm, targetContext); + context->setPrototypeDirect(vm, proxy); - PropertyDescriptor descriptor; - descriptor.setWritable(false); - descriptor.setEnumerable(false); - descriptor.setValue(context); - JSObject::defineOwnProperty(context, globalObject, Identifier::fromString(vm, "globalThis"_s), descriptor, true); - JSScope* contextScope = JSWithScope::create(vm, globalObject, globalObject->globalScope(), context); + JSScope* contextScope = JSWithScope::create(vm, targetContext, targetContext->globalScope(), context); auto* zigGlobalObject = reinterpret_cast<Zig::GlobalObject*>(globalObject); zigGlobalObject->vmModuleContextMap()->set(vm, context, contextScope); diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index b0078503b..c9411c78f 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -3760,7 +3760,7 @@ restart: if (prototypeCount++ < 5) { if (JSValue proto = prototypeObject.getPrototype(globalObject)) { - if (!(proto == globalObject->objectPrototype() || proto == globalObject->functionPrototype())) { + if (!(proto == globalObject->objectPrototype() || proto == globalObject->functionPrototype() || (proto.inherits<JSGlobalProxy>() && jsCast<JSGlobalProxy*>(proto)->target() != globalObject))) { if ((structure = proto.structureOrNull())) { prototypeObject = proto; fast = canPerformFastPropertyEnumerationForIterationBun(structure); @@ -3779,7 +3779,7 @@ restart: JSObject* iterating = prototypeObject.getObject(); - while (iterating && !(iterating == globalObject->objectPrototype() || iterating == globalObject->functionPrototype()) && prototypeCount++ < 5) { + while (iterating && !(iterating == globalObject->objectPrototype() || iterating == globalObject->functionPrototype() || (iterating->inherits<JSGlobalProxy>() && jsCast<JSGlobalProxy*>(iterating)->target() != globalObject)) && prototypeCount++ < 5) { iterating->methodTable()->getOwnPropertyNames(iterating, globalObject, properties, DontEnumPropertiesMode::Include); RETURN_IF_EXCEPTION(scope, void()); for (auto& property : properties) { |