@@ -51,21 +51,49 @@ const selectWASIBackend = () => {
51
51
return WASI . Node ;
52
52
} ;
53
53
54
+ function isUsingSharedMemory ( module ) {
55
+ const imports = WebAssembly . Module . imports ( module ) ;
56
+ for ( const entry of imports ) {
57
+ if ( entry . module === "env" && entry . name === "memory" && entry . kind == "memory" ) {
58
+ return true ;
59
+ }
60
+ }
61
+ return false ;
62
+ }
63
+
54
64
export const startWasiTask = async ( wasmPath , wasiConstructor = selectWASIBackend ( ) ) => {
55
65
const swift = new SwiftRuntime ( ) ;
56
66
// Fetch our Wasm File
57
67
const wasmBinary = await fs . readFile ( wasmPath ) ;
58
68
const wasi = wasiConstructor ( { programName : wasmPath } ) ;
59
69
60
- // Instantiate the WebAssembly file
61
- let { instance } = await WebAssembly . instantiate ( wasmBinary , {
70
+ const module = await WebAssembly . compile ( wasmBinary ) ;
71
+
72
+ const importObject = {
62
73
wasi_snapshot_preview1 : wasi . wasiImport ,
63
74
javascript_kit : swift . importObjects ( ) ,
64
75
benchmark_helper : {
65
76
noop : ( ) => { } ,
66
77
noop_with_int : ( _ ) => { } ,
67
78
}
68
- } ) ;
79
+ } ;
80
+
81
+ if ( isUsingSharedMemory ( module ) ) {
82
+ importObject [ "env" ] = {
83
+ // We don't have JS API to get memory descriptor of imported memory
84
+ // at this moment, so we assume 256 pages (16MB) memory is enough
85
+ // large for initial memory size.
86
+ memory : new WebAssembly . Memory ( { initial : 256 , maximum : 16384 , shared : true } ) ,
87
+ } ;
88
+ importObject [ "wasi" ] = {
89
+ "thread-spawn" : ( ) => {
90
+ throw new Error ( "thread-spawn not implemented" ) ;
91
+ }
92
+ }
93
+ }
94
+
95
+ // Instantiate the WebAssembly file
96
+ const instance = await WebAssembly . instantiate ( module , importObject ) ;
69
97
70
98
swift . setInstance ( instance ) ;
71
99
// Start the WebAssembly WASI instance!
0 commit comments