StackTraceLimitOOS.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. var output = (function(){
  6. var glob = this;
  7. var buf = "";
  8. return {
  9. clear: function() {
  10. buf = "";
  11. },
  12. echo: function(s) {
  13. buf += s + "\n";
  14. },
  15. all: function() {
  16. return buf;
  17. },
  18. first: function(lines) {
  19. var i = -1, len;
  20. while (lines--) {
  21. i = buf.indexOf("\n", i + 1);
  22. if (i < 0) {
  23. break;
  24. }
  25. len = i;
  26. }
  27. return buf.substring(0, len);
  28. },
  29. last: function(lines) {
  30. var i = buf.length;
  31. while (lines--) {
  32. if (i < 0) {
  33. break;
  34. }
  35. i = buf.lastIndexOf("\n", i - 1);
  36. }
  37. return buf.substring(i);
  38. },
  39. capture: function(f) {
  40. glob.echo = this.echo;
  41. f();
  42. glob.echo = undefined;
  43. }
  44. };
  45. })();
  46. function Dump(output)
  47. {
  48. if (this.echo)
  49. {
  50. this.echo(output);
  51. }
  52. else if (this.WScript)
  53. {
  54. WScript.Echo(output);
  55. }
  56. else
  57. {
  58. alert(output);
  59. }
  60. }
  61. function throwExceptionWithCatch()
  62. {
  63. try
  64. {
  65. throwException();
  66. }
  67. catch(e)
  68. {
  69. Dump(TrimStackTracePath(e.stack));
  70. }
  71. }
  72. function throwException()
  73. {
  74. function foo() {
  75. bar();
  76. }
  77. function bar() {
  78. foo();
  79. }
  80. foo();
  81. }
  82. function runtest(catchException)
  83. {
  84. return catchException == undefined ? throwExceptionWithCatch() : throwException();
  85. }
  86. if (this.WScript && this.WScript.LoadScriptFile) {
  87. this.WScript.LoadScriptFile("TrimStackTracePath.js");
  88. }
  89. Error.stackTraceLimit = Infinity;
  90. Dump("Error.stackTraceLimit: " + Error.stackTraceLimit);
  91. output.capture(runtest);
  92. Dump(output.first(1) + "\n ..." + output.last(20));