Quickjs-android is the Android interface framework for quickJS JavaScript engine. It is based on object-oriented design and provides automatic GC function. It is easy to use. At just 350KB in size, the Armeabi-V7A is a good alternative to Google’s V8, starting faster than V8, with a lower memory footprint and ES2020 support.
- On armeabI-V7A, the total APK space is only 350KB;
- JS object automatic GC, without manual release;
- Support ES6 Module, can use import, export functions;
- Support node. js CommonJS specification, can use require, exports functions;
- Support for binding Java annotation functions;
- Support to register JS functions through Java Function Callback functions;
- Provide multithreaded Executor;
Using the tutorial
Github.com/taoweiji/qu…
Introduction of depend on
implementation 'io.github.taoweiji.quickjs:quickjs-android:1.+'
Copy the code
A simple example
QuickJS quickJS = QuickJS.createRuntime();
JSContext context = quickJS.createContext();
int result = context.executeIntegerScript("var a = 2+10; \n a;"."file.js");
context.close();
quickJS.close();
Copy the code
The object is introduced
QuickJS
Runtime environment. Multiple runtime environments can be created. Objects cannot be shared between different environments and need to be destroyed when not in use.
QuickJS quickJS = QuickJS.createRuntime();
Copy the code
JSContext
Created by QuickJS, one QuickJS can create multiple JsContexts, which need to be destroyed when not in use.
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();
Copy the code
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"));
Copy the code
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)));
Copy the code
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);
Copy the code
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(a) {
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));
Copy the code