Skip to content

module: bootstrap module loaders in shadow realm - #48655

Closed
legendecas wants to merge 6 commits into
nodejs:mainfrom
legendecas:shadow-realm/module
Closed

module: bootstrap module loaders in shadow realm#48655
legendecas wants to merge 6 commits into
nodejs:mainfrom
legendecas:shadow-realm/module

Conversation

@legendecas

@legendecaslegendecas commented Jul 5, 2023

Copy link
Copy Markdown
Member

This bootstraps ESM loaders in the ShadowRealm with ShadowRealm.prototype.importValue as its entry point and enables loading ESM and CJS modules in the ShadowRealm. The module is imported without a parent URL and resolved with the current process's working directory.

@nodejs-github-bot

nodejs-github-bot commented Jul 5, 2023

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/modules
  • @nodejs/realm
  • @nodejs/startup
  • @nodejs/v8-update
  • @nodejs/vm

@nodejs-github-botnodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jul 5, 2023
@legendecaslegendecas added module Issues and PRs related to the module subsystem. realm Issues and PRs related to the ShadowRealm API and node::Realm labels Jul 5, 2023
Comment threadlib/internal/bootstrap/realm.js Outdated
Comment threadlib/internal/vm/module.js Outdated
@legendecas

legendecas commented Aug 15, 2023

Copy link
Copy Markdown
MemberAuthor

Rebased on the tip of the main branch and removed dependencies on #48510 (with known limitation on memory leaks). Marking as ready to be reviewed. This PR would unblock the verification of built-in modules support in the shadow realm.

@legendecas
legendecas marked this pull request as ready for review August 15, 2023 17:52
@legendecaslegendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 15, 2023
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 15, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment threadlib/internal/bootstrap/shadow_realm.js Outdated
@mcollinamcollina reopened this Aug 16, 2023

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@legendecaslegendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 17, 2023
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 17, 2023
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@GeoffreyBooth

Copy link
Copy Markdown
Member

We’re working on eventually removing the CommonJS loader, leaving only the ESM loader as the single module loader (as it can load both CommonJS and ESM code). Is it possible to refactor this so that ShadowRealm always loads everything only through the ESM loader? Then this part of things won’t need refactoring when the CommonJS loader goes away, and there would also be more uniformity in how modules are loaded within ShadowRealm.

@legendecas

Copy link
Copy Markdown
MemberAuthor

@GeoffreyBooth The ShadowRealm interface only exposes ESM ShadowRealm.prototype.importValue(speficier) and no CJS equivalent. The test case in the PR merely shows that the modules imported in the ShadowRealm can create a require function, just like what we can do in the principal realm.

Are you suggesting this PR didn't load modules in ShadowRealm.prototype.importValue(speficier) with the ESM loader? If so, could you share a hint of how I can fix that?

@GeoffreyBooth

Copy link
Copy Markdown
Member

Are you suggesting this PR didn’t load modules in ShadowRealm.prototype.importValue(speficier) with the ESM loader? If so, could you share a hint of how I can fix that?

I was just responding to the PR description, “This bootstraps ESM and CJS module loaders in the ShadowRealm.” Did what you mean to say that it allows for loading ESM and CJS modules in the ShadowRealm?

I don’t really follow what a lot of the code in this PR does. If its only internal interactions with module loaders are through the ESM loader, then we’re good.

@legendecas

Copy link
Copy Markdown
MemberAuthor

Yes, CJS support in ShadowRealm is enabled by ESM loaders. It seems that the PR description is misleading somehow, I've fixed the wording.

@GeoffreyBooth

GeoffreyBooth commented Aug 17, 2023

Copy link
Copy Markdown
Member

The module is imported without a parent URL and resolved with the current process’s working directory.

We had a similar situation with node:moduleregister, of needing to resolve relative to process.cwd as an unfortunate default that we dislike but couldn’t find a better alternative for. We added a second parameter to register allowing the user to pass parentURL to resolve against, so that most usage would be like register('ts-node', import.meta.url). You might want to consider the same here.

Also please make sure that the string is interpreted as a file URL and not as a path, because import specifiers must be file URLs and you’re calling this API importValue. Then users can do things like realm.importValue(import.meta.resolve('./file.js'), 'foo').

