@@ -238,8 +238,7 @@ This is documented here...
238238In our Zig Program, this is how we import and call a JavaScript Function: [ demo/mandelbrot.zig] ( demo/mandelbrot.zig )
239239
240240``` zig
241- // extern functions refer to the exterior JS namespace
242- // when importing wasm code, the `print` func must be provided
241+ /// Import `print` Function from JavaScript
243242extern fn print(i32) void;
244243...
245244// Test printing to JavaScript Console.
@@ -250,33 +249,38 @@ if (iterations == 1) { print(iterations); }
250249We define the JavaScript Function ` print ` when loading the WebAssembly Module in our JavaScript: [ demo/game.js] ( demo/game.js )
251250
252251``` javascript
253- // On Loading the WebAssembly Module...
254- request .onload = function () {
255- var bytes = request .response ;
256- WebAssembly .instantiate (bytes, {
252+ // Export JavaScript Functions to Zig
253+ let importObject = {
257254 // JavaScript Environment exported to Zig
258255 env: {
259- // JavaScript Print Function exported to Zig
260- print : function (x ) { console .log (x); }
256+ // JavaScript Print Function exported to Zig
257+ print : function (x ) { console .log (x); }
261258 }
262- }).then (result => {
263- // Store references to Zig functions
264- Game = result .instance .exports ;
259+ };
260+
261+ // Load the WebAssembly Module
262+ // https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/instantiateStreaming
263+ async function bootstrap () {
265264
266- // Start the Main Loop
265+ // Store references to WebAssembly Functions and Memory exported by Zig
266+ Game = await WebAssembly .instantiateStreaming (
267+ fetch (" mandelbrot.wasm" ),
268+ importObject
269+ );
270+
271+ // Start the Main Function
267272 main ();
268- });
269- };
273+ }
274+
275+ // Start the loading of WebAssembly Module
276+ bootstrap ();
270277```
271278
272279_ Will this work for passing Strings and Buffers as parameters?_
273280
274281Nope, the parameter will be passed as a number. (Probably a WebAssembly Data Address)
275282
276- To pass Strings and Buffers between JavaScript and Zig, see [ daneelsan/zig-wasm-logger] ( https://github.com/daneelsan/zig-wasm-logger )
277- and [ mitchellh/zig-js] ( https://github.com/mitchellh/zig-js )
278-
279- TODO: Change ` request.onload ` to ` fetch ` [ (Like this)] ( https://github.com/daneelsan/zig-wasm-logger/blob/master/script.js )
283+ To pass Strings and Buffers between JavaScript and Zig, see [ daneelsan/zig-wasm-logger] ( https://github.com/daneelsan/zig-wasm-logger ) .
280284
281285# Compile Zig LVGL App to WebAssembly
282286
0 commit comments