stack.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. function printError(e) {
  6. print(e.name);
  7. print(e.number);
  8. print(e.description);
  9. }
  10. var isMac = (WScript.Platform && WScript.Platform.OS == 'darwin');
  11. var expects = [
  12. '#1', // 0
  13. 'In finally',
  14. 'Error: Out of stack space', // 2
  15. '#2',
  16. 'In finally', // 4
  17. 'Error: Out of stack space',
  18. '#3', // 6
  19. 'In finally',
  20. 'Error: Out of stack space', // 8
  21. 'testing stack overflow handling with catch block',
  22. 'Error: Out of stack space', // 10
  23. 'testing stack overflow handling with finally block',
  24. 'Error: Out of stack space' ]; // 12
  25. if (!isMac) // last test (sometimes) we hit timeout before we hit stackoverflow.
  26. expects.push('Error: Out of stack space')
  27. expects.push('END');
  28. var index = 0;
  29. function printLog(str) {
  30. if (expects[index++] != str) {
  31. WScript.Echo('At ' + (index - 1) + ' expected \n' + expects[index - 1] + '\nOutput:' + str);
  32. WScript.Quit(1);
  33. }
  34. }
  35. for (var i = 1; i < 4; i++) {
  36. printLog("#" + i);
  37. try {
  38. try {
  39. function f() {
  40. f();
  41. }
  42. f();
  43. } finally {
  44. printLog("In finally");
  45. }
  46. }
  47. catch (e) {
  48. printLog(e);
  49. }
  50. }
  51. printLog("testing stack overflow handling with catch block");
  52. try {
  53. function stackOverFlowCatch() {
  54. try {
  55. stackOverFlowCatch();
  56. while (true) {
  57. }
  58. }
  59. catch (e) {
  60. throw e;
  61. }
  62. }
  63. stackOverFlowCatch();
  64. }
  65. catch (e) {
  66. printLog(e);
  67. }
  68. printLog("testing stack overflow handling with finally block");
  69. try
  70. {
  71. function stackOverFlowFinally() {
  72. try {
  73. stackOverFlowFinally();
  74. while (true) {
  75. }
  76. }
  77. finally {
  78. DoSomething();
  79. }
  80. }
  81. stackOverFlowFinally();
  82. }
  83. catch(e) {
  84. printLog(e);
  85. }
  86. function DoSomething()
  87. {
  88. }
  89. // 10K is not enough for our osx setup.
  90. // for bigger numbers, we hit to timeout on CI (before we actually hit to S.O)
  91. if (!isMac) {
  92. try
  93. {
  94. var count = 20000;
  95. var a = {};
  96. var b = a;
  97. for (var i = 0; i < count; i++)
  98. {
  99. a.x = {};
  100. a = a.x;
  101. }
  102. eval("JSON.stringify(b)");
  103. }
  104. catch(e) {
  105. printLog(e);
  106. }
  107. }
  108. printLog('END'); // do not remove this
  109. WScript.Echo("Pass");