setToGlobalObject

This commit is contained in:
ekibun
2021-01-24 16:43:19 +08:00
parent 1589f87194
commit 6b0bab2faf
9 changed files with 228 additions and 142 deletions

View File

@@ -14,31 +14,32 @@ import 'package:flutter_qjs/flutter_qjs.dart';
import 'package:flutter_qjs/isolate.dart';
import 'package:flutter_test/flutter_test.dart';
dynamic myMethodHandler(method, args) {
return args;
dynamic myMethodHandler(List args, {String thisVal}) {
return [thisVal, ...args];
}
Future testEvaluate(qjs) async {
var value = await qjs.evaluate("""
final value = await qjs.evaluate("""
const a = {};
a.a = a;
import('test').then((module) => channel('channel', [
import('test').then((module) => channel.call('this', [
(...args)=>`hello \${args}!`, a,
Promise.reject('test Promise.reject'), Promise.resolve('test Promise.resolve'),
0.1, true, false, 1, "world", module
]));
""", name: "<eval>");
expect(await value[0]('world'), 'hello world!', reason: "js function call");
expect(value[1]['a'], value[1], reason: "recursive object");
expect(value[2], isInstanceOf<Future>(), reason: "promise object");
expect(value[0], 'this', reason: "js function this");
expect(await value[1]('world'), 'hello world!', reason: "js function call");
expect(value[2]['a'], value[2], reason: "recursive object");
expect(value[3], isInstanceOf<Future>(), reason: "promise object");
try {
await value[2];
await value[3];
throw 'Future not reject';
} catch (e) {
expect(e, startsWith('test Promise.reject\n'),
reason: "promise object reject");
}
expect(await value[3], 'test Promise.resolve',
expect(await value[4], 'test Promise.resolve',
reason: "promise object resolve");
}
@@ -96,12 +97,12 @@ void main() async {
});
test('jsToDart', () async {
final qjs = FlutterQjs(
methodHandler: myMethodHandler,
moduleHandler: (name) {
return "export default '${new DateTime.now()}'";
},
hostPromiseRejectionHandler: (_) {},
);
qjs.setToGlobalObject("channel", myMethodHandler);
qjs.dispatch();
await testEvaluate(qjs);
qjs.close();
@@ -109,12 +110,12 @@ void main() async {
test('isolate', () async {
await runZonedGuarded(() async {
final qjs = IsolateQjs(
methodHandler: myMethodHandler,
moduleHandler: (name) async {
return "export default '${new DateTime.now()}'";
},
hostPromiseRejectionHandler: (_) {},
);
await qjs.setToGlobalObject("channel", myMethodHandler);
await testEvaluate(qjs);
qjs.close();
}, (e, stack) {
@@ -122,13 +123,12 @@ void main() async {
});
});
test('dart object', () async {
final qjs = FlutterQjs(
methodHandler: (method, args) {
return FlutterQjs();
},
);
final qjs = FlutterQjs();
qjs.setToGlobalObject("channel", () {
return FlutterQjs();
});
qjs.dispatch();
var value = await qjs.evaluate("channel('channel', [])", name: "<eval>");
var value = await qjs.evaluate("channel()", name: "<eval>");
expect(value, isInstanceOf<FlutterQjs>(), reason: "dart object");
qjs.close();
});