evaluation.js 11 KB

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