loopyield.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. const mod = new WebAssembly.Module(WebAssembly.wabt.convertWast2Wasm(`(module
  6. (func (export "foo") (param $d i32) (result i32)
  7. (local $tmp i32)
  8. (set_local $tmp (i32.mul (get_local $d) (i32.const 100)))
  9. (block $b1 (result i32)
  10. (block $b2 (result i32)
  11. (block $b3 (result i32)
  12. (block $b4 (result i32)
  13. (block $b5 (result i32)
  14. (loop (result i32)
  15. (tee_local $tmp (i32.sub (get_local $tmp) (get_local $d)))
  16. (br_if 0 (i32.gt_u (get_local $d)))
  17. (block $bloop (result i32)
  18. (block $breturn (result i32)
  19. (get_local $tmp) ;; Yield value
  20. (get_local $tmp) ;; br table index
  21. (br_table
  22. $bloop $b5 $b4 $b3 $b2 $b1 8 ;; 8 refers to the func depth
  23. $breturn ;; default
  24. )
  25. )
  26. (return)
  27. )
  28. )
  29. (i32.add (i32.const 0x10))
  30. )
  31. (i32.add (i32.const 0x100))
  32. )
  33. (i32.add (i32.const 0x1000))
  34. )
  35. (i32.add (i32.const 0x10000))
  36. )
  37. (i32.add (i32.const 0x100000))
  38. )
  39. (i32.add (i32.const 0x1000000))
  40. )
  41. )`));
  42. const {exports: {foo}} = new WebAssembly.Instance(mod);
  43. const expected = [
  44. 0x1111110,
  45. 0x1111101,
  46. 0x1111002,
  47. 0x1110003,
  48. 0x1100004,
  49. 0x1000005,
  50. 0x6,
  51. 0x7,
  52. 0x8,
  53. ];
  54. for (const i in expected) {
  55. const res = foo(i);
  56. if (res !== expected[i]) {
  57. console.log(`Failed foo(${i}). Expected 0x${expected[i].toString(16)}, got 0x${res.toString(16)}`);
  58. }
  59. }
  60. console.log("pass");