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:
18
README.md
18
README.md
@@ -3,7 +3,7 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-08-08 08:16:50
|
* @Date: 2020-08-08 08:16:50
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-20 12:02:54
|
* @LastEditTime: 2020-08-20 13:58:47
|
||||||
-->
|
-->
|
||||||
# flutter_qjs
|
# flutter_qjs
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ A quickjs engine for flutter.
|
|||||||
|
|
||||||
## Feature
|
## 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`.
|
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
|
## 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
|
```dart
|
||||||
FlutterJs engine = FlutterJs();
|
FlutterJs engine = FlutterJs();
|
||||||
@@ -62,20 +62,20 @@ engine.setMethodHandler((String method, List arg) async {
|
|||||||
and in javascript, call `dart` function to get data:
|
and in javascript, call `dart` function to get data:
|
||||||
|
|
||||||
```javascript
|
```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 {
|
try {
|
||||||
resp = "${await engine.evaluate(code ?? '', "<eval>")}";
|
print(await engine.evaluate(code ?? '', "<eval>"));
|
||||||
} catch (e) {
|
} 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:**
|
**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`.
|
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,25 +3,13 @@
|
|||||||
* @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-20 11:29:32
|
* @LastEditTime: 2020-08-20 13:09:08
|
||||||
*/
|
*/
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include "jni_helper.hpp"
|
#include "jni_helper.hpp"
|
||||||
#include "../../../../cxx/js_engine.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
|
namespace qjs
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@@ -3,13 +3,25 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-08-07 13:55:52
|
* @Date: 2020-08-07 13:55:52
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-20 11:10:20
|
* @LastEditTime: 2020-08-20 13:09:52
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "quickjs/quickjspp.hpp"
|
#include "quickjs/quickjspp.hpp"
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <string.h>
|
#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
|
namespace qjs
|
||||||
{
|
{
|
||||||
#include "quickjs/list.h"
|
#include "quickjs/list.h"
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
* @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-20 12:51:21
|
* @LastEditTime: 2020-08-20 13:11:33
|
||||||
*/
|
*/
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
@@ -45,8 +45,7 @@ class _TestPageState extends State<TestPage> {
|
|||||||
engine.setMethodHandler((String method, List arg) async {
|
engine.setMethodHandler((String method, List arg) async {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "http":
|
case "http":
|
||||||
Response response = await Dio()
|
Response response = await Dio().get(arg[0]);
|
||||||
.get(arg[0], options: Options(responseType: ResponseType.bytes));
|
|
||||||
return response.data;
|
return response.data;
|
||||||
case "test":
|
case "test":
|
||||||
return await arg[0]([
|
return await arg[0]([
|
||||||
|
@@ -3,23 +3,11 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-08-14 21:45:02
|
* @Date: 2020-08-14 21:45:02
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-19 00:34:43
|
* @LastEditTime: 2020-08-20 13:09:21
|
||||||
*/
|
*/
|
||||||
#include "../cxx/js_engine.hpp"
|
#include "../cxx/js_engine.hpp"
|
||||||
#include <flutter_linux/flutter_linux.h>
|
#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
|
namespace qjs
|
||||||
{
|
{
|
||||||
JSValue dartToJs(JSContext *ctx, FlValue *val)
|
JSValue dartToJs(JSContext *ctx, FlValue *val)
|
||||||
|
@@ -3,24 +3,12 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-08-14 21:45:02
|
* @Date: 2020-08-14 21:45:02
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-19 00:35:29
|
* @LastEditTime: 2020-08-20 13:06:24
|
||||||
*/
|
*/
|
||||||
#include "../cxx/js_engine.hpp"
|
#include "../cxx/js_engine.hpp"
|
||||||
#include <flutter/standard_method_codec.h>
|
#include <flutter/standard_method_codec.h>
|
||||||
#include <variant>
|
#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
|
namespace qjs
|
||||||
{
|
{
|
||||||
JSValue dartToJs(JSContext *ctx, flutter::EncodableValue val)
|
JSValue dartToJs(JSContext *ctx, flutter::EncodableValue val)
|
||||||
|
Reference in New Issue
Block a user