evaluation.js 11 KB

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