Multiple Register operations can now be called in a single recipe

This commit is contained in:
n1474335 2017-09-28 18:39:35 +00:00
parent e2ac297102
commit 877ab57f0a
6 changed files with 36 additions and 18 deletions

View File

@ -92,7 +92,7 @@ async function bake(data) {
} catch (err) {
self.postMessage({
action: "bakeError",
data: err.message
data: err.message.split(":").slice(1).join(":").slice(1) // Cut off worker blurb
});
}
}
@ -182,13 +182,15 @@ self.setOption = function(option, value) {
* Send register values back to the app.
*
* @param {number} opIndex
* @param {number} numPrevRegisters
* @param {string[]} registers
*/
self.setRegisters = function(opIndex, registers) {
self.setRegisters = function(opIndex, numPrevRegisters, registers) {
self.postMessage({
action: "setRegisters",
data: {
opIndex: opIndex,
numPrevRegisters: numPrevRegisters,
registers: registers
}
});

View File

@ -116,7 +116,7 @@ const FlowControl = {
if (!registers) return state;
if (ENVIRONMENT_IS_WORKER()) {
self.setRegisters(state.progress, registers.slice(1));
self.setRegisters(state.progress, state.numRegisters, registers.slice(1));
}
/**
@ -127,31 +127,34 @@ const FlowControl = {
*/
function replaceRegister(str) {
// Replace references to registers ($Rn) with contents of registers
str = str.replace(/((?:^|[^\\])(?:\\.|[^\\])*?)\$R(\d{1,2})/g, (match, pre, regNum) => {
str = str ? str.replace(/((?:^|[^\\])(?:\\.|[^\\])*?)\$R(\d{1,2})/g, (match, pre, regNum) => {
const index = parseInt(regNum, 10) + 1;
return (index >= registers.length) ? match : pre + registers[index];
});
return (index <= state.numRegisters || index >= state.numRegisters + registers.length) ?
match : pre + registers[index - state.numRegisters];
}) : str;
// Unescape remaining register references
return str.replace(/\\\$R(\d{1,2})/, "$R$1");
return str ? str.replace(/\\\$R(\d{1,2})/, "$R$1") : str;
}
// Step through all subsequent ops and replace registers in args with extracted content
for (let i = state.progress + 1; i < state.opList.length; i++) {
if (state.opList[i].isDisabled()) continue;
let args = state.opList[i].getIngValues();
args = args.map(arg => {
if (typeof arg !== "string" && typeof arg !== "object") return arg;
if (typeof arg === "object" && arg.string) {
if (typeof arg === "object" && arg.hasOwnProperty("string")) {
arg.string = replaceRegister(arg.string);
return arg;
}
return replaceRegister(arg);
});
state.opList[i].setIngValues(args);
}
state.numRegisters += registers.length - 1;
return state;
},

View File

@ -145,7 +145,7 @@ Recipe.prototype.lastOpIndex = function(startIndex) {
*/
Recipe.prototype.execute = async function(dish, startFrom) {
startFrom = startFrom || 0;
let op, input, output, numJumps = 0;
let op, input, output, numJumps = 0, numRegisters = 0;
for (let i = startFrom; i < this.opList.length; i++) {
op = this.opList[i];
@ -162,15 +162,17 @@ Recipe.prototype.execute = async function(dish, startFrom) {
if (op.isFlowControl()) {
// Package up the current state
let state = {
"progress": i,
"dish": dish,
"opList": this.opList,
"numJumps": numJumps
"progress": i,
"dish": dish,
"opList": this.opList,
"numJumps": numJumps,
"numRegisters": numRegisters
};
state = await op.run(state);
i = state.progress;
numJumps = state.numJumps;
numRegisters = state.numRegisters;
} else {
output = await op.run(input, op.getIngValues());
dish.set(output, op.outputType);

View File

@ -441,18 +441,19 @@ RecipeWaiter.prototype.opRemove = function(e) {
* Sets register values.
*
* @param {number} opIndex
* @param {number} numPrevRegisters
* @param {string[]} registers
*/
RecipeWaiter.prototype.setRegisters = function(opIndex, registers) {
RecipeWaiter.prototype.setRegisters = function(opIndex, numPrevRegisters, registers) {
const op = document.querySelector(`#rec-list .operation:nth-child(${opIndex + 1})`),
prevRegList = op.querySelector(".register-list");
// Remove previous div
if (prevRegList) prevRegList.remove();
window.Utils = Utils;
let registerList = [];
for (let i = 0; i < registers.length; i++) {
registerList.push(`$R${i} = ${Utils.truncate(Utils.printable(registers[i]), 100)}`);
registerList.push(`$R${numPrevRegisters + i} = ${Utils.escapeHtml(Utils.truncate(Utils.printable(registers[i]), 100))}`);
}
const registerListEl = `<div class="register-list">
${registerList.join("<br>")}

View File

@ -62,7 +62,7 @@ WorkerWaiter.prototype.handleChefMessage = function(e) {
this.app.options[r.data.option] = r.data.value;
break;
case "setRegisters":
this.manager.recipe.setRegisters(r.data.opIndex, r.data.registers);
this.manager.recipe.setRegisters(r.data.opIndex, r.data.numPrevRegisters, r.data.registers);
break;
case "highlightsCalculated":
this.manager.highlighter.displayHighlights(r.data.pos, r.data.direction);

View File

@ -201,3 +201,13 @@ button.dropdown-toggle {
background-color: var(--disabled-bg-colour) !important;
border-color: var(--disabled-border-colour) !important;
}
.break .register-list {
color: var(--fc-breakpoint-operation-font-colour) !important;
background-color: var(--fc-breakpoint-operation-border-colour) !important;
}
.disabled .register-list {
color: var(--disabled-font-colour) !important;
background-color: var(--disabled-border-colour) !important;
}