1- import { wasi , readFile , readURL , readBuffer } from "./grain-module" ;
1+ import { WASI } from "@wasmer/wasi/lib/index.cjs" ;
2+ import { WasmFs } from "@wasmer/wasmfs" ;
3+ import { readFile , readURL , readBuffer } from "./grain-module" ;
24import { GRAIN_STRING_HEAP_TAG , GRAIN_GENERIC_HEAP_TAG_TYPE } from "./tags" ;
35
6+ let bindings ;
7+
8+ if ( __RUNNER_BROWSER ) {
9+ const wasmFs = new WasmFs ( ) ;
10+ const decoder = new TextDecoder ( "utf-8" ) ;
11+ // Monkeypatching the writeSync for stdout/stderr printing
12+ const originalWriteSync = wasmFs . fs . writeSync ;
13+ wasmFs . fs . writeSync = ( fd , buf , offset , length , position ) => {
14+ if ( fd === 1 ) {
15+ console . log ( decoder . decode ( buf ) ) ;
16+ return ;
17+ }
18+ if ( fd === 2 ) {
19+ console . error ( decoder . decode ( buf ) ) ;
20+ return ;
21+ }
22+
23+ originalWriteSync ( fd , buf , offset , length , position ) ;
24+ } ;
25+ bindings = {
26+ ...wasiBindings . default ,
27+ fs : wasmFs . fs ,
28+ } ;
29+ } else {
30+ bindings = wasiBindings . default ;
31+ }
32+
433const MALLOC_MODULE = "GRAIN$MODULE$runtime/gc" ;
534const STRING_MODULE = "GRAIN$MODULE$runtime/string" ;
635
@@ -24,6 +53,12 @@ export class GrainRunner {
2453 element : "anyfunc" ,
2554 initial : 1024 ,
2655 } ) ;
56+ this . wasi = new WASI ( {
57+ args : __RUNNER_BROWSER ? [ ] : process . argv ,
58+ env : __RUNNER_BROWSER ? { } : process . env ,
59+ bindings,
60+ preopens : opts . preopenDirs ,
61+ } ) ;
2762 }
2863
2964 get memoryManager ( ) {
@@ -52,7 +87,7 @@ export class GrainRunner {
5287 throw new Error ( `Failed to ensure string module.` ) ;
5388 }
5489 await this . load ( STRING_MODULE , located ) ;
55- located . start ( ) ;
90+ located . start ( this . wasi ) ;
5691 }
5792
5893 grainValueToString ( v ) {
@@ -117,7 +152,7 @@ export class GrainRunner {
117152 continue ;
118153 }
119154 if ( imp . module . startsWith ( "wasi_" ) ) {
120- Object . assign ( this . imports , wasi . getImports ( mod . wasmModule ) ) ;
155+ Object . assign ( this . imports , this . wasi . getImports ( mod . wasmModule ) ) ;
121156 continue ;
122157 }
123158 // Should return an instance of GrainModule
@@ -135,7 +170,7 @@ export class GrainRunner {
135170 located . loadTypeMetadata ( ) ;
136171 }
137172 if ( located . isStartable ) {
138- located . start ( ) ;
173+ located . start ( this . wasi ) ;
139174 }
140175 this . ptrZero = this . ptr ;
141176 this . imports [ imp . module ] = located . exports ;
@@ -166,12 +201,12 @@ export class GrainRunner {
166201
167202 async runFileUnboxed ( path ) {
168203 let module = await this . loadFile ( path ) ;
169- return module . runUnboxed ( ) ;
204+ return module . runUnboxed ( this . wasi ) ;
170205 }
171206
172207 async runFile ( path ) {
173208 let module = await this . loadFile ( path ) ;
174- return module . start ( ) ;
209+ return module . start ( this . wasi ) ;
175210 }
176211
177212 async loadURL ( url ) {
@@ -181,12 +216,12 @@ export class GrainRunner {
181216
182217 async runURL ( path ) {
183218 let module = await this . loadURL ( path ) ;
184- return module . start ( ) ;
219+ return module . start ( this . wasi ) ;
185220 }
186221
187222 async runURLUnboxed ( path ) {
188223 let module = await this . loadURL ( path ) ;
189- return module . runUnboxed ( ) ;
224+ return module . runUnboxed ( this . wasi ) ;
190225 }
191226
192227 async loadBuffer ( buffer ) {
@@ -196,11 +231,11 @@ export class GrainRunner {
196231
197232 async runBuffer ( buffer ) {
198233 let module = await this . loadBuffer ( buffer ) ;
199- return module . start ( ) ;
234+ return module . start ( this . wasi ) ;
200235 }
201236
202237 async runBufferUnboxed ( buffer ) {
203238 let module = await this . loadBuffer ( buffer ) ;
204- return module . runUnboxed ( ) ;
239+ return module . runUnboxed ( this . wasi ) ;
205240 }
206241}
0 commit comments