async-instantiate.js (1249B)
1 const { Module, Instance, Global, instantiate, instantiateStreaming } = WebAssembly; 2 3 const g = new Global({value: "i32", mutable:true}, 0); 4 5 const code = wasmTextToBinary(`(module 6 (global $g (import "" "g") (mut i32)) 7 (func $start (global.set $g (i32.add (global.get $g) (i32.const 1)))) 8 (start $start) 9 )`); 10 const module = new Module(code); 11 12 const importObj = { '': { get g() { g.value++; return g } } }; 13 14 g.value = 0; 15 new Instance(module, importObj); 16 assertEq(g.value, 2); 17 18 g.value = 0; 19 instantiate(module, importObj).then(i => { 20 assertEq(i instanceof Instance, true); 21 assertEq(g.value, 2); 22 g.value++; 23 }); 24 assertEq(g.value, 1); 25 drainJobQueue(); 26 assertEq(g.value, 3); 27 28 g.value = 0; 29 instantiate(code, importObj).then(({module,instance}) => { 30 assertEq(module instanceof Module, true); 31 assertEq(instance instanceof Instance, true); 32 assertEq(g.value, 2); g.value++; } 33 ); 34 drainJobQueue(); 35 assertEq(g.value, 3); 36 37 if (wasmStreamingEnabled()) { 38 g.value = 0; 39 instantiateStreaming(code, importObj).then(({module,instance}) => { 40 assertEq(module instanceof Module, true); 41 assertEq(instance instanceof Instance, true); 42 assertEq(g.value, 2); 43 g.value++; 44 }); 45 drainJobQueue(); 46 assertEq(g.value, 3); 47 }