nestedblocks.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.Flag("-off:wasmdeferred");
  6. function test(nNestedBlocks) {
  7. // print(`Test(${nNestedBlocks})`)
  8. let nestedBlocks = "";
  9. for (let i = 0; i < nNestedBlocks; ++i) {
  10. nestedBlocks += "(block (result i32) ";
  11. }
  12. nestedBlocks += "(i32.const 5)";
  13. for (let i = 0; i < nNestedBlocks; ++i) {
  14. nestedBlocks += ")";
  15. }
  16. const buf = WebAssembly.wabt.convertWast2Wasm(`(module
  17. (func (export "foo") (result i32)
  18. ${nestedBlocks}
  19. )
  20. )`);
  21. try {
  22. new WebAssembly.Module(buf);
  23. return true;
  24. } catch (e) {
  25. if (e.message.includes("Maximum supported nested blocks reached")) {
  26. return false;
  27. } else {
  28. print(`FAILED. Unexpected error: ${e.message}`);
  29. }
  30. }
  31. }
  32. let blocks = 1001;
  33. let inc = 1000;
  34. let direction = true;
  35. while (inc !== 0) {
  36. if (test(blocks)) {
  37. if (direction) {
  38. blocks += inc;
  39. } else {
  40. direction = true;
  41. inc >>= 1;
  42. blocks += inc;
  43. }
  44. } else {
  45. if (!direction) {
  46. blocks -= inc;
  47. } else {
  48. direction = false;
  49. inc >>= 1;
  50. blocks -= inc;
  51. }
  52. }
  53. if (blocks > 100000 || blocks < 0) {
  54. print(`FAILED. Nested blocks reached ${blocks} blocks deep. Expected an error by now`);
  55. break;
  56. }
  57. }
  58. print("PASSED");
  59. // print(blocks);