FlowControl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import Recipe from "./Recipe.js";
  2. import Dish from "./Dish.js";
  3. import Magic from "./lib/Magic.js";
  4. import Utils from "./Utils.js";
  5. /**
  6. * Flow Control operations.
  7. *
  8. * @author n1474335 [n1474335@gmail.com]
  9. * @copyright Crown Copyright 2016
  10. * @license Apache-2.0
  11. *
  12. * @namespace
  13. */
  14. const FlowControl = {
  15. /**
  16. * Fork operation.
  17. *
  18. * @param {Object} state - The current state of the recipe.
  19. * @param {number} state.progress - The current position in the recipe.
  20. * @param {Dish} state.dish - The Dish being operated on.
  21. * @param {Operation[]} state.opList - The list of operations in the recipe.
  22. * @returns {Object} The updated state of the recipe.
  23. */
  24. runFork: async function(state) {
  25. let opList = state.opList,
  26. inputType = opList[state.progress].inputType,
  27. outputType = opList[state.progress].outputType,
  28. input = state.dish.get(inputType),
  29. ings = opList[state.progress].getIngValues(),
  30. splitDelim = ings[0],
  31. mergeDelim = ings[1],
  32. ignoreErrors = ings[2],
  33. subOpList = [],
  34. inputs = [],
  35. i;
  36. if (input)
  37. inputs = input.split(splitDelim);
  38. // Create subOpList for each tranche to operate on
  39. // (all remaining operations unless we encounter a Merge)
  40. for (i = state.progress + 1; i < opList.length; i++) {
  41. if (opList[i].name === "Merge" && !opList[i].isDisabled()) {
  42. break;
  43. } else {
  44. subOpList.push(opList[i]);
  45. }
  46. }
  47. let recipe = new Recipe(),
  48. output = "",
  49. progress = 0;
  50. recipe.addOperations(subOpList);
  51. // Run recipe over each tranche
  52. for (i = 0; i < inputs.length; i++) {
  53. log.debug(`Entering tranche ${i + 1} of ${inputs.length}`);
  54. const dish = new Dish(inputs[i], inputType);
  55. try {
  56. progress = await recipe.execute(dish, 0);
  57. } catch (err) {
  58. if (!ignoreErrors) {
  59. throw err;
  60. }
  61. progress = err.progress + 1;
  62. }
  63. output += dish.get(outputType) + mergeDelim;
  64. }
  65. state.dish.set(output, outputType);
  66. state.progress += progress;
  67. return state;
  68. },
  69. /**
  70. * Merge operation.
  71. *
  72. * @param {Object} state - The current state of the recipe.
  73. * @param {number} state.progress - The current position in the recipe.
  74. * @param {Dish} state.dish - The Dish being operated on.
  75. * @param {Operation[]} state.opList - The list of operations in the recipe.
  76. * @returns {Object} The updated state of the recipe.
  77. */
  78. runMerge: function(state) {
  79. // No need to actually do anything here. The fork operation will
  80. // merge when it sees this operation.
  81. return state;
  82. },
  83. /**
  84. * Register operation.
  85. *
  86. * @param {Object} state - The current state of the recipe.
  87. * @param {number} state.progress - The current position in the recipe.
  88. * @param {Dish} state.dish - The Dish being operated on.
  89. * @param {Operation[]} state.opList - The list of operations in the recipe.
  90. * @returns {Object} The updated state of the recipe.
  91. */
  92. runRegister: function(state) {
  93. const ings = state.opList[state.progress].getIngValues(),
  94. extractorStr = ings[0],
  95. i = ings[1],
  96. m = ings[2];
  97. let modifiers = "";
  98. if (i) modifiers += "i";
  99. if (m) modifiers += "m";
  100. const extractor = new RegExp(extractorStr, modifiers),
  101. input = state.dish.get(Dish.STRING),
  102. registers = input.match(extractor);
  103. if (!registers) return state;
  104. if (ENVIRONMENT_IS_WORKER()) {
  105. self.setRegisters(state.progress, state.numRegisters, registers.slice(1));
  106. }
  107. /**
  108. * Replaces references to registers (e.g. $R0) with the contents of those registers.
  109. *
  110. * @param {string} str
  111. * @returns {string}
  112. */
  113. function replaceRegister(str) {
  114. // Replace references to registers ($Rn) with contents of registers
  115. return str.replace(/(\\*)\$R(\d{1,2})/g, (match, slashes, regNum) => {
  116. const index = parseInt(regNum, 10) + 1;
  117. if (index <= state.numRegisters || index >= state.numRegisters + registers.length)
  118. return match;
  119. if (slashes.length % 2 !== 0) return match.slice(1); // Remove escape
  120. return slashes + registers[index - state.numRegisters];
  121. });
  122. }
  123. // Step through all subsequent ops and replace registers in args with extracted content
  124. for (let i = state.progress + 1; i < state.opList.length; i++) {
  125. if (state.opList[i].isDisabled()) continue;
  126. let args = state.opList[i].getIngValues();
  127. args = args.map(arg => {
  128. if (typeof arg !== "string" && typeof arg !== "object") return arg;
  129. if (typeof arg === "object" && arg.hasOwnProperty("string")) {
  130. arg.string = replaceRegister(arg.string);
  131. return arg;
  132. }
  133. return replaceRegister(arg);
  134. });
  135. state.opList[i].setIngValues(args);
  136. }
  137. state.numRegisters += registers.length - 1;
  138. return state;
  139. },
  140. /**
  141. * Jump operation.
  142. *
  143. * @param {Object} state - The current state of the recipe.
  144. * @param {number} state.progress - The current position in the recipe.
  145. * @param {Dish} state.dish - The Dish being operated on.
  146. * @param {Operation[]} state.opList - The list of operations in the recipe.
  147. * @param {number} state.numJumps - The number of jumps taken so far.
  148. * @returns {Object} The updated state of the recipe.
  149. */
  150. runJump: function(state) {
  151. const ings = state.opList[state.progress].getIngValues(),
  152. label = ings[0],
  153. maxJumps = ings[1],
  154. jmpIndex = FlowControl._getLabelIndex(label, state);
  155. if (state.numJumps >= maxJumps || jmpIndex === -1) {
  156. log.debug("Maximum jumps reached or label cannot be found");
  157. return state;
  158. }
  159. state.progress = jmpIndex;
  160. state.numJumps++;
  161. log.debug(`Jumping to label '${label}' at position ${jmpIndex} (jumps = ${state.numJumps})`);
  162. return state;
  163. },
  164. /**
  165. * Conditional Jump operation.
  166. *
  167. * @param {Object} state - The current state of the recipe.
  168. * @param {number} state.progress - The current position in the recipe.
  169. * @param {Dish} state.dish - The Dish being operated on.
  170. * @param {Operation[]} state.opList - The list of operations in the recipe.
  171. * @param {number} state.numJumps - The number of jumps taken so far.
  172. * @returns {Object} The updated state of the recipe.
  173. */
  174. runCondJump: function(state) {
  175. const ings = state.opList[state.progress].getIngValues(),
  176. dish = state.dish,
  177. regexStr = ings[0],
  178. invert = ings[1],
  179. label = ings[2],
  180. maxJumps = ings[3],
  181. jmpIndex = FlowControl._getLabelIndex(label, state);
  182. if (state.numJumps >= maxJumps || jmpIndex === -1) {
  183. log.debug("Maximum jumps reached or label cannot be found");
  184. return state;
  185. }
  186. if (regexStr !== "") {
  187. let strMatch = dish.get(Dish.STRING).search(regexStr) > -1;
  188. if (!invert && strMatch || invert && !strMatch) {
  189. state.progress = jmpIndex;
  190. state.numJumps++;
  191. log.debug(`Jumping to label '${label}' at position ${jmpIndex} (jumps = ${state.numJumps})`);
  192. }
  193. }
  194. return state;
  195. },
  196. /**
  197. * Return operation.
  198. *
  199. * @param {Object} state - The current state of the recipe.
  200. * @param {number} state.progress - The current position in the recipe.
  201. * @param {Dish} state.dish - The Dish being operated on.
  202. * @param {Operation[]} state.opList - The list of operations in the recipe.
  203. * @returns {Object} The updated state of the recipe.
  204. */
  205. runReturn: function(state) {
  206. state.progress = state.opList.length;
  207. return state;
  208. },
  209. /**
  210. * Comment operation.
  211. *
  212. * @param {Object} state - The current state of the recipe.
  213. * @param {number} state.progress - The current position in the recipe.
  214. * @param {Dish} state.dish - The Dish being operated on.
  215. * @param {Operation[]} state.opList - The list of operations in the recipe.
  216. * @returns {Object} The updated state of the recipe.
  217. */
  218. runComment: function(state) {
  219. return state;
  220. },
  221. /**
  222. * Magic operation.
  223. *
  224. * @param {Object} state - The current state of the recipe.
  225. * @param {number} state.progress - The current position in the recipe.
  226. * @param {Dish} state.dish - The Dish being operated on.
  227. * @param {Operation[]} state.opList - The list of operations in the recipe.
  228. * @returns {Object} The updated state of the recipe.
  229. */
  230. runMagic: async function(state) {
  231. const ings = state.opList[state.progress].getIngValues(),
  232. depth = ings[0],
  233. intensive = ings[1],
  234. extLang = ings[2],
  235. dish = state.dish,
  236. currentRecipeConfig = state.opList.map(op => op.getConfig()),
  237. magic = new Magic(dish.get(Dish.ARRAY_BUFFER)),
  238. options = await magic.speculativeExecution(depth, extLang, intensive);
  239. let output = `<table
  240. class='table table-hover table-condensed table-bordered'
  241. style='table-layout: fixed;'>
  242. <tr>
  243. <th>Recipe (click to load)</th>
  244. <th>Result snippet</th>
  245. <th>Properties</th>
  246. </tr>`;
  247. options.forEach(option => {
  248. // Construct recipe URL
  249. // Replace this Magic op with the generated recipe
  250. const recipeConfig = currentRecipeConfig.slice(0, state.progress)
  251. .concat(option.recipe)
  252. .concat(currentRecipeConfig.slice(state.progress + 1)),
  253. recipeURL = "recipe=" + Utils.encodeURIFragment(Utils.generatePrettyRecipe(recipeConfig));
  254. let language = "",
  255. fileType = "",
  256. matchingOps = "",
  257. useful = "",
  258. validUTF8 = option.isUTF8 ? "Valid UTF8\n" : "";
  259. if (option.languageScores[0].probability > 0) {
  260. let likelyLangs = option.languageScores.filter(l => l.probability > 0);
  261. if (likelyLangs.length < 1) likelyLangs = [option.languageScores[0]];
  262. language = "Possible languages:\n " + likelyLangs.map(lang => {
  263. return Magic.codeToLanguage(lang.lang);
  264. }).join("\n ") + "\n";
  265. }
  266. if (option.fileType) {
  267. fileType = `File type: ${option.fileType.mime} (${option.fileType.ext})\n`;
  268. }
  269. if (option.matchingOps.length) {
  270. matchingOps = `Matching ops: ${[...new Set(option.matchingOps.map(op => op.op))].join(", ")}\n`;
  271. }
  272. if (option.useful) {
  273. useful = "Useful op detected\n";
  274. }
  275. output += `<tr>
  276. <td><a href="#${recipeURL}">${Utils.generatePrettyRecipe(option.recipe, true)}</a></td>
  277. <td>${Utils.escapeHtml(Utils.printable(Utils.truncate(option.data, 99)))}</td>
  278. <td>${language}${fileType}${matchingOps}${useful}${validUTF8}</td>
  279. </tr>`;
  280. });
  281. output += "</table>";
  282. if (!options.length) {
  283. output = "Nothing of interest could be detected about the input data.\nHave you tried modifying the operation arguments?";
  284. }
  285. dish.set(output, Dish.HTML);
  286. return state;
  287. },
  288. /**
  289. * Returns the index of a label.
  290. *
  291. * @private
  292. * @param {Object} state
  293. * @param {string} name
  294. * @returns {number}
  295. */
  296. _getLabelIndex: function(name, state) {
  297. for (let o = 0; o < state.opList.length; o++) {
  298. let operation = state.opList[o];
  299. if (operation.name === "Label"){
  300. let ings = operation.getIngValues();
  301. if (name === ings[0]) {
  302. return o;
  303. }
  304. }
  305. }
  306. return -1;
  307. },
  308. };
  309. export default FlowControl;