You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you have some C++ code called from JS via Embind and you want that to return a value or raise an exception, you currently have three not great choices:
Compile with -fexceptions, which has some big drawbacks if your code is currently no-exceptions.
Do some sorcery at the threshold between no-exceptions-C++-land and yes-exceptions-JS-land to raise the exception without causing memory corruption or incorrectly eliding destructors.
Return a JS object packaging the error or value, requiring the client to handle that themselves. (In the case of an object packaging maybe-error for a function otherwise without a return value, it can be easy to drop the error accidentally.)
Embind could add some functionality that handles the threshold error-raising automatically, bridging the gap from no-exceptions C++ code to idiomatic JS/TS without the client having to handle status-or-value objects in a manual way, and without Embind API implementers having to implement those in various idiosyncratic ways.
This would require something like:
A type result_or_error<T>, which can be initialized with result(T) or error(E) (where T is some otherwise-supported type and E is a val that's a JS Error).
A type void_or_error, which can be initialized with void() or error(E).
Overloads of function (at least) that take a callable returning one of those types. The JS/TS interface for those are the same as a function that returns T or void, but in the error case the error is raised in JS after the call into C++ completes.
(Potentially this could be done in terms of support for std::expected, though that requires C++23.)
A way to make this available for a user-defined type or library type in C++20 (like absl::Status) would be to implement it for std::expected as a built-in but provide a way to register a user-defined type for the same mechanism, similar to how smart pointer support is implemented in embind.
So if I'm understanding this correctly, supporting something like absl::StatusOr<T> would be a matter of implementing:
namespace emscripten {
template<typename T, typename E>
struct expected_trait<absl::StatusOr<T>> {
using value_type = T;
using error_type = val;
...
}
And then implementing the rest of the methods of that accordingly so it can tell which is held and return either T or a val that's the JS Error. Then call e.g. register_expected<absl::StatusOr<std::string>> for whatever types are going to be contained. That seems very reasonable. (Though I'm curious to hear @wffurr's more knowledgeable take as well.)
Would it make sense for those implementations of get_value and get_error to use perfect forwarding (just the generalized rvalue reference version with std::forward) instead of two separate implementations? It seems the implementations are just doing what std::forward would do anyways, using std::move in the rvalue-only case.
(Also the comment by gemini-code-assist[bot] about BigInt seems potentially important.)
On the one hand, this looks like a seamless way to handle std::expected-like types such as absl::StatusOr in the Wasm bindings. On the other hand, there's no indication in the generated typings that these functions are "expected" to throw an error. TypeScript lacks any notion of "checked exceptions" or declared exceptions. Is it generally the case in C++ compiled with exceptions that any arbitrary Wasm call can throw? I suppose it would be, and the equivalent when compiled without exceptions is a crash (which presumably C++ compiled with exceptions can also do).
On the gripping hand, this matches how Rust's wasm-bindgen handles the Result<T, E> type, so it's probably a good change:
Activity
sbc100 commentedon Oct 30, 2026
FWIW, I think for new features like this is seems reasonable to require C++23.
sfreilich commentedon Oct 30, 2026
Does sound reasonable. Though I would like to see something that supports C++20 for work reasons.
wffurr commentedon Oct 30, 2026
A way to make this available for a user-defined type or library type in C++20 (like
absl::Status) would be to implement it forstd::expectedas a built-in but provide a way to register a user-defined type for the same mechanism, similar to how smart pointer support is implemented in embind.Add support for std::expected in embind
brendandahl commentedon Nov 3, 2026
I've opened a PR with a fix. Please take a look and let me know if this is what you expect. For c++20, see the custom expected in embind_test.cpp.
Add support for std::expected in embind
sfreilich commentedon Nov 4, 2026
Neat!
So if I'm understanding this correctly, supporting something like
absl::StatusOr<T>would be a matter of implementing:And then implementing the rest of the methods of that accordingly so it can tell which is held and return either
Tor avalthat's the JSError. Then call e.g.register_expected<absl::StatusOr<std::string>>for whatever types are going to be contained. That seems very reasonable. (Though I'm curious to hear @wffurr's more knowledgeable take as well.)Would it make sense for those implementations of
get_valueandget_errorto use perfect forwarding (just the generalized rvalue reference version withstd::forward) instead of two separate implementations? It seems the implementations are just doing whatstd::forwardwould do anyways, usingstd::movein the rvalue-only case.(Also the comment by
gemini-code-assist[bot]aboutBigIntseems potentially important.)wffurr commentedon Nov 4, 2026
On the one hand, this looks like a seamless way to handle
std::expected-like types such asabsl::StatusOrin the Wasm bindings. On the other hand, there's no indication in the generated typings that these functions are "expected" to throw an error. TypeScript lacks any notion of "checked exceptions" or declared exceptions. Is it generally the case in C++ compiled with exceptions that any arbitrary Wasm call can throw? I suppose it would be, and the equivalent when compiled without exceptions is a crash (which presumably C++ compiled with exceptions can also do).On the gripping hand, this matches how Rust's wasm-bindgen handles the
Result<T, E>type, so it's probably a good change: