MCPcopy Index your code
hub / github.com/nodejs/node / spawn

Function spawn

test/fixtures/wasi-preview-1.js:66–151  ·  view source on GitHub ↗
(startArg, threadId)

Source from the content-addressed store, hash-verified

64 return proc_exit.call(this, code);
65 };
66 const spawn = (startArg, threadId) => {
67 const tid = nextTid++;
68 const name = `pthread-${tid}`;
69 const sab = new SharedArrayBuffer(8 + 8192);
70 const result = new Int32Array(sab);
71
72 const workerData = {
73 name,
74 startArg,
75 tid,
76 wasmModule,
77 memory: importObject.env.memory,
78 result,
79 };
80
81 const worker = new Worker(__filename, {
82 name,
83 argv: process.argv.slice(2),
84 execArgv: [
85 '--experimental-wasi-unstable-preview1',
86 ],
87 workerData,
88 });
89 workers[tid] = worker;
90
91 worker.on('message', ({ cmd, startArg, threadId, tid }) => {
92 if (cmd === 'loaded') {
93 worker.unref();
94 } else if (cmd === 'thread-spawn') {
95 spawn(startArg, threadId);
96 } else if (cmd === 'cleanup-thread') {
97 workers[tid].terminate();
98 delete workers[tid];
99 } else if (cmd === 'terminate-all-threads') {
100 terminateAllThreads();
101 }
102 });
103
104 worker.on('error', (e) => {
105 terminateAllThreads();
106 throw new Error(e);
107 });
108
109 const r = Atomics.wait(result, 0, 0, 1000);
110 if (r === 'timed-out') {
111 workers[tid].terminate();
112 delete workers[tid];
113 if (threadId) {
114 Atomics.store(threadId, 0, -6);
115 Atomics.notify(threadId, 0);
116 }
117 return -6;
118 }
119 if (Atomics.load(result, 0) !== 0) {
120 const decoder = new TextDecoder();
121 const nameLength = Atomics.load(result, 1);
122 const messageLength = Atomics.load(result, 2);
123 const stackLength = Atomics.load(result, 3);

Calls 10

unrefMethod · 0.95
decodeMethod · 0.95
terminateAllThreadsFunction · 0.85
sliceMethod · 0.65
onMethod · 0.45
terminateMethod · 0.45
waitMethod · 0.45
storeMethod · 0.45
loadMethod · 0.45
definePropertyMethod · 0.45

Tested by 1

testFunction · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…