diff --git a/public/vm/c0vm.js b/public/vm/c0vm.js index db8b3b7..2e9ae9d 100755 --- a/public/vm/c0vm.js +++ b/public/vm/c0vm.js @@ -478,7 +478,7 @@ ProgramState.prototype.remove_breakpoint = function(opcode_index) { console.log("Error: negative opcode_index" + opcode_index.toString()); 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:" + opcode_index.toString()); 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()); 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:" + opcode_index.toString()); 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]); } +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) { verbose = typeof v !== 'undefined' ? v : true; log("Initializing with file " + file); @@ -520,37 +551,11 @@ function initialize_vm(file, callbacks, v) { 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 function execute(file, callbacks, v) { var state = initialize_vm(file, callbacks, v); - return run_vm(state); + return state.run(); } exports.execute = execute; exports.initialize_vm = initialize_vm; -exports.run_vm = run_vm; diff --git a/test/tests.js b/test/tests.js index 47a03b2..abbcd2e 100644 --- a/test/tests.js +++ b/test/tests.js @@ -110,3 +110,18 @@ exports.testSTRINGS = function(test) { printout); 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(); +}