-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function("import(specifier)")
#89
Comments
Note that the current layer 0 spec already calls the |
Honestly this seems like a benefit to evaluators as it means there doesn't need to be multiple copies of i.e. In the current proposal the pattern would be to do this: const evaluator = new Evaluator({
globalThis: {
...globalThis,
SOME_GLOBAL: "SOME_GLOBAL",
},
});
evaluator.globalThis.Function = evaluator.Function;
evaluator.eval(`
const fn = Function("return SOME_GLOBAL")
console.log(fn()); // SOME_GLOBAL
`); however in doing this we wind up with multiple copies of const evaluator = new Evaluator({ globalThis: { ...globalThis } });
evaluator.globalThis.Function = evaluator.Function;
evaluator.eval(`
// Multiple Function constructors are floating around
console.log(parseInt.constructor === Function); // false
`); However with the existing behaviour we need not bother create a new const evaluator = new Evaluator({
globalThis: {
...globalThis.
someGlobal: "SOME_GLOBAL",
},
});
// With the current spec, this should just work
evaluator.eval(`
const fn = Function("return SOME_GLOBAL")
console.log(fn()); // SOME_GLOBAL
`);
// No Function discontinuity
evaluator.eval(`
console.log(parseInt.constructor === Function); // true
`); The only thing lost is the ability to do |
Frankly I think const evaluator = new Evaluator({ ...globalThis, SOME_GLOBAL: "SOME_GLOBAL" });
evaluator.eval(`
// Would just work as eval would capture current script/module and get it's associated
// evaluator
console.log(eval.call(null, "SOME_GLOBAL"));
`);
// But we can still customize eval by wrapping with a closure that ensures the nearest script/module record is our current level
const evaluator2 = new Evaluator({
globalThis: {
...globalThis,
eval: (arg) => eval.call(null, arg),
SOME_GLOBAL: "SOME_GLOBAL",
},
});
globalThis.SOME_GLOBAL = "NOT_IN_EVALUATOR";
evaluator2.eval(`
console.log(eval.call(null, "SOME_GLOBAL")); // NOT_IN_EVALUATOR
`); |
The function constructor captures the active script or module when creating a function object, at step 15 of https://tc39.es/ecma262/#sec-ordinaryfunctioncreate.
This means that, given this
a.js
module:it will call the host hook with
a.js
as the referrer and./b.js
as the specifier.I would thus expect it to call the
importHook
when running in anew Module
already in layer 0. If instead it goes through the evaluators from layer 3, they should have a way to get the correct referrer module.The text was updated successfully, but these errors were encountered: