Skip to content

Result-or-exception interface for Embind #28112

Open

Description

@sfreilich

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.)

Activity

  1. sbc100 commented on Oct 30, 2026

    @sbc100
    Collaborator

    FWIW, I think for new features like this is seems reasonable to require C++23.

  2. sfreilich commented on Oct 30, 2026

    @sfreilich
    Author

    Does sound reasonable. Though I would like to see something that supports C++20 for work reasons.

  3. wffurr commented on Oct 30, 2026

    @wffurr
    Contributor

    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.

  4. added a commit that references this issue on Nov 3, 2026
    9332daa
  5. brendandahl commented on Nov 3, 2026

    @brendandahl
    Collaborator

    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.

  6. added a commit that references this issue on Nov 3, 2026
    a8fedfa
  7. sfreilich commented on Nov 4, 2026

    @sfreilich
    Author

    Neat!

    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.)

  8. wffurr commented on Nov 4, 2026

    @wffurr
    Contributor

    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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions