add option to change max stack size.

This commit is contained in:
ekibun
2021-01-03 21:47:14 +08:00
parent d7224dd840
commit e8ba97011a
9 changed files with 41 additions and 5 deletions

View File

@@ -132,6 +132,19 @@ Pointer jsNewRuntime(
return rt;
}
/// DLLEXPORT void jsSetMaxStackSize(JSRuntime *rt, size_t stack_size)
final void Function(
Pointer,
int,
) jsSetMaxStackSize = qjsLib
.lookup<
NativeFunction<
Void Function(
Pointer,
IntPtr,
)>>("jsSetMaxStackSize")
.asFunction();
/// void jsFreeRuntime(JSRuntime *rt)
final void Function(
Pointer,

View File

@@ -23,6 +23,9 @@ class FlutterQjs {
Pointer _rt;
Pointer _ctx;
/// Max stack size for quickjs.
final int stackSize;
/// Message Port for event loop. Close it to stop dispatching event loop.
ReceivePort port = ReceivePort();
@@ -35,7 +38,7 @@ class FlutterQjs {
/// Quickjs engine for flutter.
///
/// Pass handlers to implement js-dart interaction and resolving modules.
FlutterQjs({this.methodHandler, this.moduleHandler});
FlutterQjs({this.methodHandler, this.moduleHandler, this.stackSize});
_ensureEngine() {
if (_rt != null) return;
@@ -69,6 +72,8 @@ class FlutterQjs {
return err;
}
}, port);
if (this.stackSize != null && this.stackSize > 0)
jsSetMaxStackSize(_rt, this.stackSize);
_ctx = jsNewContextWithPromsieWrapper(_rt);
}

View File

@@ -147,6 +147,7 @@ void _runJsIsolate(Map spawnMessage) async {
ReceivePort port = ReceivePort();
sendPort.send(port.sendPort);
var qjs = FlutterQjs(
stackSize: spawnMessage['stackSize'],
methodHandler: methodHandler,
moduleHandler: (name) {
var ptr = allocate<Pointer<Utf8>>();
@@ -211,6 +212,9 @@ typedef JsIsolateSpawn = void Function(SendPort sendPort);
class IsolateQjs {
Future<SendPort> _sendPort;
/// Max stack size for quickjs.
final int stackSize;
/// Handler to manage js call with `channel(method, [...args])` function.
/// The function must be a top-level function or a static method.
JsMethodHandler methodHandler;
@@ -222,7 +226,7 @@ class IsolateQjs {
///
/// Pass handlers to implement js-dart interaction and resolving modules. The `methodHandler` is
/// used in isolate, so **the handler function must be a top-level function or a static method**.
IsolateQjs({this.methodHandler, this.moduleHandler});
IsolateQjs({this.methodHandler, this.moduleHandler, this.stackSize});
_ensureEngine() {
if (_sendPort != null) return;
@@ -232,6 +236,7 @@ class IsolateQjs {
{
'port': port.sendPort,
'handler': methodHandler,
'stackSize': stackSize,
},
errorsAreFatal: true,
);