StackTraceLimit.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if (this.WScript && this.WScript.LoadScriptFile) {
  6. this.WScript.LoadScriptFile("LongCallStackThrow.js");
  7. }
  8. Dump("-- Error.stackTraceLimit property descriptor");
  9. var desc = Object.getOwnPropertyDescriptor(Error, "stackTraceLimit");
  10. for(var p in desc) {
  11. Dump(p + ": " + desc[p]);
  12. }
  13. Dump("");
  14. function testLongCallStack(limit, noHeader) {
  15. if (limit !== undefined) {
  16. Error.stackTraceLimit = 7; // if next assignment is rejected
  17. Error.stackTraceLimit = limit;
  18. }
  19. if (!noHeader) { Dump("-- Error.stackTraceLimit: " + String(Error.stackTraceLimit)); }
  20. runtest(30);
  21. Dump("");
  22. }
  23. testLongCallStack();
  24. testLongCallStack(4);
  25. testLongCallStack(Infinity);
  26. testLongCallStack(1);
  27. testLongCallStack(20);
  28. testLongCallStack(5.1);
  29. testLongCallStack(-1);
  30. testLongCallStack(-3.2);
  31. testLongCallStack(-Infinity);
  32. testLongCallStack(0);
  33. testLongCallStack(+0);
  34. testLongCallStack(-0);
  35. testLongCallStack(Number.NaN);
  36. testLongCallStack("not a number");
  37. testLongCallStack(new Object());
  38. Error.stackTraceLimit = 8;
  39. Dump("-- preset Error.stackTraceLimit: " + Error.stackTraceLimit);
  40. Dump("");
  41. Dump("--Reconfigure to a getter");
  42. Object.defineProperty(Error, "stackTraceLimit", {
  43. get: function () {
  44. Dump("** Custom stackTraceLimit getter Called, return 3");
  45. return 3;
  46. },
  47. configurable: true
  48. });
  49. testLongCallStack(undefined, true);
  50. Dump("--Delete it");
  51. delete Error.stackTraceLimit;
  52. testLongCallStack();
  53. Dump("--Available on prototype");
  54. Function.prototype.stackTraceLimit = 2;
  55. testLongCallStack();
  56. Dump("--Set to data property again");
  57. Error.stackTraceLimit = 5;
  58. testLongCallStack();
  59. Dump("--Throw in getter");
  60. Object.defineProperty(Error, "stackTraceLimit", {
  61. get: function () {
  62. Dump("** Custom stackTraceLimit getter Called, throw");
  63. throw "My error in custom stackTraceLimit getter";
  64. },
  65. configurable: true
  66. });
  67. testLongCallStack(undefined, true);
  68. Dump("--Throw new Error() in getter");
  69. Object.defineProperty(Error, "stackTraceLimit", {
  70. get: function () {
  71. throw new Error("My error in custom stackTraceLimit getter");
  72. },
  73. configurable: true
  74. });
  75. testLongCallStack(undefined, true);
  76. Dump("--Throw new Error() in getter for a number of times");
  77. var throwErrorCount = 4;
  78. Object.defineProperty(Error, "stackTraceLimit", {
  79. get: function () {
  80. if (throwErrorCount-- > 0) {
  81. throw new Error("My error in custom stackTraceLimit getter");
  82. } else {
  83. return -1;
  84. }
  85. },
  86. configurable: true
  87. });
  88. testLongCallStack(undefined, true);
  89. // Some more tests of different types (appending here to avoid affecting above tests' baseline)
  90. delete Error.stackTraceLimit;
  91. var moreTests = [
  92. null,
  93. undefined,
  94. true,
  95. false,
  96. new Boolean(true),
  97. new Boolean(false),
  98. "4",
  99. new String("5"),
  100. new Number(6),
  101. new Number(Number.NaN),
  102. new Number(Number.Infinity),
  103. new Number(-2),
  104. [],
  105. [1, 2, 3],
  106. {},
  107. { valueOf: function () { return 2; }, toString: function () { return "valueOf override"; } },
  108. { valueOf: function () { throw new Error("evil"); }, toString: function () { return "valueOf override throw"; } },
  109. ];
  110. moreTests.forEach(function (x) {
  111. if (x === undefined) {
  112. Error.stackTraceLimit = undefined;
  113. }
  114. testLongCallStack(x);
  115. });