23.reservedWords.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.useStrict === undefined)
  6. this.useStrict = false;
  7. var keywords = [
  8. "break",
  9. "case",
  10. "catch",
  11. "continue",
  12. "debugger",
  13. "default",
  14. "delete",
  15. "do",
  16. "else",
  17. "finally",
  18. "for",
  19. "function",
  20. "if",
  21. "in",
  22. "instanceof",
  23. "new",
  24. "return",
  25. "switch",
  26. "this",
  27. "throw",
  28. "try",
  29. "typeof",
  30. "var",
  31. "void",
  32. "while",
  33. "with",
  34. "null",
  35. "false",
  36. "true"
  37. ];
  38. var futureReservedWordsInStrictAndNonStrictModes = [
  39. "class",
  40. "const",
  41. "enum",
  42. "export",
  43. "extends",
  44. "import",
  45. "super"
  46. ];
  47. var futureReservedWordsOnlyInStrictMode = [
  48. "implements",
  49. "interface",
  50. "let",
  51. "package",
  52. "private",
  53. "protected",
  54. "public",
  55. "static",
  56. "yield"
  57. ];
  58. var otherWords = [
  59. "foo",
  60. "<"
  61. ];
  62. var wordSets = [
  63. keywords,
  64. futureReservedWordsInStrictAndNonStrictModes,
  65. futureReservedWordsOnlyInStrictMode,
  66. otherWords
  67. ];
  68. var wordUsage = {
  69. varDeclaration: 0,
  70. functionDeclarationName: 1,
  71. functionExpressionName: 2,
  72. functionArgument: 3,
  73. catchArgument: 4,
  74. propertyNameInLiteral: 5,
  75. propertyNameInDotNotation: 6,
  76. propertyNameInElementNotation: 7
  77. };
  78. function generateAndRunTest(word, usage) {
  79. var f = "(function(){";
  80. if(usage === wordUsage.varDeclaration)
  81. f += "var " + word + ";";
  82. else if(usage === wordUsage.functionDeclarationName)
  83. f += "function " + word + "(){}";
  84. else if(usage === wordUsage.functionExpressionName)
  85. f += "(function " + word + "(){})";
  86. else if(usage === wordUsage.functionArgument)
  87. f += "(function(" + word + "){});";
  88. else if(usage === wordUsage.catchArgument)
  89. f += "try{} catch(" + word + "){}";
  90. else if(usage === wordUsage.propertyNameInLiteral)
  91. f += "({" + word + ": 0});";
  92. else if(usage === wordUsage.propertyNameInDotNotation)
  93. f += "var o = {}; o." + word + " = 0;";
  94. else if(usage === wordUsage.propertyNameInElementNotation)
  95. f += "var o = {}; o[\"" + word + "\"] = 0;";
  96. else
  97. throw new Error();
  98. f += "})();";
  99. echo(f);
  100. if(useStrict)
  101. f = "\"use strict\";" + f;
  102. safeCall(eval, f);
  103. }
  104. for(var wordUsagePropertyName in wordUsage) {
  105. var usage = wordUsage[wordUsagePropertyName];
  106. for(var wordSetsPropertyName in wordSets) {
  107. var wordSet = wordSets[wordSetsPropertyName];
  108. for(var wordSetPropertyName in wordSet) {
  109. var word = wordSet[wordSetPropertyName];
  110. generateAndRunTest(word, usage);
  111. }
  112. }
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. function measureTime(f) {
  116. var d = new Date();
  117. f();
  118. echo(new Date() - d);
  119. }
  120. function myToString(o, quoteStrings) {
  121. switch(o) {
  122. case null:
  123. case undefined:
  124. return "" + o;
  125. }
  126. switch(typeof o) {
  127. case "boolean":
  128. return "" + o;
  129. case "number":
  130. {
  131. var s = "" + o;
  132. var i = s.indexOf("e");
  133. var end = i === -1 ? s.length : e;
  134. i = s.indexOf(".");
  135. var start = i === -1 ? 0 : i + 1;
  136. if(start !== 0) {
  137. if(end % 3 !== 0)
  138. end += 3 - end % 3;
  139. for(i = end - 3; i > start; i -= 3)
  140. s = s.substring(0, i) + "," + s.substring(i);
  141. end = start - 1;
  142. start = 0;
  143. }
  144. for(i = end - 3; i > start; i -= 3)
  145. s = s.substring(0, i) + "," + s.substring(i);
  146. return s;
  147. }
  148. case "string":
  149. {
  150. var hex = "0123456789abcdef";
  151. var s = "";
  152. for(var i = 0; i < o.length; ++i) {
  153. var c = o.charCodeAt(i);
  154. switch(c) {
  155. case 0x0:
  156. s += "\\0";
  157. continue;
  158. case 0x8:
  159. s += "\\b";
  160. continue;
  161. case 0xb:
  162. s += "\\v";
  163. continue;
  164. case 0xc:
  165. s += "\\f";
  166. continue;
  167. }
  168. if(quoteStrings) {
  169. switch(c) {
  170. case 0x9:
  171. s += "\\t";
  172. continue;
  173. case 0xa:
  174. s += "\\n";
  175. continue;
  176. case 0xd:
  177. s += "\\r";
  178. continue;
  179. case 0x22:
  180. s += "\\\"";
  181. continue;
  182. case 0x5c:
  183. s += "\\\\";
  184. continue;
  185. }
  186. }
  187. if(c >= 0x20 && c < 0x7f)
  188. s += o.charAt(i);
  189. else if(c <= 0xff)
  190. s += "\\x" + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  191. else
  192. s += "\\u" + hex.charAt((c >> 12) & 0xf) + hex.charAt((c >> 8) & 0xf) + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  193. }
  194. if(quoteStrings)
  195. s = "\"" + s + "\"";
  196. return s;
  197. }
  198. case "object":
  199. case "function":
  200. break;
  201. default:
  202. return "<unknown type '" + typeof o + "'>";
  203. }
  204. if(o instanceof Array) {
  205. var s = "[";
  206. for(var i = 0; i < o.length; ++i) {
  207. if(i)
  208. s += ", ";
  209. s += myToString(o[i], true);
  210. }
  211. return s + "]";
  212. }
  213. if(o instanceof Error)
  214. return o.name + ": " + o.message;
  215. if(o instanceof RegExp)
  216. return o.toString() + (o.lastIndex === 0 ? "" : " (lastIndex: " + o.lastIndex + ")");
  217. if(o instanceof Object) {
  218. var s = "";
  219. for(var p in o)
  220. s += myToString(p) + ": " + myToString(o[p], true) + ", ";
  221. if(s.length !== 0)
  222. s = s.substring(0, s.length - ", ".length);
  223. return "{" + s + "}";
  224. }
  225. return "" + o;
  226. }
  227. function echo() {
  228. var doEcho;
  229. if(this.WScript)
  230. doEcho = function (s) { this.WScript.Echo(s); };
  231. else if(this.document)
  232. doEcho =
  233. function (s) {
  234. s = s
  235. .replace(/&/g, "&&")
  236. .replace(/</g, "&lt;")
  237. .replace(/>/g, "&gt;");
  238. this.document.write(s + "<br/>");
  239. };
  240. else
  241. doEcho = function (s) { this.print(s); };
  242. echo =
  243. function () {
  244. var s = "";
  245. for(var i = 0; i < arguments.length; ++i)
  246. s += myToString(arguments[i]);
  247. doEcho(s);
  248. };
  249. echo.apply(this, arguments);
  250. }
  251. function safeCall(f) {
  252. var args = [];
  253. for(var a = 1; a < arguments.length; ++a)
  254. args.push(arguments[a]);
  255. try {
  256. return f.apply(this, args);
  257. } catch(ex) {
  258. echo(ex);
  259. }
  260. }