evaluation.js 9.2 KB

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