Skip to content

BridgeJS: Support throwing any error conforming to ConvertibleToJSException#695

Open
wfltaylor wants to merge 2 commits intoswiftwasm:mainfrom
wfltaylor:throwing
Open

BridgeJS: Support throwing any error conforming to ConvertibleToJSException#695
wfltaylor wants to merge 2 commits intoswiftwasm:mainfrom
wfltaylor:throwing

Conversation

@wfltaylor
Copy link

Introduce a new protocol ConvertibleToJSException and support throwing any error (as long as it is specified using typed throws) which conforms to this protocol. It is beneficial for @JS methods to be usable by non-JS consumers, but requiring the error type to be JSException makes this unachievable for methods which throw.

This PR also fixes an issue where the generated code for throwing methods doesn’t compile at all using Embedded Swift, since it uses String(describing:).

A potential alternative to this approach would be to emit code to support @JS annotated Swift Errors, which would bridge them to JavaScript. This is a bit more complex so I stuck with the more straightforward approach as a first attempt.

@MaxDesiatov
Copy link
Member

MaxDesiatov commented Mar 10, 2026

It is beneficial for @JS methods to be usable by non-JS consumers, but requiring the error type to be JSException makes this unachievable for methods which throw.

I'm not sure I understand, can you elaborate and provide an example?

@wfltaylor
Copy link
Author

It is beneficial for @JS methods to be usable by non-JS consumers, but requiring the error type to be JSException makes this unachievable for methods which throw.

I'm not sure I understand, can you elaborate and provide an example?

For example, someone might want to write cross platform SDK which is consumed by JavaScript on the web, but Java on Android, and C++ on the server. Currently, any throwing function would need to be wrapped to map any error to JSException to annotate it with @JS. Instead, if we could use a type which conforms to ConvertibleToJSException, the same method could be called from JavaScript code, Swift code, and any other suitable consumer.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants