convert js Map to dart Map

This commit is contained in:
wgh19
2024-05-03 11:53:16 +08:00
parent 2c7d60cc1c
commit 022ab462c6
9 changed files with 71 additions and 8 deletions

View File

@@ -320,6 +320,11 @@ extern "C"
return JS_IsArray(ctx, *val); 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) DLLEXPORT int32_t jsIsError(JSContext *ctx, JSValueConst *val)
{ {
return JS_IsError(ctx, *val); return JS_IsError(ctx, *val);

View File

@@ -97,6 +97,8 @@ extern "C"
DLLEXPORT int32_t jsIsArray(JSContext *ctx, JSValueConst *val); 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 int32_t jsIsError(JSContext *ctx, JSValueConst *val);
DLLEXPORT JSValue *jsNewError(JSContext *ctx); DLLEXPORT JSValue *jsNewError(JSContext *ctx);

View File

@@ -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) static double js_pow(double a, double b)
{ {
if (unlikely(!isfinite(b)) && fabs(a) == 1) { 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); 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 = { static const JSClassExoticMethods js_proxy_exotic_methods = {
.get_own_property = js_proxy_get_own_property, .get_own_property = js_proxy_get_own_property,
.define_own_property = js_proxy_define_own_property, .define_own_property = js_proxy_define_own_property,

View File

@@ -738,6 +738,7 @@ JS_BOOL JS_SetConstructorBit(JSContext *ctx, JSValueConst func_obj, JS_BOOL val)
JSValue JS_NewArray(JSContext *ctx); JSValue JS_NewArray(JSContext *ctx);
int JS_IsArray(JSContext *ctx, JSValueConst val); int JS_IsArray(JSContext *ctx, JSValueConst val);
int JS_IsMap(JSContext *ctx, JSValueConst val);
JSValue JS_NewDate(JSContext *ctx, double epoch_ms); JSValue JS_NewDate(JSContext *ctx, double epoch_ms);

View File

@@ -5,8 +5,6 @@
import FlutterMacOS import FlutterMacOS
import Foundation import Foundation
import flutter_qjs
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterQjsPlugin.register(with: registry.registrar(forPlugin: "FlutterQjsPlugin"))
} }

View File

@@ -51,25 +51,25 @@ class FlutterQjs {
switch (type) { switch (type) {
case JSChannelType.METHON: case JSChannelType.METHON:
final pdata = ptr.cast<Pointer<JSValue>>(); 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 = []; final pargs = [];
for (var i = 0; i < argc; ++i) { for (var i = 0; i < argc; ++i) {
pargs.add(_jsToDart( pargs.add(_jsToDart(
ctx, ctx,
Pointer.fromAddress( Pointer.fromAddress(
pdata.elementAt(2).value.address + sizeOfJSValue * i, (pdata + 2).value.address + sizeOfJSValue * i,
), ),
)); ));
} }
final JSInvokable func = _jsToDart( final JSInvokable func = _jsToDart(
ctx, ctx,
pdata.elementAt(3).value, (pdata + 3).value,
); );
return _dartToJs( return _dartToJs(
ctx, ctx,
func.invoke( func.invoke(
pargs, pargs,
_jsToDart(ctx, pdata.elementAt(0).value), _jsToDart(ctx, pdata.value),
)); ));
case JSChannelType.MODULE: case JSChannelType.MODULE:
if (moduleHandler == null) throw JSError('No ModuleHandler'); if (moduleHandler == null) throw JSError('No ModuleHandler');

View File

@@ -759,6 +759,19 @@ final int Function(
)>>('jsIsArray') )>>('jsIsArray')
.asFunction(); .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); /// DLLEXPORT int32_t jsIsError(JSContext *ctx, JSValueConst *val);
final int Function( final int Function(
Pointer<JSContext> ctx, Pointer<JSContext> ctx,

View File

@@ -192,7 +192,7 @@ class _JSFunction extends _JSObject implements JSInvokable, _IsolateEncodable {
} }
@override @override
call(List<dynamic> args) => invoke(args); call(List<dynamic> args, [dynamic thisVal]) => invoke(args, thisVal);
} }
/// Dart function wrapper for isolate /// Dart function wrapper for isolate

View File

@@ -218,6 +218,19 @@ dynamic _jsToDart(Pointer<JSContext> ctx, Pointer<JSValue> val,
jsFreeValue(ctx, jsProp); jsFreeValue(ctx, jsProp);
} }
return ret; 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{ } else{
final ptab = malloc<Pointer<JSPropertyEnum>>(); final ptab = malloc<Pointer<JSPropertyEnum>>();
final plen = malloc<Uint32>(); final plen = malloc<Uint32>();