use native promise constructor

This commit is contained in:
ekibun
2020-09-22 00:32:17 +08:00
parent 15ae27d7b0
commit 29c2b04a36
11 changed files with 128 additions and 71 deletions

View File

@@ -3,7 +3,7 @@
* @Author: ekibun
* @Date: 2020-08-08 08:16:50
* @LastEditors: ekibun
* @LastEditTime: 2020-08-27 21:11:55
* @LastEditTime: 2020-09-22 00:03:48
-->
# flutter_qjs
@@ -11,13 +11,13 @@ A quickjs engine for flutter.
## Feature
This plugin is a simple js engine for flutter using the `quickjs` project. Plugin currently supports Windows, Linux, and Android.
This plugin is a simple js engine for flutter using the `quickjs` project with `dart:ffi`. Plugin currently supports Windows, Linux, and Android.
Each `FlutterJs` object creates a new thread that runs a simple js loop.
Event loop of `FlutterQjs` should be implemented by calling `FlutterQjs.dispatch()`.
ES6 module with `import` function is supported and can manage in dart with `setModuleHandler`.
A global async function `dart` is presented to invoke dart function, and `Promise` is supported so that you can use `await` or `then` to get external result from `dart`. Data convertion between dart and js are implemented as follow:
A global function `convert` is presented to invoke dart function. Data convertion between dart and js are implemented as follow:
| dart | js |
| --- | --- |
@@ -25,54 +25,50 @@ A global async function `dart` is presented to invoke dart function, and `Promis
| Int | number |
| Double | number |
| String | string |
| Uint8List/Int32List/Int64List | ArrayBuffer |
| Float64List | number[] |
| Uint8List | ArrayBuffer |
| List | Array |
| Map | Object |
| Closure(List) => Future | function(....args) |
| JSFunction | function(....args) |
| Future | Promise |
**notice:**
1. All the `Uint8List/Int32List/Int64List` sent from dart will be converted to `ArrayBuffer` without marked the size of elements, and the `ArrayBuffer` will be converted to `Uint8List`.
2. `function` can only sent from js to dart and all the arguments will be packed in a dart `List` object.
**notice:** `function` can only sent from js to dart.
## Getting Started
1. Create a `FlutterJs` object. Make sure call `destroy` to terminate thread and release memory when you don't need it.
1. Create a `FlutterQjs` object. Call `dispatch` to dispatch event loop.
```dart
FlutterJs engine = FlutterJs();
// do something ...
await engine.destroy();
engine = null;
final engine = FlutterQjs();
await engine.dispatch();
```
2. Call `setMethodHandler` to implements `dart` interaction. For example, you can use `Dio` to implements http in js:
```dart
await engine.setMethodHandler((String method, List arg) async {
await engine.setMethodHandler((String method, List arg) {
switch (method) {
case "http":
Response response = await Dio().get(arg[0]);
return response.data;
return Dio().get(arg[0]).then((response) => response.data);
default:
return JsMethodHandlerNotImplement();
throw Exception("No such method");
}
});
```
and in javascript, call `dart` function to get data:
and in javascript, call `convert` function to get data, make sure the second memeber is a list:
```javascript
dart("http", "http://example.com/");
convert("http", ["http://example.com/"]);
```
3. Call `setModuleHandler` to resolve js module. For example, you can use assets files as module:
3. Call `setModuleHandler` to resolve js module.
**important:** I cannot find a way to convert the sync ffi callback into an async function. So the assets files got by async function `rootBundle.loadString` cannot be used in this version. I will be appreciated that you can provied me a solution to make `ModuleHandler` async.
```dart
await engine.setModuleHandler((String module) async {
return await rootBundle.loadString(
"js/" + module.replaceFirst(new RegExp(r".js$"), "") + ".js");
await engine.setModuleHandler((String module) {
if(module == "hello") return "export default (name) => `hello \${name}!`;";
throw Exception("Module Not found");
});
```
@@ -82,7 +78,7 @@ and in javascript, call `import` function to get module:
import("hello").then(({default: greet}) => greet("world"));
```
4. Use `evaluate` to run js script, and try-catch is needed to capture js exception.
4. Use `evaluate` to run js script:
```dart
try {
@@ -92,4 +88,6 @@ try {
}
```
5. Method `recreate` can free quickjs runtime that can be recreated again when calling `evaluate`, it can be used to reset the module cache. Call `close` to stop `dispatch` when you do not need it.
[This example](example/lib/main.dart) contains a complete demonstration on how to use this plugin.