JSContext context = quickJS.createContext();
int result = context.executeIntegerScript("var a = 2+10;\n a;", "file.js");
String result = context.executeStringScript("'Hello World';", "file.js");
context.close();
JSObject
JSObject user = new JSObject(context).set("name", "Wiki").set("age", 18).set("time",System.currentTimeMillis());
Log.e("QuickJS", String.valueOf(user.getString("name")));
Log.e("QuickJS", String.valueOf(user.getInteger("age")));
Log.e("QuickJS", String.valueOf(user.getDouble("time")));
user.registerJavaMethod(new JavaVoidCallback() {
@Override
public void invoke(JSObject receiver, JSArray args) {
Log.e("QuickJS", args.getString(0));
}
}, "log");
user.executeVoidFunction("log", new JSArray(context).push("Hello World"));
JSArray
JSArray array = new JSArray(context).push(1).push(3.14).push(true).push("Hello World");
Log.e("QuickJS", String.valueOf(array.getInteger(0)));
Log.e("QuickJS", String.valueOf(array.getDouble(1)));
JSFunction
JSFunction log = new JSFunction(context, new JavaVoidCallback() {
@Override
public void invoke(JSObject receiver, JSArray args) {
Log.e("QuickJS", args.getString(0));
}
});
JSFunction message = new JSFunction(context, new JavaCallback() {
@Override
public Object invoke(JSObject receiver, JSArray array) {
return "Hello World";
}
});
context.set("console", new JSObject(context).set("log", log).set("message", message));
context.executeVoidScript("console.log(console.message())", null);
addJavascriptInterface
public class Console {
int count = 0;
@JavascriptInterface
public void log(String msg) {
count++;
Log.d("console", msg);
}
@JavascriptInterface
public void info(String msg) {
count++;
Log.i("console", msg);
}
@JavascriptInterface
public void error(String msg) {
count++;
Log.e("console", msg);
}
@JavascriptInterface
public int count() {
return count;
}
}
context.addJavascriptInterface(new Console(), "console");
context.executeVoidScript("console.log('Hello World')", null);
int count = context.executeIntegerScript("console.count()", null);
Log.d("console", String.valueOf(count));