Inject logger

This commit is contained in:
Jordan Eldredge 2019-07-11 20:57:18 -07:00
parent c9fc58ea44
commit 25bd25fde2
2 changed files with 6 additions and 7 deletions

View file

@ -3,7 +3,7 @@ const { getClass, getFormattedId } = require("./objects");
const interpret = require("./virtualMachine");
const { printCommand } = require("./prettyPrinter");
function main({ runtime, data, system, log }) {
function main({ runtime, data, system, log, logger }) {
const program = parse(data);
// Replace class hashes with actual JavaScript classes from the runtime
@ -26,10 +26,11 @@ function main({ runtime, data, system, log }) {
const { commandOffset, variableOffset, methodOffset } = binding;
const variable = program.variables[variableOffset];
const method = program.methods[methodOffset];
const logger = log ? printCommand : null;
// TODO: Handle disposing of this.
// TODO: Handle passing in variables.
variable.hook(method.name, () => {
interpret(commandOffset, program, { log });
interpret(commandOffset, program, { logger });
});
});

View file

@ -1,6 +1,4 @@
const { printCommand } = require("./prettyPrinter");
function interpret(start, program, { log = false }) {
function interpret(start, program, { logger = null }) {
const { commands, methods, variables, classes, offsetToCommand } = program;
// Run all the commands that are safe to run. Increment this number to find
@ -102,8 +100,8 @@ function interpret(start, program, { log = false }) {
}
// Print some debug info
if (log) {
printCommand({ i, command, stack, variables });
if (logger) {
logger({ i, command, stack, variables });
}
}
}