11// @ts -check
22import { MODULE_PATH /* #if USE_SHARED_MEMORY */ , MEMORY_TYPE /* #endif */ } from "../instantiate.js"
33/* #if IS_WASI */
4+ /* #if USE_UWASI */
5+ /* #if USE_WASI_CDN */
6+ // @ts -ignore
7+ import { WASI , MemoryFileSystem , useAll , lineBuffered } from 'https://cdn.jsdelivr.net/npm/uwasi@1.6.0/+esm' ;
8+ /* #else */
9+ import { WASI , MemoryFileSystem , useAll , lineBuffered } from 'uwasi' ;
10+ /* #endif */
11+ import { browserMainThreadSleep , createUwasi } from './uwasi.js' ;
12+ /* #else */
413/* #if USE_WASI_CDN */
514// @ts -ignore
615import { WASI , File , OpenFile , ConsoleStdout , PreopenDirectory } from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.4.1/+esm' ;
@@ -9,6 +18,7 @@ import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from 'https://c
918import { WASI , File , OpenFile , ConsoleStdout , PreopenDirectory } from '@bjorn3/browser_wasi_shim' ;
1019/* #endif */
1120/* #endif */
21+ /* #endif */
1222
1323/* #if USE_SHARED_MEMORY */
1424export async function defaultBrowserThreadSetup ( ) {
@@ -27,6 +37,12 @@ export async function defaultBrowserThreadSetup() {
2737 }
2838
2939/* #if IS_WASI */
40+ /* #if USE_UWASI */
41+ const { wasi } = createUwasi (
42+ { WASI , MemoryFileSystem, useAll, lineBuffered } ,
43+ { modulePath : MODULE_PATH }
44+ )
45+ /* #else */
3046 const wasi = new WASI ( /* args */ [ MODULE_PATH ] , /* env */ [ ] , /* fd */ [
3147 new OpenFile ( new File ( [ ] ) ) , // stdin
3248 ConsoleStdout . lineBuffered ( ( stdout ) => {
@@ -37,14 +53,19 @@ export async function defaultBrowserThreadSetup() {
3753 } ) ,
3854 new PreopenDirectory ( "/" , new Map ( ) ) ,
3955 ] , { debug : false } )
56+ /* #endif */
4057/* #endif */
4158 return {
4259/* #if IS_WASI */
60+ /* #if USE_UWASI */
61+ wasi,
62+ /* #else */
4363 wasi : Object . assign ( wasi , {
4464 setInstance ( instance ) {
4565 wasi . inst = instance ;
4666 }
4767 } ) ,
68+ /* #endif */
4869/* #endif */
4970 threadChannel,
5071 }
@@ -105,6 +126,18 @@ export async function defaultBrowserSetup(options) {
105126 const args = options . args ?? [ ]
106127 const onStdoutLine = options . onStdoutLine ?? ( ( line ) => console . log ( line ) )
107128 const onStderrLine = options . onStderrLine ?? ( ( line ) => console . error ( line ) )
129+ /* #if USE_UWASI */
130+ const { wasi } = createUwasi (
131+ { WASI , MemoryFileSystem, useAll, lineBuffered } ,
132+ {
133+ modulePath : MODULE_PATH ,
134+ args,
135+ onStdoutLine,
136+ onStderrLine,
137+ sleep : browserMainThreadSleep ( ) ,
138+ }
139+ )
140+ /* #else */
108141 const wasi = new WASI ( /* args */ [ MODULE_PATH , ...args ] , /* env */ [ ] , /* fd */ [
109142 new OpenFile ( new File ( [ ] ) ) , // stdin
110143 ConsoleStdout . lineBuffered ( ( stdout ) => {
@@ -116,6 +149,7 @@ export async function defaultBrowserSetup(options) {
116149 new PreopenDirectory ( "/" , new Map ( ) ) ,
117150 ] , { debug : false } )
118151/* #endif */
152+ /* #endif */
119153/* #if USE_SHARED_MEMORY */
120154 const memory = new WebAssembly . Memory ( MEMORY_TYPE ) ;
121155 const threadChannel = new DefaultBrowserThreadRegistry ( options . spawnWorker || createDefaultWorkerFactory ( ) )
@@ -127,12 +161,16 @@ export async function defaultBrowserSetup(options) {
127161 getImports ( ) { return options . getImports ( ) } ,
128162/* #endif */
129163/* #if IS_WASI */
164+ /* #if USE_UWASI */
165+ wasi,
166+ /* #else */
130167 wasi : Object . assign ( wasi , {
131168 setInstance ( instance ) {
132169 wasi . inst = instance ;
133170 }
134171 } ) ,
135172/* #endif */
173+ /* #endif */
136174/* #if USE_SHARED_MEMORY */
137175 memory, threadChannel,
138176/* #endif */
0 commit comments