-
Notifications
You must be signed in to change notification settings - Fork 4
Contribute minijoe
MINIJOE "Minimal Javascript Object Environment" is the javascript engine that enables mods to work. I explain a bit about it on the Getting Started page.
The official Wiki for minijoe is on code.google
Minijoe bytecode is interpreted in JsFunction.java which is key to how it works. The key to porting a java class method to Javascipt is by the evalNative method. Any class overriding this method defined in JsObject.java is sent the current execution stack, stack pointer, opcode (id) and function parameter count. A simple example can be seen in DebugConsole.java
First, the class extends JsObject which means it is an Object (the key to javascript)
The class constructor must call the super constructor first like this super(JsObject.OBJECT_PROTOTYPE);
OBJECT_PROTOTYPE is the prototype the object inherits. For most of the time OBJECT_PROTOTYPE is used.
addNative adds a function property to the object, the first parameter "log" is the javascript name for the function (i.e. Console.log
or Console['log']
. The second parameter is the identifier used in evalNative. It must be >= to 100, the lower id's are reserved and the third parameter is the expected number of arguments passed to the function (not strict).
evalNative is declared to handle function calls you can see on DebugConsole.java the parameters given and what is typically done with these parameters.
A switch-case chooses from the given id
what to do. All implementations of this function must include the following code at the end of their switch-case
default:
super.evalNative(id, stack, sp, parCount);
You can see the function log
runs this code: System.out.println(stack.getObject(sp + 2));
sp + 2
is the location where the first argument is given.
sp + 2
up to sp + 1 + parCount
are the parameters. Access them using stack.get*(index)
where * is the types defined in JsArray.java
If a function returns a value, use stack.set*(sp)
again where * is the types defined in JsArray.java. It must be set on sp
to return.
For more of a challenge, try working out how Block.java works
An alternative is to use CCMLGen.py explained in the next chapter. (Though it is used for different purposes)
Contributing
Home -> Setup, Requirements -> Using MINIJOE -> Using CCMLGen.py