debugger_basic.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 realPrint = print;
  6. WScript.Echo = print = console.log = () => {};
  7. if (WScript.Arguments.indexOf("--verbose") !== -1) {
  8. WScript.Echo = print = console.log = realPrint;
  9. }
  10. let isAttached = WScript.Arguments.indexOf("debuglaunch") !== -1;
  11. function attach() {
  12. if (isAttached) return Promise.resolve();
  13. return new Promise(r => WScript.Attach(() => {
  14. isAttached = true;
  15. print("attached");
  16. r();
  17. }));
  18. }
  19. function detach() {
  20. if (!isAttached) return Promise.resolve();
  21. return new Promise(r => WScript.Detach(() => {
  22. isAttached = false;
  23. print("detached");
  24. r();
  25. }));
  26. }
  27. const buf = WebAssembly.wabt.convertWast2Wasm(`(module
  28. (import "test" "mem" (memory 1))
  29. (import "test" "table" (table 15 anyfunc))
  30. (import "test" "foo" (func $foo (param i32)))
  31. (func (export "a") (param i32)
  32. (call $foo (get_local 0))
  33. )
  34. (func $b (export "b") (param i32)
  35. (call $foo (get_local 0))
  36. )
  37. (func $c (export "c") (param i32)
  38. (call $d (get_local 0))
  39. )
  40. (func $d (param i32)
  41. (call $b (get_local 0))
  42. )
  43. )`);
  44. function makeInstance(foo) {
  45. const mem = new WebAssembly.Memory({initial: 1});
  46. const table = new WebAssembly.Table({initial: 15, element: "anyfunc"});
  47. const module = new WebAssembly.Module(buf);
  48. const instance = new WebAssembly.Instance(module, {test: {mem, table, foo}});
  49. debugger;
  50. return instance;
  51. }
  52. const {exports: {a, b, c}} = makeInstance(val => {
  53. print(val);
  54. // causes exception
  55. return val.b.c;
  56. });
  57. let id = 0;
  58. function runTest(fn) {
  59. print(fn.name);
  60. fn(id++|0);
  61. }
  62. function caught(e) {
  63. if (!(e instanceof TypeError)) {
  64. realPrint(`Unexpected error: ${e.stack}`);
  65. }
  66. }
  67. function runA() {
  68. debugger;
  69. try {runTest(a);} catch (e) {caught(e);}
  70. }
  71. function runB() {
  72. try {runTest(b);} catch (e) {caught(e);}
  73. }
  74. function runC() {
  75. try {runTest(c);} catch (e) {caught(e);}
  76. }
  77. // preparse b
  78. runB();
  79. attach()
  80. .then(runA)
  81. .then(detach)
  82. .then(runA)
  83. .then(attach)
  84. .then(runB)
  85. .then(detach)
  86. .then(runB)
  87. .then(() => {
  88. let p = Promise.resolve();
  89. for (let i = 0; i < 20; ++i) {
  90. p = p
  91. .then(attach)
  92. .then(runB)
  93. .then(detach);
  94. if (i % 4 < 2) {
  95. p = p.then(runB);
  96. }
  97. }
  98. return p;
  99. })
  100. .then(() => {
  101. for (let i = 0; i < 20; ++i) {
  102. runC();
  103. }
  104. })
  105. .then(attach)
  106. .then(() => {
  107. for (let i = 0; i < 20; ++i) {
  108. runC();
  109. }
  110. })
  111. .then(() => {
  112. let testValue = 0;
  113. const {exports: {c: newC}} = makeInstance(val => {
  114. debugger;
  115. testValue = val;
  116. });
  117. debugger;
  118. newC(15);
  119. if (testValue !== 15) {
  120. realPrint("Invalid assignment through import under debugger");
  121. }
  122. })
  123. .then(detach)
  124. .then(() => realPrint("PASSED"), realPrint);