Skip to content

BridgeJS: Fix optional Bool returns from imported JS functions - #839

Merged
krodak merged 1 commit into
swiftwasm:mainfrom
PassiveLogic:kr/bridgejs-optional-bool-import
Dec 2, 2026
Merged

krodak merged 1 commit into
swiftwasm:mainfrom
PassiveLogic:kr/bridgejs-optional-bool-import

Conversation

@krodak

@krodak krodak commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Overview

An imported JS function, getter, protocol method or JS closure that returns Bool? (or JSUndefinedOr<Bool>) always produced false on the Swift side, even when JS returned true, null or undefined.

Bool does not use a side channel for optional returns on the import side. The generated Swift extern returns an Int32 and lifts it with Optional<Bool>.bridgeJSLiftReturn(_:), which expects -1 for nil, 0 for false and 1 for true. The JS glue instead wrote the value into the swift_js_return_optional_bool side channel and returned nothing, so Wasm received undefined, which became 0.

This PR changes the JS lowering of optional Bool returns to produce the encoded i32 that Swift already expects. Values flowing the other way (Swift returning Bool? to JS) already use the side channel on both sides and work correctly, so that path is not changed.

Before:

let ret = swift.memory.getObject(self).roundTripBoolOrNull(...);
const isSome = ret != null;
bjs["swift_js_return_optional_bool"](isSome ? 1 : 0, isSome ? (ret ? 1 : 0) : 0);

After:

let ret = swift.memory.getObject(self).roundTripBoolOrNull(...);
const isSome = ret != null;
return isSome ? (ret ? 1 : 0) : -1;

Runtime tests now cover Bool? and JSUndefinedOr<Bool> returns from imported functions, an imported getter, and a JS closure returned to Swift.

@krodak
krodak merged commit 29dd909 into swiftwasm:main Dec 2, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants