mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 13:27:24 +00:00
test ffi
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-08-08 08:16:51
|
* @Date: 2020-08-08 08:16:51
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-27 20:39:32
|
* @LastEditTime: 2020-09-06 19:44:32
|
||||||
*/
|
*/
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
@@ -46,13 +46,13 @@ class TestPage extends StatefulWidget {
|
|||||||
|
|
||||||
class _TestPageState extends State<TestPage> {
|
class _TestPageState extends State<TestPage> {
|
||||||
String resp;
|
String resp;
|
||||||
FlutterJs engine;
|
FlutterQjs engine;
|
||||||
|
|
||||||
CodeInputController _controller = CodeInputController();
|
CodeInputController _controller = CodeInputController();
|
||||||
|
|
||||||
_createEngine() async {
|
_createEngine() async {
|
||||||
if (engine != null) return;
|
if (engine != null) return;
|
||||||
engine = FlutterJs();
|
engine = FlutterQjs();
|
||||||
await engine.setMethodHandler((String method, List arg) async {
|
await engine.setMethodHandler((String method, List arg) async {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "http":
|
case "http":
|
||||||
|
@@ -57,6 +57,13 @@ packages:
|
|||||||
url: "https://pub.flutter-io.cn"
|
url: "https://pub.flutter-io.cn"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.1.0-nullsafety"
|
version: "1.1.0-nullsafety"
|
||||||
|
ffi:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: ffi
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "0.1.3"
|
||||||
flutter:
|
flutter:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description: flutter
|
description: flutter
|
||||||
|
27
example/test/widget_test.dart
Normal file
27
example/test/widget_test.dart
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// This is a basic Flutter widget test.
|
||||||
|
//
|
||||||
|
// To perform an interaction with a widget in your test, use the WidgetTester
|
||||||
|
// utility that Flutter provides. For example, you can send tap and scroll
|
||||||
|
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||||||
|
// tree, read text, and verify that the values of widget properties are correct.
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
import 'package:flutter_qjs_example/main.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
testWidgets('Verify Platform version', (WidgetTester tester) async {
|
||||||
|
// Build our app and trigger a frame.
|
||||||
|
await tester.pumpWidget(MyApp());
|
||||||
|
|
||||||
|
// Verify that platform version is retrieved.
|
||||||
|
expect(
|
||||||
|
find.byWidgetPredicate(
|
||||||
|
(Widget widget) => widget is Text &&
|
||||||
|
widget.data.startsWith('Running on:'),
|
||||||
|
),
|
||||||
|
findsOneWidget,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@@ -3,7 +3,7 @@
|
|||||||
* @Author: ekibun
|
* @Author: ekibun
|
||||||
* @Date: 2020-08-08 08:29:09
|
* @Date: 2020-08-08 08:29:09
|
||||||
* @LastEditors: ekibun
|
* @LastEditors: ekibun
|
||||||
* @LastEditTime: 2020-08-28 10:45:14
|
* @LastEditTime: 2020-09-06 13:03:56
|
||||||
*/
|
*/
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
@@ -19,9 +19,9 @@ typedef JsModuleHandler = Future<String> Function(String name);
|
|||||||
class JsMethodHandlerNotImplement {}
|
class JsMethodHandlerNotImplement {}
|
||||||
|
|
||||||
/// FlutterJs instance.
|
/// FlutterJs instance.
|
||||||
/// Each [FlutterJs] object creates a new thread that runs a simple js loop.
|
/// Each [FlutterQjs] object creates a new thread that runs a simple js loop.
|
||||||
/// Make sure call `destroy` to terminate thread and release memory when you don't need it.
|
/// Make sure call `destroy` to terminate thread and release memory when you don't need it.
|
||||||
class FlutterJs {
|
class FlutterQjs {
|
||||||
dynamic _engine;
|
dynamic _engine;
|
||||||
dynamic get pointer => _engine;
|
dynamic get pointer => _engine;
|
||||||
|
|
||||||
|
@@ -50,6 +50,13 @@ packages:
|
|||||||
url: "https://pub.flutter-io.cn"
|
url: "https://pub.flutter-io.cn"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.1.0-nullsafety"
|
version: "1.1.0-nullsafety"
|
||||||
|
ffi:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: ffi
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "0.1.3"
|
||||||
flutter:
|
flutter:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description: flutter
|
description: flutter
|
||||||
|
@@ -10,6 +10,7 @@ environment:
|
|||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
ffi: 0.1.3
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
33
test/flutter_qjs_test.dart
Normal file
33
test/flutter_qjs_test.dart
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* @Description:
|
||||||
|
* @Author: ekibun
|
||||||
|
* @Date: 2020-09-06 13:02:46
|
||||||
|
* @LastEditors: ekibun
|
||||||
|
* @LastEditTime: 2020-09-13 17:29:26
|
||||||
|
*/
|
||||||
|
import 'dart:ffi';
|
||||||
|
import 'package:ffi/ffi.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final DynamicLibrary qjsLib = DynamicLibrary.open("test/lib/build/Debug/ffi_library.dll");
|
||||||
|
print(qjsLib);
|
||||||
|
// JSRuntime *js_NewRuntime(void);
|
||||||
|
final Pointer Function() jsNewRuntime =
|
||||||
|
qjsLib.lookup<NativeFunction<Pointer Function()>>("jsNewRuntime").asFunction();
|
||||||
|
final rt = jsNewRuntime();
|
||||||
|
print(rt);
|
||||||
|
// JSContext *js_NewContext(JSRuntime *rt);
|
||||||
|
final Pointer Function(Pointer rt) jsNewContext =
|
||||||
|
qjsLib.lookup<NativeFunction<Pointer Function(Pointer)>>("jsNewContext").asFunction();
|
||||||
|
final ctx = jsNewContext(rt);
|
||||||
|
print(ctx);
|
||||||
|
// JSValue *js_Eval(JSContext *ctx, const char *input, const char *filename, int eval_flags)
|
||||||
|
final Pointer Function(Pointer rt, Pointer<Utf8> input, Pointer<Utf8> filename, int evalFlags) jsEval =
|
||||||
|
qjsLib.lookup<NativeFunction<Pointer Function(Pointer,Pointer<Utf8>,Pointer<Utf8>, Int32)>>("jsEval").asFunction();
|
||||||
|
final jsval = jsEval(ctx, Utf8.toUtf8("1+1"), Utf8.toUtf8("<eval>"), 0);
|
||||||
|
// const char *js_ToCString(JSContext *ctx, JSValue *val)
|
||||||
|
final Pointer<Utf8> Function(Pointer rt, Pointer val) jsToCString =
|
||||||
|
qjsLib.lookup<NativeFunction<Pointer<Utf8> Function(Pointer,Pointer)>>("jsToCString").asFunction();
|
||||||
|
final str = Utf8.fromUtf8(jsToCString(ctx, jsval));
|
||||||
|
print(str);
|
||||||
|
}
|
19
test/lib/CMakeLists.txt
Normal file
19
test/lib/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
|
||||||
|
project(ffi_library LANGUAGES CXX)
|
||||||
|
add_library(ffi_library SHARED ffi.cpp)
|
||||||
|
|
||||||
|
# quickjs
|
||||||
|
set(QUICK_JS_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../cxx/quickjs)
|
||||||
|
file (STRINGS "${QUICK_JS_LIB_DIR}/VERSION" QUICKJS_VERSION)
|
||||||
|
add_library(libquickjs STATIC
|
||||||
|
${QUICK_JS_LIB_DIR}/cutils.c
|
||||||
|
${QUICK_JS_LIB_DIR}/libregexp.c
|
||||||
|
${QUICK_JS_LIB_DIR}/libunicode.c
|
||||||
|
${QUICK_JS_LIB_DIR}/quickjs.c
|
||||||
|
)
|
||||||
|
|
||||||
|
project(libquickjs LANGUAGES C)
|
||||||
|
target_compile_options(libquickjs PRIVATE "-DCONFIG_VERSION=\"${QUICKJS_VERSION}\"")
|
||||||
|
target_compile_options(libquickjs PRIVATE "-DDUMP_LEAKS")
|
||||||
|
|
||||||
|
target_link_libraries(ffi_library PRIVATE libquickjs)
|
39
test/lib/ffi.cpp
Normal file
39
test/lib/ffi.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* @Description:
|
||||||
|
* @Author: ekibun
|
||||||
|
* @Date: 2020-09-06 18:32:45
|
||||||
|
* @LastEditors: ekibun
|
||||||
|
* @LastEditTime: 2020-09-13 17:26:29
|
||||||
|
*/
|
||||||
|
#include "../../cxx/quickjs/quickjs.h"
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define DLLEXPORT __declspec(dllexport)
|
||||||
|
#else
|
||||||
|
#define DLLEXPORT __attribute__((visibility("default")))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
|
||||||
|
DLLEXPORT JSRuntime *jsNewRuntime()
|
||||||
|
{
|
||||||
|
return JS_NewRuntime();
|
||||||
|
}
|
||||||
|
|
||||||
|
DLLEXPORT JSContext *jsNewContext(JSRuntime *rt)
|
||||||
|
{
|
||||||
|
return JS_NewContext(rt);
|
||||||
|
}
|
||||||
|
|
||||||
|
DLLEXPORT JSValue *jsEval(JSContext *ctx, const char *input, const char *filename, int eval_flags)
|
||||||
|
{
|
||||||
|
return new JSValue{JS_Eval(ctx, input, strlen(input), filename, eval_flags)};
|
||||||
|
}
|
||||||
|
|
||||||
|
DLLEXPORT const char *jsToCString(JSContext *ctx, JSValue *val)
|
||||||
|
{
|
||||||
|
return JS_ToCString(ctx, *val);
|
||||||
|
}
|
||||||
|
}
|
5
test/lib/make.cmd
Normal file
5
test/lib/make.cmd
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
cd %~dp0
|
||||||
|
set CMAKE="C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
|
||||||
|
set BUILD_DIR="./build"
|
||||||
|
%CMAKE% -S ./ -B %BUILD_DIR%
|
||||||
|
%CMAKE% --build %BUILD_DIR% --verbose
|
@@ -121,7 +121,7 @@ namespace
|
|||||||
qjs::Engine *engine = (qjs::Engine *)std::get<int64_t>(ValueOrNull(args, "engine"));
|
qjs::Engine *engine = (qjs::Engine *)std::get<int64_t>(ValueOrNull(args, "engine"));
|
||||||
std::string script = std::get<std::string>(ValueOrNull(args, "script"));
|
std::string script = std::get<std::string>(ValueOrNull(args, "script"));
|
||||||
std::string name = std::get<std::string>(ValueOrNull(args, "name"));
|
std::string name = std::get<std::string>(ValueOrNull(args, "name"));
|
||||||
auto presult = result.release();
|
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> presult = std::move(result);
|
||||||
engine->commit(qjs::EngineTask{
|
engine->commit(qjs::EngineTask{
|
||||||
[script, name](qjs::Context &ctx) {
|
[script, name](qjs::Context &ctx) {
|
||||||
return ctx.eval(script, name.c_str(), JS_EVAL_TYPE_GLOBAL);
|
return ctx.eval(script, name.c_str(), JS_EVAL_TYPE_GLOBAL);
|
||||||
@@ -129,11 +129,9 @@ namespace
|
|||||||
[presult](qjs::Value resolve) {
|
[presult](qjs::Value resolve) {
|
||||||
flutter::EncodableValue response = qjs::jsToDart(resolve);
|
flutter::EncodableValue response = qjs::jsToDart(resolve);
|
||||||
presult->Success(&response);
|
presult->Success(&response);
|
||||||
delete presult;
|
|
||||||
},
|
},
|
||||||
[presult](qjs::Value reject) {
|
[presult](qjs::Value reject) {
|
||||||
presult->Error("FlutterJSException", qjs::getStackTrack(reject));
|
presult->Error("FlutterJSException", qjs::getStackTrack(reject));
|
||||||
delete presult;
|
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
else if (method_call.method_name().compare("call") == 0)
|
else if (method_call.method_name().compare("call") == 0)
|
||||||
@@ -142,7 +140,7 @@ namespace
|
|||||||
qjs::Engine *engine = (qjs::Engine *)std::get<int64_t>(ValueOrNull(args, "engine"));
|
qjs::Engine *engine = (qjs::Engine *)std::get<int64_t>(ValueOrNull(args, "engine"));
|
||||||
qjs::JSValue *function = (qjs::JSValue *)std::get<int64_t>(ValueOrNull(args, "function"));
|
qjs::JSValue *function = (qjs::JSValue *)std::get<int64_t>(ValueOrNull(args, "function"));
|
||||||
flutter::EncodableList arguments = std::get<flutter::EncodableList>(ValueOrNull(args, "arguments"));
|
flutter::EncodableList arguments = std::get<flutter::EncodableList>(ValueOrNull(args, "arguments"));
|
||||||
auto presult = result.release();
|
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> presult = std::move(result);
|
||||||
engine->commit(qjs::EngineTask{
|
engine->commit(qjs::EngineTask{
|
||||||
[function, arguments](qjs::Context &ctx) {
|
[function, arguments](qjs::Context &ctx) {
|
||||||
size_t argscount = arguments.size();
|
size_t argscount = arguments.size();
|
||||||
|
Reference in New Issue
Block a user