From e8ba97011a688194f74eb88cff8750ebb8a8364d Mon Sep 17 00:00:00 2001 From: ekibun Date: Sun, 3 Jan 2021 21:47:14 +0800 Subject: [PATCH] add option to change max stack size. --- CHANGELOG.md | 4 ++++ README.md | 4 +++- cxx/ffi.cpp | 5 +++++ cxx/ffi.h | 2 ++ example/pubspec.lock | 2 +- lib/ffi.dart | 13 +++++++++++++ lib/flutter_qjs.dart | 7 ++++++- lib/isolate.dart | 7 ++++++- pubspec.yaml | 2 +- 9 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acf702a..8be4be5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ * @LastEditTime: 2020-12-02 11:36:40 --> +## 0.2.2 + +* add option to change max stack size. + ## 0.2.1 * code cleanup. diff --git a/README.md b/README.md index 390e373..ca015a1 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,9 @@ This plugin is a simple js engine for flutter using the `quickjs` project with ` Firstly, create a `FlutterQjs` object, then call `dispatch` to dispatch event loop: ```dart -final engine = FlutterQjs() +final engine = FlutterQjs( + stackSize: 1024 * 1024, // change stack size here. +); engine.dispatch(); ``` diff --git a/cxx/ffi.cpp b/cxx/ffi.cpp index e3dc7ba..1d1ff7e 100644 --- a/cxx/ffi.cpp +++ b/cxx/ffi.cpp @@ -71,6 +71,11 @@ extern "C" return rt; } + DLLEXPORT void jsSetMaxStackSize(JSRuntime *rt, size_t stack_size) + { + JS_SetMaxStackSize(rt, stack_size); + } + DLLEXPORT void jsFreeRuntime(JSRuntime *rt) { JS_SetRuntimeOpaque(rt, nullptr); diff --git a/cxx/ffi.h b/cxx/ffi.h index be25049..399cc97 100644 --- a/cxx/ffi.h +++ b/cxx/ffi.h @@ -21,6 +21,8 @@ extern "C" DLLEXPORT JSRuntime *jsNewRuntime(JSChannel channel); + DLLEXPORT void jsSetMaxStackSize(JSRuntime *rt, size_t stack_size); + DLLEXPORT void jsFreeRuntime(JSRuntime *rt); DLLEXPORT JSContext *jsNewContext(JSRuntime *rt); diff --git a/example/pubspec.lock b/example/pubspec.lock index ba43bba..d608348 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -82,7 +82,7 @@ packages: path: ".." relative: true source: path - version: "0.2.1" + version: "0.2.2" flutter_test: dependency: "direct dev" description: flutter diff --git a/lib/ffi.dart b/lib/ffi.dart index f606297..25c602f 100644 --- a/lib/ffi.dart +++ b/lib/ffi.dart @@ -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, diff --git a/lib/flutter_qjs.dart b/lib/flutter_qjs.dart index 7c30f5c..c75dd40 100644 --- a/lib/flutter_qjs.dart +++ b/lib/flutter_qjs.dart @@ -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); } diff --git a/lib/isolate.dart b/lib/isolate.dart index 9b69150..91550e2 100644 --- a/lib/isolate.dart +++ b/lib/isolate.dart @@ -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>(); @@ -211,6 +212,9 @@ typedef JsIsolateSpawn = void Function(SendPort sendPort); class IsolateQjs { Future _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, ); diff --git a/pubspec.yaml b/pubspec.yaml index 57152e2..4a3b182 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_qjs description: This plugin is a simple js engine for flutter using the `quickjs` project. Plugin currently supports all the platforms except web! -version: 0.2.1 +version: 0.2.2 homepage: https://github.com/ekibun/flutter_qjs environment: