Skip to content

Commit

Permalink
Increase ASYNCIFY_STACK_SIZE (bellard#114)
Browse files Browse the repository at this point in the history
* Increase ASYNCIFY_STACK_SIZE

* Add dynamic asyncify stack size
  • Loading branch information
yar2001 authored and menduz committed May 3, 2023
1 parent a992448 commit c6bcdf9
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 341 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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+=-s ENVIRONMENT=web
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.

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.

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

0 comments on commit c6bcdf9

Please sign in to comment.