一个为flutter开发的quickjs引擎。
官方站点:https://bellard.org/quickjs/
中文站点:https://github.com/quickjs-zh/
QuickJS QQ群:598609506。
这是一个使用quickjs为底层,为flutter开发的一个轻量级JS引擎插件。当前插件支持Windows,Linux和Android。
每个 FlutterJs 对象创建一个新的线程来执行js loop。
ES6模块中import函数已经支持可以通过dart中setModuleHandler来管理。
提供一个全局的dart函数来调用dart方法, 并且Promise 已经被支持,所以你可以使用await或者then来获取来自dart中处理的结果。dart和js直接的数据转化可以参考:
| dart | js | 
|---|---|
| Bool | boolean | 
| Int | number | 
| Double | number | 
| String | string | 
| Uint8List/Int32List/Int64List | ArrayBuffer | 
| Float64List | number[] | 
| List | Array | 
| Map | Object | 
| Closure(List) => Future | function(....args) | 
notice:
- 
All the Uint8List/Int32List/Int64Listsent from dart will be converted toArrayBufferwithout marked the size of elements, and theArrayBufferwill be converted toUint8List.
- 
functioncan only sent from js to dart and all the arguments will be packed in a dartListobject.
- Create a FlutterJsobject. Make sure calldestroyto terminate thread and release memory when you don't need it.
FlutterJs engine = FlutterJs();
// do something ...
await engine.destroy();
engine = null;- Call setMethodHandlerto implementsdartinteraction. For example, you can useDioto implements http in js:
await engine.setMethodHandler((String method, List arg) async {
  switch (method) {
    case "http":
      Response response = await Dio().get(arg[0]);
      return response.data;
    default:
      return JsMethodHandlerNotImplement();
  }
});and in javascript, call dart function to get data:
dart("http", "http://example.com/");- Call setModuleHandlerto resolve js module. For example, you can use assets files as module:
await engine.setModuleHandler((String module) async {
  return await rootBundle.loadString(
      "js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
});and in javascript, call import function to get module:
import("hello").then(({default: greet}) => greet("world"));- Use evaluateto run js script, and try-catch is needed to capture js exception.
try {
  print(await engine.evaluate(code ?? '', "<eval>"));
} catch (e) {
  print(e.toString());
}This example contains a complete demonstration on how to use this plugin.