11const SwiftRuntime = require ( "javascript-kit-swift" ) . SwiftRuntime ;
2- const WASI = require ( "@wasmer/wasi" ) . WASI ;
2+ const WasmerWASI = require ( "@wasmer/wasi" ) . WASI ;
33const WasmFs = require ( "@wasmer/wasmfs" ) . WasmFs ;
4+ const NodeWASI = require ( "wasi" ) . WASI ;
45
56const promisify = require ( "util" ) . promisify ;
67const fs = require ( "fs" ) ;
78const readFile = promisify ( fs . readFile ) ;
89
9- const startWasiTask = async ( wasmPath ) => {
10- // Instantiate a new WASI Instance
11- const wasmFs = new WasmFs ( ) ;
12- // Output stdout and stderr to console
13- const originalWriteSync = wasmFs . fs . writeSync ;
14- wasmFs . fs . writeSync = ( fd , buffer , offset , length , position ) => {
15- const text = new TextDecoder ( "utf-8" ) . decode ( buffer ) ;
16- switch ( fd ) {
17- case 1 :
18- console . log ( text ) ;
19- break ;
20- case 2 :
21- console . error ( text ) ;
22- break ;
10+ const WASI = {
11+ Wasmer : ( ) => {
12+ // Instantiate a new WASI Instance
13+ const wasmFs = new WasmFs ( ) ;
14+ // Output stdout and stderr to console
15+ const originalWriteSync = wasmFs . fs . writeSync ;
16+ wasmFs . fs . writeSync = ( fd , buffer , offset , length , position ) => {
17+ const text = new TextDecoder ( "utf-8" ) . decode ( buffer ) ;
18+ switch ( fd ) {
19+ case 1 :
20+ console . log ( text ) ;
21+ break ;
22+ case 2 :
23+ console . error ( text ) ;
24+ break ;
25+ }
26+ return originalWriteSync ( fd , buffer , offset , length , position ) ;
27+ } ;
28+ const wasi = new WasmerWASI ( {
29+ args : [ ] ,
30+ env : { } ,
31+ bindings : {
32+ ...WasmerWASI . defaultBindings ,
33+ fs : wasmFs . fs ,
34+ } ,
35+ } ) ;
36+
37+ return {
38+ wasiImport : wasi . wasiImport ,
39+ start ( instance ) {
40+ wasi . start ( instance ) ;
41+ instance . exports . _initialize ( ) ;
42+ instance . exports . main ( ) ;
43+ }
2344 }
24- return originalWriteSync ( fd , buffer , offset , length , position ) ;
25- } ;
26- let wasi = new WASI ( {
27- args : [ ] ,
28- env : { } ,
29- bindings : {
30- ...WASI . defaultBindings ,
31- fs : wasmFs . fs ,
32- } ,
33- } ) ;
45+ } ,
46+ Node : ( ) => {
47+ const wasi = new NodeWASI ( {
48+ args : [ ] ,
49+ env : { } ,
50+ returnOnExit : true ,
51+ } )
52+
53+ return {
54+ wasiImport : wasi . wasiImport ,
55+ start ( instance ) {
56+ wasi . initialize ( instance ) ;
57+ instance . exports . main ( ) ;
58+ }
59+ }
60+ } ,
61+ } ;
62+
63+ const selectWASIBackend = ( ) => {
64+ const value = process . env [ "JAVASCRIPTKIT_WASI_BACKEND" ]
65+ if ( value ) {
66+ const backend = WASI [ value ] ;
67+ if ( backend ) {
68+ return backend ;
69+ }
70+ }
71+ return WASI . Node ;
72+ } ;
3473
74+ const startWasiTask = async ( wasmPath , wasiConstructor = selectWASIBackend ( ) ) => {
3575 const swift = new SwiftRuntime ( ) ;
3676 // Fetch our Wasm File
3777 const wasmBinary = await readFile ( wasmPath ) ;
78+ const wasi = wasiConstructor ( ) ;
3879
3980 // Instantiate the WebAssembly file
4081 let { instance } = await WebAssembly . instantiate ( wasmBinary , {
@@ -45,8 +86,6 @@ const startWasiTask = async (wasmPath) => {
4586 swift . setInstance ( instance ) ;
4687 // Start the WebAssembly WASI instance!
4788 wasi . start ( instance ) ;
48- instance . exports . _initialize ( ) ;
49- instance . exports . main ( ) ;
5089} ;
5190
52- module . exports = { startWasiTask } ;
91+ module . exports = { startWasiTask, WASI } ;
0 commit comments