esm: do not bind loader hook functions

PR-URL: https://github.com/nodejs/node/pull/44122
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Antoine du Hamel authored and Ruy Adorno committed 2022-08-22 19:37:20 -04:00
1 parent d09bc5402d
commit a6dccc969f
3 files changed
+24 -11

No files matched your search

+6 -11
View File
@@ -8,7 +8,6 @@ const {
ArrayIsArray,
ArrayPrototypeJoin,
ArrayPrototypePush,
FunctionPrototypeBind,
FunctionPrototypeCall,
ObjectAssign,
ObjectCreate,
@@ -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;
@@ -346,7 +343,7 @@ class ESMLoader {
ArrayPrototypePush(
this.#globalPreloaders,
{
fn: FunctionPrototypeBind(globalPreloader), // [1]
fn: globalPreloader,
url,
},
);
@@ -355,7 +352,7 @@ class ESMLoader {
ArrayPrototypePush(
this.#resolvers,
{
fn: FunctionPrototypeBind(resolver), // [1]
fn: resolver,
url,
},
);
@@ -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();
}
@@ -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 "";
}
@@ -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.