evaluator.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. /*jshint eqeqeq:false*/
  22. (function () {
  23. 'use strict';
  24. var Syntax, common;
  25. common = require('./common');
  26. Syntax = common.Syntax;
  27. // constant
  28. function isConstant(node, allowRegExp) {
  29. if (node.type === Syntax.Literal) {
  30. if (typeof node.value === 'object' && node.value !== null) {
  31. // This is RegExp
  32. return allowRegExp;
  33. }
  34. return true;
  35. }
  36. if (node.type === Syntax.UnaryExpression) {
  37. if (node.operator === 'void' || node.operator === 'delete' || node.operator === '!') {
  38. return isConstant(node.argument, true);
  39. }
  40. return isConstant(node.argument, false);
  41. }
  42. if (node.type === Syntax.BinaryExpression) {
  43. if (node.operator === 'in' || node.operator === 'instanceof') {
  44. return false;
  45. }
  46. return isConstant(node.left, false) && isConstant(node.right, false);
  47. }
  48. if (node.type === Syntax.LogicalExpression) {
  49. return isConstant(node.left, true) && isConstant(node.right, true);
  50. }
  51. return false;
  52. }
  53. function getConstant(node) {
  54. if (node.type === Syntax.Literal) {
  55. return node.value;
  56. }
  57. if (node.type === Syntax.UnaryExpression) {
  58. return doUnary(node.operator, getConstant(node.argument));
  59. }
  60. if (node.type === Syntax.BinaryExpression) {
  61. return doBinary(node.operator, getConstant(node.left), getConstant(node.right));
  62. }
  63. if (node.type === Syntax.LogicalExpression) {
  64. return doLogical(node.operator, getConstant(node.left), getConstant(node.right));
  65. }
  66. common.unreachable();
  67. }
  68. function doLogical(operator, left, right) {
  69. if (operator === '||') {
  70. return left || right;
  71. }
  72. if (operator === '&&') {
  73. return left && right;
  74. }
  75. common.unreachable();
  76. }
  77. function doUnary(operator, argument) {
  78. switch (operator) {
  79. case '+':
  80. return +argument;
  81. case '-':
  82. return -argument;
  83. case '~':
  84. return ~argument;
  85. case '!':
  86. return !argument;
  87. case 'delete':
  88. // do delete on constant value (not considering identifier in this tree based constant folding)
  89. return true;
  90. case 'void':
  91. return undefined;
  92. case 'typeof':
  93. return typeof argument;
  94. }
  95. common.unreachable();
  96. }
  97. function doBinary(operator, left, right) {
  98. switch (operator) {
  99. case '|':
  100. return left | right;
  101. case '^':
  102. return left ^ right;
  103. case '&':
  104. return left & right;
  105. case '==':
  106. return left == right;
  107. case '!=':
  108. return left != right;
  109. case '===':
  110. return left === right;
  111. case '!==':
  112. return left !== right;
  113. case '<':
  114. return left < right;
  115. case '>':
  116. return left > right;
  117. case '<=':
  118. return left <= right;
  119. case '>=':
  120. return left >= right;
  121. // case 'in':
  122. // return left in right;
  123. // case 'instanceof':
  124. // return left instanceof right;
  125. case '<<':
  126. return left << right;
  127. case '>>':
  128. return left >> right;
  129. case '>>>':
  130. return left >>> right;
  131. case '+':
  132. return left + right;
  133. case '-':
  134. return left - right;
  135. case '*':
  136. return left * right;
  137. case '/':
  138. return left / right;
  139. case '%':
  140. return left % right;
  141. }
  142. common.unreachable();
  143. }
  144. exports.constant = {
  145. doBinary: doBinary,
  146. doUnary: doUnary,
  147. doLogical: doLogical,
  148. evaluate: getConstant,
  149. isConstant: isConstant
  150. };
  151. // has side effect
  152. function hasSideEffect(expr, scope) {
  153. function visit(expr) {
  154. var i, iz, ref;
  155. switch (expr.type) {
  156. case Syntax.AssignmentExpression:
  157. return true;
  158. case Syntax.ArrayExpression:
  159. for (i = 0, iz = expr.elements.length; i < iz; ++i) {
  160. if (expr.elements[i] !== null && visit(expr.elements[i])) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. case Syntax.BinaryExpression:
  166. return !isConstant(expr);
  167. case Syntax.CallExpression:
  168. return true;
  169. case Syntax.ConditionalExpression:
  170. return visit(expr.test) || visit(expr.consequent) || visit(expr.alternate);
  171. case Syntax.FunctionExpression:
  172. return false;
  173. case Syntax.Identifier:
  174. ref = scope.resolve(expr);
  175. if (ref && ref.isStatic()) {
  176. return false;
  177. }
  178. return true;
  179. case Syntax.Literal:
  180. return false;
  181. case Syntax.LogicalExpression:
  182. return visit(expr.left) || visit(expr.right);
  183. case Syntax.MemberExpression:
  184. return true;
  185. case Syntax.NewExpression:
  186. return true;
  187. case Syntax.ObjectExpression:
  188. for (i = 0, iz = expr.properties.length; i < iz; ++i) {
  189. if (visit(expr.properties[i])) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. case Syntax.Property:
  195. return visit(expr.value);
  196. case Syntax.SequenceExpression:
  197. for (i = 0, iz = expr.expressions.length; i < iz; ++i) {
  198. if (visit(expr.expressions[i])) {
  199. return true;
  200. }
  201. }
  202. return false;
  203. case Syntax.ThisExpression:
  204. return false;
  205. case Syntax.UnaryExpression:
  206. if (expr.operator === 'void' || expr.operator === 'delete' || expr.operator === 'typeof' || expr.operator === '!') {
  207. return visit(expr.argument);
  208. }
  209. return !isConstant(expr);
  210. case Syntax.UpdateExpression:
  211. return true;
  212. }
  213. return true;
  214. }
  215. return visit(expr);
  216. }
  217. exports.hasSideEffect = hasSideEffect;
  218. // boolean decision
  219. // @return {boolean|null} when indeterminate value comes, returns null
  220. function booleanCondition(expr) {
  221. var ret;
  222. switch (expr.type) {
  223. case Syntax.AssignmentExpression:
  224. return booleanCondition(expr.right);
  225. case Syntax.ArrayExpression:
  226. return true;
  227. case Syntax.BinaryExpression:
  228. if (isConstant(expr)) {
  229. return !!getConstant(expr);
  230. }
  231. return null;
  232. case Syntax.CallExpression:
  233. return null;
  234. case Syntax.ConditionalExpression:
  235. ret = booleanCondition(expr.test);
  236. if (ret === true) {
  237. return booleanCondition(expr.consequent);
  238. }
  239. if (ret === false) {
  240. return booleanCondition(expr.alternate);
  241. }
  242. ret = booleanCondition(expr.consequent);
  243. if (ret === booleanCondition(expr.alternate)) {
  244. return ret;
  245. }
  246. return null;
  247. case Syntax.FunctionExpression:
  248. return true;
  249. case Syntax.Identifier:
  250. return null;
  251. case Syntax.Literal:
  252. return !!getConstant(expr);
  253. case Syntax.LogicalExpression:
  254. if (expr.operator === '&&') {
  255. ret = booleanCondition(expr.left);
  256. if (ret === null) {
  257. return null;
  258. }
  259. if (!ret) {
  260. return false;
  261. }
  262. return booleanCondition(expr.right);
  263. } else {
  264. ret = booleanCondition(expr.left);
  265. if (ret === null) {
  266. return null;
  267. }
  268. if (ret) {
  269. return true;
  270. }
  271. return booleanCondition(expr.right);
  272. }
  273. return null;
  274. case Syntax.MemberExpression:
  275. return null;
  276. case Syntax.NewExpression:
  277. // always return object
  278. return true;
  279. case Syntax.ObjectExpression:
  280. return true;
  281. case Syntax.Property:
  282. common.unreachable();
  283. return null;
  284. case Syntax.SequenceExpression:
  285. return booleanCondition(common.Array.last(expr.expressions));
  286. case Syntax.ThisExpression:
  287. // in strict mode, this may be null / undefined
  288. return null;
  289. case Syntax.UnaryExpression:
  290. if (expr.operator === 'void') {
  291. return false;
  292. }
  293. if (expr.operator === 'typeof') {
  294. return true;
  295. }
  296. if (expr.operator === '!') {
  297. ret = booleanCondition(expr.argument);
  298. if (ret === null) {
  299. return null;
  300. }
  301. return !ret;
  302. }
  303. if (isConstant(expr)) {
  304. return !!getConstant(expr);
  305. }
  306. return null;
  307. case Syntax.UpdateExpression:
  308. return null;
  309. }
  310. return null;
  311. }
  312. exports.booleanCondition = booleanCondition;
  313. }());