bootstrap: run preload prior to frozen-intrinsics

This is used to allow people to run polyfills.

Co-Authored-By: Anna Henningsen <github@addaleax.net>

PR-URL: https://github.com/nodejs/node/pull/28940
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Bradley Farias 2019-08-02 09:06:34 -05:00
parent 9fd9efa492
commit 85898e0aca
7 changed files with 49 additions and 2 deletions

View File

@ -218,6 +218,9 @@ Support is currently only provided for the root context and no guarantees are
currently provided that `global.Array` is indeed the default intrinsic
reference. Code may break under this flag.
`--require` runs prior to freezing intrinsics in order to allow polyfills to
be added.
### `--heapsnapshot-signal=signal`
<!-- YAML
added: v12.0.0

View File

@ -48,10 +48,10 @@ function prepareMainThreadExecution(expandArgv1 = false) {
initializeClusterIPC();
initializeDeprecations();
initializeFrozenIntrinsics();
initializeCJSLoader();
initializeESMLoader();
loadPreloadModules();
initializeFrozenIntrinsics();
}
function patchProcessObject(expandArgv1) {

View File

@ -106,10 +106,10 @@ port.on('message', (message) => {
require('internal/process/policy').setup(manifestSrc, manifestURL);
}
initializeDeprecations();
initializeFrozenIntrinsics();
initializeCJSLoader();
initializeESMLoader();
loadPreloadModules();
initializeFrozenIntrinsics();
publicWorker.parentPort = publicPort;
publicWorker.workerData = workerData;

10
test/fixtures/intrinsic-mutation.js vendored Normal file
View File

@ -0,0 +1,10 @@
'use strict';
Object.defineProperty(
Object.prototype,
'flatten', {
enumerable: false,
// purposefully named something that
// would never land in JS itself
value: function smoosh() {}
}
);

View File

@ -0,0 +1,2 @@
'use strict';
console.log({}.flatten.name);

3
test/fixtures/worker-from-argv.js vendored Normal file
View File

@ -0,0 +1,3 @@
'use strict';
const {Worker} = require('worker_threads');
new Worker(process.argv[2]).on('exit', process.exit);

View File

@ -23,6 +23,9 @@ const fixtureA = fixtures.path('printA.js');
const fixtureB = fixtures.path('printB.js');
const fixtureC = fixtures.path('printC.js');
const fixtureD = fixtures.path('define-global.js');
const fixtureE = fixtures.path('intrinsic-mutation.js');
const fixtureF = fixtures.path('print-intrinsic-mutation-name.js');
const fixtureG = fixtures.path('worker-from-argv.js');
const fixtureThrows = fixtures.path('throws_error4.js');
// Test preloading a single module works
@ -62,6 +65,32 @@ childProcess.exec(
}
);
// Test that preload can be used with --frozen-intrinsics
childProcess.exec(
`"${nodeBinary}" --frozen-intrinsics ${
preloadOption([fixtureE])
} ${
fixtureF
}`,
function(err, stdout) {
assert.ifError(err);
assert.strictEqual(stdout, 'smoosh\n');
}
);
childProcess.exec(
`"${
nodeBinary
}" --frozen-intrinsics ${
preloadOption([fixtureE])
} ${
fixtureG
} ${fixtureF}`,
function(err, stdout) {
assert.ifError(err);
assert.strictEqual(stdout, 'smoosh\n');
}
);
// Test that preload can be used with stdin
const stdinProc = childProcess.spawn(
nodeBinary,