sea: only assert snapshot main function for main threads · nodejs/node@1cfa31f
1+'use strict';
2+3+require('../common');
4+5+const {
6+ generateSEA,
7+ skipIfSingleExecutableIsNotSupported,
8+} = require('../common/sea');
9+10+skipIfSingleExecutableIsNotSupported();
11+12+// This tests the snapshot support in single executable applications.
13+14+const tmpdir = require('../common/tmpdir');
15+const { writeFileSync, existsSync } = require('fs');
16+const {
17+ spawnSyncAndAssert, spawnSyncAndExitWithoutError,
18+} = require('../common/child_process');
19+const assert = require('assert');
20+21+const configFile = tmpdir.resolve('sea-config.json');
22+const seaPrepBlob = tmpdir.resolve('sea-prep.blob');
23+const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'sea');
24+25+{
26+tmpdir.refresh();
27+28+// FIXME(joyeecheung): currently `worker_threads` cannot be loaded during the
29+// snapshot building process because internal/worker.js is accessing isMainThread at
30+// the top level (and there are maybe more code that access these at the top-level),
31+// and have to be loaded in the deserialized snapshot main function.
32+// Change these states to be accessed on-demand.
33+const code = `
34+ const {
35+ setDeserializeMainFunction,
36+ } = require('v8').startupSnapshot;
37+ setDeserializeMainFunction(() => {
38+ const { Worker } = require('worker_threads');
39+ new Worker("console.log('Hello from Worker')", { eval: true });
40+ });
41+ `;
42+43+writeFileSync(tmpdir.resolve('snapshot.js'), code, 'utf-8');
44+writeFileSync(configFile, `
45+ {
46+ "main": "snapshot.js",
47+ "output": "sea-prep.blob",
48+ "useSnapshot": true
49+ }
50+ `);
51+52+spawnSyncAndExitWithoutError(
53+process.execPath,
54+['--experimental-sea-config', 'sea-config.json'],
55+{
56+cwd: tmpdir.path,
57+env: {
58+NODE_DEBUG_NATIVE: 'SEA',
59+ ...process.env,
60+},
61+});
62+63+assert(existsSync(seaPrepBlob));
64+65+generateSEA(outputFile, process.execPath, seaPrepBlob);
66+67+spawnSyncAndAssert(
68+outputFile,
69+{
70+env: {
71+NODE_DEBUG_NATIVE: 'SEA',
72+ ...process.env,
73+}
74+},
75+{
76+trim: true,
77+stdout: 'Hello from Worker'
78+}
79+);
80+}