Skip to content

Add setMaxStackSize method to JSRuntime #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: wrapper
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions library/src/main/c/quickjs-jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ Java_com_hippo_quickjs_android_QuickJS_setRuntimeMallocLimit(
JS_SetMemoryLimit(qj_rt->rt, (size_t) malloc_limit);
}

JNIEXPORT void JNICALL
Java_com_hippo_quickjs_android_QuickJS_setRuntimeMaxStackSize(
JNIEnv *env,
jclass __unused clazz,
jlong runtime,
jint stack_size
) {
QJRuntime *qj_rt = (QJRuntime *) runtime;
CHECK_NULL(env, qj_rt, MSG_NULL_JS_RUNTIME);
JS_SetMaxStackSize(qj_rt->rt, (size_t) stack_size);
}

static int on_interrupt(JSRuntime __unused *rt, void *opaque) {
int result = 0;

Expand Down
15 changes: 15 additions & 0 deletions library/src/main/java/com/hippo/quickjs/android/JSRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ public synchronized void setMallocLimit(int mallocLimit) {
QuickJS.setRuntimeMallocLimit(pointer, mallocLimit);
}

/**
* Set max stack size for this JSRuntime.
* Only positive number and {@code 0} are accepted.
* {@code 0} for no stack size check.
*/
public synchronized void setMaxStackSize(int stackSize) {
checkClosed();

if (stackSize < 0) {
throw new IllegalArgumentException("Only positive number and 0 are accepted as max stack size");
}

QuickJS.setRuntimeMaxStackSize(pointer, stackSize);
}

/**
* Set the InterruptHandler for this JSRuntime.
* {@link InterruptHandler#onInterrupt()} is called every 10000 js instructions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public QuickJS build() {

static native long createRuntime();
static native void setRuntimeMallocLimit(long runtime, int mallocLimit);
static native void setRuntimeMaxStackSize(long runtime, int stackSize);
static native void setRuntimeInterruptHandler(long runtime, JSRuntime.InterruptHandler interruptHandler);
static native void destroyRuntime(long runtime);

Expand Down