evaluation.js 11 KB

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