add call method to JSInvokable;

Convert js object to dart Map<String, dynamic>
This commit is contained in:
wgh19
2024-04-20 17:56:36 +08:00
parent 9ecfe2bf6c
commit 67dd0dff9b
2 changed files with 9 additions and 3 deletions

View File

@@ -11,6 +11,8 @@ part of '../flutter_qjs.dart';
abstract class JSInvokable extends JSRef {
dynamic invoke(List args, [dynamic thisVal]);
dynamic call(List args) => invoke(args);
static dynamic _wrap(dynamic func) {
return func is JSInvokable
? func
@@ -188,6 +190,9 @@ class _JSFunction extends _JSObject implements JSInvokable, _IsolateEncodable {
Map _encode() {
return IsolateFunction._new(this)._encode();
}
@override
call(List<dynamic> args) => invoke(args);
}
/// Dart function wrapper for isolate

View File

@@ -197,7 +197,8 @@ dynamic _jsToDart(Pointer<JSContext> ctx, Pointer<JSValue> val,
},
(e) {
JSRef.dupRecursive(e);
if (!completer.isCompleted) completer.completeError(e);
// `throw null` is allowed in js
if (!completer.isCompleted) completer.completeError(e ?? "null");
},
], jsPromise);
jsPromise.free();
@@ -227,13 +228,13 @@ dynamic _jsToDart(Pointer<JSContext> ctx, Pointer<JSValue> val,
}
final len = plen.value;
malloc.free(plen);
final ret = Map();
final ret = Map<String, dynamic>();
cache[valptr] = ret;
for (var i = 0; i < len; ++i) {
final jsAtom = jsPropertyEnumGetAtom(ptab.value, i);
final jsAtomValue = jsAtomToValue(ctx, jsAtom);
final jsProp = jsGetProperty(ctx, val, jsAtom);
ret[_jsToDart(ctx, jsAtomValue, cache: cache)] =
ret[_jsToDart(ctx, jsAtomValue, cache: cache).toString()] =
_jsToDart(ctx, jsProp, cache: cache);
jsFreeValue(ctx, jsAtomValue);
jsFreeValue(ctx, jsProp);