evaluation.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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()) {
  97. const value = object.node.value;
  98. const type = typeof value;
  99. let key = null;
  100. if (path.node.computed) {
  101. key = evaluateCached(property, state);
  102. if (!state.confident) return;
  103. } else if (property.isIdentifier()) {
  104. key = property.node.name;
  105. }
  106. if ((type === "number" || type === "string") && key != null && (typeof key === "number" || typeof key === "string")) {
  107. return value[key];
  108. }
  109. }
  110. }
  111. if (path.isReferencedIdentifier()) {
  112. const binding = path.scope.getBinding(path.node.name);
  113. if (binding && binding.constantViolations.length > 0) {
  114. return deopt(binding.path, state);
  115. }
  116. if (binding && path.node.start < binding.path.node.end) {
  117. return deopt(binding.path, state);
  118. }
  119. if (binding != null && binding.hasValue) {
  120. return binding.value;
  121. } else {
  122. if (path.node.name === "undefined") {
  123. return binding ? deopt(binding.path, state) : undefined;
  124. } else if (path.node.name === "Infinity") {
  125. return binding ? deopt(binding.path, state) : Infinity;
  126. } else if (path.node.name === "NaN") {
  127. return binding ? deopt(binding.path, state) : NaN;
  128. }
  129. const resolved = path.resolve();
  130. if (resolved === path) {
  131. return deopt(path, state);
  132. } else {
  133. return evaluateCached(resolved, state);
  134. }
  135. }
  136. }
  137. if (path.isUnaryExpression({
  138. prefix: true
  139. })) {
  140. if (path.node.operator === "void") {
  141. return undefined;
  142. }
  143. const argument = path.get("argument");
  144. if (path.node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
  145. return "function";
  146. }
  147. const arg = evaluateCached(argument, state);
  148. if (!state.confident) return;
  149. switch (path.node.operator) {
  150. case "!":
  151. return !arg;
  152. case "+":
  153. return +arg;
  154. case "-":
  155. return -arg;
  156. case "~":
  157. return ~arg;
  158. case "typeof":
  159. return typeof arg;
  160. }
  161. }
  162. if (path.isArrayExpression()) {
  163. const arr = [];
  164. const elems = path.get("elements");
  165. for (const elem of elems) {
  166. const elemValue = elem.evaluate();
  167. if (elemValue.confident) {
  168. arr.push(elemValue.value);
  169. } else {
  170. return deopt(elemValue.deopt, state);
  171. }
  172. }
  173. return arr;
  174. }
  175. if (path.isObjectExpression()) {
  176. const obj = {};
  177. const props = path.get("properties");
  178. for (const prop of props) {
  179. if (prop.isObjectMethod() || prop.isSpreadElement()) {
  180. return deopt(prop, state);
  181. }
  182. const keyPath = prop.get("key");
  183. let key;
  184. if (prop.node.computed) {
  185. key = keyPath.evaluate();
  186. if (!key.confident) {
  187. return deopt(key.deopt, state);
  188. }
  189. key = key.value;
  190. } else if (keyPath.isIdentifier()) {
  191. key = keyPath.node.name;
  192. } else {
  193. key = keyPath.node.value;
  194. }
  195. const valuePath = prop.get("value");
  196. let value = valuePath.evaluate();
  197. if (!value.confident) {
  198. return deopt(value.deopt, state);
  199. }
  200. value = value.value;
  201. obj[key] = value;
  202. }
  203. return obj;
  204. }
  205. if (path.isLogicalExpression()) {
  206. const wasConfident = state.confident;
  207. const left = evaluateCached(path.get("left"), state);
  208. const leftConfident = state.confident;
  209. state.confident = wasConfident;
  210. const right = evaluateCached(path.get("right"), state);
  211. const rightConfident = state.confident;
  212. switch (path.node.operator) {
  213. case "||":
  214. state.confident = leftConfident && (!!left || rightConfident);
  215. if (!state.confident) return;
  216. return left || right;
  217. case "&&":
  218. state.confident = leftConfident && (!left || rightConfident);
  219. if (!state.confident) return;
  220. return left && right;
  221. case "??":
  222. state.confident = leftConfident && (left != null || rightConfident);
  223. if (!state.confident) return;
  224. return left != null ? left : right;
  225. }
  226. }
  227. if (path.isBinaryExpression()) {
  228. const left = evaluateCached(path.get("left"), state);
  229. if (!state.confident) return;
  230. const right = evaluateCached(path.get("right"), state);
  231. if (!state.confident) return;
  232. switch (path.node.operator) {
  233. case "-":
  234. return left - right;
  235. case "+":
  236. return left + right;
  237. case "/":
  238. return left / right;
  239. case "*":
  240. return left * right;
  241. case "%":
  242. return left % right;
  243. case "**":
  244. return Math.pow(left, right);
  245. case "<":
  246. return left < right;
  247. case ">":
  248. return left > right;
  249. case "<=":
  250. return left <= right;
  251. case ">=":
  252. return left >= right;
  253. case "==":
  254. return left == right;
  255. case "!=":
  256. return left != right;
  257. case "===":
  258. return left === right;
  259. case "!==":
  260. return left !== right;
  261. case "|":
  262. return left | right;
  263. case "&":
  264. return left & right;
  265. case "^":
  266. return left ^ right;
  267. case "<<":
  268. return left << right;
  269. case ">>":
  270. return left >> right;
  271. case ">>>":
  272. return left >>> right;
  273. }
  274. }
  275. if (path.isCallExpression()) {
  276. const callee = path.get("callee");
  277. let context;
  278. let func;
  279. if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && isValidCallee(callee.node.name)) {
  280. func = global[callee.node.name];
  281. }
  282. if (callee.isMemberExpression()) {
  283. const object = callee.get("object");
  284. const property = callee.get("property");
  285. if (object.isIdentifier() && property.isIdentifier() && isValidCallee(object.node.name) && !isInvalidMethod(property.node.name)) {
  286. context = global[object.node.name];
  287. func = context[property.node.name];
  288. }
  289. if (object.isLiteral() && property.isIdentifier()) {
  290. const type = typeof object.node.value;
  291. if (type === "string" || type === "number") {
  292. context = object.node.value;
  293. func = context[property.node.name];
  294. }
  295. }
  296. }
  297. if (func) {
  298. const args = path.get("arguments").map(arg => evaluateCached(arg, state));
  299. if (!state.confident) return;
  300. return func.apply(context, args);
  301. }
  302. }
  303. deopt(path, state);
  304. }
  305. function evaluateQuasis(path, quasis, state, raw = false) {
  306. let str = "";
  307. let i = 0;
  308. const exprs = path.isTemplateLiteral() ? path.get("expressions") : path.get("quasi.expressions");
  309. for (const elem of quasis) {
  310. if (!state.confident) break;
  311. str += raw ? elem.value.raw : elem.value.cooked;
  312. const expr = exprs[i++];
  313. if (expr) str += String(evaluateCached(expr, state));
  314. }
  315. if (!state.confident) return;
  316. return str;
  317. }
  318. function evaluate() {
  319. const state = {
  320. confident: true,
  321. deoptPath: null,
  322. seen: new Map()
  323. };
  324. let value = evaluateCached(this, state);
  325. if (!state.confident) value = undefined;
  326. return {
  327. confident: state.confident,
  328. deopt: state.deoptPath,
  329. value: value
  330. };
  331. }
  332. //# sourceMappingURL=evaluation.js.map