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