Skip to content

Commit 6f9eff8

Browse files
authored
esm: do not bind loader hook functions
PR-URL: #44122 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 80ef02b commit 6f9eff8

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

lib/internal/modules/esm/loader.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
ArrayIsArray,
99
ArrayPrototypeJoin,
1010
ArrayPrototypePush,
11-
FunctionPrototypeBind,
1211
FunctionPrototypeCall,
1312
ObjectAssign,
1413
ObjectCreate,
@@ -306,16 +305,14 @@ class ESMLoader {
306305
'DeprecationWarning',
307306
);
308307

309-
// Use .bind() to avoid giving access to the Loader instance when called.
310308
if (globalPreload) {
311-
acceptedHooks.globalPreloader =
312-
FunctionPrototypeBind(globalPreload, null);
309+
acceptedHooks.globalPreloader = globalPreload;
313310
}
314311
if (resolve) {
315-
acceptedHooks.resolver = FunctionPrototypeBind(resolve, null);
312+
acceptedHooks.resolver = resolve;
316313
}
317314
if (load) {
318-
acceptedHooks.loader = FunctionPrototypeBind(load, null);
315+
acceptedHooks.loader = load;
319316
}
320317

321318
return acceptedHooks;
@@ -346,7 +343,7 @@ class ESMLoader {
346343
ArrayPrototypePush(
347344
this.#globalPreloaders,
348345
{
349-
fn: FunctionPrototypeBind(globalPreloader), // [1]
346+
fn: globalPreloader,
350347
url,
351348
},
352349
);
@@ -355,7 +352,7 @@ class ESMLoader {
355352
ArrayPrototypePush(
356353
this.#resolvers,
357354
{
358-
fn: FunctionPrototypeBind(resolver), // [1]
355+
fn: resolver,
359356
url,
360357
},
361358
);
@@ -364,15 +361,13 @@ class ESMLoader {
364361
ArrayPrototypePush(
365362
this.#loaders,
366363
{
367-
fn: FunctionPrototypeBind(loader), // [1]
364+
fn: loader,
368365
url,
369366
},
370367
);
371368
}
372369
}
373370

374-
// [1] ensure hook function is not bound to ESMLoader instance
375-
376371
this.preload();
377372
}
378373

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function resolve(url, _, next) {
2+
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
3+
return next(url);
4+
}
5+
6+
export function load(url, _, next) {
7+
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
8+
return next(url);
9+
}
10+
11+
export function globalPreload() {
12+
if (this != null) throw new Error('hook function must not be bound to ESMLoader instance');
13+
return "";
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/loader-this-value-inside-hook-functions.mjs
2+
import '../common/index.mjs';
3+
4+
// Actual test is inside the loader module.

0 commit comments

Comments
 (0)