debugger.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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("-v") !== -1) {
  8. WScript.Echo = print = console.log = realPrint;
  9. }
  10. function attach() {
  11. return new Promise(r => WScript.Attach(() => {
  12. print("attached");
  13. r();
  14. }));
  15. }
  16. function detach() {
  17. return new Promise(r => WScript.Detach(() => {
  18. print("detached");
  19. r();
  20. }));
  21. }
  22. const buf = readbuffer("debugger.wasm");
  23. const {exports: {a, b, c}} = new WebAssembly.Instance(new WebAssembly.Module(buf), {test: {
  24. foo: function(val) {
  25. print(val);
  26. // causes exception
  27. return val.b.c;
  28. }
  29. }});
  30. let id = 0;
  31. function runTest(fn) {
  32. print(fn.name);
  33. fn(id++|0);
  34. }
  35. function caught(e) {
  36. if (!(e instanceof TypeError)) {
  37. realPrint(`Unexpected error: ${e.stack}`);
  38. }
  39. }
  40. function runA() {
  41. try {runTest(a);} catch (e) {caught(e);}
  42. }
  43. function runB() {
  44. try {runTest(b);} catch (e) {caught(e);}
  45. }
  46. function runC() {
  47. try {runTest(c);} catch (e) {caught(e);}
  48. }
  49. // preparse b
  50. runB();
  51. attach()
  52. .then(runA)
  53. .then(detach)
  54. .then(runA)
  55. .then(attach)
  56. .then(runB)
  57. .then(detach)
  58. .then(runB)
  59. .then(() => {
  60. let p = Promise.resolve();
  61. for(let i = 0; i < 20; ++i) {
  62. p = p
  63. .then(attach)
  64. .then(runB)
  65. .then(detach);
  66. if (i % 4 < 2) {
  67. p = p.then(runB);
  68. }
  69. }
  70. return p;
  71. })
  72. .then(() => {
  73. for(let i = 0; i < 20; ++i) {
  74. runC();
  75. }
  76. })
  77. .then(attach)
  78. .then(() => {
  79. for(let i = 0; i < 20; ++i) {
  80. runC();
  81. }
  82. })
  83. .then(() => {
  84. let testValue = 0;
  85. const {exports: {c: newC}} = new WebAssembly.Instance(new WebAssembly.Module(buf), {test: {
  86. foo: function(val) {
  87. testValue = val;
  88. }
  89. }});
  90. newC(15);
  91. if (testValue !== 15) {
  92. realPrint("Invalid assignment through import under debugger");
  93. }
  94. })
  95. .then(detach)
  96. .then(() => realPrint("PASSED"), realPrint);