update readme

This commit is contained in:
ekibun
2020-08-20 14:00:12 +08:00
parent b3c9f5b23f
commit aaf365c2fc
6 changed files with 27 additions and 52 deletions

View File

@@ -3,7 +3,7 @@
* @Author: ekibun
* @Date: 2020-08-08 08:16:50
* @LastEditors: ekibun
* @LastEditTime: 2020-08-20 12:02:54
* @LastEditTime: 2020-08-20 13:58:47
-->
# flutter_qjs
@@ -11,7 +11,7 @@ A quickjs engine for flutter.
## Feature
This plugin is a simple js engine for flutter used `quickjs` project. Plugin currently supports Windows, Linux, and Android.
This plugin is a simple js engine for flutter using the `quickjs` project. Plugin currently supports Windows, Linux, and Android.
Each `FlutterJs` object creates a new thread that runs a simple js loop. 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`.
@@ -36,7 +36,7 @@ Data convertion between dart and js are implemented as follow:
## Getting Started
1. Creat a `FlutterJs` object. Make sure call `close` to terminate thread and release memory when you don't need it.
1. Create a `FlutterJs` object. Make sure call `destroy` to terminate thread and release memory when you don't need it.
```dart
FlutterJs engine = FlutterJs();
@@ -62,20 +62,20 @@ engine.setMethodHandler((String method, List arg) async {
and in javascript, call `dart` function to get data:
```javascript
dart("http", "https://baidu.com");
dart("http", "http://example.com/");
```
3. Use `evaluate` to run js script, and try-cacth is needed to capture exception.
3. Use `evaluate` to run js script, and try-catch is needed to capture exception.
```
```dart
try {
resp = "${await engine.evaluate(code ?? '', "<eval>")}";
print(await engine.evaluate(code ?? '', "<eval>"));
} catch (e) {
resp = e.toString();
print(e.toString());
}
```
[Example](example/lib/test.dart) contains a fully use of this plugin.
[This example](example/lib/test.dart) contains a complete demonstration on how to use 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`.

View File

@@ -3,25 +3,13 @@
* @Author: ekibun
* @Date: 2020-08-16 11:08:23
* @LastEditors: ekibun
* @LastEditTime: 2020-08-20 11:29:32
* @LastEditTime: 2020-08-20 13:09:08
*/
#include <string>
#include <unordered_map>
#include "jni_helper.hpp"
#include "../../../../cxx/js_engine.hpp"
namespace std
{
template <>
struct hash<qjs::Value>
{
std::size_t operator()(const qjs::Value &key) const
{
return JS_VALUE_GET_TAG(key.v);
}
};
} // namespace std
namespace qjs
{

View File

@@ -3,13 +3,25 @@
* @Author: ekibun
* @Date: 2020-08-07 13:55:52
* @LastEditors: ekibun
* @LastEditTime: 2020-08-20 11:10:20
* @LastEditTime: 2020-08-20 13:09:52
*/
#pragma once
#include "quickjs/quickjspp.hpp"
#include <future>
#include <string.h>
namespace std
{
template <>
struct hash<qjs::Value>
{
size_t operator()(const qjs::Value &key) const
{
return (size_t) JS_VALUE_GET_PTR(key.v);
}
};
} // namespace std
namespace qjs
{
#include "quickjs/list.h"

View File

@@ -3,7 +3,7 @@
* @Author: ekibun
* @Date: 2020-07-18 23:28:55
* @LastEditors: ekibun
* @LastEditTime: 2020-08-20 12:51:21
* @LastEditTime: 2020-08-20 13:11:33
*/
import 'dart:typed_data';
@@ -45,8 +45,7 @@ class _TestPageState extends State<TestPage> {
engine.setMethodHandler((String method, List arg) async {
switch (method) {
case "http":
Response response = await Dio()
.get(arg[0], options: Options(responseType: ResponseType.bytes));
Response response = await Dio().get(arg[0]);
return response.data;
case "test":
return await arg[0]([

View File

@@ -3,23 +3,11 @@
* @Author: ekibun
* @Date: 2020-08-14 21:45:02
* @LastEditors: ekibun
* @LastEditTime: 2020-08-19 00:34:43
* @LastEditTime: 2020-08-20 13:09:21
*/
#include "../cxx/js_engine.hpp"
#include <flutter_linux/flutter_linux.h>
namespace std
{
template <>
struct hash<qjs::Value>
{
size_t operator()(const qjs::Value &key) const
{
return JS_VALUE_GET_TAG(key.v);
}
};
} // namespace std
namespace qjs
{
JSValue dartToJs(JSContext *ctx, FlValue *val)

View File

@@ -3,24 +3,12 @@
* @Author: ekibun
* @Date: 2020-08-14 21:45:02
* @LastEditors: ekibun
* @LastEditTime: 2020-08-19 00:35:29
* @LastEditTime: 2020-08-20 13:06:24
*/
#include "../cxx/js_engine.hpp"
#include <flutter/standard_method_codec.h>
#include <variant>
namespace std
{
template <>
struct hash<qjs::Value>
{
size_t operator()(const qjs::Value &key) const
{
return JS_VALUE_GET_TAG(key.v);
}
};
} // namespace std
namespace qjs
{
JSValue dartToJs(JSContext *ctx, flutter::EncodableValue val)