Skip to content

Commit

Permalink
esm: do not bind loader hook functions
Browse files Browse the repository at this point in the history
PR-URL: #44122
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Sep 5, 2022
1 parent 4eb23a6 commit 46c8f5d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
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 @@ -306,16 +305,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 @@ -346,7 +343,7 @@ class ESMLoader {
ArrayPrototypePush(
this.#globalPreloaders,
{
fn: FunctionPrototypeBind(globalPreloader), // [1]
fn: globalPreloader,
url,
},
);
Expand All @@ -355,7 +352,7 @@ class ESMLoader {
ArrayPrototypePush(
this.#resolvers,
{
fn: FunctionPrototypeBind(resolver), // [1]
fn: resolver,
url,
},
);
Expand All @@ -364,15 +361,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.

0 comments on commit 46c8f5d

Please sign in to comment.