diff options
author | Alon Zakai <azakai@google.com> | 2020-03-04 13:21:39 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-04 13:21:39 -0800 |
commit | 57a81a04f7a49593438dcae4ca8cbad8e465dc2e (patch) | |
tree | 02f8732873e8ef2608a94610857e2fb13de813ca /src | |
parent | 2a0f6a513c6d6a99d16ca31de3e76a63b1490b4c (diff) | |
download | binaryen-57a81a04f7a49593438dcae4ca8cbad8e465dc2e.tar.gz binaryen-57a81a04f7a49593438dcae4ca8cbad8e465dc2e.tar.bz2 binaryen-57a81a04f7a49593438dcae4ca8cbad8e465dc2e.zip |
Expose asyncify state via a getter (#2679)
Normally, a wrapper has to track state separately to know when to
unwind/rewind and when to actually call import functions.
Exposing Asyncify state can help avoid this duplication and avoid
subtle bugs when internal and wrapper state get out of sync.
Since this is a tiny function and it's useful for any Asyncify
embedder, I've decided to expose it by default rather than hide behind an option.
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/Asyncify.cpp | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/passes/Asyncify.cpp b/src/passes/Asyncify.cpp index a4ff5794b..c1cec4a36 100644 --- a/src/passes/Asyncify.cpp +++ b/src/passes/Asyncify.cpp @@ -127,7 +127,7 @@ // proper place, and the end to the proper end based on how much memory // you reserved. Note that asyncify will grow the stack up. // -// The pass will also create four functions that let you control unwinding +// The pass will also create five functions that let you control unwinding // and rewinding: // // * asyncify_start_unwind(data : i32): call this to start unwinding the @@ -150,6 +150,12 @@ // * asyncify_stop_rewind(): call this to note that rewinding has // concluded, and normal execution can resume. // +// * asyncify_get_state(): call this to get the current value of the +// internal "__asyncify_state" variable as described above. +// It can be used to distinguish between unwinding/rewinding and normal +// calls, so that you know when to start an asynchronous operation and +// when to propagate results back. +// // These four functions are exported so that you can call them from the // outside. If you want to manage things from inside the wasm, then you // couldn't have called them before they were created by this pass. To work @@ -269,6 +275,7 @@ namespace wasm { namespace { static const Name ASYNCIFY_STATE = "__asyncify_state"; +static const Name ASYNCIFY_GET_STATE = "asyncify_get_state"; static const Name ASYNCIFY_DATA = "__asyncify_data"; static const Name ASYNCIFY_START_UNWIND = "asyncify_start_unwind"; static const Name ASYNCIFY_STOP_UNWIND = "asyncify_stop_unwind"; @@ -1336,6 +1343,14 @@ private: makeFunction(ASYNCIFY_STOP_UNWIND, false, State::Normal); makeFunction(ASYNCIFY_START_REWIND, true, State::Rewinding); makeFunction(ASYNCIFY_STOP_REWIND, false, State::Normal); + + module->addFunction( + builder.makeFunction(ASYNCIFY_GET_STATE, + Signature(Type::none, Type::i32), + {}, + builder.makeGlobalGet(ASYNCIFY_STATE, Type::i32))); + module->addExport(builder.makeExport( + ASYNCIFY_GET_STATE, ASYNCIFY_GET_STATE, ExternalKind::Function)); } }; |