Skip to content

BridgeJS: Check thrown errors before lifting return values - #30

Merged
krodak merged 2 commits into
mainfrom
kr/bridgejs-throw-before-lift
Nov 3, 2026
Merged

krodak merged 2 commits into
mainfrom
kr/bridgejs-throw-before-lift

Conversation

@krodak

@krodak krodak commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

An exported throws(JSException) function that returns a JSObject or a @JS protocol threw ReferenceError: Attempted to use invalid reference 0 in JavaScript instead of the Swift error. The generated glue lifted the return value before it checked tmpRetException, so it read an object id that Swift never returned.

The exception check now runs right after the instance.exports.* call, before the lift:

const ret = instance.exports.bjs_MyViewController_validatedDelegate(this.pointer);
if (tmpRetException) {
    // ...
    throw error;
}
const ret1 = swift.memory.getObject(ret);
swift.memory.release(ret);
return ret1;

The second commit pins the example build jobs to ubuntu-22.04. ubuntu-latest is now 26.04, where apt-get cannot find libgcc-9-dev for the ubuntu2204 toolchain.

Testing

Added a throwing protocol return to the Protocol.swift snapshot input and two runtime tests that throw from a JSObject and a protocol return. Both runtime tests fail without the fix. BridgeJS snapshot tests and make unittest pass.

@krodak
krodak force-pushed the kr/bridgejs-throw-before-lift branch from 4a290f6 to 835ee61 Compare November 3, 2026 00:59
@krodak
krodak merged commit 461360a into main Nov 3, 2026
13 of 16 checks passed
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.

1 participant