@@ -3,7 +3,6 @@ import { WASI as NodeWASI } from "wasi"
33import { WASI as MicroWASI , useAll } from "uwasi"
44import * as fs from "fs/promises"
55import path from "path" ;
6- import { Worker , parentPort } from "node:worker_threads" ;
76
87const WASI = {
98 MicroWASI : ( { args } ) => {
@@ -53,16 +52,6 @@ const selectWASIBackend = () => {
5352 return "Node"
5453} ;
5554
56- function isUsingSharedMemory ( module ) {
57- const imports = WebAssembly . Module . imports ( module ) ;
58- for ( const entry of imports ) {
59- if ( entry . module === "env" && entry . name === "memory" && entry . kind == "memory" ) {
60- return true ;
61- }
62- }
63- return false ;
64- }
65-
6655function constructBaseImportObject ( wasi , swift ) {
6756 return {
6857 wasi_snapshot_preview1 : wasi . wasiImport ,
@@ -74,79 +63,6 @@ function constructBaseImportObject(wasi, swift) {
7463 }
7564}
7665
77- export async function startWasiChildThread ( event ) {
78- const { module, programName, memory, tid, startArg } = event ;
79- const swift = new SwiftRuntime ( {
80- sharedMemory : true ,
81- threadChannel : {
82- postMessageToMainThread : ( message , transfer ) => {
83- parentPort . postMessage ( message , transfer ) ;
84- } ,
85- listenMessageFromMainThread : ( listener ) => {
86- parentPort . on ( "message" , listener )
87- }
88- }
89- } ) ;
90- // Use uwasi for child threads because Node.js WASI cannot be used without calling
91- // `WASI.start` or `WASI.initialize`, which is already called in the main thread and
92- // will cause an error if called again.
93- const wasi = WASI . MicroWASI ( { programName } ) ;
94-
95- const importObject = constructBaseImportObject ( wasi , swift ) ;
96-
97- importObject [ "wasi" ] = {
98- "thread-spawn" : ( ) => {
99- throw new Error ( "Cannot spawn a new thread from a worker thread" )
100- }
101- } ;
102- importObject [ "env" ] = { memory } ;
103- importObject [ "JavaScriptEventLoopTestSupportTests" ] = {
104- "isMainThread" : ( ) => false ,
105- }
106-
107- const instance = await WebAssembly . instantiate ( module , importObject ) ;
108- swift . setInstance ( instance ) ;
109- wasi . setInstance ( instance ) ;
110- swift . startThread ( tid , startArg ) ;
111- }
112-
113- class ThreadRegistry {
114- workers = new Map ( ) ;
115- nextTid = 1 ;
116-
117- spawnThread ( module , programName , memory , startArg ) {
118- const tid = this . nextTid ++ ;
119- const selfFilePath = new URL ( import . meta. url ) . pathname ;
120- const worker = new Worker ( `
121- const { parentPort } = require('node:worker_threads');
122-
123- Error.stackTraceLimit = 100;
124- parentPort.once("message", async (event) => {
125- const { selfFilePath } = event;
126- const { startWasiChildThread } = await import(selfFilePath);
127- await startWasiChildThread(event);
128- })
129- ` , { type : "module" , eval : true } )
130-
131- worker . on ( "error" , ( error ) => {
132- console . error ( `Worker thread ${ tid } error:` , error ) ;
133- throw error ;
134- } ) ;
135- this . workers . set ( tid , worker ) ;
136- worker . postMessage ( { selfFilePath, module, programName, memory, tid, startArg } ) ;
137- return tid ;
138- }
139-
140- worker ( tid ) {
141- return this . workers . get ( tid ) ;
142- }
143-
144- wakeUpWorkerThread ( tid , message , transfer ) {
145- const worker = this . workers . get ( tid ) ;
146- worker . postMessage ( message , transfer ) ;
147- }
148- }
149-
15066export const startWasiTask = async ( wasmPath , wasiConstructorKey = selectWASIBackend ( ) ) => {
15167 // Fetch our Wasm File
15268 const wasmBinary = await fs . readFile ( wasmPath ) ;
@@ -157,38 +73,10 @@ export const startWasiTask = async (wasmPath, wasiConstructorKey = selectWASIBac
15773
15874 const module = await WebAssembly . compile ( wasmBinary ) ;
15975
160- const sharedMemory = isUsingSharedMemory ( module ) ;
161- const threadRegistry = new ThreadRegistry ( ) ;
162- const swift = new SwiftRuntime ( {
163- sharedMemory,
164- threadChannel : {
165- postMessageToWorkerThread : threadRegistry . wakeUpWorkerThread . bind ( threadRegistry ) ,
166- listenMessageFromWorkerThread : ( tid , listener ) => {
167- const worker = threadRegistry . worker ( tid ) ;
168- worker . on ( "message" , listener ) ;
169- }
170- }
171- } ) ;
76+ const swift = new SwiftRuntime ( ) ;
17277
17378 const importObject = constructBaseImportObject ( wasi , swift ) ;
17479
175- importObject [ "JavaScriptEventLoopTestSupportTests" ] = {
176- "isMainThread" : ( ) => true ,
177- }
178-
179- if ( sharedMemory ) {
180- // We don't have JS API to get memory descriptor of imported memory
181- // at this moment, so we assume 256 pages (16MB) memory is enough
182- // large for initial memory size.
183- const memory = new WebAssembly . Memory ( { initial : 1024 , maximum : 16384 , shared : true } )
184- importObject [ "env" ] = { memory } ;
185- importObject [ "wasi" ] = {
186- "thread-spawn" : ( startArg ) => {
187- return threadRegistry . spawnThread ( module , programName , memory , startArg ) ;
188- }
189- }
190- }
191-
19280 // Instantiate the WebAssembly file
19381 const instance = await WebAssembly . instantiate ( module , importObject ) ;
19482
0 commit comments