Skip to content

Commit 81a5372

Browse files
committed
[GR-35922] Backport: CompileFunctionInContext() should create the function in the right context.
PullRequest: js/2274
2 parents c173cf9 + b6f1e97 commit 81a5372

File tree

2 files changed

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

2 files changed

+18
-4
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,9 +2186,15 @@ public Object scriptCompilerCompileFunctionInContext(Object context, String sour
21862186
suffix,
21872187
String.valueOf(isStrict)
21882188
};
2189-
CallTarget target = realm.getEnv().parsePublic(source, args);
2190-
DynamicObject script = (DynamicObject) target.call();
2191-
function = anyExtension ? JSFunction.call(script, Undefined.instance, extensions) : script;
2189+
JSRealm mainRealm = JSRealm.getMain(null);
2190+
JSRealm prevRealm = mainRealm.enterRealm(null, realm);
2191+
try {
2192+
CallTarget target = realm.getEnv().parsePublic(source, args);
2193+
DynamicObject script = (DynamicObject) target.call();
2194+
function = anyExtension ? JSFunction.call(script, Undefined.instance, extensions) : script;
2195+
} finally {
2196+
mainRealm.leaveRealm(null, prevRealm);
2197+
}
21922198
}
21932199
assert JSFunction.isJSFunction(function);
21942200

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -88,4 +88,12 @@ describe('vm', function () {
8888
}, SyntaxError);
8989
assert.strictEqual(new vm.Script('6*7', { filename: 'vm.js' }).runInThisContext(), 42);
9090
});
91+
it('should honor parsingContext option of compileFunction()', function () {
92+
// Extracted from parallel/test-vm-basic.js.
93+
// Similar pattern is used by jest testing framework.
94+
assert.strictEqual(
95+
vm.compileFunction('return varInContext', [], { parsingContext: vm.createContext({varInContext: 'abc'}) })(),
96+
'abc'
97+
);
98+
});
9199
});

0 commit comments

Comments
 (0)