Skip to content
Closed
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
8 changes: 4 additions & 4 deletions Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1428,26 +1428,26 @@ private final class ExportSwiftAPICollector: SyntaxAnyVisitor {
for enumCase in exportedEnum.cases {
for associatedValue in enumCase.associatedValues {
switch associatedValue.type {
case .string, .int, .float, .double, .bool:
case .string, .int, .float, .double, .bool, .caseEnum, .rawValueEnum:
break
case .nullable(let wrappedType, _):
switch wrappedType {
case .string, .int, .float, .double, .bool:
case .string, .int, .float, .double, .bool, .caseEnum, .rawValueEnum:
break
default:
diagnose(
node: node,
message: "Unsupported associated value type: \(associatedValue.type.swiftType)",
hint:
"Only primitive types and optional primitives (String?, Int?, Float?, Double?, Bool?) are supported in associated-value enums"
"Only primitive types, enums, and their optionals are supported in associated-value enums"
)
}
default:
diagnose(
node: node,
message: "Unsupported associated value type: \(associatedValue.type.swiftType)",
hint:
"Only primitive types and optional primitives (String?, Int?, Float?, Double?, Bool?) are supported in associated-value enums"
"Only primitive types, enums, and their optionals are supported in associated-value enums"
)
}
}
Expand Down
176 changes: 176 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,64 @@ struct IntrinsicJSFragment: Sendable {
return []
}
)
case .caseEnum:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push((\(arguments[0]) | 0));")
return []
}
)
case .rawValueEnum(_, let rawType):
switch rawType {
case .string:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
let value = arguments[0]
let bytesVar = scope.variable("bytes")
let idVar = scope.variable("id")
printer.write(
"const \(bytesVar) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(value));"
)
printer.write(
"const \(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesVar));"
)
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(bytesVar).length);")
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(idVar));")
cleanup.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
return []
}
)
case .float:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
printer.write(
"\(JSGlueVariableScope.reservedTmpParamF32s).push(Math.fround(\(arguments[0])));"
)
return []
}
)
case .double:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
printer.write("\(JSGlueVariableScope.reservedTmpParamF64s).push(\(arguments[0]));")
return []
}
)
default:
return IntrinsicJSFragment(
parameters: ["value"],
printCode: { arguments, scope, printer, cleanup in
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push((\(arguments[0]) | 0));"
)
return []
}
)
}
case .nullable(let wrappedType, let kind):
return IntrinsicJSFragment(
parameters: ["value"],
Expand Down Expand Up @@ -1999,6 +2057,68 @@ struct IntrinsicJSFragment: Sendable {
"\(JSGlueVariableScope.reservedTmpParamF64s).push(\(isSomeVar) ? \(value) : 0.0);"
)
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);")
case .caseEnum:
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? (\(value) | 0) : 0);"
)
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);")
case .rawValueEnum(_, let rawType):
switch rawType {
case .string:
let idVar = scope.variable("id")
printer.write("let \(idVar);")
printer.write("if (\(isSomeVar)) {")
printer.indent {
let bytesVar = scope.variable("bytes")
printer.write(
"let \(bytesVar) = \(JSGlueVariableScope.reservedTextEncoder).encode(\(value));"
)
printer.write(
"\(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(bytesVar));"
)
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(bytesVar).length);"
)
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(idVar));")
}
printer.write("} else {")
printer.indent {
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(0);")
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(0);")
}
printer.write("}")
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);"
)
cleanup.write("if(\(idVar)) {")
cleanup.indent {
cleanup.write(
"\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));"
)
}
cleanup.write("}")
case .float:
printer.write(
"\(JSGlueVariableScope.reservedTmpParamF32s).push(\(isSomeVar) ? Math.fround(\(value)) : 0.0);"
)
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);"
)
case .double:
printer.write(
"\(JSGlueVariableScope.reservedTmpParamF64s).push(\(isSomeVar) ? \(value) : 0.0);"
)
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);"
)
default:
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? (\(value) | 0) : 0);"
)
printer.write(
"\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);"
)
}
default:
printer.write("\(JSGlueVariableScope.reservedTmpParamInts).push(\(isSomeVar) ? 1 : 0);")
}
Expand Down Expand Up @@ -2063,6 +2183,62 @@ struct IntrinsicJSFragment: Sendable {
return [dVar]
}
)
case .caseEnum:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
let iVar = scope.variable("int")
printer.write("const \(iVar) = \(JSGlueVariableScope.reservedTmpRetInts).pop();")
return [iVar]
}
)
case .rawValueEnum(_, let rawType):
switch rawType {
case .string:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
let strVar = scope.variable("string")
printer.write(
"const \(strVar) = \(JSGlueVariableScope.reservedTmpRetStrings).pop();"
)
return [strVar]
}
)
case .float:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
let fVar = scope.variable("f32")
printer.write(
"const \(fVar) = \(JSGlueVariableScope.reservedTmpRetF32s).pop();"
)
return [fVar]
}
)
case .double:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
let dVar = scope.variable("f64")
printer.write(
"const \(dVar) = \(JSGlueVariableScope.reservedTmpRetF64s).pop();"
)
return [dVar]
}
)
default:
return IntrinsicJSFragment(
parameters: [],
printCode: { arguments, scope, printer, cleanup in
let iVar = scope.variable("int")
printer.write(
"const \(iVar) = \(JSGlueVariableScope.reservedTmpRetInts).pop();"
)
return [iVar]
}
)
}
case .nullable(let wrappedType, let kind):
return IntrinsicJSFragment(
parameters: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,27 @@ enum APIOptionalResult {
}
@JS func roundTripOptionalAPIOptionalResult(result: APIOptionalResult?) -> APIOptionalResult?
@JS func compareAPIResults(result1: APIOptionalResult?, result2: APIOptionalResult?) -> APIOptionalResult?

@JS enum Precision: Float {
case rough = 0.1
case fine = 0.001
}

@JS enum CardinalDirection {
case north
case south
case east
case west
}

@JS
enum TypedPayloadResult {
case precision(Precision)
case direction(CardinalDirection)
case optPrecision(Precision?)
case optDirection(CardinalDirection?)
case empty
}

@JS func roundTripTypedPayloadResult(_ result: TypedPayloadResult) -> TypedPayloadResult
@JS func roundTripOptionalTypedPayloadResult(_ result: TypedPayloadResult?) -> TypedPayloadResult?
Loading
Loading