From aaf365c2fc0317fe3b7aca79e39809c0f01f619b Mon Sep 17 00:00:00 2001 From: ekibun Date: Thu, 20 Aug 2020 14:00:12 +0800 Subject: [PATCH] update readme --- README.md | 18 +++++++++--------- android/src/main/jni/java_js_wrapper.hpp | 14 +------------- cxx/js_dart_promise.hpp | 14 +++++++++++++- example/lib/test.dart | 5 ++--- linux/dart_js_wrapper.hpp | 14 +------------- windows/dart_js_wrapper.hpp | 14 +------------- 6 files changed, 27 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 5c1bfa2..297044f 100644 --- a/README.md +++ b/README.md @@ -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 ?? '', "")}"; + print(await engine.evaluate(code ?? '', "")); } 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`. \ No newline at end of file diff --git a/android/src/main/jni/java_js_wrapper.hpp b/android/src/main/jni/java_js_wrapper.hpp index ab8d6c8..02ea3c6 100644 --- a/android/src/main/jni/java_js_wrapper.hpp +++ b/android/src/main/jni/java_js_wrapper.hpp @@ -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 #include #include "jni_helper.hpp" #include "../../../../cxx/js_engine.hpp" -namespace std -{ - template <> - struct hash - { - std::size_t operator()(const qjs::Value &key) const - { - return JS_VALUE_GET_TAG(key.v); - } - }; -} // namespace std - namespace qjs { diff --git a/cxx/js_dart_promise.hpp b/cxx/js_dart_promise.hpp index bf672a8..d96c373 100644 --- a/cxx/js_dart_promise.hpp +++ b/cxx/js_dart_promise.hpp @@ -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 #include +namespace std +{ + template <> + struct hash + { + 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" diff --git a/example/lib/test.dart b/example/lib/test.dart index d005715..2af2c55 100644 --- a/example/lib/test.dart +++ b/example/lib/test.dart @@ -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 { 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]([ diff --git a/linux/dart_js_wrapper.hpp b/linux/dart_js_wrapper.hpp index 4e75a99..75e48e4 100644 --- a/linux/dart_js_wrapper.hpp +++ b/linux/dart_js_wrapper.hpp @@ -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 -namespace std -{ - template <> - struct hash - { - 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) diff --git a/windows/dart_js_wrapper.hpp b/windows/dart_js_wrapper.hpp index 44b62a3..da45ef9 100644 --- a/windows/dart_js_wrapper.hpp +++ b/windows/dart_js_wrapper.hpp @@ -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 #include -namespace std -{ - template <> - struct hash - { - 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)