evaluation.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.evaluate = evaluate;
  6. exports.evaluateTruthy = evaluateTruthy;
  7. const VALID_CALLEES = ["String", "Number", "Math"];
  8. const INVALID_METHODS = ["random"];
  9. function isValidCallee(val) {
  10. return VALID_CALLEES.includes(val);
  11. }
  12. function isInvalidMethod(val) {
  13. return INVALID_METHODS.includes(val);
  14. }
  15. function evaluateTruthy() {
  16. const res = this.evaluate();
  17. if (res.confident) return !!res.value;
  18. }
  19. function deopt(path, state) {
  20. if (!state.confident) return;
  21. state.deoptPath = path;
  22. state.confident = false;
  23. }
  24. function evaluateCached(path, state) {
  25. const {
  26. node
  27. } = path;
  28. const {
  29. seen
  30. } = state;
  31. if (seen.has(node)) {
  32. const existing = seen.get(node);
  33. if (existing.resolved) {
  34. return existing.value;
  35. } else {
  36. deopt(path, state);
  37. return;
  38. }
  39. } else {
  40. const item = {
  41. resolved: false
  42. };
  43. seen.set(node, item);
  44. const val = _evaluate(path, state);
  45. if (state.confident) {
  46. item.resolved = true;
  47. item.value = val;
  48. }
  49. return val;
  50. }
  51. }
  52. function _evaluate(path, state) {
  53. if (!state.confident) return;
  54. if (path.isSequenceExpression()) {
  55. const exprs = path.get("expressions");
  56. return evaluateCached(exprs[exprs.length - 1], state);
  57. }
  58. if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
  59. return path.node.value;
  60. }
  61. if (path.isNullLiteral()) {
  62. return null;
  63. }
  64. if (path.isTemplateLiteral()) {
  65. return evaluateQuasis(path, path.node.quasis, state);
  66. }
  67. if (path.isTaggedTemplateExpression() && path.get("tag").isMemberExpression()) {
  68. const object = path.get("tag.object");
  69. const {
  70. node: {
  71. name
  72. }
  73. } = object;
  74. const property = path.get("tag.property");
  75. if (object.isIdentifier() && name === "String" && !path.scope.getBinding(name) && property.isIdentifier() && property.node.name === "raw") {
  76. return evaluateQuasis(path, path.node.quasi.quasis, state, true);
  77. }
  78. }
  79. if (path.isConditionalExpression()) {
  80. const testResult = evaluateCached(path.get("test"), state);
  81. if (!state.confident) return;
  82. if (testResult) {
  83. return evaluateCached(path.get("consequent"), state);
  84. } else {
  85. return evaluateCached(path.get("alternate"), state);
  86. }
  87. }
  88. if (path.isExpressionWrapper()) {
  89. return evaluateCached(path.get("expression"), state);
  90. }
  91. if (path.isMemberExpression() && !path.parentPath.isCallExpression({
  92. callee: path.node
  93. })) {
  94. const property = path.get("property");
  95. const object = path.get("object");
  96. if (object.isLiteral() && property.isIdentifier()) {
  97. const value = object.node.value;
  98. const type = typeof value;
  99. if (type === "number" || type === "string") {
  100. return value[property.node.name];
  101. }
  102. }
  103. }
  104. if (path.isReferencedIdentifier()) {
  105. const binding = path.scope.getBinding(path.node.name);
  106. if (binding && binding.constantViolations.length > 0) {
  107. return deopt(binding.path, state);
  108. }
  109. if (binding && path.node.start < binding.path.node.end) {
  110. return deopt(binding.path, state);
  111. }
  112. if (binding != null && binding.hasValue) {
  113. return binding.value;
  114. } else {
  115. if (path.node.name === "undefined") {
  116. return binding ? deopt(binding.path, state) : undefined;
  117. } else if (path.node.name === "Infinity") {
  118. return binding ? deopt(binding.path, state) : Infinity;
  119. } else if (path.node.name === "NaN") {
  120. return binding ? deopt(binding.path, state) : NaN;
  121. }
  122. const resolved = path.resolve();
  123. if (resolved === path) {
  124. return deopt(path, state);
  125. } else {
  126. return evaluateCached(resolved, state);
  127. }
  128. }
  129. }
  130. if (path.isUnaryExpression({
  131. prefix: true
  132. })) {
  133. if (path.node.operator === "void") {
  134. return undefined;
  135. }
  136. const argument = path.get("argument");
  137. if (path.node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
  138. return "function";
  139. }
  140. const arg = evaluateCached(argument, state);
  141. if (!state.confident) return;
  142. switch (path.node.operator) {
  143. case "!":
  144. return !arg;
  145. case "+":
  146. return +arg;
  147. case "-":
  148. return -arg;
  149. case "~":
  150. return ~arg;
  151. case "typeof":
  152. return typeof arg;
  153. }
  154. }
  155. if (path.isArrayExpression()) {
  156. const arr = [];
  157. const elems = path.get("elements");
  158. for (const elem of elems) {
  159. const elemValue = elem.evaluate();
  160. if (elemValue.confident) {
  161. arr.push(elemValue.value);
  162. } else {
  163. return deopt(elemValue.deopt, state);
  164. }
  165. }
  166. return arr;
  167. }
  168. if (path.isObjectExpression()) {
  169. const obj = {};
  170. const props = path.get("properties");
  171. for (const prop of props) {
  172. if (prop.isObjectMethod() || prop.isSpreadElement()) {
  173. return deopt(prop, state);
  174. }
  175. const keyPath = prop.get("key");
  176. let key = keyPath;
  177. if (prop.node.computed) {
  178. key = key.evaluate();
  179. if (!key.confident) {
  180. return deopt(key.deopt, state);
  181. }
  182. key = key.value;
  183. } else if (key.isIdentifier()) {
  184. key = key.node.name;
  185. } else {
  186. key = key.node.value;
  187. }
  188. const valuePath = prop.get("value");
  189. let value = valuePath.evaluate();
  190. if (!value.confident) {
  191. return deopt(value.deopt, state);
  192. }
  193. value = value.value;
  194. obj[key] = value;
  195. }
  196. return obj;
  197. }
  198. if (path.isLogicalExpression()) {
  199. const wasConfident = state.confident;
  200. const left = evaluateCached(path.get("left"), state);
  201. const leftConfident = state.confident;
  202. state.confident = wasConfident;
  203. const right = evaluateCached(path.get("right"), state);
  204. const rightConfident = state.confident;
  205. switch (path.node.operator) {
  206. case "||":
  207. state.confident = leftConfident && (!!left || rightConfident);
  208. if (!state.confident) return;
  209. return left || right;
  210. case "&&":
  211. state.confident = leftConfident && (!left || rightConfident);
  212. if (!state.confident) return;
  213. return left && right;
  214. }
  215. }
  216. if (path.isBinaryExpression()) {
  217. const left = evaluateCached(path.get("left"), state);
  218. if (!state.confident) return;
  219. const right = evaluateCached(path.get("right"), state);
  220. if (!state.confident) return;
  221. switch (path.node.operator) {
  222. case "-":
  223. return left - right;
  224. case "+":
  225. return left + right;
  226. case "/":
  227. return left / right;
  228. case "*":
  229. return left * right;
  230. case "%":
  231. return left % right;
  232. case "**":
  233. return Math.pow(left, right);
  234. case "<":
  235. return left < right;
  236. case ">":
  237. return left > right;
  238. case "<=":
  239. return left <= right;
  240. case ">=":
  241. return left >= right;
  242. case "==":
  243. return left == right;
  244. case "!=":
  245. return left != right;
  246. case "===":
  247. return left === right;
  248. case "!==":
  249. return left !== right;
  250. case "|":
  251. return left | right;
  252. case "&":
  253. return left & right;
  254. case "^":
  255. return left ^ right;
  256. case "<<":
  257. return left << right;
  258. case ">>":
  259. return left >> right;
  260. case ">>>":
  261. return left >>> right;
  262. }
  263. }
  264. if (path.isCallExpression()) {
  265. const callee = path.get("callee");
  266. let context;
  267. let func;
  268. if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && isValidCallee(callee.node.name)) {
  269. func = global[callee.node.name];
  270. }
  271. if (callee.isMemberExpression()) {
  272. const object = callee.get("object");
  273. const property = callee.get("property");
  274. if (object.isIdentifier() && property.isIdentifier() && isValidCallee(object.node.name) && !isInvalidMethod(property.node.name)) {
  275. context = global[object.node.name];
  276. func = context[property.node.name];
  277. }
  278. if (object.isLiteral() && property.isIdentifier()) {
  279. const type = typeof object.node.value;
  280. if (type === "string" || type === "number") {
  281. context = object.node.value;
  282. func = context[property.node.name];
  283. }
  284. }
  285. }
  286. if (func) {
  287. const args = path.get("arguments").map(arg => evaluateCached(arg, state));
  288. if (!state.confident) return;
  289. return func.apply(context, args);
  290. }
  291. }
  292. deopt(path, state);
  293. }
  294. function evaluateQuasis(path, quasis, state, raw = false) {
  295. let str = "";
  296. let i = 0;
  297. const exprs = path.get("expressions");
  298. for (const elem of quasis) {
  299. if (!state.confident) break;
  300. str += raw ? elem.value.raw : elem.value.cooked;
  301. const expr = exprs[i++];
  302. if (expr) str += String(evaluateCached(expr, state));
  303. }
  304. if (!state.confident) return;
  305. return str;
  306. }
  307. function evaluate() {
  308. const state = {
  309. confident: true,
  310. deoptPath: null,
  311. seen: new Map()
  312. };
  313. let value = evaluateCached(this, state);
  314. if (!state.confident) value = undefined;
  315. return {
  316. confident: state.confident,
  317. deopt: state.deoptPath,
  318. value: value
  319. };
  320. }