emscripten-core / emscripten-core/emscripten

Can't return bigint from async function in Asyncify

Open
#23,441 9 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

This works in JSPI but is broken in Asyncify. The problem seems to be somewhere in `Asyncify.handleAsync`.

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.1 (89ce854a99238d04116a3d9b5afe241eec90c6c3)
clang version 20.0.0git (https:/github.com/llvm/llvm-project a32e36faf84bd7da3df0c7d50bb9020568128417)
Target: wasm32-unknown-emscripten
Thread model: posix
```

**Failing command line in full:**
Repro here: https://github.com/kainino0x/asyncify-bigint-bug
(live here http://kai.graphics/asyncify-bigint-bug/main_asyncify_bigint.html)
Just call `make`.
The args are in the makefile:
```make
all:

ARGS := -g -sASYNCIFY_IMPORTS=my_async_i64,my_async_u64

# These don't work
main_asyncify_bigint.html: main.cpp Makefile
em++ main.cpp -o $@ ${ARGS} -sASYNCIFY=1 -sWASM_BIGINT=1
main_asyncify_wasm64.html: main.cpp Makefile
em++ main.cpp -o $@ ${ARGS} -sASYNCIFY=1 -sMEMORY64=1

# These do work
main_jspi_bigint.html: main.cpp Makefile
em++ main.cpp -o $@ ${ARGS} -sJSPI=1 -sWASM_BIGINT=1
main_jspi_wasm64.html: main.cpp Makefile
em++ main.cpp -o $@ ${ARGS} -sJSPI=1 -sMEMORY64=1
```

Code:

```cpp
EM_JS(int64_t, my_async_i64, (), {
return Asyncify.handleAsync(async () => {
return -123n; // bigint
});
});

EM_JS(uint64_t, my_async_u64, (), {
return Asyncify.handleAsync(async () => {
return -123n; // bigint
});
});

int main() {
int64_t i = my_async_i64();
uint64_t u = my_async_u64();
printf("got: %lli %llu\n", i, u);
}
```

**Full link command and output with `-v` appended:**

There isn't anything notable in the link output, the problem is a crash:

```
Uncaught TypeError: Cannot convert 0 to a BigInt
at main_asyncify_bigint.wasm.__original_main (main_asyncify_bigint.wasm:0x580)
at main_asyncify_bigint.wasm.main (main_asyncify_bigint.wasm:0x780)
at ret. (main_asyncify_bigint.js:1273:24)
at main_asyncify_bigint.js:718:12
at callMain (main_asyncify_bigint.js:1832:15)
at doRun (main_asyncify_bigint.js:1884:24)
at main_asyncify_bigint.js:1893:7
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.