Pure JS

I want to write the simple JavaScript program without Node.js to be my first try.

/src/main/assets/pure.js

var ClassName = class {
    get member() {
        return 'member';
    }

    getSomething() {
        return 'something';
    }

    static getConstance() {
        return 3;
    }
};

function getA() {
    return "A";
};

var instance = new ClassName();

Below is Java program.

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;

public class JavaClass {

    ......

    public void exeJavaScript() {
        String jsContent = readFromAsset("pure.js");

        V8 runtime = V8.createV8Runtime();
        runtime.executeVoidScript(jsContent);

        // static function
        V8Object className = runtime.getObject("ClassName");
        Log.d("Test", "" + className.executeIntegerFunction("getConstance", null));

        // interact with instance
        V8Object instance = runtime.getObject("instance");
        Log.d("Test", instance.getString("member"));
        Log.d("Test", instance.executeStringFunction("getSomething", null));

        // global function
        Object result = runtime.executeJSFunction("getA");
        Log.d("Test", "" + result.toString());

        className.release();
        instance.release();
        runtime.release();
    }

    private String readFromAsset(String fileName) {
        ......
    }
}

Result in LogCat:

09-22 01:47:54.963 3441-3441/? D/Test: 3
09-22 01:47:54.963 3441-3441/? D/Test: member
09-22 01:47:54.963 3441-3441/? D/Test: something
09-22 01:47:54.963 3441-3441/? D/Test: A

results matching ""

    No results matching ""