@@ -238,8 +238,7 @@ This is documented here...
238
238
In our Zig Program, this is how we import and call a JavaScript Function: [ demo/mandelbrot.zig] ( demo/mandelbrot.zig )
239
239
240
240
``` 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
243
242
extern fn print(i32) void;
244
243
...
245
244
// Test printing to JavaScript Console.
@@ -250,33 +249,38 @@ if (iterations == 1) { print(iterations); }
250
249
We define the JavaScript Function ` print ` when loading the WebAssembly Module in our JavaScript: [ demo/game.js] ( demo/game.js )
251
250
252
251
``` 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 = {
257
254
// JavaScript Environment exported to Zig
258
255
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); }
261
258
}
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 () {
265
264
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
267
272
main ();
268
- });
269
- };
273
+ }
274
+
275
+ // Start the loading of WebAssembly Module
276
+ bootstrap ();
270
277
```
271
278
272
279
_ Will this work for passing Strings and Buffers as parameters?_
273
280
274
281
Nope, the parameter will be passed as a number. (Probably a WebAssembly Data Address)
275
282
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 ) .
280
284
281
285
# Compile Zig LVGL App to WebAssembly
282
286
0 commit comments