Skip to content
New issue

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

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

Already on GitHub? # to your account

Increase ASYNCIFY_STACK_SIZE #114

Merged
merged 2 commits into from
Apr 22, 2023
Merged
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ CFLAGS_WASM+=-s NODEJS_CATCH_EXIT=0
# https://emscripten.org/docs/porting/asyncify.html
CFLAGS_WASM_ASYNCIFY+=-s ASYNCIFY=1
CFLAGS_WASM_ASYNCIFY+=-DQTS_ASYNCIFY=1
CFLAGS_WASM_ASYNCIFY+=-s ASYNCIFY_STACK_SIZE=81920
CFLAGS_WASM_ASYNCIFY+=-s ASYNCIFY_REMOVE=@$(BUILD_WRAPPER)/asyncify-remove.json
CFLAGS_WASM_ASYNCIFY+=-s ASYNCIFY_IMPORTS=@$(BUILD_WRAPPER)/asyncify-imports.json
CFLAGS_WASM_ASYNCIFY+=-lasync.js
Expand Down
9 changes: 9 additions & 0 deletions c/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,19 @@ int QTS_BuildIsSanitizeLeak() {
#endif
}

#ifdef QTS_ASYNCIFY
EM_JS(void, set_asyncify_stack_size, (size_t size), {
Asyncify.StackSize = size || 81920;
});
#endif

/**
* Set the stack size limit, in bytes. Set to 0 to disable.
*/
void QTS_RuntimeSetMaxStackSize(JSRuntime *rt, size_t stack_size) {
#ifdef QTS_ASYNCIFY
set_asyncify_stack_size(stack_size);
#endif
JS_SetMaxStackSize(rt, stack_size);
}

Expand Down
220 changes: 107 additions & 113 deletions ts/generated/emscripten-module.WASM_DEBUG_ASYNCIFY.js

Large diffs are not rendered by default.

Binary file modified ts/generated/emscripten-module.WASM_DEBUG_ASYNCIFY.wasm
Binary file not shown.

Large diffs are not rendered by default.

310 changes: 146 additions & 164 deletions ts/generated/emscripten-module.WASM_DEBUG_SYNC.js

Large diffs are not rendered by default.

Binary file modified ts/generated/emscripten-module.WASM_DEBUG_SYNC.wasm
Binary file not shown.
78 changes: 38 additions & 40 deletions ts/generated/emscripten-module.WASM_RELEASE_ASYNCIFY.js

Large diffs are not rendered by default.

44 changes: 21 additions & 23 deletions ts/generated/emscripten-module.WASM_RELEASE_SYNC.js

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions ts/quickjs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,38 @@ function asyncContextTests(getContext: () => Promise<QuickJSAsyncContext>) {
)
})
})

describe("ASYNCIFY_STACK_SIZE", () => {
// | ASYNCIFY_STACK_SIZE | Max Nesting Levels |
// |---------------------|--------------------|
// | 4096 (default) | 12 |
// | 81920 | 297 |
it("is enough to support at least 20 levels of function nesting", async () => {
// The nesting levels of the test cannot be too high, otherwise the
// node.js call stack will overflow when executing `yarn test`
let asyncFunctionCalls = 0
const asyncFn = async () => {
asyncFunctionCalls++
}
vm.newAsyncifiedFunction("asyncFn", asyncFn).consume((fn) =>
vm.setProp(vm.global, "asyncFn", fn)
)

await vm.evalCodeAsync(`
let nestingLevels = 0
function nestingFn() {
nestingLevels++
asyncFn()
if (nestingLevels < 20) {
nestingFn()
}
}
nestingFn();
`)

assert.equal(asyncFunctionCalls, 20, "20 levels of nesting")
})
})
}

describe("QuickJSContext", function () {
Expand Down