Fixed strings

This commit is contained in:
Mitchell Plamann
2015-04-19 19:11:20 -04:00
parent 65197535bc
commit d39df5314d
2 changed files with 26 additions and 16 deletions

View File

@@ -28,7 +28,6 @@ function i32_to_array(i32) {
((i32 >> 8) & 0xFF),
((i32 >> 16) & 0xFF),
((i32 >> 24) & 0xFF)];
}
function array_to_i32(array) {
@@ -63,13 +62,13 @@ var ProgramState = function(parsed_file, callback_dict) {
try {
this.natives[i] = callback_dict[i];
} catch (key_not_found) {
dict[i] = function (arg) {
this.natives[i] = function (arg) {
console.log("Native function " + name + " called, ran method stub.");
return 0;
};
}
}
// Memory is just a big array of bytes, right?
// "Allocation" is appending onto this array
// A pointer to memory is an index into this array.
@@ -138,7 +137,7 @@ ProgramState.prototype.step = function() {
case op.BIPUSH:
this.frame.pc += 2;
var val = this.frame.program[this.frame.pc-1];
// Do sign extension if necessary
if ((val & 0x80) != 0)
val = -0x80 + (val & 0x7F);
@@ -340,9 +339,9 @@ ProgramState.prototype.step = function() {
};
console.log("Unknown native function index " + f.function_table_index);
}
log("Calling native function with index " + index + " with arguments " +
log("Calling native function with index " + index + " with arguments " +
arg_array);
this.push(native_function(arg_array));
this.push(native_function(arg_array, this));
break;
// Memory allocation operations:
@@ -352,7 +351,7 @@ ProgramState.prototype.step = function() {
var address = this.heap.length;
for (var i = 0; i < size; i++) this.heap.push(0);
this.push(address);
this.frame.pc += 2;
break;
@@ -365,7 +364,7 @@ ProgramState.prototype.step = function() {
this.heap.push(num_elements);
this.heap.push(size);
for (var i = 0; i < num_elements; i++) {
for (var j = 0; j < size; j++)
this.heap.push(0);
@@ -481,7 +480,7 @@ function execute(file, callbacks, v) {
if (verbose) log(file);
log("Beginning execution");
while (true) {
var val = state.step();
if (val !== undefined) return val;
@@ -500,7 +499,7 @@ function execute(file, callbacks, v) {
// save state (maybe in a global in this file?)
// return;
// }
}
}
}
exports.execute = execute;

View File

@@ -24,13 +24,25 @@ callbacks[c0ffi.NATIVE_STRING_LENGTH] = function(args) {
return args[0].length;
}
callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args) {
return args[0];
callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args, vm) {
var address = vm.heap.length;
vm.heap.push(args[0].length+1);
vm.heap.push(1);
for (var i = 0; i < args[0].length; i++) {
vm.heap.push(args[0][i]);
}
vm.heap.push(0);
return address;
}
callbacks[c0ffi.NATIVE_STRING_FROM_CHARARRAY] = function(args) {
console.log("string_from_chararray: " + args);
return args[0];
callbacks[c0ffi.NATIVE_STRING_FROM_CHARARRAY] = function(args, vm) {
var i = args[0] + 2;
var result = "";
while (vm.heap[i] !== 0) {
result += vm.heap[i];
i++;
}
return result;
}
callbacks[c0ffi.NATIVE_CHAR_CHR] = function(args) {
@@ -38,7 +50,6 @@ callbacks[c0ffi.NATIVE_CHAR_CHR] = function(args) {
}
callbacks[c0ffi.NATIVE_CHAR_ORD] = function(args) {
console.log("native_car_ord: " + args);
if (typeof args[0] == "string")
return args[0].charCodeAt(0);
return args[0];