mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
reference leak
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Description: unit test
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-09-06 13:02:46
|
||||
* @LastEditors: ekibun
|
||||
@@ -10,9 +10,6 @@ import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_qjs/flutter_qjs.dart';
|
||||
import 'package:flutter_qjs/isolate.dart';
|
||||
import 'package:flutter_qjs/ffi.dart';
|
||||
import 'package:flutter_qjs/wrapper.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
dynamic myFunction(String args, {thisVal}) {
|
||||
@@ -21,69 +18,72 @@ dynamic myFunction(String args, {thisVal}) {
|
||||
|
||||
Future testEvaluate(qjs) async {
|
||||
final testWrap = await qjs.evaluate(
|
||||
"(a) => a",
|
||||
name: "<testWrap>",
|
||||
'(a) => a',
|
||||
name: '<testWrap>',
|
||||
);
|
||||
final wrapNull = await testWrap(null);
|
||||
expect(wrapNull, null, reason: "wrap null");
|
||||
final primities = [0, 1, 0.1, true, false, "str"];
|
||||
expect(wrapNull, null, reason: 'wrap null');
|
||||
final primities = [0, 1, 0.1, true, false, 'str'];
|
||||
final wrapPrimities = await testWrap(primities);
|
||||
for (var i = 0; i < primities.length; i++) {
|
||||
expect(wrapPrimities[i], primities[i], reason: "wrap primities");
|
||||
for (int i = 0; i < primities.length; i++) {
|
||||
expect(wrapPrimities[i], primities[i], reason: 'wrap primities');
|
||||
}
|
||||
final jsError = JSError("test Error");
|
||||
final jsError = JSError('test Error');
|
||||
final wrapJsError = await testWrap(jsError);
|
||||
expect(jsError.message, (wrapJsError as JSError).message,
|
||||
reason: "wrap JSError");
|
||||
reason: 'wrap JSError');
|
||||
final wrapFunction = await testWrap(testWrap);
|
||||
final testEqual = await qjs.evaluate(
|
||||
"(a, b) => a === b",
|
||||
name: "<testEqual>",
|
||||
'(a, b) => a === b',
|
||||
name: '<testEqual>',
|
||||
);
|
||||
expect(await testEqual(wrapFunction, testWrap), true,
|
||||
reason: "wrap function");
|
||||
reason: 'wrap function');
|
||||
wrapFunction.release();
|
||||
testEqual.release();
|
||||
|
||||
expect(wrapNull, null, reason: "wrap null");
|
||||
expect(wrapNull, null, reason: 'wrap null');
|
||||
final a = {};
|
||||
a["a"] = a;
|
||||
a['a'] = a;
|
||||
final wrapA = await testWrap(a);
|
||||
expect(wrapA['a'], wrapA, reason: "recursive object");
|
||||
expect(wrapA['a'], wrapA, reason: 'recursive object');
|
||||
final testThis = await qjs.evaluate(
|
||||
"(function (func, arg) { return func.call(this, arg) })",
|
||||
name: "<testThis>",
|
||||
'(function (func, arg) { return func.call(this, arg) })',
|
||||
name: '<testThis>',
|
||||
);
|
||||
final funcRet = await testThis(myFunction, 'arg', thisVal: {'name': 'this'});
|
||||
expect(funcRet[0]['name'], 'this', reason: "js function this");
|
||||
expect(funcRet[1], 'arg', reason: "js function argument");
|
||||
testThis.release();
|
||||
expect(funcRet[0]['name'], 'this', reason: 'js function this');
|
||||
expect(funcRet[1], 'arg', reason: 'js function argument');
|
||||
final promises = await testWrap(await qjs.evaluate(
|
||||
"[Promise.reject('test Promise.reject'), Promise.resolve('test Promise.resolve')]",
|
||||
name: "<promises>",
|
||||
'[Promise.reject("reject"), Promise.resolve("resolve"), new Promise(() => {})]',
|
||||
name: '<promises>',
|
||||
));
|
||||
for (final promise in promises)
|
||||
expect(promise, isInstanceOf<Future>(), reason: "promise object");
|
||||
expect(promise, isInstanceOf<Future>(), reason: 'promise object');
|
||||
try {
|
||||
await promises[0];
|
||||
throw 'Future not reject';
|
||||
} catch (e) {
|
||||
expect(e, 'test Promise.reject', reason: "promise object reject");
|
||||
expect(e, 'reject', reason: 'promise object reject');
|
||||
}
|
||||
expect(await promises[1], 'test Promise.resolve',
|
||||
reason: "promise object resolve");
|
||||
expect(await promises[1], 'resolve', reason: 'promise object resolve');
|
||||
testWrap.release();
|
||||
}
|
||||
|
||||
void main() async {
|
||||
test('make', () async {
|
||||
final utf8Encoding = Encoding.getByName('utf-8');
|
||||
var cmakePath = "cmake";
|
||||
var cmakePath = 'cmake';
|
||||
if (Platform.isWindows) {
|
||||
var vsDir = Directory("C:/Program Files (x86)/Microsoft Visual Studio/");
|
||||
var vsDir = Directory('C:/Program Files (x86)/Microsoft Visual Studio/');
|
||||
vsDir = (vsDir.listSync().firstWhere((e) => e is Directory) as Directory)
|
||||
.listSync()
|
||||
.last as Directory;
|
||||
cmakePath = vsDir.path +
|
||||
"/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe";
|
||||
'/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe';
|
||||
}
|
||||
final buildDir = "./build";
|
||||
final buildDir = './build';
|
||||
var result = Process.runSync(
|
||||
cmakePath,
|
||||
['-S', './', '-B', buildDir],
|
||||
@@ -109,7 +109,7 @@ void main() async {
|
||||
test('module', () async {
|
||||
final qjs = FlutterQjs(
|
||||
moduleHandler: (name) {
|
||||
return "export default 'test module'";
|
||||
return 'export default "test module"';
|
||||
},
|
||||
);
|
||||
qjs.dispatch();
|
||||
@@ -120,14 +120,11 @@ void main() async {
|
||||
};
|
||||
''', name: 'evalModule', evalFlags: JSEvalFlag.MODULE);
|
||||
var result = await qjs.evaluate('import("evalModule")');
|
||||
expect(result['default']['data'], 'test module', reason: "eval module");
|
||||
expect(result['default']['data'], 'test module', reason: 'eval module');
|
||||
qjs.close();
|
||||
});
|
||||
test('data conversion', () async {
|
||||
final qjs = FlutterQjs(
|
||||
moduleHandler: (name) {
|
||||
return "export default '${new DateTime.now()}'";
|
||||
},
|
||||
hostPromiseRejectionHandler: (_) {},
|
||||
);
|
||||
qjs.dispatch();
|
||||
@@ -136,33 +133,43 @@ void main() async {
|
||||
});
|
||||
test('isolate conversion', () async {
|
||||
final qjs = IsolateQjs(
|
||||
moduleHandler: (name) async {
|
||||
return "export default '${new DateTime.now()}'";
|
||||
},
|
||||
hostPromiseRejectionHandler: (_) {},
|
||||
);
|
||||
await testEvaluate(qjs);
|
||||
qjs.close();
|
||||
await qjs.close();
|
||||
});
|
||||
test('isolate bind function', () async {
|
||||
final qjs = IsolateQjs();
|
||||
var localVar;
|
||||
final testFunc = await qjs.evaluate("(func)=>func('ret')", name: "<eval>");
|
||||
final testFunc = await qjs.evaluate('(func)=>func("ret")', name: '<eval>');
|
||||
final testFuncRet = await testFunc(await qjs.bind((args) {
|
||||
localVar = 'test';
|
||||
return args;
|
||||
}));
|
||||
expect(localVar, 'test', reason: "bind function");
|
||||
expect(testFuncRet, 'ret', reason: "bind function args return");
|
||||
qjs.close();
|
||||
testFunc.release();
|
||||
expect(localVar, 'test', reason: 'bind function');
|
||||
expect(testFuncRet, 'ret', reason: 'bind function args return');
|
||||
await qjs.close();
|
||||
});
|
||||
test('reference leak', () async {
|
||||
final qjs = FlutterQjs();
|
||||
await qjs.evaluate('()=>{}', name: '<eval>');
|
||||
try {
|
||||
qjs.close();
|
||||
throw 'Error not throw';
|
||||
} on JSError catch (e) {
|
||||
expect(e.message, startsWith('reference leak:'),
|
||||
reason: 'throw reference leak');
|
||||
}
|
||||
});
|
||||
test('stack overflow', () async {
|
||||
final qjs = FlutterQjs();
|
||||
try {
|
||||
qjs.evaluate("a=()=>a();a();", name: "<eval>");
|
||||
qjs.evaluate('a=()=>a();a();', name: '<eval>');
|
||||
throw 'Error not throw';
|
||||
} on JSError catch (e) {
|
||||
expect(e.message, 'InternalError: stack overflow',
|
||||
reason: "throw stack overflow");
|
||||
reason: 'throw stack overflow');
|
||||
}
|
||||
qjs.close();
|
||||
});
|
||||
@@ -175,13 +182,13 @@ void main() async {
|
||||
);
|
||||
qjs.dispatch();
|
||||
qjs.evaluate(
|
||||
"(() => { Promise.resolve().then(() => { throw 'unhandle' }) })()",
|
||||
name: "<eval>");
|
||||
'(() => { Promise.resolve().then(() => { throw "unhandle" }) })()',
|
||||
name: '<eval>');
|
||||
Future.delayed(Duration(seconds: 10)).then((value) {
|
||||
if (!completer.isCompleted) completer.completeError("not host reject");
|
||||
if (!completer.isCompleted) completer.completeError('not host reject');
|
||||
});
|
||||
expect(await completer.future, "unhandle",
|
||||
reason: "host promise rejection");
|
||||
expect(await completer.future, 'unhandle',
|
||||
reason: 'host promise rejection');
|
||||
qjs.close();
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user