mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 22:40:57 +00:00

- Run the embedder entry point directly through runEmbedderEntryPoint(), instead of going through another JS -> C++ trip through the function returned by getEmbedderEntryFunction() - For --build-snapshot, read the snapshot script code directly in C++ and pass it to SnapshotBuilder::Generate(), this makes the entry point more explicit instead of hiding it in JS land, and also makes it possible to invoke SnapshotBuilder::Generate() internally to create a custom snapshot. - Previously we used process.execPath for the embedder to create __filename and __dirname in the snapshot builder script while using process.argv[1] for --build-snapshot (where it's always set) which results in inconsistencies. We now require the embedder to also set args[1] when creating the Environment if they intend to run snapshot scripts with a context that contains __filename and __dirname, which would be derived from args[1]. If they prefer not to include build-time paths in the snapshot, we now provide node::GetAnonymousMainPath() as an alternative. PR-URL: https://github.com/nodejs/node/pull/48242 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
113 lines
3.4 KiB
JavaScript
113 lines
3.4 KiB
JavaScript
'use strict';
|
||
const common = require('../common');
|
||
const fixtures = require('../common/fixtures');
|
||
const tmpdir = require('../common/tmpdir');
|
||
const assert = require('assert');
|
||
const child_process = require('child_process');
|
||
const path = require('path');
|
||
const fs = require('fs');
|
||
|
||
tmpdir.refresh();
|
||
common.allowGlobals(global.require);
|
||
common.allowGlobals(global.embedVars);
|
||
|
||
function resolveBuiltBinary(bin) {
|
||
let binary = `out/${common.buildType}/${bin}`;
|
||
if (common.isWindows) {
|
||
binary += '.exe';
|
||
}
|
||
return path.resolve(__dirname, '..', '..', binary);
|
||
}
|
||
|
||
const binary = resolveBuiltBinary('embedtest');
|
||
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, ['console.log(42)'])
|
||
.stdout.toString().trim(),
|
||
'42');
|
||
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, ['console.log(embedVars.nön_ascıı)'])
|
||
.stdout.toString().trim(),
|
||
'🏳️🌈');
|
||
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, ['console.log(42)'])
|
||
.stdout.toString().trim(),
|
||
'42');
|
||
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, ['throw new Error()']).status,
|
||
1);
|
||
|
||
// Cannot require internals anymore:
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, ['require("lib/internal/test/binding")']).status,
|
||
1);
|
||
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, ['process.exitCode = 8']).status,
|
||
8);
|
||
|
||
|
||
const fixturePath = JSON.stringify(fixtures.path('exit.js'));
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, [`require(${fixturePath})`, 92]).status,
|
||
92);
|
||
|
||
function getReadFileCodeForPath(path) {
|
||
return `(require("fs").readFileSync(${JSON.stringify(path)}, "utf8"))`;
|
||
}
|
||
|
||
// Basic snapshot support
|
||
for (const extraSnapshotArgs of [[], ['--embedder-snapshot-as-file']]) {
|
||
// readSync + eval since snapshots don't support userland require() (yet)
|
||
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
|
||
const blobPath = path.join(tmpdir.path, 'embedder-snapshot.blob');
|
||
const buildSnapshotArgs = [
|
||
`eval(${getReadFileCodeForPath(snapshotFixture)})`, 'arg1', 'arg2',
|
||
'--embedder-snapshot-blob', blobPath, '--embedder-snapshot-create',
|
||
...extraSnapshotArgs,
|
||
];
|
||
const runEmbeddedArgs = [
|
||
'--embedder-snapshot-blob', blobPath, ...extraSnapshotArgs, 'arg3', 'arg4',
|
||
];
|
||
|
||
fs.rmSync(blobPath, { force: true });
|
||
const child = child_process.spawnSync(binary, [
|
||
'--', ...buildSnapshotArgs,
|
||
], {
|
||
cwd: tmpdir.path,
|
||
});
|
||
if (child.status !== 0) {
|
||
console.log(child.stderr.toString());
|
||
console.log(child.stdout.toString());
|
||
}
|
||
assert.strictEqual(child.status, 0);
|
||
const spawnResult = child_process.spawnSync(binary, ['--', ...runEmbeddedArgs]);
|
||
assert.deepStrictEqual(JSON.parse(spawnResult.stdout), {
|
||
originalArgv: [binary, ...buildSnapshotArgs],
|
||
currentArgv: [binary, ...runEmbeddedArgs],
|
||
});
|
||
}
|
||
|
||
// Create workers and vm contexts after deserialization
|
||
{
|
||
const snapshotFixture = fixtures.path('snapshot', 'create-worker-and-vm.js');
|
||
const blobPath = path.join(tmpdir.path, 'embedder-snapshot.blob');
|
||
const buildSnapshotArgs = [
|
||
`eval(${getReadFileCodeForPath(snapshotFixture)})`,
|
||
'--embedder-snapshot-blob', blobPath, '--embedder-snapshot-create',
|
||
];
|
||
|
||
fs.rmSync(blobPath, { force: true });
|
||
assert.strictEqual(child_process.spawnSync(binary, [
|
||
'--', ...buildSnapshotArgs,
|
||
], {
|
||
cwd: tmpdir.path,
|
||
}).status, 0);
|
||
assert.strictEqual(
|
||
child_process.spawnSync(binary, ['--', '--embedder-snapshot-blob', blobPath]).status,
|
||
0);
|
||
}
|