multivalue.js (1977B)
1 // Test passing of multi-value results. 2 3 var suspending_fib2 = new WebAssembly.Suspending( 4 async (i, j) => { return [j, i + j] } 5 ); 6 7 var ins = wasmEvalText(`(module 8 (import "js" "fib2" (func $tt (param i32 i32) (result i32 i32))) 9 10 (func (export "fib1") (param i32 i32) (result i32 i32) 11 local.get 1 12 local.get 1 13 local.get 0 14 i32.add 15 call $tt 16 ) 17 )`, { 18 js: { 19 fib2: suspending_fib2, 20 }, 21 }); 22 23 var fib = WebAssembly.promising(ins.exports.fib1); 24 25 var res = fib(1, 1); 26 res.then((r) => { 27 assertEq(r instanceof Array, true); 28 assertEq(r.join(","), "2,3"); 29 }).catch(e => { 30 assertEq(true, false); 31 }); 32 33 // Test no results returned. 34 35 var check = 0; 36 var ins = wasmEvalText(`(module 37 (import "js" "out" (func $out (param i32))) 38 39 (func (export "in") (param i32) 40 local.get 0 41 call $out 42 ) 43 )`, { 44 js: { 45 out: new WebAssembly.Suspending(async(i) => { 46 check += i; 47 return 42; 48 }), 49 }, 50 }); 51 52 var res = WebAssembly.promising(ins.exports.in)(1); 53 res.then((r) => { 54 assertEq(typeof r, "undefined"); 55 assertEq(check, 1); 56 }).catch(e => { 57 assertEq(true, false); 58 }); 59 60 // Check non-iterable returned from JS. 61 var ins = wasmEvalText(`(module 62 (import "js" "fail" (func $out (result i32 i32))) 63 64 (func (export "test") (result i32) 65 call $out 66 i32.add 67 ) 68 )`, { 69 js: { 70 fail: new WebAssembly.Suspending(() => 42), 71 }, 72 }); 73 74 var res = WebAssembly.promising(ins.exports.test)(); 75 res.then((r) => { 76 assertEq(true, false); 77 }).catch(e => { 78 assertEq(e instanceof TypeError, true) 79 }); 80 81 // Check wrong number of values returned from JS. 82 var ins = wasmEvalText(`(module 83 (import "js" "fail" (func $out (result i32 i32 i32))) 84 85 (func (export "test") (result i32 i32) 86 call $out 87 i32.add 88 ) 89 )`, { 90 js: { 91 fail: new WebAssembly.Suspending(() => [42, 1]), 92 }, 93 }); 94 95 var res = WebAssembly.promising(ins.exports.test)(); 96 res.then((r) => { 97 assertEq(true, false); 98 }).catch(e => { 99 assertEq(e instanceof TypeError, true) 100 });