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

Cannot return value from a SourceTextModule to the caller #37453

Closed
clecap opened this issue Feb 20, 2021 · 5 comments
Closed

Cannot return value from a SourceTextModule to the caller #37453

clecap opened this issue Feb 20, 2021 · 5 comments
Labels
doc Issues and PRs related to the documentations. esm Issues and PRs related to the ECMAScript Modules implementation. vm Issues and PRs related to the vm subsystem.

Comments

@clecap
Copy link

clecap commented Feb 20, 2021

  • Version: v15.9.0
  • Platform: macOS Catalina 10.15.6
  • Subsystem: VM

What steps will reproduce the bug?

Make file retu.js.

const vm = require('vm');
const contextifiedSandbox = vm.createContext({ });
(async () => {
  const bar = new vm.SourceTextModule(`19;`, { context: contextifiedSandbox });
  async function linker(specifier, referencingModule) { }
  await bar.link(linker);
  const  result  = await bar.evaluate();
  console.log("Result is: ", result);
})();

Run as node --experimental.vm.modules retu.js

What is the expected behavior?

According to the current state of the documentation I expect an output of Result is: 19

What do you see instead?

Output of: Result is: undefined

@targos targos added esm Issues and PRs related to the ECMAScript Modules implementation. vm Issues and PRs related to the vm subsystem. labels Feb 20, 2021
@devsnek
Copy link
Member

devsnek commented Feb 20, 2021

This is working as intended. Can you point me to the location in the docs where it suggests otherwise?

@devsnek devsnek added the doc Issues and PRs related to the documentations. label Feb 20, 2021
@clecap
Copy link
Author

clecap commented Feb 20, 2021

It pretty much is possible that I misunderstood the docs. What I found was in https://nodejs.org/dist/latest-v15.x/docs/api/vm.html#vm_module_evaluate_options the information that evaluate should return a Promise (but the docs there do not provide a hint as to how that value is derived and to which value the promise should resolve). My naive expectation is that the value of a module is the value of the last expression in the module, which should be 19. This is the usual convention in several other, similar cases.

Seems I am not the only one with that problem: A similar question on https://www.titanwolf.org/Network/q/6b88da95-c21a-46dd-bc37-100842de1151/y is still unanswered and the 0.5 line answer in https://stackoverflow.com/questions/60796382/how-to-get-default-export-out-of-vm-sourcetextmodule I admit I do not understand.

So the issue might as well be an issue with the docs (or with my ability to understand the docs).

@devsnek
Copy link
Member

devsnek commented Feb 20, 2021

I think the fix here would be to change the docs to say Returns: <Promise> Fulfills with undefined upon success. Would you like to do so? If not I will get around to it eventually.

@clecap
Copy link
Author

clecap commented Feb 20, 2021

What then would be the way to obtain a value from a module and, for example, access a default export?

Wrt a PR on the doc: I am not so knowledgable on how to do that in github :(

@devsnek
Copy link
Member

devsnek commented Feb 20, 2021

@clecap the default export is available via module.namespace.default, though it would be undefined in your example. There is no way to access the "last evaluated expression" as there is with vm.Script.

jasnell added a commit to jasnell/node that referenced this issue Mar 9, 2021
Fixes: nodejs#37453
Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell jasnell closed this as completed in 16151be Mar 9, 2021
danielleadams pushed a commit that referenced this issue Mar 16, 2021
Fixes: #37453
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #37663
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
targos pushed a commit that referenced this issue May 1, 2021
Fixes: #37453
Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #37663
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
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. esm Issues and PRs related to the ECMAScript Modules implementation. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants