Add the ability to inject a command handler into the interpreter

This commit is contained in:
Jordan Eldredge 2019-07-23 19:53:29 -07:00
parent e66726f02a
commit aa54ee33e0
2 changed files with 7 additions and 11 deletions

View file

@ -11,7 +11,7 @@ function runGeneratorUntilReturn(gen) {
return val.value;
}
function main({ runtime, data, system, log, logger }) {
function main({ runtime, data, system, log, debugHandler }) {
const program = parse(data);
// Replace class hashes with actual JavaScript classes from the runtime
@ -29,6 +29,8 @@ function main({ runtime, data, system, log, logger }) {
return resolved;
});
const handler = debugHandler || runGeneratorUntilReturn;
// Bind top level hooks.
program.bindings.forEach(binding => {
const { commandOffset, variableOffset, methodOffset } = binding;
@ -38,9 +40,7 @@ function main({ runtime, data, system, log, logger }) {
// TODO: Handle disposing of this.
// TODO: Handle passing in variables.
variable.hook(method.name, () => {
runGeneratorUntilReturn(
interpret(commandOffset, program, [], { logger })
);
handler(interpret(commandOffset, program, []));
});
});

View file

@ -10,7 +10,7 @@ function coerceTypes(var1, var2, val1, val2) {
return val1;
}
function* interpret(start, program, stack = [], { logger = null }) {
function* interpret(start, program, stack = []) {
const { commands, methods, variables, classes } = program;
function twoArgCoercingOperator(operator) {
@ -35,9 +35,7 @@ function* interpret(start, program, stack = [], { logger = null }) {
let i = start;
while (i < commands.length) {
const command = commands[i];
if (logger) {
yield { i, command, stack, variables, program };
}
yield { i, command, stack, variables, program };
switch (command.opcode) {
// push
@ -141,9 +139,7 @@ function* interpret(start, program, stack = [], { logger = null }) {
// callGlobal
case 25: {
const offset = command.arg;
const value = yield* interpret(offset, program, stack, {
logger,
});
const value = yield* interpret(offset, program, stack);
stack.push(value);
break;
}