Skip to content

Commit 1dbdd61

Browse files
committed
Fix globalThis in vm context.
1 parent a819274 commit 1dbdd61

File tree

2 files changed

+38
-1
lines changed
  • graal-nodejs
    • mx.graal-nodejs/com.oracle.truffle.trufflenode/src/com/oracle/truffle/trufflenode
    • test/graal/unit

2 files changed

+38
-1
lines changed

graal-nodejs/mx.graal-nodejs/com.oracle.truffle.trufflenode/src/com/oracle/truffle/trufflenode/GraalJSAccess.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ private GraalJSAccess(String[] args) throws Exception {
392392
contextBuilder.option(JSContextOptions.CONSOLE_NAME, "false");
393393
// Node.js does not have global arguments property
394394
contextBuilder.option(JSContextOptions.GLOBAL_ARGUMENTS_NAME, "false");
395+
// Node.js context does not have a predefined global 'global' property.
396+
contextBuilder.option(JSContextOptions.GLOBAL_PROPERTY_NAME, "false");
395397
contextBuilder.useSystemExit(true);
396398

397399
exposeGC = options.isGCExposed();
@@ -2836,7 +2838,10 @@ public Object contextNew(Object templateObj) {
28362838
if (templateObj != null) {
28372839
ObjectTemplate template = (ObjectTemplate) templateObj;
28382840
if (template.hasPropertyHandler()) {
2839-
global = propertyHandlerInstantiate(context, realm, template, global, true);
2841+
var globalProxy = propertyHandlerInstantiate(context, realm, template, global, true);
2842+
assert JSObject.hasOwnProperty(global, Strings.GLOBAL_THIS) && !JSObject.hasOwnProperty(global, Strings.GLOBAL);
2843+
JSObject.set(global, Strings.GLOBAL_THIS, globalProxy);
2844+
global = globalProxy;
28402845
realm.setGlobalObject(global);
28412846
} else {
28422847
JSObject prototype = JSOrdinary.create(context, realm);

graal-nodejs/test/graal/unit/vm.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,36 @@ describe('vm', function () {
9696
'abc'
9797
);
9898
});
99+
it('should have correct globalThis', function () {
100+
var sandbox = {};
101+
var fooValue = 42;
102+
Object.defineProperty(sandbox, 'foo', {value: fooValue});
103+
Object.defineProperty(sandbox, 'setTimeout', {value: setTimeout});
104+
var context = vm.createContext(sandbox);
105+
assert.strictEqual(vm.runInContext('foo', context), fooValue);
106+
assert.strictEqual(vm.runInContext('globalThis.foo', context), fooValue);
107+
var desc = vm.runInContext('Reflect.getOwnPropertyDescriptor(globalThis, "setTimeout")', context);
108+
assert.ok(desc);
109+
assert.strictEqual(desc.value, setTimeout);
110+
assert.strictEqual(desc.writable, false);
111+
assert.strictEqual(desc.enumerable, false);
112+
assert.strictEqual(desc.configurable, false);
113+
});
114+
it('should allow overriding globalThis', function () {
115+
var sandbox = {};
116+
var fooValue = 42;
117+
var globalThisOverride = {foo: 43};
118+
Object.defineProperty(sandbox, 'foo', {value: fooValue});
119+
Object.defineProperty(sandbox, 'globalThis', {value: globalThisOverride, configurable: true, writable: true});
120+
var context = vm.createContext(sandbox);
121+
assert.strictEqual(vm.runInContext('foo', context), fooValue);
122+
assert.strictEqual(vm.runInContext('globalThis', context), globalThisOverride);
123+
assert.strictEqual(vm.runInContext('globalThis.foo', context), globalThisOverride.foo);
124+
assert.strictEqual(sandbox.globalThis, globalThisOverride);
125+
});
126+
it('should not have "global" property', function () {
127+
var context = vm.createContext();
128+
assert.strictEqual(vm.runInContext('typeof global', context), 'undefined');
129+
assert.strictEqual(vm.runInContext('typeof globalThis.global', context), 'undefined');
130+
});
99131
});

0 commit comments

Comments
 (0)