Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2195,35 +2195,6 @@ struct IntrinsicJSFragment: Sendable {
wrappedType: BridgeType,
kind: JSOptionalKind
) throws -> IntrinsicJSFragment {
if case .associatedValueEnum(let fullName) = wrappedType {
let base = fullName.components(separatedBy: ".").last ?? fullName
let absenceLiteral = kind.absenceLiteral
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, context in
let (scope, printer) = (context.scope, context.printer)
let caseIdVar = scope.variable("caseId")
let resultVar = scope.variable("optValue")

printer.write("const \(caseIdVar) = \(scope.popI32());")
printer.write("let \(resultVar);")
printer.write("if (\(caseIdVar) === -1) {")
printer.indent {
printer.write("\(resultVar) = \(absenceLiteral);")
}
printer.write("} else {")
printer.indent {
printer.write(
"\(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseIdVar));"
)
}
printer.write("}")

return [resultVar]
}
)
}

let absenceLiteral = kind.absenceLiteral
return IntrinsicJSFragment(
parameters: [],
Expand Down Expand Up @@ -2261,36 +2232,6 @@ struct IntrinsicJSFragment: Sendable {
wrappedType: BridgeType,
kind: JSOptionalKind
) throws -> IntrinsicJSFragment {
if case .associatedValueEnum(let fullName) = wrappedType {
let base = fullName.components(separatedBy: ".").last ?? fullName
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, context in
let (scope, printer) = (context.scope, context.printer)
let value = arguments[0]
let isSomeVar = scope.variable("isSome")
let presenceExpr = kind.presenceCheck(value: value)

printer.write("const \(isSomeVar) = \(presenceExpr) ? 1 : 0;")
printer.write("if (\(isSomeVar)) {")
printer.indent {
let caseIdVar = scope.variable("caseId")
printer.write(
"const \(caseIdVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lower(\(value));"
)
scope.emitPushI32Parameter(caseIdVar, printer: printer)
}
printer.write("} else {")
printer.indent {
scope.emitPushI32Parameter("-1", printer: printer)
}
printer.write("}")

return []
}
)
}

return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, context in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,8 @@ export async function createInstantiator(options, swift) {
if (isSome) {
const caseId = enumHelpers.InnerTag.lower(elem);
i32Stack.push(caseId);
} else {
i32Stack.push(-1);
}
i32Stack.push(isSome);
}
i32Stack.push(xs.length);
instance.exports.bjs_roundtripTags();
Expand All @@ -495,12 +494,13 @@ export async function createInstantiator(options, swift) {
} else {
arrayResult = [];
for (let i = 0; i < arrayLen; i++) {
const caseId1 = i32Stack.pop();
const isSome1 = i32Stack.pop();
let optValue;
if (caseId1 === -1) {
if (isSome1 === 0) {
optValue = null;
} else {
optValue = enumHelpers.InnerTag.lift(caseId1);
const enumValue = enumHelpers.InnerTag.lift(i32Stack.pop());
optValue = enumValue;
}
arrayResult.push(optValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,8 @@ export async function createInstantiator(options, swift) {
if (isSome) {
const caseId = enumHelpers.APIResult.lower(value.param0);
i32Stack.push(caseId);
} else {
i32Stack.push(-1);
}
i32Stack.push(isSome);
return OptionalAllTypesResultValues.Tag.OptNestedEnum;
}
case OptionalAllTypesResultValues.Tag.OptArray: {
Expand Down Expand Up @@ -738,12 +737,13 @@ export async function createInstantiator(options, swift) {
return { tag: OptionalAllTypesResultValues.Tag.OptJSObject, param0: optValue };
}
case OptionalAllTypesResultValues.Tag.OptNestedEnum: {
const caseId = i32Stack.pop();
const isSome = i32Stack.pop();
let optValue;
if (caseId === -1) {
if (isSome === 0) {
optValue = null;
} else {
optValue = enumHelpers.APIResult.lift(caseId);
const enumValue = enumHelpers.APIResult.lift(i32Stack.pop());
optValue = enumValue;
}
return { tag: OptionalAllTypesResultValues.Tag.OptNestedEnum, param0: optValue };
}
Expand Down
24 changes: 6 additions & 18 deletions Sources/JavaScriptKit/BridgeJSIntrinsics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1100,23 +1100,6 @@ extension _BridgedSwiftAssociatedValueEnum {
_swift_js_push_i32(bridgeJSStackPushPayload())
}

public static func bridgeJSStackPopAsOptional() -> Self? {
let discriminator = _swift_js_pop_i32()
if discriminator == -1 {
return nil
}
return bridgeJSStackPopPayload(discriminator)
}

public static func bridgeJSStackPushAsOptional(_ value: consuming Self?) {
switch consume value {
case .none:
_swift_js_push_i32(-1)
case .some(let value):
_swift_js_push_i32(value.bridgeJSStackPushPayload())
}
}

@_spi(BridgeJS) public static func bridgeJSLiftParameter(_ caseId: Int32) -> Self {
return bridgeJSStackPopPayload(caseId)
}
Expand Down Expand Up @@ -2378,7 +2361,12 @@ extension _BridgedAsOptional where Wrapped: _BridgedSwiftAssociatedValueEnum {
}

@_spi(BridgeJS) public consuming func bridgeJSLowerReturn() {
Wrapped.bridgeJSStackPushAsOptional(asOptional)
switch asOptional {
case .none:
_swift_js_push_i32(-1)
case .some(let value):
_swift_js_push_i32(value.bridgeJSStackPushPayload())
}
}
}

Expand Down
Loading