22.callerCalleeArguments_sm.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 useStrict = true;
  6. var PropertyAccessType = {
  7. HasOwnProperty: 0,
  8. DirectGet: 1,
  9. DirectSet: 2,
  10. DefineProperty: 3,
  11. GetOwnPropertyDescriptor: 4
  12. };
  13. var SpecialProperty =
  14. {
  15. Caller: "caller",
  16. Callee: "callee",
  17. Arguments: "arguments"
  18. };
  19. function GenerateSpecialPropertyAccess(o, propertyAccessType, specialProperty) {
  20. if(propertyAccessType === PropertyAccessType.HasOwnProperty)
  21. return "echo(\"hasOwnProperty(" + specialProperty + "): \", " + o + ".hasOwnProperty(\"" + specialProperty + "\"));";
  22. if(propertyAccessType === PropertyAccessType.DirectGet)
  23. return o + "." + specialProperty + ";";
  24. if(propertyAccessType === PropertyAccessType.DirectSet)
  25. return o + "." + specialProperty + " = 0;";
  26. if(propertyAccessType === PropertyAccessType.DefineProperty)
  27. return "Object.defineProperty(" + o + ", \"" + specialProperty + "\", {value: 0});";
  28. if(propertyAccessType === PropertyAccessType.GetOwnPropertyDescriptor)
  29. return "var descriptor = Object.getOwnPropertyDescriptor(" + o + ", \"" + specialProperty + "\");" +
  30. "if(descriptor.hasOwnProperty(\"get\")) safeCall(descriptor.get);" +
  31. "if(descriptor.hasOwnProperty(\"set\")) safeCall(descriptor.set);";
  32. throw new Error();
  33. }
  34. function GenerateAndRunArgumentsSpecialPropertyTest(propertyAccessType, specialProperty) {
  35. if(specialProperty == SpecialProperty.Arguments)
  36. return;
  37. var f = "(function(){";
  38. if(useStrict)
  39. f += "\"use strict\";";
  40. f += GenerateSpecialPropertyAccess("arguments", propertyAccessType, specialProperty);
  41. f += "})();";
  42. echo(f);
  43. safeCall(eval, f);
  44. echo();
  45. }
  46. function GenerateAndRunFunctionSpecialPropertyTest(propertyAccessType, specialProperty, accessFromStrictCode) {
  47. if(specialProperty == SpecialProperty.Callee)
  48. return;
  49. var f = "var foo = function(){";
  50. if(useStrict)
  51. f += "\"use strict\";";
  52. f += "};";
  53. f += "(function(){";
  54. if(accessFromStrictCode)
  55. f += "\"use strict\";";
  56. f += GenerateSpecialPropertyAccess("foo", propertyAccessType, specialProperty);
  57. f += "})();";
  58. echo(f);
  59. safeCall(eval, f);
  60. echo();
  61. }
  62. var bools = [false, true];
  63. for(var propertyAccessTypePropertyName in PropertyAccessType) {
  64. var propertyAccessType = PropertyAccessType[propertyAccessTypePropertyName];
  65. for(var specialPropertyPropertyName in SpecialProperty) {
  66. var specialProperty = SpecialProperty[specialPropertyPropertyName];
  67. GenerateAndRunArgumentsSpecialPropertyTest(propertyAccessType, specialProperty);
  68. for(var accessFromStrictCodePropertyName in bools) {
  69. var accessFromStrictCode = bools[accessFromStrictCodePropertyName];
  70. GenerateAndRunFunctionSpecialPropertyTest(propertyAccessType, specialProperty, accessFromStrictCode);
  71. }
  72. }
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. function measureTime(f) {
  76. var d = new Date();
  77. f();
  78. echo(new Date() - d);
  79. }
  80. function myToString(o, quoteStrings) {
  81. switch(o) {
  82. case null:
  83. case undefined:
  84. return "" + o;
  85. }
  86. switch(typeof o) {
  87. case "boolean":
  88. return "" + o;
  89. case "number":
  90. {
  91. var s = "" + o;
  92. var i = s.indexOf("e");
  93. var end = i === -1 ? s.length : e;
  94. i = s.indexOf(".");
  95. var start = i === -1 ? 0 : i + 1;
  96. if(start !== 0) {
  97. if(end % 3 !== 0)
  98. end += 3 - end % 3;
  99. for(i = end - 3; i > start; i -= 3)
  100. s = s.substring(0, i) + "," + s.substring(i);
  101. end = start - 1;
  102. start = 0;
  103. }
  104. for(i = end - 3; i > start; i -= 3)
  105. s = s.substring(0, i) + "," + s.substring(i);
  106. return s;
  107. }
  108. case "string":
  109. {
  110. var hex = "0123456789abcdef";
  111. var s = "";
  112. for(var i = 0; i < o.length; ++i) {
  113. var c = o.charCodeAt(i);
  114. switch(c) {
  115. case 0x0:
  116. s += "\\0";
  117. continue;
  118. case 0x8:
  119. s += "\\b";
  120. continue;
  121. case 0xb:
  122. s += "\\v";
  123. continue;
  124. case 0xc:
  125. s += "\\f";
  126. continue;
  127. }
  128. if(quoteStrings) {
  129. switch(c) {
  130. case 0x9:
  131. s += "\\t";
  132. continue;
  133. case 0xa:
  134. s += "\\n";
  135. continue;
  136. case 0xd:
  137. s += "\\r";
  138. continue;
  139. case 0x22:
  140. s += "\\\"";
  141. continue;
  142. case 0x5c:
  143. s += "\\\\";
  144. continue;
  145. }
  146. }
  147. if(c >= 0x20 && c < 0x7f)
  148. s += o.charAt(i);
  149. else if(c <= 0xff)
  150. s += "\\x" + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  151. else
  152. s += "\\u" + hex.charAt((c >> 12) & 0xf) + hex.charAt((c >> 8) & 0xf) + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  153. }
  154. if(quoteStrings)
  155. s = "\"" + s + "\"";
  156. return s;
  157. }
  158. case "object":
  159. case "function":
  160. break;
  161. default:
  162. return "<unknown type '" + typeof o + "'>";
  163. }
  164. if(o instanceof Array) {
  165. var s = "[";
  166. for(var i = 0; i < o.length; ++i) {
  167. if(i)
  168. s += ", ";
  169. s += myToString(o[i], true);
  170. }
  171. return s + "]";
  172. }
  173. if(o instanceof Error)
  174. return o.name + ": " + o.message;
  175. if(o instanceof RegExp)
  176. return o.toString() + (o.lastIndex === 0 ? "" : " (lastIndex: " + o.lastIndex + ")");
  177. if(o instanceof Object) {
  178. var s = "";
  179. for(var p in o)
  180. s += myToString(p) + ": " + myToString(o[p], true) + ", ";
  181. if(s.length !== 0)
  182. s = s.substring(0, s.length - ", ".length);
  183. return "{" + s + "}";
  184. }
  185. return "" + o;
  186. }
  187. function echo() {
  188. var doEcho;
  189. if(this.WScript)
  190. doEcho = function (s) { this.WScript.Echo(s); };
  191. else if(this.document)
  192. doEcho =
  193. function (s) {
  194. s = s
  195. .replace(/&/g, "&&")
  196. .replace(/</g, "&lt;")
  197. .replace(/>/g, "&gt;");
  198. this.document.write(s + "<br/>");
  199. };
  200. else
  201. doEcho = function (s) { this.print(s); };
  202. echo =
  203. function () {
  204. var s = "";
  205. for(var i = 0; i < arguments.length; ++i)
  206. s += myToString(arguments[i]);
  207. doEcho(s);
  208. };
  209. echo.apply(this, arguments);
  210. }
  211. function safeCall(f) {
  212. var args = [];
  213. for(var a = 1; a < arguments.length; ++a)
  214. args.push(arguments[a]);
  215. try {
  216. return f.apply(this, args);
  217. } catch(ex) {
  218. echo(ex);
  219. }
  220. }