Fixed strings
This commit is contained in:
19
src/c0vm.js
19
src/c0vm.js
@@ -28,7 +28,6 @@ function i32_to_array(i32) {
|
|||||||
((i32 >> 8) & 0xFF),
|
((i32 >> 8) & 0xFF),
|
||||||
((i32 >> 16) & 0xFF),
|
((i32 >> 16) & 0xFF),
|
||||||
((i32 >> 24) & 0xFF)];
|
((i32 >> 24) & 0xFF)];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function array_to_i32(array) {
|
function array_to_i32(array) {
|
||||||
@@ -63,13 +62,13 @@ var ProgramState = function(parsed_file, callback_dict) {
|
|||||||
try {
|
try {
|
||||||
this.natives[i] = callback_dict[i];
|
this.natives[i] = callback_dict[i];
|
||||||
} catch (key_not_found) {
|
} catch (key_not_found) {
|
||||||
dict[i] = function (arg) {
|
this.natives[i] = function (arg) {
|
||||||
console.log("Native function " + name + " called, ran method stub.");
|
console.log("Native function " + name + " called, ran method stub.");
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Memory is just a big array of bytes, right?
|
// Memory is just a big array of bytes, right?
|
||||||
// "Allocation" is appending onto this array
|
// "Allocation" is appending onto this array
|
||||||
// A pointer to memory is an index into this array.
|
// A pointer to memory is an index into this array.
|
||||||
@@ -138,7 +137,7 @@ ProgramState.prototype.step = function() {
|
|||||||
case op.BIPUSH:
|
case op.BIPUSH:
|
||||||
this.frame.pc += 2;
|
this.frame.pc += 2;
|
||||||
var val = this.frame.program[this.frame.pc-1];
|
var val = this.frame.program[this.frame.pc-1];
|
||||||
|
|
||||||
// Do sign extension if necessary
|
// Do sign extension if necessary
|
||||||
if ((val & 0x80) != 0)
|
if ((val & 0x80) != 0)
|
||||||
val = -0x80 + (val & 0x7F);
|
val = -0x80 + (val & 0x7F);
|
||||||
@@ -340,9 +339,9 @@ ProgramState.prototype.step = function() {
|
|||||||
};
|
};
|
||||||
console.log("Unknown native function index " + f.function_table_index);
|
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);
|
arg_array);
|
||||||
this.push(native_function(arg_array));
|
this.push(native_function(arg_array, this));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Memory allocation operations:
|
// Memory allocation operations:
|
||||||
@@ -352,7 +351,7 @@ ProgramState.prototype.step = function() {
|
|||||||
var address = this.heap.length;
|
var address = this.heap.length;
|
||||||
|
|
||||||
for (var i = 0; i < size; i++) this.heap.push(0);
|
for (var i = 0; i < size; i++) this.heap.push(0);
|
||||||
|
|
||||||
this.push(address);
|
this.push(address);
|
||||||
this.frame.pc += 2;
|
this.frame.pc += 2;
|
||||||
break;
|
break;
|
||||||
@@ -365,7 +364,7 @@ ProgramState.prototype.step = function() {
|
|||||||
|
|
||||||
this.heap.push(num_elements);
|
this.heap.push(num_elements);
|
||||||
this.heap.push(size);
|
this.heap.push(size);
|
||||||
|
|
||||||
for (var i = 0; i < num_elements; i++) {
|
for (var i = 0; i < num_elements; i++) {
|
||||||
for (var j = 0; j < size; j++)
|
for (var j = 0; j < size; j++)
|
||||||
this.heap.push(0);
|
this.heap.push(0);
|
||||||
@@ -481,7 +480,7 @@ function execute(file, callbacks, v) {
|
|||||||
if (verbose) log(file);
|
if (verbose) log(file);
|
||||||
|
|
||||||
log("Beginning execution");
|
log("Beginning execution");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
var val = state.step();
|
var val = state.step();
|
||||||
if (val !== undefined) return val;
|
if (val !== undefined) return val;
|
||||||
@@ -500,7 +499,7 @@ function execute(file, callbacks, v) {
|
|||||||
// save state (maybe in a global in this file?)
|
// save state (maybe in a global in this file?)
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.execute = execute;
|
exports.execute = execute;
|
||||||
|
|||||||
@@ -24,13 +24,25 @@ callbacks[c0ffi.NATIVE_STRING_LENGTH] = function(args) {
|
|||||||
return args[0].length;
|
return args[0].length;
|
||||||
}
|
}
|
||||||
|
|
||||||
callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args) {
|
callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args, vm) {
|
||||||
return args[0];
|
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) {
|
callbacks[c0ffi.NATIVE_STRING_FROM_CHARARRAY] = function(args, vm) {
|
||||||
console.log("string_from_chararray: " + args);
|
var i = args[0] + 2;
|
||||||
return args[0];
|
var result = "";
|
||||||
|
while (vm.heap[i] !== 0) {
|
||||||
|
result += vm.heap[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
callbacks[c0ffi.NATIVE_CHAR_CHR] = function(args) {
|
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) {
|
callbacks[c0ffi.NATIVE_CHAR_ORD] = function(args) {
|
||||||
console.log("native_car_ord: " + args);
|
|
||||||
if (typeof args[0] == "string")
|
if (typeof args[0] == "string")
|
||||||
return args[0].charCodeAt(0);
|
return args[0].charCodeAt(0);
|
||||||
return args[0];
|
return args[0];
|
||||||
|
|||||||
Reference in New Issue
Block a user