mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 23:52:40 +00:00

Avoid calling Array.prototype.join() in debug() calls. These are evaluated on every call, even if the debug() call is a no-op. This commit replaces the join() calls with the %j placeholder. PR-URL: https://github.com/nodejs/node/pull/25241 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');
|
|
const debug = require('util').debuglog('esm');
|
|
const ArrayJoin = Function.call.bind(Array.prototype.join);
|
|
const ArrayMap = Function.call.bind(Array.prototype.map);
|
|
|
|
const createDynamicModule = (exports, url = '', evaluate) => {
|
|
debug('creating ESM facade for %s with exports: %j', url, exports);
|
|
const names = ArrayMap(exports, (name) => `${name}`);
|
|
|
|
const source = `
|
|
${ArrayJoin(ArrayMap(names, (name) =>
|
|
`let $${name};
|
|
export { $${name} as ${name} };
|
|
import.meta.exports.${name} = {
|
|
get: () => $${name},
|
|
set: (v) => $${name} = v,
|
|
};`), '\n')
|
|
}
|
|
|
|
import.meta.done();
|
|
`;
|
|
|
|
const m = new ModuleWrap(source, `${url}`);
|
|
m.link(() => 0);
|
|
m.instantiate();
|
|
|
|
const readyfns = new Set();
|
|
const reflect = {
|
|
namespace: m.namespace(),
|
|
exports: {},
|
|
onReady: (cb) => { readyfns.add(cb); },
|
|
};
|
|
|
|
callbackMap.set(m, {
|
|
initializeImportMeta: (meta, wrap) => {
|
|
meta.exports = reflect.exports;
|
|
meta.done = () => {
|
|
evaluate(reflect);
|
|
reflect.onReady = (cb) => cb(reflect);
|
|
for (const fn of readyfns) {
|
|
readyfns.delete(fn);
|
|
fn(reflect);
|
|
}
|
|
};
|
|
},
|
|
});
|
|
|
|
return {
|
|
module: m,
|
|
reflect,
|
|
};
|
|
};
|
|
|
|
module.exports = createDynamicModule;
|