stack.js 2.9 KB

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