1- async function roc_web_platform_run ( wasm_filename , input , callback ) {
2- let exit_code ;
3-
1+ async function load_wasm ( file ) {
42 const importObj = {
53 wasi_snapshot_preview1 : {
64 proc_exit : ( code ) => {
75 if ( code !== 0 ) {
86 console . error ( `Exited with code ${ code } ` ) ;
97 }
10- exit_code = code ;
118 } ,
129 fd_write : ( x ) => {
1310 console . error ( `fd_write not supported: ${ x } ` ) ;
@@ -20,7 +17,7 @@ async function roc_web_platform_run(wasm_filename, input, callback) {
2017 } ,
2118 } ;
2219
23- const fetchPromise = fetch ( wasm_filename ) ;
20+ const fetchPromise = fetch ( file ) ;
2421
2522 let wasm ;
2623 if ( WebAssembly . instantiateStreaming ) {
@@ -33,41 +30,43 @@ async function roc_web_platform_run(wasm_filename, input, callback) {
3330 wasm = await WebAssembly . instantiate ( module_bytes , importObj ) ;
3431 }
3532
36- try {
37- // Encode the input message as json
38- const message = new TextEncoder ( ) . encode ( JSON . stringify ( input ) ) ;
39-
40- // Allocate enough memory in wasm
41- const in_pointer = wasm . instance . exports . allocUint8 ( message . length ) ;
33+ return async function ( input , callback ) {
34+ try {
35+ // Encode the input message as json
36+ const message = new TextEncoder ( ) . encode ( JSON . stringify ( input ) ) ;
37+
38+ // Allocate enough memory in wasm
39+ const in_pointer = wasm . instance . exports . allocUint8 ( message . length ) ;
40+
41+ // Init the wasm memory
42+ const memory_bytes = new Uint8Array ( wasm . instance . exports . memory . buffer ) ;
43+
44+ // Write the encoded input to the wasm memory
45+ memory_bytes . set ( message , in_pointer ) ;
4246
43- // Init the wasm memory
44- const memory_bytes = new Uint8Array ( wasm . instance . exports . memory . buffer ) ;
47+ // Call the roc code
48+ const out_pointer = wasm . instance . exports . run_roc ( in_pointer , message . length ) ;
4549
46- // Write the encoded input to the wasm memory
47- memory_bytes . set ( message , in_pointer ) ;
48-
49- // Call the roc code
50- const out_pointer = wasm . instance . exports . run_roc ( in_pointer , message . length ) ;
51-
52- // Find the end of the roc return value (the first 0 byte)
53- let stop ;
54- for ( stop = out_pointer ; memory_bytes [ stop ] != 0 ; stop ++ ) ;
50+ // Find the end of the roc return value (the first 0 byte)
51+ let stop ;
52+ for ( stop = out_pointer ; memory_bytes [ stop ] != 0 ; stop ++ ) ;
5553
56- // Decode the roc value
57- let result = JSON . parse ( new TextDecoder ( ) . decode ( memory_bytes . slice ( out_pointer , stop ) ) ) ;
54+ // Decode the roc value
55+ const result = JSON . parse ( new TextDecoder ( ) . decode ( memory_bytes . slice ( out_pointer , stop ) ) ) ;
5856
59- callback ( result ) ;
57+ callback ( result ) ;
6058
61- } catch ( e ) {
62- const is_ok = e . message === "unreachable" && exit_code === 0 ;
63- if ( ! is_ok ) {
64- console . error ( e ) ;
59+ } catch ( e ) {
60+ const is_ok = e . message === "unreachable" && exit_code === 0 ;
61+ if ( ! is_ok ) {
62+ console . error ( e ) ;
63+ }
6564 }
6665 }
6766}
6867
6968if ( typeof module !== "undefined" ) {
7069 module . exports = {
71- roc_web_platform_run ,
70+ load_wasm ,
7271 } ;
7372}
0 commit comments