mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
convert js Map to dart Map
This commit is contained in:
@@ -320,6 +320,11 @@ extern "C"
|
||||
return JS_IsArray(ctx, *val);
|
||||
}
|
||||
|
||||
DLLEXPORT int32_t jsIsMap(JSContext *ctx, JSValueConst *val)
|
||||
{
|
||||
return JS_IsMap(ctx, *val);
|
||||
}
|
||||
|
||||
DLLEXPORT int32_t jsIsError(JSContext *ctx, JSValueConst *val)
|
||||
{
|
||||
return JS_IsError(ctx, *val);
|
||||
|
@@ -97,6 +97,8 @@ extern "C"
|
||||
|
||||
DLLEXPORT int32_t jsIsArray(JSContext *ctx, JSValueConst *val);
|
||||
|
||||
DLLEXPORT int32_t jsIsMap(JSContext *ctx, JSValueConst *val);
|
||||
|
||||
DLLEXPORT int32_t jsIsError(JSContext *ctx, JSValueConst *val);
|
||||
|
||||
DLLEXPORT JSValue *jsNewError(JSContext *ctx);
|
||||
|
@@ -12148,6 +12148,21 @@ int JS_IsArray(JSContext *ctx, JSValueConst val)
|
||||
}
|
||||
}
|
||||
|
||||
/* return -1 if exception (proxy case) or TRUE/FALSE */
|
||||
int JS_IsMap(JSContext *ctx, JSValueConst val)
|
||||
{
|
||||
JSObject *p;
|
||||
if (JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
|
||||
p = JS_VALUE_GET_OBJ(val);
|
||||
if (unlikely(p->class_id == JS_CLASS_PROXY))
|
||||
return js_proxy_isMap(ctx, val);
|
||||
else
|
||||
return p->class_id == JS_CLASS_MAP;
|
||||
} else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static double js_pow(double a, double b)
|
||||
{
|
||||
if (unlikely(!isfinite(b)) && fabs(a) == 1) {
|
||||
@@ -46709,6 +46724,22 @@ static int js_proxy_isArray(JSContext *ctx, JSValueConst obj)
|
||||
return JS_IsArray(ctx, s->target);
|
||||
}
|
||||
|
||||
static int js_proxy_isMap(JSContext *ctx, JSValueConst obj)
|
||||
{
|
||||
JSProxyData *s = JS_GetOpaque(obj, JS_CLASS_PROXY);
|
||||
if (!s)
|
||||
return FALSE;
|
||||
if (js_check_stack_overflow(ctx->rt, 0)) {
|
||||
JS_ThrowStackOverflow(ctx);
|
||||
return -1;
|
||||
}
|
||||
if (s->is_revoked) {
|
||||
JS_ThrowTypeErrorRevokedProxy(ctx);
|
||||
return -1;
|
||||
}
|
||||
return JS_IsMap(ctx, s->target);
|
||||
}
|
||||
|
||||
static const JSClassExoticMethods js_proxy_exotic_methods = {
|
||||
.get_own_property = js_proxy_get_own_property,
|
||||
.define_own_property = js_proxy_define_own_property,
|
||||
|
@@ -738,6 +738,7 @@ JS_BOOL JS_SetConstructorBit(JSContext *ctx, JSValueConst func_obj, JS_BOOL val)
|
||||
|
||||
JSValue JS_NewArray(JSContext *ctx);
|
||||
int JS_IsArray(JSContext *ctx, JSValueConst val);
|
||||
int JS_IsMap(JSContext *ctx, JSValueConst val);
|
||||
|
||||
JSValue JS_NewDate(JSContext *ctx, double epoch_ms);
|
||||
|
||||
|
@@ -5,8 +5,6 @@
|
||||
import FlutterMacOS
|
||||
import Foundation
|
||||
|
||||
import flutter_qjs
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
FlutterQjsPlugin.register(with: registry.registrar(forPlugin: "FlutterQjsPlugin"))
|
||||
}
|
||||
|
@@ -51,25 +51,25 @@ class FlutterQjs {
|
||||
switch (type) {
|
||||
case JSChannelType.METHON:
|
||||
final pdata = ptr.cast<Pointer<JSValue>>();
|
||||
final argc = pdata.elementAt(1).value.cast<Int32>().value;
|
||||
final argc = (pdata + 1).value.cast<Int32>().value;
|
||||
final pargs = [];
|
||||
for (var i = 0; i < argc; ++i) {
|
||||
pargs.add(_jsToDart(
|
||||
ctx,
|
||||
Pointer.fromAddress(
|
||||
pdata.elementAt(2).value.address + sizeOfJSValue * i,
|
||||
(pdata + 2).value.address + sizeOfJSValue * i,
|
||||
),
|
||||
));
|
||||
}
|
||||
final JSInvokable func = _jsToDart(
|
||||
ctx,
|
||||
pdata.elementAt(3).value,
|
||||
(pdata + 3).value,
|
||||
);
|
||||
return _dartToJs(
|
||||
ctx,
|
||||
func.invoke(
|
||||
pargs,
|
||||
_jsToDart(ctx, pdata.elementAt(0).value),
|
||||
_jsToDart(ctx, pdata.value),
|
||||
));
|
||||
case JSChannelType.MODULE:
|
||||
if (moduleHandler == null) throw JSError('No ModuleHandler');
|
||||
|
@@ -759,6 +759,19 @@ final int Function(
|
||||
)>>('jsIsArray')
|
||||
.asFunction();
|
||||
|
||||
/// int32_t jsIsMap(JSContext *ctx, JSValueConst *val)
|
||||
final int Function(
|
||||
Pointer<JSContext> ctx,
|
||||
Pointer<JSValue> val,
|
||||
) jsIsMap = _qjsLib
|
||||
.lookup<
|
||||
NativeFunction<
|
||||
Int32 Function(
|
||||
Pointer<JSContext>,
|
||||
Pointer<JSValue>,
|
||||
)>>('jsIsMap')
|
||||
.asFunction();
|
||||
|
||||
/// DLLEXPORT int32_t jsIsError(JSContext *ctx, JSValueConst *val);
|
||||
final int Function(
|
||||
Pointer<JSContext> ctx,
|
||||
|
@@ -192,7 +192,7 @@ class _JSFunction extends _JSObject implements JSInvokable, _IsolateEncodable {
|
||||
}
|
||||
|
||||
@override
|
||||
call(List<dynamic> args) => invoke(args);
|
||||
call(List<dynamic> args, [dynamic thisVal]) => invoke(args, thisVal);
|
||||
}
|
||||
|
||||
/// Dart function wrapper for isolate
|
||||
|
@@ -218,6 +218,19 @@ dynamic _jsToDart(Pointer<JSContext> ctx, Pointer<JSValue> val,
|
||||
jsFreeValue(ctx, jsProp);
|
||||
}
|
||||
return ret;
|
||||
} else if(jsIsMap(ctx, val) != 0) {
|
||||
final map = Map();
|
||||
final jsMap = _JSObject(ctx, val);
|
||||
void callback(key, value) {
|
||||
map[key] = value;
|
||||
};
|
||||
var jsFunc = jsEval(ctx, "(m, callback) => { m.forEach((value, key, _) => { callback(key, value) }) }", "eval", JSEvalFlag.GLOBAL);
|
||||
var func = _jsToDart(ctx, jsFunc) as JSInvokable;
|
||||
jsFreeValue(ctx, jsFunc);
|
||||
func.invoke([jsMap, callback]);
|
||||
jsMap.destroy();
|
||||
func.destroy();
|
||||
return map;
|
||||
} else{
|
||||
final ptab = malloc<Pointer<JSPropertyEnum>>();
|
||||
final plen = malloc<Uint32>();
|
||||
|
Reference in New Issue
Block a user