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

esm: do not bind loader hook functions #44122

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
ArrayIsArray,
ArrayPrototypeJoin,
ArrayPrototypePush,
FunctionPrototypeBind,
FunctionPrototypeCall,
ObjectAssign,
ObjectCreate,
Expand Down Expand Up @@ -314,16 +313,14 @@ class ESMLoader {
'DeprecationWarning',
);

// Use .bind() to avoid giving access to the Loader instance when called.
if (globalPreload) {
acceptedHooks.globalPreloader =
FunctionPrototypeBind(globalPreload, null);
acceptedHooks.globalPreloader = globalPreload;
}
if (resolve) {
acceptedHooks.resolver = FunctionPrototypeBind(resolve, null);
acceptedHooks.resolver = resolve;
}
if (load) {
acceptedHooks.loader = FunctionPrototypeBind(load, null);
acceptedHooks.loader = load;
}

return acceptedHooks;
Expand Down Expand Up @@ -354,7 +351,7 @@ class ESMLoader {
ArrayPrototypePush(
this.#globalPreloaders,
{
fn: FunctionPrototypeBind(globalPreloader), // [1]
fn: globalPreloader,
url,
},
);
Expand All @@ -363,7 +360,7 @@ class ESMLoader {
ArrayPrototypePush(
this.#resolvers,
{
fn: FunctionPrototypeBind(resolver), // [1]
fn: resolver,
url,
},
);
Expand All @@ -372,15 +369,13 @@ class ESMLoader {
ArrayPrototypePush(
this.#loaders,
{
fn: FunctionPrototypeBind(loader), // [1]
fn: loader,
url,
},
);
}
}

// [1] ensure hook function is not bound to ESMLoader instance

this.preload();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function resolve(url, _, next) {
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
return next(url);
}

export function load(url, _, next) {
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
return next(url);
}

export function globalPreload() {
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
return "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/loader-this-value-inside-hook-functions.mjs
import '../common/index.mjs';

// Actual test is inside the loader module.