quickjs-android 是 QuickJS JavaScript 引擎的 Android 接口框架,整体基于面向对象设计,提供了自动GC功能,使用简单。armeabi-v7a 的大小仅 350KB,是 Google V8 不错的替代品,启动速度比 V8 快,内存占用更低,支持 ES2020。
https://github.com/taoweiji/quickjs-android
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.+'
QuickJS quickJS = QuickJS.createRuntime();
JSContext context = quickJS.createContext();
int result = context.executeIntegerScript("var a = 2+10;\n a;", "file.js");
context.close();
quickJS.close();
运行环境,可以创建多个运行时环境,不同的环境之间不能共享对象,不使用的时候需要销毁。
QuickJS quickJS = QuickJS.createRuntime();
由 QuickJS 创建,一个 QuickJS 可以创建多个 JSContext,不使用的时候需要销毁。
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 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 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 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);
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));