nestedblocks.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. WScript.LoadScriptFile("../WasmSpec/testsuite/harness/wasm-constants.js");
  7. WScript.LoadScriptFile("../WasmSpec/testsuite/harness/wasm-module-builder.js");
  8. function test(nNestedBlocks) {
  9. const builder = new WasmModuleBuilder();
  10. const iType = builder.addType({params: [], results: [kWasmI32]});
  11. const body = [];
  12. for (let i = 0; i < nNestedBlocks; ++i) {
  13. body.push(kExprBlock|0, kWasmI32|0);
  14. }
  15. body.push(kExprI32Const|0, 5);
  16. for (let i = 0; i < nNestedBlocks; ++i) {
  17. body.push(kExprEnd|0);
  18. }
  19. builder
  20. .addFunction("foo", iType)
  21. .addBody(body)
  22. .exportFunc();
  23. try {
  24. new WebAssembly.Module(builder.toBuffer());
  25. return true;
  26. } catch (e) {
  27. if (e.message.includes("Maximum supported nested blocks reached")) {
  28. return false;
  29. } else {
  30. print(`FAILED. Unexpected error: ${e.message}`);
  31. }
  32. }
  33. }
  34. let blocks = 1001;
  35. let inc = 1000;
  36. let direction = true;
  37. while (inc !== 0) {
  38. if (test(blocks)) {
  39. if (direction) {
  40. blocks += inc;
  41. } else {
  42. direction = true;
  43. inc >>= 1;
  44. blocks += inc;
  45. }
  46. } else {
  47. if (!direction) {
  48. blocks -= inc;
  49. } else {
  50. direction = false;
  51. inc >>= 1;
  52. blocks -= inc;
  53. }
  54. }
  55. if (blocks > 100000 || blocks < 0) {
  56. print(`FAILED. Nested blocks reached ${blocks} blocks deep. Expected an error by now`);
  57. break;
  58. }
  59. }
  60. print("PASSED");
  61. // print(blocks);