Skip to content

Commit

Permalink
fix: Optional enums or ArrayBuffers can be bridged directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Sep 16, 2024
1 parent 885ecd4 commit a8630ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
10 changes: 10 additions & 0 deletions packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
case 'enum':
// Enums cannot be referenced from C++ <-> Swift bi-directionally,
// so we just pass the underlying raw value (int32), and cast from Int <-> Enum.
if (this.isBridgingToDirectCppTarget) {
// ...unless we bridge directly to a C++ target. Then we don't need special conversion.
return false
}
return true
case 'hybrid-object':
// Swift HybridObjects need to be wrapped in our own *Cxx Swift classes.
Expand Down Expand Up @@ -83,6 +87,9 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
return true
case 'array-buffer':
// ArrayBufferHolder <> std::shared_ptr<ArrayBuffer>
if (this.isBridgingToDirectCppTarget) {
return false
}
return true
case 'promise':
// PromiseHolder<T> <> std::shared_ptr<std::promise<T>>
Expand Down Expand Up @@ -351,6 +358,9 @@ export class SwiftCxxBridgedType implements BridgedType<'swift', 'c++'> {
const wrapping = new SwiftCxxBridgedType(optional.wrappingType, true)
switch (language) {
case 'swift':
if (!wrapping.needsSpecialHandling) {
return `${cppParameterName}.value`
}
return `
{ () -> ${optional.getCode('swift')} in
if let actualValue = ${cppParameterName}.value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,7 @@ public final class HybridTestObjectSwiftKotlinSpecCxx {
@inline(__always)
public func tryMiddleParam(num: Double, boo: bridge.std__optional_bool_, str: std.string) -> std.string {
do {
let result = try self.implementation.tryMiddleParam(num: num, boo: { () -> Bool? in
if let actualValue = boo.value {
return actualValue
} else {
return nil
}
}(), str: String(str))
let result = try self.implementation.tryMiddleParam(num: num, boo: boo.value, str: String(str))
return std.string(result)
} catch {
let message = "\(error.localizedDescription)"
Expand All @@ -294,13 +288,7 @@ public final class HybridTestObjectSwiftKotlinSpecCxx {
@inline(__always)
public func tryOptionalEnum(value: bridge.std__optional_Powertrain_) -> bridge.std__optional_Powertrain_ {
do {
let result = try self.implementation.tryOptionalEnum(value: { () -> Powertrain? in
if let actualValue = value.value {
return actualValue
} else {
return nil
}
}())
let result = try self.implementation.tryOptionalEnum(value: value.value)
return { () -> bridge.std__optional_Powertrain_ in
if let actualValue = result {
return bridge.create_std__optional_Powertrain_(actualValue)
Expand Down

0 comments on commit a8630ac

Please sign in to comment.