QuickJS wrapper for Android.
- Java types are supported with JavaScript
- Support promise execute
console.log
implement- JavaScript exception handler
- Optimize not a function with its name when type error
Experimental Features Stability not guaranteed.
- Compile bytecode
- ESModule (import, export)
repositories {
mavenCentral()
}
dependencies {
implementation 'wang.harlon.quickjs:wrapper:latest.version'
}
See here for the latest version
QuickJSContext context = QuickJSContext.create();
QuickJSContext context = QuickJSContext.create();
context.evaluate("var a = 1 + 2;");
Currently, the following Java types are supported with JavaScript:
boolean
int
when calling JavaScript from Java.double
String
null
JSObject
represents a JavaScript objectJSFunction
represents a JavaScript functionJSArray
represents a JavaScript Array
Java
JSObject globalObj = context.getGlobalObject();
JSObject obj1 = context.createNewJSObject();
obj1.setProperty("stringProperty", "hello");
obj1.setProperty("intProperty", 1);
obj1.setProperty("doubleProperty", 0.1);
obj1.setProperty("booleanProperty", true);
obj1.setProperty("functionProperty", (JSCallFunction) args -> {
return args[0] + "Wang";
});
globalObj.setProperty("obj1", obj1);
JavaScript
obj1.stringProperty; // hello string
obj1.intProperty; // 1
obj1.doubleProperty; // 0.1
obj1.booleanProperty; // true
obj1.functionProperty('Harlon'); // HarlonWang
JavaScript
var obj1 = {
stringProperty: 'hello string',
intProperety: 1,
doubleProperty: 0.1,
booleanProperty: true,
functionProperty: (name) => { return name + 'Wang'; }
}
Java
JSObject globalObject = context.getGlobalObject();
JSObject obj1 = globalObject.getJSObjectProperty("obj1");
obj1.getProperty("stringProperty"); // hello
obj1.getProperty("intProperty"); // 1
obj1.getProperty("doubleProperty"); // 0.1
obj1.getProperty("booleanProperty"); // true
obj1.getJSFunctionProperty("functionProperty").call("Harlon"); // HarlonWang
byte[] code = context.compile("'hello, world!'.toUpperCase();");
context.execute(code);
Java
JSModule.setModuleLoader(new JSModule.Loader() {
@Override
public String getModuleScript(String moduleName) {
return "export var name = 'Hello world';\n" +
"export var age = 18;";
}
});
JavaScript
import {name, age} from './a.js';
console.log('name:' + name); // Jack
console.log('age:' + age); // 18
JavaScript runtimes are single threaded. All execution in the JavaScript runtime is guaranteed thread safe, by way of Java synchronization.