-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ac2pic/master
Give callables more control over execution (also includes `MERGE_CONTENT` by EL) Co-Authored-By: ac2pic <ac2pic@gmail.com> Co-Authored-By: EL2020 <el2020202020@gmail.com>
- Loading branch information
Showing
6 changed files
with
376 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {appliers, DebugState, callables} from "./patchsteps-patch.js"; | ||
|
||
/** | ||
* @param {string} id | ||
* @param {Callable} callable | ||
*/ | ||
export function register(id, callable) { | ||
if (typeof id !== "string") { | ||
throw Error('Id must be a string'); | ||
} | ||
|
||
if (id.length === 0) { | ||
throw Error('Id must not be empty.'); | ||
} | ||
|
||
if (typeof callable !== "function") { | ||
throw Error('Callable must be a function.'); | ||
} | ||
if (callables.has(id)) { | ||
throw Error(`Callable ${id} is already registered.`); | ||
} | ||
callables.set(id, callable); | ||
} | ||
|
||
appliers["CALL"] = async function(state) { | ||
const id = this["id"]; | ||
const args = this["args"]; | ||
|
||
// Any falsey values are invalid | ||
if (!id) { | ||
state.debugState.throwError('ValueError', 'Id must be set.'); | ||
} | ||
|
||
if (!callables.has(id)) { | ||
state.debugState.throwError('ValueError', `${id} is not a valid callable.`); | ||
} | ||
|
||
/** @type{Callable} **/ | ||
const callable = callables.get(id); | ||
|
||
try { | ||
await callable(state, args); | ||
} catch (e) { | ||
if (e !== state.debugState) { | ||
// So they know what happened | ||
console.error(e); | ||
state.debugState.throwError('ValueError', `Callable ${id} did not properly throw an error.`); | ||
} | ||
// They properly threw the error | ||
throw e; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
export class StepMachine { | ||
constructor(steps) { | ||
this.steps = steps; | ||
this.si = 0; | ||
this.finished = false; | ||
} | ||
|
||
* run() { | ||
while (this.si < this.steps.length) { | ||
yield [this.si, this.steps[this.si]]; | ||
if (this.finished) { | ||
break; | ||
} | ||
this.si++; | ||
} | ||
} | ||
|
||
addSteps(newSteps) { | ||
if (Array.isArray(newSteps)) { | ||
this.steps = this.steps.concat(newSteps); | ||
} else { | ||
this.steps.push(newSteps); | ||
} | ||
} | ||
|
||
exit() { | ||
this.finished = true; | ||
} | ||
|
||
gotoLabel(labelName) { | ||
const labelIndex = this.findLabelIndex(labelName); | ||
if (labelIndex == -1) { | ||
return false; | ||
} | ||
this.setStepIndex(labelIndex); | ||
return true; | ||
} | ||
|
||
setStepIndex(newStepIndex) { | ||
if(newStepIndex < 0 || this.steps.length <= newStepIndex) { | ||
return false; | ||
} | ||
this.si = newStepIndex; | ||
return true; | ||
} | ||
|
||
getCurrentStep() { | ||
return this.steps[this.si]; | ||
} | ||
|
||
getStepIndex() { | ||
return this.si; | ||
} | ||
|
||
findLabelIndex(labelName) { | ||
let stepIndex = -1; | ||
|
||
for(const [index, step] of this.steps.entries()) { | ||
if (step["type"] !== "LABEL") { | ||
continue; | ||
} | ||
if (step["name"] == labelName) { | ||
stepIndex = index; | ||
break; | ||
} | ||
} | ||
|
||
return stepIndex; | ||
} | ||
} | ||
|
Oops, something went wrong.