PropertyValidation.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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) { WScript.LoadScriptFile("TrimStackTracePath.js"); }
  6. function dump(output)
  7. {
  8. if (WScript != undefined)
  9. {
  10. WScript.Echo(output);
  11. }
  12. else
  13. {
  14. alert(output);
  15. }
  16. }
  17. function dumpProperties(e)
  18. {
  19. var output = "Properties of e:\n";
  20. var p;
  21. for (p in e)
  22. {
  23. output += " " + p + "\n";
  24. }
  25. output += "\ne.number: " + e.number
  26. + "\ne.stack: " + TrimStackTracePath(e.stack);
  27. dump(output);
  28. }
  29. function ensureCanDeleteStackProperty(e)
  30. {
  31. dump("\n\nDeleting e.stack ...\n");
  32. try
  33. {
  34. delete e.stack
  35. }
  36. catch (e2)
  37. {
  38. dump("Exception when deleting stack property: " + e2);
  39. }
  40. dump("e.stack after delete:\n" + TrimStackTracePath(e.stack));
  41. }
  42. function ensureCanUpdateStackProperty(e)
  43. {
  44. dump("\n\nUpdating e.stack ...\n");
  45. try
  46. {
  47. e.stack = "foo";
  48. }
  49. catch (e2)
  50. {
  51. dump("Exception when updating stack property: " + e2);
  52. }
  53. dump("e.stack after update:\n" + TrimStackTracePath(e.stack));
  54. }
  55. function throwException()
  56. {
  57. try
  58. {
  59. BadType.someProperty = 0;
  60. }
  61. catch(e)
  62. {
  63. dumpProperties(e);
  64. ensureCanDeleteStackProperty(e);
  65. }
  66. try
  67. {
  68. BadType.someProperty = 0;
  69. }
  70. catch(e)
  71. {
  72. dumpProperties(e);
  73. ensureCanUpdateStackProperty(e);
  74. }
  75. }
  76. function bar()
  77. {
  78. throwException();
  79. }
  80. function foo()
  81. {
  82. bar();
  83. }
  84. foo();
  85. function preventExt() {
  86. try {
  87. dump("");
  88. dump("Object.preventExtensions test:");
  89. var x = Error();
  90. dump(x.hasOwnProperty("stack"));
  91. Object.preventExtensions(x);
  92. dump(x.hasOwnProperty("stack"));
  93. throw x;
  94. } catch (e) {
  95. dump(x.hasOwnProperty("stack"));
  96. dump(e.hasOwnProperty("stack"));
  97. }
  98. }
  99. preventExt();