diff --git a/src/c0vm.js b/src/c0vm.js index 237cf44..bf8170e 100755 --- a/src/c0vm.js +++ b/src/c0vm.js @@ -78,6 +78,8 @@ ProgramState.prototype.push = function(val) { } ProgramState.prototype.pop = function() { + if (this.frame.stack === []) + throw "Tried to pop from an empty stack!"; return this.frame.stack.pop(); } @@ -372,7 +374,7 @@ ProgramState.prototype.step = function() { // Read offset into a struct var offset = this.frame.program[this.frame.pc + 1]; var index = this.pop(); - this.push(this.heap[index + offset]); + this.push(index + offset); this.frame.pc += 2; break; @@ -407,6 +409,7 @@ ProgramState.prototype.step = function() { for (var i = 0; i < 4; i++) this.heap[addr + i] = array[i]; this.frame.pc++; + break; case op.AMLOAD: var addr = this.pop(); @@ -460,6 +463,16 @@ function execute(file, callbacks, v) { var val = state.step(); if (val !== undefined) return val; + if (verbose) { + console.log("Machine state:"); + console.log(" Current Stack Frame:"); + console.log(" Stack: " + state.frame.stack); + console.log(" PC: " + state.frame.pc); + console.log(" Vars: " + state.frame.variables); + console.log(" Code: " + state.frame.program); + console.log(" Heap: " + state.heap); + } + // if (at_breakpoint) { // save state (maybe in a global in this file?) // return; diff --git a/src/index.js b/src/index.js index 09a1678..901e9a8 100644 --- a/src/index.js +++ b/src/index.js @@ -13,9 +13,11 @@ c0ffi = require("./c0ffi.js"); callbacks = {}; callbacks[c0ffi.NATIVE_PRINT] = function(args) { console.log("Print function says: " + args[0]); + return 0; } callbacks[c0ffi.NATIVE_PRINTINT] = function(args) { console.log("Printint function says: " + args[0]); + return 0; } console.log(callbacks);