Skip to content

Commit d58d2c6

Browse files
authored
Merge pull request swiftwasm#786 from PassiveLogic/kr/unify-optional-stack-encoding
BridgeJS: Unify optional stack encoding to presence-flag form
2 parents 8c08980 + bc356b5 commit d58d2c6

4 files changed

Lines changed: 16 additions & 87 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,35 +2195,6 @@ struct IntrinsicJSFragment: Sendable {
21952195
wrappedType: BridgeType,
21962196
kind: JSOptionalKind
21972197
) throws -> IntrinsicJSFragment {
2198-
if case .associatedValueEnum(let fullName) = wrappedType {
2199-
let base = fullName.components(separatedBy: ".").last ?? fullName
2200-
let absenceLiteral = kind.absenceLiteral
2201-
return IntrinsicJSFragment(
2202-
parameters: [],
2203-
printCode: { arguments, context in
2204-
let (scope, printer) = (context.scope, context.printer)
2205-
let caseIdVar = scope.variable("caseId")
2206-
let resultVar = scope.variable("optValue")
2207-
2208-
printer.write("const \(caseIdVar) = \(scope.popI32());")
2209-
printer.write("let \(resultVar);")
2210-
printer.write("if (\(caseIdVar) === -1) {")
2211-
printer.indent {
2212-
printer.write("\(resultVar) = \(absenceLiteral);")
2213-
}
2214-
printer.write("} else {")
2215-
printer.indent {
2216-
printer.write(
2217-
"\(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseIdVar));"
2218-
)
2219-
}
2220-
printer.write("}")
2221-
2222-
return [resultVar]
2223-
}
2224-
)
2225-
}
2226-
22272198
let absenceLiteral = kind.absenceLiteral
22282199
return IntrinsicJSFragment(
22292200
parameters: [],
@@ -2261,36 +2232,6 @@ struct IntrinsicJSFragment: Sendable {
22612232
wrappedType: BridgeType,
22622233
kind: JSOptionalKind
22632234
) throws -> IntrinsicJSFragment {
2264-
if case .associatedValueEnum(let fullName) = wrappedType {
2265-
let base = fullName.components(separatedBy: ".").last ?? fullName
2266-
return IntrinsicJSFragment(
2267-
parameters: ["value"],
2268-
printCode: { arguments, context in
2269-
let (scope, printer) = (context.scope, context.printer)
2270-
let value = arguments[0]
2271-
let isSomeVar = scope.variable("isSome")
2272-
let presenceExpr = kind.presenceCheck(value: value)
2273-
2274-
printer.write("const \(isSomeVar) = \(presenceExpr) ? 1 : 0;")
2275-
printer.write("if (\(isSomeVar)) {")
2276-
printer.indent {
2277-
let caseIdVar = scope.variable("caseId")
2278-
printer.write(
2279-
"const \(caseIdVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lower(\(value));"
2280-
)
2281-
scope.emitPushI32Parameter(caseIdVar, printer: printer)
2282-
}
2283-
printer.write("} else {")
2284-
printer.indent {
2285-
scope.emitPushI32Parameter("-1", printer: printer)
2286-
}
2287-
printer.write("}")
2288-
2289-
return []
2290-
}
2291-
)
2292-
}
2293-
22942235
return IntrinsicJSFragment(
22952236
parameters: ["value"],
22962237
printCode: { arguments, context in

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/Alias.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,8 @@ export async function createInstantiator(options, swift) {
482482
if (isSome) {
483483
const caseId = enumHelpers.InnerTag.lower(elem);
484484
i32Stack.push(caseId);
485-
} else {
486-
i32Stack.push(-1);
487485
}
486+
i32Stack.push(isSome);
488487
}
489488
i32Stack.push(xs.length);
490489
instance.exports.bjs_roundtripTags();
@@ -495,12 +494,13 @@ export async function createInstantiator(options, swift) {
495494
} else {
496495
arrayResult = [];
497496
for (let i = 0; i < arrayLen; i++) {
498-
const caseId1 = i32Stack.pop();
497+
const isSome1 = i32Stack.pop();
499498
let optValue;
500-
if (caseId1 === -1) {
499+
if (isSome1 === 0) {
501500
optValue = null;
502501
} else {
503-
optValue = enumHelpers.InnerTag.lift(caseId1);
502+
const enumValue = enumHelpers.InnerTag.lift(i32Stack.pop());
503+
optValue = enumValue;
504504
}
505505
arrayResult.push(optValue);
506506
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,8 @@ export async function createInstantiator(options, swift) {
676676
if (isSome) {
677677
const caseId = enumHelpers.APIResult.lower(value.param0);
678678
i32Stack.push(caseId);
679-
} else {
680-
i32Stack.push(-1);
681679
}
680+
i32Stack.push(isSome);
682681
return OptionalAllTypesResultValues.Tag.OptNestedEnum;
683682
}
684683
case OptionalAllTypesResultValues.Tag.OptArray: {
@@ -738,12 +737,13 @@ export async function createInstantiator(options, swift) {
738737
return { tag: OptionalAllTypesResultValues.Tag.OptJSObject, param0: optValue };
739738
}
740739
case OptionalAllTypesResultValues.Tag.OptNestedEnum: {
741-
const caseId = i32Stack.pop();
740+
const isSome = i32Stack.pop();
742741
let optValue;
743-
if (caseId === -1) {
742+
if (isSome === 0) {
744743
optValue = null;
745744
} else {
746-
optValue = enumHelpers.APIResult.lift(caseId);
745+
const enumValue = enumHelpers.APIResult.lift(i32Stack.pop());
746+
optValue = enumValue;
747747
}
748748
return { tag: OptionalAllTypesResultValues.Tag.OptNestedEnum, param0: optValue };
749749
}

Sources/JavaScriptKit/BridgeJSIntrinsics.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,23 +1100,6 @@ extension _BridgedSwiftAssociatedValueEnum {
11001100
_swift_js_push_i32(bridgeJSStackPushPayload())
11011101
}
11021102

1103-
public static func bridgeJSStackPopAsOptional() -> Self? {
1104-
let discriminator = _swift_js_pop_i32()
1105-
if discriminator == -1 {
1106-
return nil
1107-
}
1108-
return bridgeJSStackPopPayload(discriminator)
1109-
}
1110-
1111-
public static func bridgeJSStackPushAsOptional(_ value: consuming Self?) {
1112-
switch consume value {
1113-
case .none:
1114-
_swift_js_push_i32(-1)
1115-
case .some(let value):
1116-
_swift_js_push_i32(value.bridgeJSStackPushPayload())
1117-
}
1118-
}
1119-
11201103
@_spi(BridgeJS) public static func bridgeJSLiftParameter(_ caseId: Int32) -> Self {
11211104
return bridgeJSStackPopPayload(caseId)
11221105
}
@@ -2378,7 +2361,12 @@ extension _BridgedAsOptional where Wrapped: _BridgedSwiftAssociatedValueEnum {
23782361
}
23792362

23802363
@_spi(BridgeJS) public consuming func bridgeJSLowerReturn() {
2381-
Wrapped.bridgeJSStackPushAsOptional(asOptional)
2364+
switch asOptional {
2365+
case .none:
2366+
_swift_js_push_i32(-1)
2367+
case .some(let value):
2368+
_swift_js_push_i32(value.bridgeJSStackPushPayload())
2369+
}
23822370
}
23832371
}
23842372

0 commit comments

Comments
 (0)