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"
105
116const 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- } ,
477 MicroWASI : ( { programName } ) => {
488 const wasi = new MicroWASI ( {
499 args : [ programName ] ,
@@ -53,25 +13,28 @@ const WASI = {
5313
5414 return {
5515 wasiImport : wasi . wasiImport ,
56- start ( instance ) {
16+ start ( instance , swift ) {
5717 wasi . initialize ( instance ) ;
58- instance . exports . main ( ) ;
18+ swift . main ( ) ;
5919 }
6020 }
6121 } ,
6222 Node : ( { programName } ) => {
6323 const wasi = new NodeWASI ( {
6424 args : [ programName ] ,
6525 env : { } ,
26+ preopens : {
27+ "/" : "./" ,
28+ } ,
6629 returnOnExit : false ,
6730 version : "preview1" ,
6831 } )
6932
7033 return {
7134 wasiImport : wasi . wasiImport ,
72- start ( instance ) {
35+ start ( instance , swift ) {
7336 wasi . initialize ( instance ) ;
74- instance . exports . main ( ) ;
37+ swift . main ( ) ;
7538 }
7639 }
7740 } ,
@@ -88,10 +51,10 @@ const selectWASIBackend = () => {
8851 return WASI . Node ;
8952} ;
9053
91- const startWasiTask = async ( wasmPath , wasiConstructor = selectWASIBackend ( ) ) => {
54+ export const startWasiTask = async ( wasmPath , wasiConstructor = selectWASIBackend ( ) ) => {
9255 const swift = new SwiftRuntime ( ) ;
9356 // Fetch our Wasm File
94- const wasmBinary = await readFile ( wasmPath ) ;
57+ const wasmBinary = await fs . readFile ( wasmPath ) ;
9558 const wasi = wasiConstructor ( { programName : wasmPath } ) ;
9659
9760 // Instantiate the WebAssembly file
@@ -106,7 +69,5 @@ const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) =>
10669
10770 swift . setInstance ( instance ) ;
10871 // Start the WebAssembly WASI instance!
109- wasi . start ( instance ) ;
72+ wasi . start ( instance , swift ) ;
11073} ;
111-
112- module . exports = { startWasiTask, WASI } ;
0 commit comments