mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 21:37:24 +00:00
update README
This commit is contained in:
50
README.md
50
README.md
@@ -1,18 +1,48 @@
|
|||||||
|
<!--
|
||||||
|
* @Description:
|
||||||
|
* @Author: ekibun
|
||||||
|
* @Date: 2020-08-08 08:16:50
|
||||||
|
* @LastEditors: ekibun
|
||||||
|
* @LastEditTime: 2020-08-20 00:33:22
|
||||||
|
-->
|
||||||
# flutter_qjs
|
# flutter_qjs
|
||||||
|
|
||||||
A quickjs engine for flutter.
|
A quickjs engine for flutter.
|
||||||
|
|
||||||
|
## Feature
|
||||||
|
|
||||||
|
This plugin is a simple js engine for flutter used `quickjs` project.
|
||||||
|
|
||||||
|
Each `FlutterJs` object create a new thread that running a simple js loop. A global async function `dart` is presented to invoke dart function, and `Promise` is supported in evaluating js script so that you can use `await` or `then` to get external result from `dart`.
|
||||||
|
|
||||||
|
Data convertion between dart and js are implemented as follow:
|
||||||
|
|
||||||
|
| 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:**
|
||||||
|
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.
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
This project is a starting point for a Flutter
|
1. Creat a `FlutterJs` object. Makes sure call `close` to release memory when not need it.
|
||||||
[plug-in package](https://flutter.dev/developing-packages/),
|
|
||||||
a specialized package that includes platform-specific implementation code for
|
|
||||||
Android and/or iOS.
|
|
||||||
|
|
||||||
For help getting started with Flutter, view our
|
2. Call `setMethodHandler` to maintain `dart` function.
|
||||||
[online documentation](https://flutter.dev/docs), which offers tutorials,
|
|
||||||
samples, guidance on mobile development, and a full API reference.
|
|
||||||
|
|
||||||
The plugin project was generated without specifying the `--platforms` flag, no platforms are currently supported.
|
3. Use `evaluate` to evaluate js script. Makes sure surrounding try-cacth to capture evaluating error.
|
||||||
To add platforms, run `flutter create -t plugin --platforms <platforms> .` under the same
|
|
||||||
directory. You can also find a detailed instruction on how to add platforms in the `pubspec.yaml` at https://flutter.dev/docs/development/packages-and-plugins/developing-packages#plugin-platforms.
|
[this](example/lib/test.dart) contains a fully use of this plugin.
|
||||||
|
|
||||||
|
**notice:**
|
||||||
|
To use this plugin in Linux desktop application, you must change `cxx_std_14` to `cxx_std_17` in your project's `CMakeLists.txt`.
|
@@ -3,7 +3,7 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-08-16 11:08:23
|
* @Date: 2020-08-16 11:08:23
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-19 00:40:54
|
* @LastEditTime: 2020-08-19 13:06:19
|
||||||
*/
|
*/
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@@ -55,14 +55,13 @@ namespace qjs
|
|||||||
}
|
}
|
||||||
else if (className.compare("[D") == 0)
|
else if (className.compare("[D") == 0)
|
||||||
{
|
{
|
||||||
jobjectArray list = jniToArray(env, val);
|
jsize size = env->GetArrayLength((jdoubleArray)val);
|
||||||
jsize size = env->GetArrayLength(list);
|
|
||||||
JSValue array = JS_NewArray(ctx);
|
JSValue array = JS_NewArray(ctx);
|
||||||
cache[val] = array;
|
auto buf = env->GetDoubleArrayElements((jdoubleArray)val, 0);
|
||||||
for (uint32_t i = 0; i < size; i++)
|
for (uint32_t i = 0; i < size; i++)
|
||||||
JS_DefinePropertyValue(
|
JS_DefinePropertyValue(
|
||||||
ctx, array, JS_NewAtomUInt32(ctx, i),
|
ctx, array, JS_NewAtomUInt32(ctx, i),
|
||||||
javaToJs(ctx, env, env->GetObjectArrayElement(list, i), cache),
|
JS_NewFloat64(ctx, buf[i]),
|
||||||
JS_PROP_C_W_E);
|
JS_PROP_C_W_E);
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
@@ -3,8 +3,10 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-07-18 23:28:55
|
* @Date: 2020-07-18 23:28:55
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-18 23:23:46
|
* @LastEditTime: 2020-08-19 13:26:52
|
||||||
*/
|
*/
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_qjs/flutter_qjs.dart';
|
import 'package:flutter_qjs/flutter_qjs.dart';
|
||||||
@@ -51,6 +53,18 @@ class _TestPageState extends State<TestPage> {
|
|||||||
return response.data;
|
return response.data;
|
||||||
case "hello":
|
case "hello":
|
||||||
return await arg[0](["hello: "]);
|
return await arg[0](["hello: "]);
|
||||||
|
case "test":
|
||||||
|
return [
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
0.5,
|
||||||
|
"str",
|
||||||
|
{ "key": "val", 0: 1 },
|
||||||
|
Uint8List(2),
|
||||||
|
Int32List(2),
|
||||||
|
Int64List(2),
|
||||||
|
Float64List(2),
|
||||||
|
Float32List(2)];
|
||||||
default:
|
default:
|
||||||
return JsMethodHandlerNotImplement();
|
return JsMethodHandlerNotImplement();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user