mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
multi engine
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-14 21:45:02
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-15 14:08:04
|
||||
* @LastEditTime: 2020-08-15 15:42:55
|
||||
*/
|
||||
#include "../cxx/js_engine.hpp"
|
||||
#include <flutter/standard_method_codec.h>
|
||||
@@ -47,7 +47,7 @@ namespace qjs
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
JSValue dartToJs(JSContext *ctx, flutter::EncodableValue val, std::unordered_map<flutter::EncodableValue, JSValue> cache)
|
||||
JSValue dartToJs(JSContext *ctx, flutter::EncodableValue val, std::unordered_map<flutter::EncodableValue, JSValue> cache = std::unordered_map<flutter::EncodableValue, JSValue>())
|
||||
{
|
||||
if (val.IsNull())
|
||||
return JS_UNDEFINED;
|
||||
@@ -111,7 +111,7 @@ namespace qjs
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
flutter::EncodableValue jsToDart(Value val, std::unordered_map<Value, flutter::EncodableValue> cache)
|
||||
flutter::EncodableValue jsToDart(Value val, std::unordered_map<Value, flutter::EncodableValue> cache = std::unordered_map<Value, flutter::EncodableValue>())
|
||||
{
|
||||
if (cache.find(val) != cache.end())
|
||||
return cache[val];
|
||||
|
@@ -30,17 +30,20 @@ namespace
|
||||
};
|
||||
|
||||
std::shared_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel;
|
||||
std::promise<qjs::JSFutureReturn> *invokeChannelMethod(std::string name, qjs::Value args)
|
||||
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, std::unordered_map<qjs::Value, flutter::EncodableValue>());
|
||||
channel->InvokeMethod(
|
||||
name,
|
||||
std::make_unique<flutter::EncodableValue>(qjs::jsToDart(args, std::unordered_map<qjs::Value, flutter::EncodableValue>())),
|
||||
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, std::unordered_map<flutter::EncodableValue, qjs::JSValue>())};
|
||||
qjs::JSValue *ret = new qjs::JSValue{qjs::dartToJs(ctx, result)};
|
||||
return qjs::JSOSFutureArgv{1, ret};
|
||||
});
|
||||
},
|
||||
@@ -96,8 +99,6 @@ namespace
|
||||
return it->second;
|
||||
}
|
||||
|
||||
qjs::Engine *engine = nullptr;
|
||||
|
||||
void FlutterQjsPlugin::HandleMethodCall(
|
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call,
|
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result)
|
||||
@@ -108,15 +109,16 @@ namespace
|
||||
// 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("initEngine") == 0)
|
||||
if (method_call.method_name().compare("createEngine") == 0)
|
||||
{
|
||||
engine = new qjs::Engine((qjs::DartChannel)invokeChannelMethod);
|
||||
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"));
|
||||
auto presult = result.release();
|
||||
@@ -124,17 +126,18 @@ namespace
|
||||
[script, name](qjs::Context &ctx) {
|
||||
return ctx.eval(script, name.c_str(), JS_EVAL_TYPE_GLOBAL);
|
||||
},
|
||||
[presult](std::string resolve) {
|
||||
flutter::EncodableValue response = resolve;
|
||||
[presult](qjs::Value resolve) {
|
||||
flutter::EncodableValue response = qjs::jsToDart(resolve);
|
||||
presult->Success(&response);
|
||||
},
|
||||
[presult](std::string reject) {
|
||||
presult->Error("FlutterJSException", reject);
|
||||
[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"));
|
||||
auto presult = result.release();
|
||||
@@ -144,7 +147,7 @@ namespace
|
||||
qjs::JSValue *callargs = new qjs::JSValue[argscount];
|
||||
for (size_t i = 0; i < argscount; i++)
|
||||
{
|
||||
callargs[i] = qjs::dartToJs(ctx.ctx, arguments[i], std::unordered_map<flutter::EncodableValue, qjs::JSValue>());
|
||||
callargs[i] = qjs::dartToJs(ctx.ctx, arguments[i]);
|
||||
}
|
||||
qjs::JSValue ret = JS_Call(ctx.ctx, *function, qjs::JSValue{qjs::JSValueUnion{0}, qjs::JS_TAG_UNDEFINED}, (int)argscount, callargs);
|
||||
qjs::JS_FreeValue(ctx.ctx, *function);
|
||||
@@ -152,16 +155,17 @@ namespace
|
||||
throw qjs::exception{};
|
||||
return qjs::Value{ctx.ctx, ret};
|
||||
},
|
||||
[presult](std::string resolve) {
|
||||
flutter::EncodableValue response = resolve;
|
||||
[presult](qjs::Value resolve) {
|
||||
flutter::EncodableValue response = qjs::jsToDart(resolve);
|
||||
presult->Success(&response);
|
||||
},
|
||||
[presult](std::string reject) {
|
||||
presult->Error("FlutterJSException", reject);
|
||||
[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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user