So, I'm currently trying to get into emulator programming and I chose JavaScript because it's the language I'm fully fluent in.
Basically my Problem boils down to this: I have an Array which is my Opcode Table, thus it binds my functions to a number.
E. g.:
function function_one() {
alert("hello");
}
function function_two() {
alert("World")
}
var optable = [
function_one,
function_two
];
optable[0](); //runs function_one()
optable[1](); //runs function_two();
Now, I want to write a simple and primitive compiler which takes part of the assembly code line, searches for the first string in this array and returns the index of the function inside the optable, but everything I tried just returns NaN. The currentLine[] is encoded like this:
["function_one", "foo", "bar"] (As strings, yeah)
E.g.:
var currentLine = byLine[i].split(" ");
console.log(currentLine[0]);
currentProgram += optable.findIndex(currentLine[0]); //<-- NaN
console.log(currentProgram);
I understand that this is not how this works because the optable array holds functions and I search for a string, but how can I get this to work? I got something to work with eval(), but I can already see where this could be leading... (Security etc.)
You can make use of the object[propertyName] notation:
const optable = {
function_one : ()=>console.log("Hello"),
function_two : ()=>console.log("world!"),
};
stringContainingAFunctionName = "function_one";
optable[stringContainingAFunctionName]();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With