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

running vm.SourceTextModule in thisContext #47280

Closed
axkibe opened this issue Mar 28, 2023 · 2 comments · Fixed by #47295
Closed

running vm.SourceTextModule in thisContext #47280

axkibe opened this issue Mar 28, 2023 · 2 comments · Fixed by #47295
Labels
doc Issues and PRs related to the documentations. vm Issues and PRs related to the vm subsystem.

Comments

@axkibe
Copy link
Contributor

axkibe commented Mar 28, 2023

What is the problem this feature will solve?

Currently as far I'm aware there is no way to use vm.SourceTextModule to run ESM code in the current context like it was possible with CJS with vm.runInThisContext().

What is the feature you are proposing to solve the problem?

Access to the current context as "contextObject" that can be supplied as context paramter to vm.SourceTextModule. Or the ability to give "globalThis" as context, right now the result is "TypeError [ERR_INVALID_ARG_TYPE]: The "options.context" property must be an vm.Context. Received an instance of Object"

What alternatives have you considered?

custom ESM loaders are of course now a very suitable alternative. (using a custom loader and then just calling dynamic import).

However in that case a way to add them on runtime would be needed. (effective for dynamic imports of course, and static from dynamically added, all static one from entry point of course need to have been resolved without)

Right now as alternativ I do use vm.SourceTextModule in a separate context and whenever something inside wants to import something that should resolve into the current node.js context, I'm calling import and then create a synthetic module as glue. It works, albeit there are a lot of caveats, since from a coders perspective it should be one context, and breakare of instanceof to ES basic objects can for example be nasty. (see ldapjs/node-ldapjs#864, or I had another case where some Sets are no longer instance of Set, but there isn't a Set.isSet like Array.isArray)

@axkibe axkibe added the feature request Issues that request new features to be added to Node.js. label Mar 28, 2023
@bnoordhuis bnoordhuis added doc Issues and PRs related to the documentations. vm Issues and PRs related to the vm subsystem. and removed feature request Issues that request new features to be added to Node.js. labels Mar 29, 2023
@bnoordhuis
Copy link
Member

It's probably not well-documented but new vm.SourceTextModule(source) evaluates the module in the current execution context. Only when you pass in an explicit context argument does it get evaluated in that context.

Pull request welcome. I've updated the labels.

@axkibe
Copy link
Contributor Author

axkibe commented Mar 29, 2023

OMG thank you.. this would have saved me a lot of headache don't know why I never tried to run it without context (likely because I started by copying the code example from the docs)

axkibe pushed a commit to axkibe/node that referenced this issue Mar 29, 2023
Explaining that vm.SourceTextModule() is able to evaluate in current
context if non is given

Fixes: nodejs#47280
nodejs-github-bot pushed a commit that referenced this issue Mar 31, 2023
Explaining that vm.SourceTextModule() is able to evaluate in current
context if non is given

Fixes: #47280
PR-URL: #47295
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
RafaelGSS pushed a commit that referenced this issue Apr 5, 2023
Explaining that vm.SourceTextModule() is able to evaluate in current
context if non is given

Fixes: #47280
PR-URL: #47295
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
RafaelGSS pushed a commit that referenced this issue Apr 6, 2023
Explaining that vm.SourceTextModule() is able to evaluate in current
context if non is given

Fixes: #47280
PR-URL: #47295
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
RafaelGSS pushed a commit that referenced this issue Apr 7, 2023
Explaining that vm.SourceTextModule() is able to evaluate in current
context if non is given

Fixes: #47280
PR-URL: #47295
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
RafaelGSS pushed a commit that referenced this issue Apr 8, 2023
Explaining that vm.SourceTextModule() is able to evaluate in current
context if non is given

Fixes: #47280
PR-URL: #47295
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
danielleadams pushed a commit that referenced this issue Jul 6, 2023
Explaining that vm.SourceTextModule() is able to evaluate in current
context if non is given

Fixes: #47280
PR-URL: #47295
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants