Skip to content

Commit f9ae972

Browse files
authored
Merge pull request #1 from MELScience/feature/add_max_stack_size_to_java_wrapper
add max stack size to java wrapper
2 parents 20a0038 + 6470acb commit f9ae972

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

library/src/main/c/quickjs-jni.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ Java_com_hippo_quickjs_android_QuickJS_setRuntimeMallocLimit(
4747
JS_SetMemoryLimit(qj_rt->rt, (size_t) malloc_limit);
4848
}
4949

50+
JNIEXPORT void JNICALL
51+
Java_com_hippo_quickjs_android_QuickJS_setRuntimeMaxStackSize(
52+
JNIEnv *env,
53+
jclass __unused clazz,
54+
jlong runtime,
55+
jint stack_size
56+
) {
57+
QJRuntime *qj_rt = (QJRuntime *) runtime;
58+
CHECK_NULL(env, qj_rt, MSG_NULL_JS_RUNTIME);
59+
JS_SetMaxStackSize(qj_rt->rt, (size_t) stack_size);
60+
}
61+
5062
static int on_interrupt(JSRuntime __unused *rt, void *opaque) {
5163
int result = 0;
5264

library/src/main/java/com/hippo/quickjs/android/JSRuntime.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ public synchronized void setMallocLimit(int mallocLimit) {
5959
QuickJS.setRuntimeMallocLimit(pointer, mallocLimit);
6060
}
6161

62+
/**
63+
* Set max stack size for this JSRuntime.
64+
* Only positive number and {@code 0} are accepted.
65+
* {@code 0} for no stack size check.
66+
*/
67+
public synchronized void setMaxStackSize(int stackSize) {
68+
checkClosed();
69+
70+
if (stackSize < 0) {
71+
throw new IllegalArgumentException("Only positive number and 0 are accepted as max stack size");
72+
}
73+
74+
QuickJS.setRuntimeMaxStackSize(pointer, stackSize);
75+
}
76+
6277
/**
6378
* Set the InterruptHandler for this JSRuntime.
6479
* {@link InterruptHandler#onInterrupt()} is called every 10000 js instructions.

library/src/main/java/com/hippo/quickjs/android/QuickJS.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public QuickJS build() {
109109

110110
static native long createRuntime();
111111
static native void setRuntimeMallocLimit(long runtime, int mallocLimit);
112+
static native void setRuntimeMaxStackSize(long runtime, int stackSize);
112113
static native void setRuntimeInterruptHandler(long runtime, JSRuntime.InterruptHandler interruptHandler);
113114
static native void destroyRuntime(long runtime);
114115

0 commit comments

Comments
 (0)