mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
windows wrapper
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
|
||||
set(CXX_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../cxx)
|
||||
|
||||
# quickjs
|
||||
set(QUICK_JS_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../cxx/quickjs)
|
||||
set(QUICK_JS_LIB_DIR ${CXX_LIB_DIR}/quickjs)
|
||||
file (STRINGS "${QUICK_JS_LIB_DIR}/VERSION" QUICKJS_VERSION)
|
||||
add_library(libquickjs STATIC
|
||||
${QUICK_JS_LIB_DIR}/cutils.c
|
||||
@@ -21,6 +23,7 @@ set(PLUGIN_NAME "${PROJECT_NAME}_plugin")
|
||||
|
||||
add_library(${PLUGIN_NAME} SHARED
|
||||
"${PLUGIN_NAME}.cpp"
|
||||
"${CXX_LIB_DIR}/ffi.cpp"
|
||||
)
|
||||
apply_standard_settings(${PLUGIN_NAME})
|
||||
set_target_properties(${PLUGIN_NAME} PROPERTIES
|
||||
|
@@ -1,154 +0,0 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-14 21:45:02
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-25 18:08:45
|
||||
*/
|
||||
#include "../cxx/js_engine.hpp"
|
||||
#include <flutter/standard_method_codec.h>
|
||||
#include <variant>
|
||||
|
||||
namespace qjs
|
||||
{
|
||||
JSValue dartToJs(JSContext *ctx, flutter::EncodableValue val)
|
||||
{
|
||||
if (val.IsNull())
|
||||
return JS_UNDEFINED;
|
||||
if (std::holds_alternative<bool>(val))
|
||||
return JS_NewBool(ctx, std::get<bool>(val));
|
||||
if (std::holds_alternative<int32_t>(val))
|
||||
return JS_NewInt32(ctx, std::get<int32_t>(val));
|
||||
if (std::holds_alternative<int64_t>(val))
|
||||
return JS_NewInt64(ctx, std::get<int64_t>(val));
|
||||
if (std::holds_alternative<double>(val))
|
||||
return JS_NewFloat64(ctx, std::get<double>(val));
|
||||
if (std::holds_alternative<std::string>(val))
|
||||
return JS_NewString(ctx, std::get<std::string>(val).c_str());
|
||||
if (std::holds_alternative<std::vector<uint8_t>>(val))
|
||||
{
|
||||
auto buf = std::get<std::vector<uint8_t>>(val);
|
||||
return JS_NewArrayBufferCopy(ctx, buf.data(), buf.size());
|
||||
}
|
||||
if (std::holds_alternative<std::vector<int32_t>>(val))
|
||||
{
|
||||
auto buf = std::get<std::vector<int32_t>>(val);
|
||||
return JS_NewArrayBufferCopy(ctx, (uint8_t *)buf.data(), buf.size() * 4);
|
||||
}
|
||||
if (std::holds_alternative<std::vector<int64_t>>(val))
|
||||
{
|
||||
auto buf = std::get<std::vector<int64_t>>(val);
|
||||
return JS_NewArrayBufferCopy(ctx, (uint8_t *)buf.data(), buf.size() * 8);
|
||||
}
|
||||
if (std::holds_alternative<std::vector<double>>(val))
|
||||
{
|
||||
auto buf = std::get<std::vector<double>>(val);
|
||||
JSValue array = JS_NewArray(ctx);
|
||||
auto size = (uint32_t)buf.size();
|
||||
for (uint32_t i = 0; i < size; ++i)
|
||||
{
|
||||
auto atom = JS_NewAtomUInt32(ctx, i);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, array, atom, JS_NewFloat64(ctx, buf[i]),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
if (std::holds_alternative<flutter::EncodableList>(val))
|
||||
{
|
||||
auto list = std::get<flutter::EncodableList>(val);
|
||||
JSValue array = JS_NewArray(ctx);
|
||||
auto size = (uint32_t)list.size();
|
||||
for (uint32_t i = 0; i < size; i++)
|
||||
{
|
||||
auto atom = JS_NewAtomUInt32(ctx, i);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, array, atom, dartToJs(ctx, list[i]),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
if (std::holds_alternative<flutter::EncodableMap>(val))
|
||||
{
|
||||
auto map = std::get<flutter::EncodableMap>(val);
|
||||
JSValue obj = JS_NewObject(ctx);
|
||||
for (auto iter = map.begin(); iter != map.end(); ++iter)
|
||||
{
|
||||
auto atomvalue = dartToJs(ctx, iter->first);
|
||||
auto atom = JS_ValueToAtom(ctx, atomvalue);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, obj, atom, dartToJs(ctx, iter->second),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
JS_FreeValue(ctx, atomvalue);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
flutter::EncodableValue jsToDart(Value val, std::unordered_map<Value, flutter::EncodableValue> cache = std::unordered_map<Value, flutter::EncodableValue>())
|
||||
{
|
||||
int tag = JS_VALUE_GET_TAG(val.v);
|
||||
if (JS_TAG_IS_FLOAT64(tag))
|
||||
return (double)val;
|
||||
switch (tag)
|
||||
{
|
||||
case JS_TAG_BOOL:
|
||||
return (bool)val;
|
||||
case JS_TAG_INT:
|
||||
return (int64_t)val;
|
||||
case JS_TAG_STRING:
|
||||
return (std::string)val;
|
||||
case JS_TAG_OBJECT:
|
||||
{ // ArrayBuffer
|
||||
size_t size;
|
||||
uint8_t *buf = JS_GetArrayBuffer(val.ctx, &size, val.v);
|
||||
if (buf)
|
||||
return (std::vector<uint8_t>(buf, buf + size));
|
||||
}
|
||||
if (cache.find(val) != cache.end())
|
||||
return cache[val];
|
||||
if (JS_IsFunction(val.ctx, val.v))
|
||||
{
|
||||
flutter::EncodableMap retMap;
|
||||
retMap[std::string("__js_function__")] = (int64_t) new JSValue{js_add_ref(val)};
|
||||
return retMap;
|
||||
}
|
||||
else if (JS_IsArray(val.ctx, val.v) > 0)
|
||||
{
|
||||
flutter::EncodableList retList;
|
||||
cache[val] = retList;
|
||||
uint32_t arrlen = (uint32_t)val["length"];
|
||||
for (uint32_t i = 0; i < arrlen; i++)
|
||||
{
|
||||
retList.push_back(jsToDart(val[i], cache));
|
||||
}
|
||||
return retList;
|
||||
}
|
||||
else
|
||||
{
|
||||
qjs::JSPropertyEnum *ptab;
|
||||
uint32_t plen;
|
||||
if (JS_GetOwnPropertyNames(val.ctx, &ptab, &plen, val.v, -1))
|
||||
return flutter::EncodableValue();
|
||||
flutter::EncodableMap retMap;
|
||||
cache[val] = retMap;
|
||||
for (uint32_t i = 0; i < plen; i++)
|
||||
{
|
||||
retMap[jsToDart({val.ctx, JS_AtomToValue(val.ctx, ptab[i].atom)}, cache)] =
|
||||
jsToDart({val.ctx, JS_GetProperty(val.ctx, val.v, ptab[i].atom)}, cache);
|
||||
JS_FreeAtom(val.ctx, ptab[i].atom);
|
||||
}
|
||||
js_free(val.ctx, ptab);
|
||||
return retMap;
|
||||
}
|
||||
default:
|
||||
return flutter::EncodableValue();
|
||||
}
|
||||
}
|
||||
} // namespace qjs
|
@@ -1,14 +1,16 @@
|
||||
/*
|
||||
* @Description: empty plugin
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-25 21:09:20
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-09-20 16:00:15
|
||||
*/
|
||||
#include "include/flutter_qjs/flutter_qjs_plugin.h"
|
||||
|
||||
// This must be included before many other Windows headers.
|
||||
#include <windows.h>
|
||||
|
||||
#include <flutter/method_channel.h>
|
||||
#include <flutter/plugin_registrar_windows.h>
|
||||
#include <flutter/standard_method_codec.h>
|
||||
#include <flutter/method_result_functions.h>
|
||||
|
||||
#include "dart_js_wrapper.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -21,160 +23,15 @@ namespace
|
||||
FlutterQjsPlugin();
|
||||
|
||||
virtual ~FlutterQjsPlugin();
|
||||
|
||||
private:
|
||||
// Called when a method is called on this plugin's channel from Dart.
|
||||
void HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
|
||||
};
|
||||
|
||||
std::shared_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel;
|
||||
std::promise<qjs::JSFutureReturn> *invokeChannelMethod(std::string name, qjs::Value args, qjs::Engine *engine)
|
||||
{
|
||||
auto promise = new std::promise<qjs::JSFutureReturn>();
|
||||
auto map = new flutter::EncodableMap();
|
||||
(*map)[std::string("engine")] = (int64_t)engine;
|
||||
(*map)[std::string("args")] = qjs::jsToDart(args);
|
||||
channel->InvokeMethod(
|
||||
name,
|
||||
std::make_unique<flutter::EncodableValue>(*map),
|
||||
std::make_unique<flutter::MethodResultFunctions<flutter::EncodableValue>>(
|
||||
(flutter::ResultHandlerSuccess<flutter::EncodableValue>)[promise](
|
||||
const flutter::EncodableValue *result) {
|
||||
promise->set_value((qjs::JSFutureReturn)[result = result ? *result : flutter::EncodableValue()](qjs::JSContext * ctx) {
|
||||
qjs::JSValue *ret = new qjs::JSValue{qjs::dartToJs(ctx, result)};
|
||||
return qjs::JSOSFutureArgv{1, ret};
|
||||
});
|
||||
},
|
||||
(flutter::ResultHandlerError<flutter::EncodableValue>)[promise](
|
||||
const std::string &error_code,
|
||||
const std::string &error_message,
|
||||
const flutter::EncodableValue *error_details) {
|
||||
promise->set_value((qjs::JSFutureReturn)[error_message](qjs::JSContext * ctx) {
|
||||
qjs::JSValue *ret = new qjs::JSValue{JS_NewString(ctx, error_message.c_str())};
|
||||
return qjs::JSOSFutureArgv{-1, ret};
|
||||
});
|
||||
},
|
||||
(flutter::ResultHandlerNotImplemented<flutter::EncodableValue>)[promise]() {
|
||||
promise->set_value((qjs::JSFutureReturn)[](qjs::JSContext * ctx) {
|
||||
qjs::JSValue *ret = new qjs::JSValue{JS_NewString(ctx, "NotImplemented")};
|
||||
return qjs::JSOSFutureArgv{-1, ret};
|
||||
});
|
||||
}));
|
||||
return promise;
|
||||
}
|
||||
|
||||
// static
|
||||
void FlutterQjsPlugin::RegisterWithRegistrar(
|
||||
flutter::PluginRegistrarWindows *registrar)
|
||||
{
|
||||
channel =
|
||||
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
|
||||
registrar->messenger(), "soko.ekibun.flutter_qjs",
|
||||
&flutter::StandardMethodCodec::GetInstance());
|
||||
|
||||
auto plugin = std::make_unique<FlutterQjsPlugin>();
|
||||
|
||||
channel->SetMethodCallHandler(
|
||||
[plugin_pointer = plugin.get()](const auto &call, auto result) {
|
||||
plugin_pointer->HandleMethodCall(call, std::move(result));
|
||||
});
|
||||
|
||||
registrar->AddPlugin(std::move(plugin));
|
||||
}
|
||||
flutter::PluginRegistrarWindows *registrar) {}
|
||||
|
||||
FlutterQjsPlugin::FlutterQjsPlugin() {}
|
||||
|
||||
FlutterQjsPlugin::~FlutterQjsPlugin() {}
|
||||
|
||||
const flutter::EncodableValue &ValueOrNull(const flutter::EncodableMap &map, const char *key)
|
||||
{
|
||||
static flutter::EncodableValue null_value;
|
||||
auto it = map.find(flutter::EncodableValue(key));
|
||||
if (it == map.end())
|
||||
{
|
||||
return null_value;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void FlutterQjsPlugin::HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
||||
{
|
||||
// Replace "getPlatformVersion" check with your plugin's method.
|
||||
// See:
|
||||
// https://github.com/flutter/engine/tree/master/shell/platform/common/cpp/client_wrapper/include/flutter
|
||||
// and
|
||||
// https://github.com/flutter/engine/tree/master/shell/platform/glfw/client_wrapper/include/flutter
|
||||
// for the relevant Flutter APIs.
|
||||
if (method_call.method_name().compare("createEngine") == 0)
|
||||
{
|
||||
qjs::Engine *engine = new qjs::Engine(invokeChannelMethod);
|
||||
flutter::EncodableValue response = (int64_t)engine;
|
||||
result->Success(&response);
|
||||
}
|
||||
else if (method_call.method_name().compare("evaluate") == 0)
|
||||
{
|
||||
flutter::EncodableMap args = *std::get_if<flutter::EncodableMap>(method_call.arguments());
|
||||
qjs::Engine *engine = (qjs::Engine *)std::get<int64_t>(ValueOrNull(args, "engine"));
|
||||
std::string script = std::get<std::string>(ValueOrNull(args, "script"));
|
||||
std::string name = std::get<std::string>(ValueOrNull(args, "name"));
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> presult = std::move(result);
|
||||
engine->commit(qjs::EngineTask{
|
||||
[script, name](qjs::Context &ctx) {
|
||||
return ctx.eval(script, name.c_str(), JS_EVAL_TYPE_GLOBAL);
|
||||
},
|
||||
[presult](qjs::Value resolve) {
|
||||
flutter::EncodableValue response = qjs::jsToDart(resolve);
|
||||
presult->Success(&response);
|
||||
},
|
||||
[presult](qjs::Value reject) {
|
||||
presult->Error("FlutterJSException", qjs::getStackTrack(reject));
|
||||
}});
|
||||
}
|
||||
else if (method_call.method_name().compare("call") == 0)
|
||||
{
|
||||
flutter::EncodableMap args = *std::get_if<flutter::EncodableMap>(method_call.arguments());
|
||||
qjs::Engine *engine = (qjs::Engine *)std::get<int64_t>(ValueOrNull(args, "engine"));
|
||||
qjs::JSValue *function = (qjs::JSValue *)std::get<int64_t>(ValueOrNull(args, "function"));
|
||||
flutter::EncodableList arguments = std::get<flutter::EncodableList>(ValueOrNull(args, "arguments"));
|
||||
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> presult = std::move(result);
|
||||
engine->commit(qjs::EngineTask{
|
||||
[function, arguments](qjs::Context &ctx) {
|
||||
size_t argscount = arguments.size();
|
||||
qjs::JSValue *callargs = new qjs::JSValue[argscount];
|
||||
for (size_t i = 0; i < argscount; i++)
|
||||
{
|
||||
callargs[i] = qjs::dartToJs(ctx.ctx, arguments[i]);
|
||||
}
|
||||
qjs::JSValue ret = qjs::call_handler(ctx.ctx, *function, (int)argscount, callargs);
|
||||
delete[] callargs;
|
||||
if (qjs::JS_IsException(ret))
|
||||
throw qjs::exception{};
|
||||
return qjs::Value{ctx.ctx, ret};
|
||||
},
|
||||
[presult](qjs::Value resolve) {
|
||||
flutter::EncodableValue response = qjs::jsToDart(resolve);
|
||||
presult->Success(&response);
|
||||
},
|
||||
[presult](qjs::Value reject) {
|
||||
presult->Error("FlutterJSException", qjs::getStackTrack(reject));
|
||||
}});
|
||||
}
|
||||
else if (method_call.method_name().compare("close") == 0)
|
||||
{
|
||||
qjs::Engine *engine = (qjs::Engine *)*std::get_if<int64_t>(method_call.arguments());
|
||||
delete engine;
|
||||
result->Success();
|
||||
}
|
||||
else
|
||||
{
|
||||
result->NotImplemented();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void FlutterQjsPluginRegisterWithRegistrar(
|
||||
|
Reference in New Issue
Block a user