Comment threadtest/parallel/test-shadow-realm-import-value-resolve.js Outdated
legendecas added a commit that referenced this pull request Nov 13, 2023
This bootstraps ESM loaders in the ShadowRealm with
`ShadowRealm.prototype.importValue` as its entry point and enables
loading ESM and CJS modules in the ShadowRealm. The module is imported
without a parent URL and resolved with the current process's working
directory.
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@legendecas
legendecas deleted the shadow-realm/module branch November 13, 2023 14:11
targos pushed a commit that referenced this pull request Nov 23, 2023
Avoid the realm being strongly referenced by the process binding data.
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
targos pushed a commit that referenced this pull request Nov 23, 2023
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
targos pushed a commit that referenced this pull request Nov 23, 2023
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
targos pushed a commit that referenced this pull request Nov 23, 2023
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
targos pushed a commit that referenced this pull request Nov 23, 2023
The flag is always true and can be determined by isLoaderWorker solely.
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
targos pushed a commit that referenced this pull request Nov 23, 2023
This bootstraps ESM loaders in the ShadowRealm with
`ShadowRealm.prototype.importValue` as its entry point and enables
loading ESM and CJS modules in the ShadowRealm. The module is imported
without a parent URL and resolved with the current process's working
directory.
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@RafaelGSSRafaelGSS mentioned this pull request Nov 28, 2023
RafaelGSS added a commit that referenced this pull request Nov 28, 2023
Notable changes:
crypto:
* update root certificates to NSS 3.95 (Node.js GitHub Bot) #50805
deps:
* add simdjson (Yagiz Nizipli) #50322
fs:
* add c++ fast path for writeFileSync utf8 (CanadaHonk) #49884
module:
* merge config with `package_json_reader` (Yagiz Nizipli) #50322
* (SEMVER-MINOR) bootstrap module loaders in shadow realm (Chengzhong Wu) #48655
* (SEMVER-MINOR) remove useCustomLoadersIfPresent flag (Chengzhong Wu) #48655
src:
* (SEMVER-MINOR) add `--disable-warning` option (Ethan Arrowood) #50661
* move package resolver to c++ (Yagiz Nizipli) #50322
* (SEMVER-MINOR) create per isolate proxy env template (Chengzhong Wu) #48655
* (SEMVER-MINOR) create fs_dir per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) create worker per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) make process binding data weak (Chengzhong Wu) #48655
PR-URL: #50954
RafaelGSS added a commit that referenced this pull request Nov 28, 2023
Notable changes:
crypto:
* update root certificates to NSS 3.95 (Node.js GitHub Bot) #50805
deps:
* add simdjson (Yagiz Nizipli) #50322
fs:
* add c++ fast path for writeFileSync utf8 (CanadaHonk) #49884
module:
* merge config with `package_json_reader` (Yagiz Nizipli) #50322
* (SEMVER-MINOR) bootstrap module loaders in shadow realm (Chengzhong Wu) #48655
* (SEMVER-MINOR) remove useCustomLoadersIfPresent flag (Chengzhong Wu) #48655
src:
* (SEMVER-MINOR) add `--disable-warning` option (Ethan Arrowood) #50661
* move package resolver to c++ (Yagiz Nizipli) #50322
* (SEMVER-MINOR) create per isolate proxy env template (Chengzhong Wu) #48655
* (SEMVER-MINOR) create fs_dir per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) create worker per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) make process binding data weak (Chengzhong Wu) #48655
PR-URL: #50954
RafaelGSS added a commit that referenced this pull request Nov 29, 2023
Notable changes:
crypto:
* update root certificates to NSS 3.95 (Node.js GitHub Bot) #50805
fs:
* add c++ fast path for writeFileSync utf8 (CanadaHonk) #49884
module:
* (SEMVER-MINOR) bootstrap module loaders in shadow realm (Chengzhong Wu) #48655
* (SEMVER-MINOR) remove useCustomLoadersIfPresent flag (Chengzhong Wu) #48655
src:
* (SEMVER-MINOR) add `--disable-warning` option (Ethan Arrowood) #50661
* (SEMVER-MINOR) create per isolate proxy env template (Chengzhong Wu) #48655
* (SEMVER-MINOR) create fs_dir per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) create worker per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) make process binding data weak (Chengzhong Wu) #48655
PR-URL: #50954
RafaelGSS added a commit that referenced this pull request Nov 30, 2023
Notable changes:
crypto:
* update root certificates to NSS 3.95 (Node.js GitHub Bot) #50805
fs:
* add c++ fast path for writeFileSync utf8 (CanadaHonk) #49884
module:
* (SEMVER-MINOR) bootstrap module loaders in shadow realm (Chengzhong Wu) #48655
* (SEMVER-MINOR) remove useCustomLoadersIfPresent flag (Chengzhong Wu) #48655
src:
* (SEMVER-MINOR) add `--disable-warning` option (Ethan Arrowood) #50661
* (SEMVER-MINOR) create per isolate proxy env template (Chengzhong Wu) #48655
* (SEMVER-MINOR) create fs_dir per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) create worker per isolate properties (Chengzhong Wu) #48655
* (SEMVER-MINOR) make process binding data weak (Chengzhong Wu) #48655
PR-URL: #50954
UlisesGascon pushed a commit that referenced this pull request Dec 11, 2023
Avoid the realm being strongly referenced by the process binding data.
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
UlisesGascon pushed a commit that referenced this pull request Dec 11, 2023
PR-URL: #48655
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@UlisesGasconUlisesGascon mentioned this pull request Dec 12, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / srcIssues and PRs related to general changes in the lib or src directory.moduleIssues and PRs related to the module subsystem.needs-ciPRs that need a full CI run.realmIssues and PRs related to the ShadowRealm API and node::Realmsemver-minorPRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@legendecas@nodejs-github-bot@GeoffreyBooth@joyeecheung@ljharb@mcollina@jasnell@aduh95