Skip to content
This repository has been archived by the owner on Apr 24, 2019. It is now read-only.

ExtendJS

yalp edited this page Nov 4, 2010 · 4 revisions

Extending Javascript APIs

To extend the Javascript APIs, one must add new static classes and methods to both the compiler and the player.

On the compiler side

The compiler references all classes and methods used the Javascript code.

Add your new Class

Edit compiler/src/ExternCalls.def and add a new class at the end of the file.

TestClass {
  firstMethod
  secondMethod
  thirdMethod
  sum
}

Note

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.

Rebuild the compiler

Each time you modify the ExternCalls.def file, the compiler must be compiled as this file is included in the compiler.

On the player side

Declare your new Class

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.

Implement your functions

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;
    }
}

Accessing functions parameters

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 a FixFloat float value
  • ...

Assigning a return 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 a FixFloat 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;
/* ... */

Rebuild your player

See the [Building] page for more information.

Test your new class and methods

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