Skip to content
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

Realm running module code to cover import #5

Open
leobalter opened this issue Mar 15, 2021 · 3 comments
Open

Realm running module code to cover import #5

leobalter opened this issue Mar 15, 2021 · 3 comments

Comments

@leobalter
Copy link
Owner

does having import implies Realm code is module code?

@littledan
Copy link

I don't think so, since inside that module, you can use eval, which doesn't run module code.

@mhofman
Copy link

mhofman commented Mar 24, 2021

Currently the proposal does require that the "bootstrap" is either module code, or using realm.eval(), with the latter subject to string evaluation and CSP complications in browsers. Maybe there could be an executeScript(specifier): Promise<void>, which compatible hosts (e.g. browsers) could implement by interpreting the specifier as a path? The call would be async, and resolve to undefined, or reject.

Update: I guess the inability to return a value would require the usage of eval or module bindings anyway, so I guess there isn't much benefit to an "executeScript".

@mhofman
Copy link

mhofman commented Mar 25, 2021

Actually maybe there could be something like getGlobalPropertyValue(name): PrimitiveValueOrCallable? Combined with an executeScript, that should allow executing non module code without the need of eval, while avoiding exposing the realm's globalThis directly.

Edit: for completeness, here is an example:

// my-script.js
globalThis.timesTwo = (x) => x * 2;

// main.js
const r = new Realm();
await r.executeScript('./my-script.js');

const redFunction = r.getGlobalPropertyValue('timesTwo');
redFunction(21); // yields 42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants