-
Notifications
You must be signed in to change notification settings - Fork 3
ExtendJS
To extend the Javascript APIs, one must add new static classes and methods to both the compiler and the player.
The compiler references all classes and methods used the Javascript code.
Edit compiler/src/ExternCalls.def
and add a new class at the end of the file.
TestClass {
firstMethod
secondMethod
thirdMethod
sum
}
Always add new methods to existing classes at the end of a class to ensure backward compatibility.
Each method is assigned a unique ID
by the compiler depending on its position.
This is also the case for classes. Always add new classes at the end of the ExternCalls.def
file to ensure backward compatibility.
Each time you modify the ExternCalls.def file, the compiler must be compiled as this file is included in the compiler.
Edit the src/memoplayer/ExternCalls.java and add a new case
statement to the doCall()
static method.
static void doCall (Machine mc, Context c, int o, int m, Register [] registers, int r, int nbParams) {
switch (o) {
/* ... */
case 10: doTestClass (mc, c, m, registers, r, nbParams); break;
/* ... */
}
}
The number ID
to use is the position (starting at 0) of the Class as declared in the ExternCalls.def
file in the compiler.
To implement the new static call doTestClass()
in ExternCalls.java, add the following static method:
void doTestClass (Machine mc, Context c, int o, int m, Register [] registers, int r, int nbParams) {
switch (m) {
case 0: // firstMethod
Logger.println ("DEBUG: TestClass.firstMethod() called !");
break;
case 1: // secondMethod
Logger.println ("DEBUG: TestClass.secondMethod() called !");
break;
case 2: // thirdMethod
Logger.println ("DEBUG: TestClass.thirdMethod() called !");
break;
}
}
For each call, the nbParams
contains the number of parameters
To access to the methods parameters, use the registers defined between r
and r+nbParams
and use all the get* methods of the Register
class.
String s = register[r+2].getString(); // getting the third parameter as a String
All get* methods of the Register
class can be used to set the value:
-
String Register.getString ()
: get a String value -
int Register.getInt ()
: get an integer value -
int Register.getFloat ()
: get aFixFloat
float value - ...
To set the return value, set the value in the first register pointed by the r
variable.
registers[r].setInt (3); // return integer 3
All set* methods of the Register
class can be used to set the value:
-
Register.setString (String s)
: set a string value -
Register.setInt (int i)
: set an integer value -
Register.setFloat (int f)
: set aFixFloat
float value
Here is an example of a function returning the sum of two integers:
/* ... */
case 3: // sum(int i1, int i2)
int sum = registers[r].getInt() + registers[r+1].getInt();
registers[r].setInt (sum);
break;
/* ... */
See the [Building] page for more information.
To check every thing works, juste write a small Script calling your new methods :
function initialize () {
var sum = TestClass.sum (2, 3);
Browser.print ('sum of 2 + 3 = '+sum);
}
This shoud print :
sum of 2 + 3 = 5