evaluation.js 9.8 KB

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