Breakpoints behave better now

This commit is contained in:
Mitchell Plamann
2015-04-22 20:26:29 -04:00
parent 5bee4d90c5
commit d035346d2b
2 changed files with 49 additions and 29 deletions

View File

@@ -478,7 +478,7 @@ ProgramState.prototype.remove_breakpoint = function(opcode_index) {
console.log("Error: negative opcode_index" + opcode_index.toString()); console.log("Error: negative opcode_index" + opcode_index.toString());
throw "Error - negative opcode index"; throw "Error - negative opcode index";
} }
else if (opcode_index >= this.program.length) { else if (opcode_index >= this.frame.program.length) {
console.log("Error: opcode index larger than max index:" console.log("Error: opcode index larger than max index:"
+ opcode_index.toString()); + opcode_index.toString());
throw "Error - opcode index too large"; throw "Error - opcode index too large";
@@ -499,7 +499,7 @@ ProgramState.prototype.set_breakpoint = function(function_index, opcode_index) {
console.log("Error: negative opcode_index" + opcode_index.toString()); console.log("Error: negative opcode_index" + opcode_index.toString());
throw "Error - negative opcode index"; throw "Error - negative opcode index";
} }
else if (opcode_index >= this.program.length) { else if (opcode_index >= this.frame.program.length) {
console.log("Error: opcode index larger than max index:" console.log("Error: opcode index larger than max index:"
+ opcode_index.toString()); + opcode_index.toString());
throw "Error - opcode index too large"; throw "Error - opcode index too large";
@@ -507,6 +507,37 @@ ProgramState.prototype.set_breakpoint = function(function_index, opcode_index) {
this.breakpoints.push([function_index, opcode_index]); this.breakpoints.push([function_index, opcode_index]);
} }
ProgramState.prototype.run = function() {
while (true) {
for (var i = 0; i < this.breakpoints.length; i++) {
var breakpoint = this.breakpoints[i];
if (this.frame.function_id == breakpoint[0] &&
this.frame.pc == breakpoint[1] &&
this.stoppedAt !== breakpoint) {
log("Breakpoint reached!");
this.stoppedAt = breakpoint;
return this;
}
}
this.stoppedAt = undefined;
var val = this.step();
if (val !== undefined) return val;
if (verbose) {
console.log("Machine this:");
console.log(" Current Stack Frame:");
console.log(" Stack: " + this.frame.stack);
console.log(" PC: " + this.frame.pc);
console.log(" Vars: " + this.frame.variables);
// console.log(" Code: " + this.frame.program);
console.log(" Heap: " + this.heap);
}
}
}
function initialize_vm(file, callbacks, v) { function initialize_vm(file, callbacks, v) {
verbose = typeof v !== 'undefined' ? v : true; verbose = typeof v !== 'undefined' ? v : true;
log("Initializing with file " + file); log("Initializing with file " + file);
@@ -520,37 +551,11 @@ function initialize_vm(file, callbacks, v) {
return state; return state;
} }
function run_vm(vm) {
while (true) {
for (breakpoint in vm.breakpoints) {
if (vm.frame.function_id == breakpoint[0] &&
vm.frame.pc == breakpoint[1]) {
console.log("Breakpoint reached!");
return vm;
}
}
var val = vm.step();
if (val !== undefined) return val;
if (verbose) {
console.log("Machine vm:");
console.log(" Current Stack Frame:");
console.log(" Stack: " + vm.frame.stack);
console.log(" PC: " + vm.frame.pc);
console.log(" Vars: " + vm.frame.variables);
// console.log(" Code: " + vm.frame.program);
console.log(" Heap: " + vm.heap);
}
}
}
// Takes in a parsed .bc0 file and runs it // Takes in a parsed .bc0 file and runs it
function execute(file, callbacks, v) { function execute(file, callbacks, v) {
var state = initialize_vm(file, callbacks, v); var state = initialize_vm(file, callbacks, v);
return run_vm(state); return state.run();
} }
exports.execute = execute; exports.execute = execute;
exports.initialize_vm = initialize_vm; exports.initialize_vm = initialize_vm;
exports.run_vm = run_vm;

View File

@@ -110,3 +110,18 @@ exports.testSTRINGS = function(test) {
printout); printout);
test.done(); test.done();
} }
exports.testBREAKPOINT1 = function(test) {
var vm = c0vm.initialize_vm(parser.parseFile("easyMath.c0.bc0"),
callbacks, false);
vm.set_breakpoint(0, 4);
var result = vm.run();
test.ok(result === vm,
"VM did not stop at breakpoint, instead returned " + result);
test.ok(vm.frame.stack[0] == 23 &&
vm.frame.stack[1] == 19,
"VM stack incorrect");
result = vm.run();
test.ok(result == 1748, "VM did not resume operation correctly, gave result " + result);
test.done();
}