2018-03-27 00:14:23 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-03-25 15:59:54 +01:00
|
|
|
import OperationConfig from "./config/OperationConfig.json" assert {type: "json"};
|
2019-07-09 13:23:59 +02:00
|
|
|
import OperationError from "./errors/OperationError.mjs";
|
|
|
|
import Operation from "./Operation.mjs";
|
|
|
|
import DishError from "./errors/DishError.mjs";
|
2018-04-30 19:25:13 +02:00
|
|
|
import log from "loglevel";
|
2019-07-09 13:23:59 +02:00
|
|
|
import { isWorkerEnvironment } from "./Utils.mjs";
|
2018-03-27 00:14:23 +02:00
|
|
|
|
2019-02-11 19:44:41 +01:00
|
|
|
// Cache container for modules
|
|
|
|
let modules = null;
|
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
/**
|
|
|
|
* The Recipe controls a list of Operations and the Dish they operate on.
|
|
|
|
*/
|
|
|
|
class Recipe {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recipe constructor
|
|
|
|
*
|
|
|
|
* @param {Object} recipeConfig
|
|
|
|
*/
|
|
|
|
constructor(recipeConfig) {
|
|
|
|
this.opList = [];
|
|
|
|
|
|
|
|
if (recipeConfig) {
|
|
|
|
this._parseConfig(recipeConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads and parses the given config.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} recipeConfig
|
|
|
|
*/
|
|
|
|
_parseConfig(recipeConfig) {
|
2019-02-11 19:44:41 +01:00
|
|
|
recipeConfig.forEach(c => {
|
2019-02-01 11:52:21 +01:00
|
|
|
this.opList.push({
|
|
|
|
name: c.op,
|
|
|
|
module: OperationConfig[c.op].module,
|
|
|
|
ingValues: c.args,
|
|
|
|
breakpoint: c.breakpoint,
|
2021-02-03 18:54:49 +01:00
|
|
|
disabled: c.disabled || c.op === "Comment",
|
2019-02-01 11:52:21 +01:00
|
|
|
});
|
2019-02-01 15:05:48 +01:00
|
|
|
});
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-08 15:28:53 +01:00
|
|
|
/**
|
|
|
|
* Populate elements of opList with operation instances.
|
2019-02-11 19:44:41 +01:00
|
|
|
* Dynamic import here removes top-level cyclic dependency issue.
|
2019-02-08 15:28:53 +01:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _hydrateOpList() {
|
2019-02-11 19:44:41 +01:00
|
|
|
if (!modules) {
|
|
|
|
// Using Webpack Magic Comments to force the dynamic import to be included in the main chunk
|
|
|
|
// https://webpack.js.org/api/module-methods/
|
2019-07-09 13:23:59 +02:00
|
|
|
modules = await import(/* webpackMode: "eager" */ "./config/modules/OpModules.mjs");
|
2019-02-11 19:44:41 +01:00
|
|
|
modules = modules.default;
|
|
|
|
}
|
2019-02-08 15:28:53 +01:00
|
|
|
|
2019-02-11 19:44:41 +01:00
|
|
|
this.opList = this.opList.map(o => {
|
2019-02-08 15:28:53 +01:00
|
|
|
if (o instanceof Operation) {
|
|
|
|
return o;
|
|
|
|
} else {
|
|
|
|
const op = new modules[o.module][o.name]();
|
|
|
|
op.ingValues = o.ingValues;
|
|
|
|
op.breakpoint = o.breakpoint;
|
|
|
|
op.disabled = o.disabled;
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
/**
|
|
|
|
* Returns the value of the Recipe as it should be displayed in a recipe config.
|
|
|
|
*
|
|
|
|
* @returns {Object[]}
|
|
|
|
*/
|
|
|
|
get config() {
|
2019-02-01 11:52:21 +01:00
|
|
|
return this.opList.map(op => ({
|
|
|
|
op: op.name,
|
|
|
|
args: op.ingValues,
|
|
|
|
}));
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new Operation to this Recipe.
|
|
|
|
*
|
|
|
|
* @param {Operation} operation
|
|
|
|
*/
|
|
|
|
addOperation(operation) {
|
|
|
|
this.opList.push(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a list of Operations to this Recipe.
|
|
|
|
*
|
|
|
|
* @param {Operation[]} operations
|
|
|
|
*/
|
|
|
|
addOperations(operations) {
|
2019-02-11 19:44:41 +01:00
|
|
|
operations.forEach(o => {
|
2019-02-01 11:52:21 +01:00
|
|
|
if (o instanceof Operation) {
|
|
|
|
this.opList.push(o);
|
2019-02-08 15:28:53 +01:00
|
|
|
} else {
|
|
|
|
this.opList.push({
|
|
|
|
name: o.name,
|
|
|
|
module: o.module,
|
|
|
|
ingValues: o.args,
|
|
|
|
breakpoint: o.breakpoint,
|
|
|
|
disabled: o.disabled,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a breakpoint on a specified Operation.
|
|
|
|
*
|
|
|
|
* @param {number} position - The index of the Operation
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
setBreakpoint(position, value) {
|
|
|
|
try {
|
|
|
|
this.opList[position].breakpoint = value;
|
|
|
|
} catch (err) {
|
|
|
|
// Ignore index error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove breakpoints on all Operations in the Recipe up to the specified position. Used by Flow
|
|
|
|
* Control Fork operation.
|
|
|
|
*
|
|
|
|
* @param {number} pos
|
|
|
|
*/
|
|
|
|
removeBreaksUpTo(pos) {
|
|
|
|
for (let i = 0; i < pos; i++) {
|
|
|
|
this.opList[i].breakpoint = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-02-11 19:44:41 +01:00
|
|
|
* Returns true if there is a Flow Control Operation in this Recipe.
|
2018-03-27 00:14:23 +02:00
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
containsFlowControl() {
|
|
|
|
return this.opList.reduce((acc, curr) => {
|
|
|
|
return acc || curr.flowControl;
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes each operation in the recipe over the given Dish.
|
|
|
|
*
|
|
|
|
* @param {Dish} dish
|
|
|
|
* @param {number} [startFrom=0]
|
|
|
|
* - The index of the Operation to start executing from
|
|
|
|
* @param {number} [forkState={}]
|
|
|
|
* - If this is a forked recipe, the state of the recipe up to this point
|
|
|
|
* @returns {number}
|
|
|
|
* - The final progress through the recipe
|
|
|
|
*/
|
|
|
|
async execute(dish, startFrom=0, forkState={}) {
|
2018-04-21 13:25:48 +02:00
|
|
|
let op, input, output,
|
2018-03-27 00:14:23 +02:00
|
|
|
numJumps = 0,
|
|
|
|
numRegisters = forkState.numRegisters || 0;
|
|
|
|
|
2018-04-21 13:25:48 +02:00
|
|
|
if (startFrom === 0) this.lastRunOp = null;
|
|
|
|
|
2019-02-08 15:28:53 +01:00
|
|
|
await this._hydrateOpList();
|
2019-02-01 11:52:21 +01:00
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
log.debug(`[*] Executing recipe of ${this.opList.length} operations, starting at ${startFrom}`);
|
|
|
|
|
|
|
|
for (let i = startFrom; i < this.opList.length; i++) {
|
2019-02-08 15:28:53 +01:00
|
|
|
op = this.opList[i];
|
2018-03-27 00:14:23 +02:00
|
|
|
log.debug(`[${i}] ${op.name} ${JSON.stringify(op.ingValues)}`);
|
|
|
|
if (op.disabled) {
|
|
|
|
log.debug("Operation is disabled, skipping");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (op.breakpoint) {
|
|
|
|
log.debug("Pausing at breakpoint");
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-04-06 20:11:13 +02:00
|
|
|
input = await dish.get(op.inputType);
|
2019-06-07 14:52:47 +02:00
|
|
|
log.debug(`Executing operation '${op.name}'`);
|
|
|
|
|
2019-07-05 12:35:59 +02:00
|
|
|
if (isWorkerEnvironment()) {
|
2019-06-07 14:52:47 +02:00
|
|
|
self.sendStatusMessage(`Baking... (${i+1}/${this.opList.length})`);
|
2019-06-10 16:39:21 +02:00
|
|
|
self.sendProgressMessage(i + 1, this.opList.length);
|
2019-06-07 14:52:47 +02:00
|
|
|
}
|
2018-03-27 00:14:23 +02:00
|
|
|
|
|
|
|
if (op.flowControl) {
|
|
|
|
// Package up the current state
|
|
|
|
let state = {
|
|
|
|
"progress": i,
|
|
|
|
"dish": dish,
|
|
|
|
"opList": this.opList,
|
|
|
|
"numJumps": numJumps,
|
|
|
|
"numRegisters": numRegisters,
|
|
|
|
"forkOffset": forkState.forkOffset || 0
|
|
|
|
};
|
|
|
|
|
|
|
|
state = await op.run(state);
|
|
|
|
i = state.progress;
|
|
|
|
numJumps = state.numJumps;
|
|
|
|
numRegisters = state.numRegisters;
|
|
|
|
} else {
|
|
|
|
output = await op.run(input, op.ingValues);
|
|
|
|
dish.set(output, op.outputType);
|
|
|
|
}
|
2018-05-20 17:49:42 +02:00
|
|
|
this.lastRunOp = op;
|
2018-03-27 00:14:23 +02:00
|
|
|
} catch (err) {
|
2018-04-30 19:25:13 +02:00
|
|
|
// Return expected errors as output
|
2018-05-16 11:17:49 +02:00
|
|
|
if (err instanceof OperationError ||
|
|
|
|
(err.type && err.type === "OperationError")) {
|
|
|
|
// Cannot rely on `err instanceof OperationError` here as extending
|
|
|
|
// native types is not fully supported yet.
|
2018-04-27 10:59:10 +02:00
|
|
|
dish.set(err.message, "string");
|
|
|
|
return i;
|
2018-12-25 23:38:53 +01:00
|
|
|
} else if (err instanceof DishError ||
|
|
|
|
(err.type && err.type === "DishError")) {
|
|
|
|
dish.set(err.message, "string");
|
|
|
|
return i;
|
2018-03-27 00:14:23 +02:00
|
|
|
} else {
|
2018-04-27 10:59:10 +02:00
|
|
|
const e = typeof err == "string" ? { message: err } : err;
|
|
|
|
|
|
|
|
e.progress = i;
|
|
|
|
if (e.fileName) {
|
2018-04-30 19:25:13 +02:00
|
|
|
e.displayStr = `${op.name} - ${e.name} in ${e.fileName} on line ` +
|
|
|
|
`${e.lineNumber}.<br><br>Message: ${e.displayStr || e.message}`;
|
2018-04-27 10:59:10 +02:00
|
|
|
} else {
|
2018-04-30 19:25:13 +02:00
|
|
|
e.displayStr = `${op.name} - ${e.displayStr || e.message}`;
|
2018-04-27 10:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("Recipe complete");
|
|
|
|
return this.opList.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-21 13:25:48 +02:00
|
|
|
/**
|
|
|
|
* Present the results of the final operation.
|
|
|
|
*
|
|
|
|
* @param {Dish} dish
|
|
|
|
*/
|
|
|
|
async present(dish) {
|
|
|
|
if (!this.lastRunOp) return;
|
|
|
|
|
2018-05-16 12:39:30 +02:00
|
|
|
const output = await this.lastRunOp.present(
|
|
|
|
await dish.get(this.lastRunOp.outputType),
|
|
|
|
this.lastRunOp.ingValues
|
|
|
|
);
|
2018-04-21 13:25:48 +02:00
|
|
|
dish.set(output, this.lastRunOp.presentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
/**
|
|
|
|
* Returns the recipe configuration in string format.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
toString() {
|
|
|
|
return JSON.stringify(this.config);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Recipe from a given configuration string.
|
|
|
|
*
|
|
|
|
* @param {string} recipeStr
|
|
|
|
*/
|
|
|
|
fromString(recipeStr) {
|
|
|
|
const recipeConfig = JSON.parse(recipeStr);
|
|
|
|
this._parseConfig(recipeConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a list of all the highlight functions assigned to operations in the recipe, if the
|
|
|
|
* entire recipe supports highlighting.
|
|
|
|
*
|
|
|
|
* @returns {Object[]} highlights
|
|
|
|
* @returns {function} highlights[].f
|
|
|
|
* @returns {function} highlights[].b
|
|
|
|
* @returns {Object[]} highlights[].args
|
|
|
|
*/
|
2019-02-08 15:28:53 +01:00
|
|
|
async generateHighlightList() {
|
|
|
|
await this._hydrateOpList();
|
2018-03-27 00:14:23 +02:00
|
|
|
const highlights = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < this.opList.length; i++) {
|
2018-04-02 18:10:51 +02:00
|
|
|
const op = this.opList[i];
|
2018-03-27 00:14:23 +02:00
|
|
|
if (op.disabled) continue;
|
|
|
|
|
|
|
|
// If any breakpoints are set, do not attempt to highlight
|
|
|
|
if (op.breakpoint) return false;
|
|
|
|
|
|
|
|
// If any of the operations do not support highlighting, fail immediately.
|
|
|
|
if (op.highlight === false || op.highlight === undefined) return false;
|
|
|
|
|
|
|
|
highlights.push({
|
|
|
|
f: op.highlight,
|
|
|
|
b: op.highlightReverse,
|
|
|
|
args: op.ingValues
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return highlights;
|
|
|
|
}
|
|
|
|
|
2018-05-16 12:39:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the previous operation has a different presentation type to its normal output.
|
|
|
|
*
|
|
|
|
* @param {number} progress
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
lastOpPresented(progress) {
|
|
|
|
if (progress < 1) return false;
|
|
|
|
return this.opList[progress-1].presentType !== this.opList[progress-1].outputType;
|
|
|
|
}
|
|
|
|
|
2018-03-27 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Recipe;
|