Skip to content

Commit

Permalink
bootstrap: run preload prior to frozen-intrinsics
Browse files Browse the repository at this point in the history
This is used to allow people to run polyfills.

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

PR-URL: #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>
  • Loading branch information
bmeck committed Aug 20, 2019
1 parent 9fd9efa commit 85898e0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ function prepareMainThreadExecution(expandArgv1 = false) {
initializeClusterIPC();

initializeDeprecations();
initializeFrozenIntrinsics();
initializeCJSLoader();
initializeESMLoader();
loadPreloadModules();
initializeFrozenIntrinsics();
}

function patchProcessObject(expandArgv1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/intrinsic-mutation.js
Original file line number Diff line number Diff line change
@@ -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() {}
}
);
2 changes: 2 additions & 0 deletions test/fixtures/print-intrinsic-mutation-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
console.log({}.flatten.name);
3 changes: 3 additions & 0 deletions test/fixtures/worker-from-argv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
const {Worker} = require('worker_threads');
new Worker(process.argv[2]).on('exit', process.exit);
29 changes: 29 additions & 0 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 85898e0

Please sign in to comment.