Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

evaluation.js 9.0KB

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