1
- const SwiftRuntime = require ( "javascript-kit-swift" ) . SwiftRuntime ;
2
- const WasmerWASI = require ( "@wasmer/wasi" ) . WASI ;
3
- const WasmFs = require ( "@wasmer/wasmfs" ) . WasmFs ;
4
- const NodeWASI = require ( "wasi" ) . WASI ;
5
- const { WASI : MicroWASI , useAll } = require ( "uwasi" ) ;
6
-
7
- const promisify = require ( "util" ) . promisify ;
8
- const fs = require ( "fs" ) ;
9
- const readFile = promisify ( fs . readFile ) ;
1
+ import { SwiftRuntime } from "javascript-kit-swift"
2
+ import { WASI as NodeWASI } from "wasi"
3
+ import { WASI as MicroWASI , useAll } from "uwasi"
4
+ import * as fs from "fs/promises"
10
5
11
6
const WASI = {
12
- Wasmer : ( { programName } ) => {
13
- // Instantiate a new WASI Instance
14
- const wasmFs = new WasmFs ( ) ;
15
- // Output stdout and stderr to console
16
- const originalWriteSync = wasmFs . fs . writeSync ;
17
- wasmFs . fs . writeSync = ( fd , buffer , offset , length , position ) => {
18
- const text = new TextDecoder ( "utf-8" ) . decode ( buffer ) ;
19
- switch ( fd ) {
20
- case 1 :
21
- console . log ( text ) ;
22
- break ;
23
- case 2 :
24
- console . error ( text ) ;
25
- break ;
26
- }
27
- return originalWriteSync ( fd , buffer , offset , length , position ) ;
28
- } ;
29
- const wasi = new WasmerWASI ( {
30
- args : [ programName ] ,
31
- env : { } ,
32
- bindings : {
33
- ...WasmerWASI . defaultBindings ,
34
- fs : wasmFs . fs ,
35
- } ,
36
- } ) ;
37
-
38
- return {
39
- wasiImport : wasi . wasiImport ,
40
- start ( instance ) {
41
- wasi . start ( instance ) ;
42
- instance . exports . _initialize ( ) ;
43
- instance . exports . main ( ) ;
44
- }
45
- }
46
- } ,
47
7
MicroWASI : ( { programName } ) => {
48
8
const wasi = new MicroWASI ( {
49
9
args : [ programName ] ,
@@ -53,25 +13,28 @@ const WASI = {
53
13
54
14
return {
55
15
wasiImport : wasi . wasiImport ,
56
- start ( instance ) {
16
+ start ( instance , swift ) {
57
17
wasi . initialize ( instance ) ;
58
- instance . exports . main ( ) ;
18
+ swift . main ( ) ;
59
19
}
60
20
}
61
21
} ,
62
22
Node : ( { programName } ) => {
63
23
const wasi = new NodeWASI ( {
64
24
args : [ programName ] ,
65
25
env : { } ,
26
+ preopens : {
27
+ "/" : "./" ,
28
+ } ,
66
29
returnOnExit : false ,
67
30
version : "preview1" ,
68
31
} )
69
32
70
33
return {
71
34
wasiImport : wasi . wasiImport ,
72
- start ( instance ) {
35
+ start ( instance , swift ) {
73
36
wasi . initialize ( instance ) ;
74
- instance . exports . main ( ) ;
37
+ swift . main ( ) ;
75
38
}
76
39
}
77
40
} ,
@@ -88,10 +51,10 @@ const selectWASIBackend = () => {
88
51
return WASI . Node ;
89
52
} ;
90
53
91
- const startWasiTask = async ( wasmPath , wasiConstructor = selectWASIBackend ( ) ) => {
54
+ export const startWasiTask = async ( wasmPath , wasiConstructor = selectWASIBackend ( ) ) => {
92
55
const swift = new SwiftRuntime ( ) ;
93
56
// Fetch our Wasm File
94
- const wasmBinary = await readFile ( wasmPath ) ;
57
+ const wasmBinary = await fs . readFile ( wasmPath ) ;
95
58
const wasi = wasiConstructor ( { programName : wasmPath } ) ;
96
59
97
60
// Instantiate the WebAssembly file
@@ -106,7 +69,5 @@ const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) =>
106
69
107
70
swift . setInstance ( instance ) ;
108
71
// Start the WebAssembly WASI instance!
109
- wasi . start ( instance ) ;
72
+ wasi . start ( instance , swift ) ;
110
73
} ;
111
-
112
- module . exports = { startWasiTask, WASI } ;
0 commit comments