1
1
const SwiftRuntime = require ( "javascript-kit-swift" ) . SwiftRuntime ;
2
- const WASI = require ( "@wasmer/wasi" ) . WASI ;
2
+ const WasmerWASI = require ( "@wasmer/wasi" ) . WASI ;
3
3
const WasmFs = require ( "@wasmer/wasmfs" ) . WasmFs ;
4
+ const NodeWASI = require ( "wasi" ) . WASI ;
4
5
5
6
const promisify = require ( "util" ) . promisify ;
6
7
const fs = require ( "fs" ) ;
7
8
const readFile = promisify ( fs . readFile ) ;
8
9
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
+ }
23
44
}
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
+ } ;
34
73
74
+ const startWasiTask = async ( wasmPath , wasiConstructor = selectWASIBackend ( ) ) => {
35
75
const swift = new SwiftRuntime ( ) ;
36
76
// Fetch our Wasm File
37
77
const wasmBinary = await readFile ( wasmPath ) ;
78
+ const wasi = wasiConstructor ( ) ;
38
79
39
80
// Instantiate the WebAssembly file
40
81
let { instance } = await WebAssembly . instantiate ( wasmBinary , {
@@ -45,8 +86,6 @@ const startWasiTask = async (wasmPath) => {
45
86
swift . setInstance ( instance ) ;
46
87
// Start the WebAssembly WASI instance!
47
88
wasi . start ( instance ) ;
48
- instance . exports . _initialize ( ) ;
49
- instance . exports . main ( ) ;
50
89
} ;
51
90
52
- module . exports = { startWasiTask } ;
91
+ module . exports = { startWasiTask, WASI } ;
0 commit comments