Skip to content

Commit

Permalink
[GR-35922] Backport: CompileFunctionInContext() should create the fun…
Browse files Browse the repository at this point in the history
…ction in the right context.

PullRequest: js/2274
  • Loading branch information
iamstolis committed Dec 21, 2021
2 parents c173cf9 + b6f1e97 commit 81a5372
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2186,9 +2186,15 @@ public Object scriptCompilerCompileFunctionInContext(Object context, String sour
suffix,
String.valueOf(isStrict)
};
CallTarget target = realm.getEnv().parsePublic(source, args);
DynamicObject script = (DynamicObject) target.call();
function = anyExtension ? JSFunction.call(script, Undefined.instance, extensions) : script;
JSRealm mainRealm = JSRealm.getMain(null);
JSRealm prevRealm = mainRealm.enterRealm(null, realm);
try {
CallTarget target = realm.getEnv().parsePublic(source, args);
DynamicObject script = (DynamicObject) target.call();
function = anyExtension ? JSFunction.call(script, Undefined.instance, extensions) : script;
} finally {
mainRealm.leaveRealm(null, prevRealm);
}
}
assert JSFunction.isJSFunction(function);

Expand Down
10 changes: 9 additions & 1 deletion graal-nodejs/test/graal/unit/vm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -88,4 +88,12 @@ describe('vm', function () {
}, SyntaxError);
assert.strictEqual(new vm.Script('6*7', { filename: 'vm.js' }).runInThisContext(), 42);
});
it('should honor parsingContext option of compileFunction()', function () {
// Extracted from parallel/test-vm-basic.js.
// Similar pattern is used by jest testing framework.
assert.strictEqual(
vm.compileFunction('return varInContext', [], { parsingContext: vm.createContext({varInContext: 'abc'}) })(),
'abc'
);
});
});

0 comments on commit 81a5372

Please sign in to comment.