Skip to content
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 numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,6 @@ const {
ArrayIsArray,
ArrayPrototypeJoin,
ArrayPrototypePush,
FunctionPrototypeBind,
FunctionPrototypeCall,
ObjectAssign,
ObjectCreate,
Expand DownExpand 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 DownExpand 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 numberDiff line numberDiff 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 numberDiff line numberDiff 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.