forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++20 co_await support for Embind promises (emscripten-core#20420)
- Loading branch information
Showing
9 changed files
with
252 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <emscripten.h> | ||
#include <emscripten/bind.h> | ||
#include <emscripten/val.h> | ||
#include <assert.h> | ||
#include <stdexcept> | ||
|
||
using namespace emscripten; | ||
|
||
EM_JS(EM_VAL, promise_sleep_impl, (int ms, int result), { | ||
let promise = new Promise(resolve => setTimeout(resolve, ms, result)); | ||
let handle = Emval.toHandle(promise); | ||
// FIXME. See https://github.com/emscripten-core/emscripten/issues/16975. | ||
#if __wasm64__ | ||
handle = BigInt(handle); | ||
#endif | ||
return handle; | ||
}); | ||
|
||
val promise_sleep(int ms, int result = 0) { | ||
return val::take_ownership(promise_sleep_impl(ms, result)); | ||
} | ||
|
||
val asyncCoro() { | ||
// check that just sleeping works | ||
co_await promise_sleep(1); | ||
// check that sleeping and receiving value works | ||
val v = co_await promise_sleep(1, 12); | ||
assert(v.as<int>() == 12); | ||
// check that returning value works (checked by JS in tests) | ||
co_return 34; | ||
} | ||
|
||
val throwingCoro() { | ||
throw std::runtime_error("bang from throwingCoro!"); | ||
co_return 56; | ||
} | ||
|
||
EMSCRIPTEN_BINDINGS(test_val_coro) { | ||
function("asyncCoro", asyncCoro); | ||
function("throwingCoro", throwingCoro); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters