23.reservedWords.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 leftPadZero(str, len)
  79. {
  80. var pad = len - str.length;
  81. for (var i = 0; i < pad; i++) {
  82. str = "0" + str;
  83. }
  84. return str;
  85. }
  86. function escapeFirstChar(word)
  87. {
  88. return "\\u" + leftPadZero(word.charCodeAt(0).toString(16), 4) + word.substring(1);
  89. }
  90. function generateAndRunTest(word, usage) {
  91. var f = "(function(){";
  92. if(usage === wordUsage.varDeclaration)
  93. f += "var " + word + ";";
  94. else if(usage === wordUsage.functionDeclarationName)
  95. f += "function " + word + "(){}";
  96. else if(usage === wordUsage.functionExpressionName)
  97. f += "(function " + word + "(){})";
  98. else if(usage === wordUsage.functionArgument)
  99. f += "(function(" + word + "){});";
  100. else if(usage === wordUsage.catchArgument)
  101. f += "try{} catch(" + word + "){}";
  102. else if(usage === wordUsage.propertyNameInLiteral)
  103. f += "({" + word + ": 0});";
  104. else if(usage === wordUsage.propertyNameInDotNotation)
  105. f += "var o = {}; o." + word + " = 0;";
  106. else if(usage === wordUsage.propertyNameInElementNotation)
  107. f += "var o = {}; o[\"" + word + "\"] = 0;";
  108. else
  109. throw new Error();
  110. f += "})();";
  111. echo(f);
  112. if(useStrict)
  113. f = "\"use strict\";" + f;
  114. safeCall(eval, f);
  115. }
  116. for(var wordUsagePropertyName in wordUsage) {
  117. var usage = wordUsage[wordUsagePropertyName];
  118. for(var wordSetsPropertyName in wordSets) {
  119. var wordSet = wordSets[wordSetsPropertyName];
  120. for(var wordSetPropertyName in wordSet) {
  121. var word = wordSet[wordSetPropertyName];
  122. generateAndRunTest(word, usage);
  123. generateAndRunTest(escapeFirstChar(word), usage);
  124. }
  125. }
  126. }
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. function measureTime(f) {
  129. var d = new Date();
  130. f();
  131. echo(new Date() - d);
  132. }
  133. function myToString(o, quoteStrings) {
  134. switch(o) {
  135. case null:
  136. case undefined:
  137. return "" + o;
  138. }
  139. switch(typeof o) {
  140. case "boolean":
  141. return "" + o;
  142. case "number":
  143. {
  144. var s = "" + o;
  145. var i = s.indexOf("e");
  146. var end = i === -1 ? s.length : e;
  147. i = s.indexOf(".");
  148. var start = i === -1 ? 0 : i + 1;
  149. if(start !== 0) {
  150. if(end % 3 !== 0)
  151. end += 3 - end % 3;
  152. for(i = end - 3; i > start; i -= 3)
  153. s = s.substring(0, i) + "," + s.substring(i);
  154. end = start - 1;
  155. start = 0;
  156. }
  157. for(i = end - 3; i > start; i -= 3)
  158. s = s.substring(0, i) + "," + s.substring(i);
  159. return s;
  160. }
  161. case "string":
  162. {
  163. var hex = "0123456789abcdef";
  164. var s = "";
  165. for(var i = 0; i < o.length; ++i) {
  166. var c = o.charCodeAt(i);
  167. switch(c) {
  168. case 0x0:
  169. s += "\\0";
  170. continue;
  171. case 0x8:
  172. s += "\\b";
  173. continue;
  174. case 0xb:
  175. s += "\\v";
  176. continue;
  177. case 0xc:
  178. s += "\\f";
  179. continue;
  180. }
  181. if(quoteStrings) {
  182. switch(c) {
  183. case 0x9:
  184. s += "\\t";
  185. continue;
  186. case 0xa:
  187. s += "\\n";
  188. continue;
  189. case 0xd:
  190. s += "\\r";
  191. continue;
  192. case 0x22:
  193. s += "\\\"";
  194. continue;
  195. case 0x5c:
  196. s += "\\\\";
  197. continue;
  198. }
  199. }
  200. if(c >= 0x20 && c < 0x7f)
  201. s += o.charAt(i);
  202. else if(c <= 0xff)
  203. s += "\\x" + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  204. else
  205. s += "\\u" + hex.charAt((c >> 12) & 0xf) + hex.charAt((c >> 8) & 0xf) + hex.charAt((c >> 4) & 0xf) + hex.charAt(c & 0xf);
  206. }
  207. if(quoteStrings)
  208. s = "\"" + s + "\"";
  209. return s;
  210. }
  211. case "object":
  212. case "function":
  213. break;
  214. default:
  215. return "<unknown type '" + typeof o + "'>";
  216. }
  217. if(o instanceof Array) {
  218. var s = "[";
  219. for(var i = 0; i < o.length; ++i) {
  220. if(i)
  221. s += ", ";
  222. s += myToString(o[i], true);
  223. }
  224. return s + "]";
  225. }
  226. if(o instanceof Error)
  227. return o.name + ": " + o.message;
  228. if(o instanceof RegExp)
  229. return o.toString() + (o.lastIndex === 0 ? "" : " (lastIndex: " + o.lastIndex + ")");
  230. if(o instanceof Object) {
  231. var s = "";
  232. for(var p in o)
  233. s += myToString(p) + ": " + myToString(o[p], true) + ", ";
  234. if(s.length !== 0)
  235. s = s.substring(0, s.length - ", ".length);
  236. return "{" + s + "}";
  237. }
  238. return "" + o;
  239. }
  240. function echo() {
  241. var doEcho;
  242. if(this.WScript)
  243. doEcho = function (s) { this.WScript.Echo(s); };
  244. else if(this.document)
  245. doEcho =
  246. function (s) {
  247. s = s
  248. .replace(/&/g, "&&")
  249. .replace(/</g, "&lt;")
  250. .replace(/>/g, "&gt;");
  251. this.document.write(s + "<br/>");
  252. };
  253. else
  254. doEcho = function (s) { this.print(s); };
  255. echo =
  256. function () {
  257. var s = "";
  258. for(var i = 0; i < arguments.length; ++i)
  259. s += myToString(arguments[i]);
  260. doEcho(s);
  261. };
  262. echo.apply(this, arguments);
  263. }
  264. function safeCall(f) {
  265. var args = [];
  266. for(var a = 1; a < arguments.length; ++a)
  267. args.push(arguments[a]);
  268. try {
  269. return f.apply(this, args);
  270. } catch(ex) {
  271. echo(ex);
  272. }
  273. }