-
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
Layer 3: Evaluators, how to inherit intrinsic? #73
Comments
I feel like it would simpler to go with one of the previous ideas where there was an actual // Creates a new global object with the appropriate
// intrinsics already attached
const g = new Global();
g.Array === Array; // true
const e = new Evaluator({ global: g });
e.eval(`Math`); // object Math { ... } Customizing these globals wouldn't be particularly hard, just an const e = new Evaluator({
global: Object.assign(new Global(), {
myApi: () => { ... },
}),
}); If there's desire for exotic behaviour (i.e. #38) one could just allow const fakeDocument = new FakeDocument();
const g = new Global({
// proxy hooks...
get(global, prop, receiver) {
},
});
const g = new Global();
const e = new Evaluator({
// the following problem is independent on this API shape
global: g,
});
// Set the Function global
g.Function = e.Function;
// Multiple Function constructors floating around in the same evaluator
e.eval(`
Function === Array.constructor; // false
`);
// And so code run inside the evaluator can still access parent globals fairly unrestrictedly
e.eval(`
const OuterFunction = Array.constructor;
const OuterModule = new OuterFunction("return Module");
const module = new OuterModule(new ModuleSourceText(`
// do thing in the parent evaluator's global scope
`)):
`);
Nevermind this, I thought |
Actually no this is still a problem, const g = CREATE_GLOBAL_SOMEHOW();
const e = new Evaluator({ global: g });
g.Function = e.Function;
// If this agrees:
Array === e.eval(`Array`); // true
// then so must
Array.constructor === e.eval(`Array.constructor`); // true
// hence Array.constructor is the outer Function
Function === e.eval(`Array.constructor`); // true The only way for this not to agree would be if |
My intent is that whomever creates the intrinsics is in a position to arrange the new intrinsics however they see fit, and that the common case would be: const localThis = Object.create(globalThis);
const evaluators = new Evaluators(localThis);
Object.assign(localThis, evaluators); Such that direct eval would work as-expected in evaluated code. This would be sufficient for the DSL usecase. This is not very different from a const evaluators = new Evaluators(globalThis, { importHook, importMeta }); Where the globalThis is object identical to the surrounding environment but import behavior is virtualized. I imagine this to be a common need as well. This would have the surprising but probably okay side-effect of disabling direct eval. That could of course be recovered if it’s actually needed, by binding new evaluators.Function('eval', 'text', 'eval(text)')(evaluators.eval, text); I’m not strongly partial to either |
The text was updated successfully, but these errors were encountered: