mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
add function channel
This commit is contained in:
176
windows/dart_js_wrapper.hpp
Normal file
176
windows/dart_js_wrapper.hpp
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-14 21:45:02
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-15 14:08:04
|
||||
*/
|
||||
#include "../cxx/js_engine.hpp"
|
||||
#include <flutter/standard_method_codec.h>
|
||||
#include <variant>
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct hash<qjs::Value>
|
||||
{
|
||||
std::size_t operator()(const qjs::Value &key) const
|
||||
{
|
||||
return std::hash<std::string>()((std::string)key);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct hash<flutter::EncodableValue>
|
||||
{
|
||||
std::size_t operator()(const flutter::EncodableValue &key) const
|
||||
{
|
||||
return key.index();
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
namespace qjs
|
||||
{
|
||||
JSValue dartToJsAtom(JSContext *ctx, flutter::EncodableValue val)
|
||||
{
|
||||
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());
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
JSValue dartToJs(JSContext *ctx, flutter::EncodableValue val, std::unordered_map<flutter::EncodableValue, JSValue> cache)
|
||||
{
|
||||
if (val.IsNull())
|
||||
return JS_UNDEFINED;
|
||||
if (cache.find(val) != cache.end())
|
||||
return cache[val];
|
||||
{
|
||||
JSValue atomValue = dartToJsAtom(ctx, val);
|
||||
if (!JS_IsUndefined(atomValue))
|
||||
return atomValue;
|
||||
}
|
||||
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);
|
||||
cache[val] = array;
|
||||
auto size = (uint32_t)buf.size();
|
||||
for (uint32_t i = 0; i < size; i++)
|
||||
JS_DefinePropertyValue(
|
||||
ctx, array, JS_NewAtomUInt32(ctx, i), JS_NewFloat64(ctx, buf[i]),
|
||||
JS_PROP_C_W_E);
|
||||
return array;
|
||||
}
|
||||
if (std::holds_alternative<flutter::EncodableList>(val))
|
||||
{
|
||||
auto list = std::get<flutter::EncodableList>(val);
|
||||
JSValue array = JS_NewArray(ctx);
|
||||
cache[val] = array;
|
||||
auto size = (uint32_t)list.size();
|
||||
for (uint32_t i = 0; i < size; i++)
|
||||
JS_DefinePropertyValue(
|
||||
ctx, array, JS_NewAtomUInt32(ctx, i), dartToJs(ctx, list[i], cache),
|
||||
JS_PROP_C_W_E);
|
||||
return array;
|
||||
}
|
||||
if (std::holds_alternative<flutter::EncodableMap>(val))
|
||||
{
|
||||
auto map = std::get<flutter::EncodableMap>(val);
|
||||
JSValue obj = JS_NewObject(ctx);
|
||||
cache[val] = obj;
|
||||
for (auto iter = map.begin(); iter != map.end(); ++iter)
|
||||
JS_DefinePropertyValue(
|
||||
ctx, obj, JS_ValueToAtom(ctx, dartToJs(ctx, iter->first, cache)), dartToJs(ctx, iter->second, cache),
|
||||
JS_PROP_C_W_E);
|
||||
return obj;
|
||||
}
|
||||
return JS_UNDEFINED;
|
||||
}
|
||||
|
||||
flutter::EncodableValue jsToDart(Value val, std::unordered_map<Value, flutter::EncodableValue> cache)
|
||||
{
|
||||
if (cache.find(val) != cache.end())
|
||||
return cache[val];
|
||||
if (JS_IsBool(val.v))
|
||||
return (bool)val;
|
||||
if (JS_IsNumber(val.v))
|
||||
return (double)val;
|
||||
if (JS_IsString(val.v))
|
||||
return (std::string)val;
|
||||
{ // ArrayBuffer
|
||||
size_t size;
|
||||
uint8_t *buf = JS_GetArrayBuffer(val.ctx, &size, val.v);
|
||||
if (buf)
|
||||
return (std::vector<uint8_t>(buf, buf + size));
|
||||
}
|
||||
flutter::EncodableValue ret;
|
||||
if (JS_IsUndefined(val.v) || JS_IsNull(val.v) || JS_IsUninitialized(val.v))
|
||||
goto exception;
|
||||
if (JS_IsObject(val.v))
|
||||
{
|
||||
if (JS_IsFunction(val.ctx, val.v))
|
||||
{
|
||||
flutter::EncodableMap retMap;
|
||||
retMap[std::string("__js_function__")] = (int64_t) new JSValue { JS_DupValue(val.ctx, val.v) };
|
||||
ret = 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));
|
||||
}
|
||||
ret = retList;
|
||||
}
|
||||
else
|
||||
{
|
||||
qjs::JSPropertyEnum *ptab;
|
||||
uint32_t plen;
|
||||
if (JS_GetOwnPropertyNames(val.ctx, &ptab, &plen, val.v, -1))
|
||||
goto exception;
|
||||
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);
|
||||
ret = retMap;
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
exception:
|
||||
ret = flutter::EncodableValue();
|
||||
done:
|
||||
return ret;
|
||||
}
|
||||
} // namespace qjs
|
@@ -8,7 +8,7 @@
|
||||
#include <flutter/standard_method_codec.h>
|
||||
#include <flutter/method_result_functions.h>
|
||||
|
||||
#include "../cxx/js_engine.hpp"
|
||||
#include "dart_js_wrapper.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -30,17 +30,17 @@ namespace
|
||||
};
|
||||
|
||||
std::shared_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel;
|
||||
std::future<qjs::JSFutureReturn> invokeChannelMethod(std::string name, std::string args)
|
||||
std::promise<qjs::JSFutureReturn> *invokeChannelMethod(std::string name, qjs::Value args)
|
||||
{
|
||||
auto promise = new std::promise<qjs::JSFutureReturn>();
|
||||
channel->InvokeMethod(
|
||||
name,
|
||||
std::make_unique<flutter::EncodableValue>(args),
|
||||
std::make_unique<flutter::EncodableValue>(qjs::jsToDart(args, std::unordered_map<qjs::Value, flutter::EncodableValue>())),
|
||||
std::make_unique<flutter::MethodResultFunctions<flutter::EncodableValue>>(
|
||||
(flutter::ResultHandlerSuccess<flutter::EncodableValue>)[promise](
|
||||
const flutter::EncodableValue *result) {
|
||||
promise->set_value((qjs::JSFutureReturn)[rep = std::get<std::string>(*result)](qjs::JSContext * ctx) {
|
||||
qjs::JSValue *ret = new qjs::JSValue{JS_NewString(ctx, rep.c_str())};
|
||||
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>())};
|
||||
return qjs::JSOSFutureArgv{1, ret};
|
||||
});
|
||||
},
|
||||
@@ -59,7 +59,7 @@ namespace
|
||||
return qjs::JSOSFutureArgv{-1, ret};
|
||||
});
|
||||
}));
|
||||
return promise->get_future();
|
||||
return promise;
|
||||
}
|
||||
|
||||
// static
|
||||
@@ -111,7 +111,7 @@ namespace
|
||||
if (method_call.method_name().compare("initEngine") == 0)
|
||||
{
|
||||
engine = new qjs::Engine((qjs::DartChannel)invokeChannelMethod);
|
||||
flutter::EncodableValue response((long)engine);
|
||||
flutter::EncodableValue response = (int64_t)engine;
|
||||
result->Success(&response);
|
||||
}
|
||||
else if (method_call.method_name().compare("evaluate") == 0)
|
||||
@@ -121,9 +121,39 @@ namespace
|
||||
std::string name = std::get<std::string>(ValueOrNull(args, "name"));
|
||||
auto presult = result.release();
|
||||
engine->commit(qjs::EngineTask{
|
||||
script, name,
|
||||
[script, name](qjs::Context &ctx) {
|
||||
return ctx.eval(script, name.c_str(), JS_EVAL_TYPE_GLOBAL);
|
||||
},
|
||||
[presult](std::string resolve) {
|
||||
flutter::EncodableValue response(resolve);
|
||||
flutter::EncodableValue response = resolve;
|
||||
presult->Success(&response);
|
||||
},
|
||||
[presult](std::string reject) {
|
||||
presult->Error("FlutterJSException", reject);
|
||||
}});
|
||||
}
|
||||
else if (method_call.method_name().compare("call") == 0)
|
||||
{
|
||||
flutter::EncodableMap args = *std::get_if<flutter::EncodableMap>(method_call.arguments());
|
||||
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();
|
||||
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], std::unordered_map<flutter::EncodableValue, qjs::JSValue>());
|
||||
}
|
||||
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);
|
||||
if (qjs::JS_IsException(ret))
|
||||
throw qjs::exception{};
|
||||
return qjs::Value{ctx.ctx, ret};
|
||||
},
|
||||
[presult](std::string resolve) {
|
||||
flutter::EncodableValue response = resolve;
|
||||
presult->Success(&response);
|
||||
},
|
||||
[presult](std::string reject) {
|
||||
|
Reference in New Issue
Block a user