From 0cf545a013e0d33c5218b8c9354cdb4378d04ebd Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Wed, 14 Aug 2024 20:33:18 +0200 Subject: [PATCH] perf: Explicitly declare inline closure return type to avoid stressing the Swift compiler too much (#41) * fix: Catch unknown case (impossible) * perf: Explicitly declare inline closure return type to avoid stressing the Swift compiler too much --- .../src/syntax/swift/SwiftCxxBridgedType.ts | 14 +++---- .../HybridSwiftKotlinTestObjectSpecSwift.hpp | 4 -- .../nitrogen/generated/ios/swift/Car.swift | 6 +-- .../HybridSwiftKotlinTestObjectSpec.swift | 1 - .../HybridSwiftKotlinTestObjectSpecCxx.swift | 42 +++++++------------ .../ios/swift/Variant_String_Double.swift | 17 -------- .../c++/HybridSwiftKotlinTestObjectSpec.cpp | 1 - .../c++/HybridSwiftKotlinTestObjectSpec.hpp | 2 - .../src/specs/TestObject.nitro.ts | 2 - 9 files changed, 25 insertions(+), 64 deletions(-) delete mode 100644 packages/react-native-nitro-image/nitrogen/generated/ios/swift/Variant_String_Double.swift diff --git a/packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts b/packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts index cb28affa9..ab8bc73c9 100644 --- a/packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts +++ b/packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts @@ -283,7 +283,7 @@ export class SwiftCxxBridgedType { switch (language) { case 'swift': return ` -{ +{ () -> ${optional.getCode('swift')} in if let actualValue = ${cppParameterName}.value { return ${wrapping.parseFromCppToSwift('actualValue', language)} } else { @@ -322,7 +322,7 @@ export class SwiftCxxBridgedType { switch (language) { case 'swift': return ` -{ +{ () -> ${record.getCode('swift')} in var dictionary = ${record.getCode('swift')}(minimumCapacity: ${cppParameterName}.size()) let keys = ${getKeysFunc}(${cppParameterName}) for key in keys { @@ -360,7 +360,7 @@ case ${i}: switch (language) { case 'swift': return ` -{ +{ () -> ${variant.getCode('swift')} in switch ${cppParameterName}.index() { ${indent(cases, ' ')} default: @@ -447,7 +447,7 @@ case ${i}: switch (language) { case 'swift': return ` -{ +{ () -> bridge.${bridge.specializationName} in if let actualValue = ${swiftParameterName} { return ${makeFunc}(${wrapping.parseFromSwiftToCpp('actualValue', language)}) } else { @@ -483,7 +483,7 @@ case ${i}: switch (language) { case 'swift': return ` -{ +{ () -> bridge.${bridge.specializationName} in var vector = ${makeFunc}(${swiftParameterName}.count) for item in ${swiftParameterName} { vector.push_back(${wrapping.parseFromSwiftToCpp('item', language)}) @@ -526,7 +526,7 @@ case ${i}: switch (language) { case 'swift': return ` -{ +{ () -> bridge.${bridge.specializationName} in switch ${swiftParameterName} { ${indent(cases, ' ')} } @@ -544,7 +544,7 @@ case ${i}: switch (language) { case 'swift': return ` -{ +{ () -> bridge.${bridge.specializationName} in var map = ${createMap}(${swiftParameterName}.count) for (k, v) in ${swiftParameterName} { map[${wrappingKey.parseFromSwiftToCpp('k', 'swift')}] = ${wrappingValue.parseFromSwiftToCpp('v', 'swift')} diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridSwiftKotlinTestObjectSpecSwift.hpp b/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridSwiftKotlinTestObjectSpecSwift.hpp index 63225c749..7944fad15 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridSwiftKotlinTestObjectSpecSwift.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/c++/HybridSwiftKotlinTestObjectSpecSwift.hpp @@ -44,7 +44,6 @@ namespace margelo::nitro::image { class HybridSwiftKotlinTestObjectSpecSwift; } #include #include "HybridSwiftKotlinTestObjectSpec.hpp" #include "HybridSwiftKotlinTestObjectSpecSwift.hpp" -#include #if __has_include() #include @@ -218,9 +217,6 @@ namespace margelo::nitro::image { auto value = _swiftPart.bounceBack(std::static_pointer_cast(obj)->getSwiftPart()); return HybridContext::getOrCreate(value); } - inline void passVariant(const std::variant& val) override { - _swiftPart.passVariant(val); - } private: NitroImage::HybridSwiftKotlinTestObjectSpecCxx _swiftPart; diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Car.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Car.swift index 1a2a7ce54..71609e8c0 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Car.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Car.swift @@ -18,7 +18,7 @@ public extension Car { * Create a new instance of `Car`. */ init(year: Double, make: String, model: String, power: Double, powertrain: Powertrain, driver: Person?) { - self.init(year, std.string(make), std.string(model), power, powertrain, { + self.init(year, std.string(make), std.string(model), power, powertrain, { () -> bridge.std__optional_Person_ in if let actualValue = driver { return bridge.create_std__optional_Person_(actualValue) } else { @@ -85,7 +85,7 @@ public extension Car { var driver: Person? { @inline(__always) get { - return { + return { () -> Person? in if let actualValue = self.__driver.value { return actualValue } else { @@ -95,7 +95,7 @@ public extension Car { } @inline(__always) set { - self.__driver = { + self.__driver = { () -> bridge.std__optional_Person_ in if let actualValue = newValue { return bridge.create_std__optional_Person_(actualValue) } else { diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpec.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpec.swift index 1d5cd2115..055985697 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpec.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpec.swift @@ -56,7 +56,6 @@ public protocol HybridSwiftKotlinTestObjectSpec: HybridObjectSpec { func createNewBuffer(size: Double) throws -> ArrayBufferHolder func newTestObject() throws -> HybridSwiftKotlinTestObjectSpec func bounceBack(obj: HybridSwiftKotlinTestObjectSpec) throws -> HybridSwiftKotlinTestObjectSpec - func passVariant(val: Variant_String_Double) throws -> Void } public extension HybridSwiftKotlinTestObjectSpec { diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpecCxx.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpecCxx.swift index 90c082438..4b12c5642 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpecCxx.swift +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/HybridSwiftKotlinTestObjectSpecCxx.swift @@ -107,7 +107,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public var stringOrUndefined: bridge.std__optional_std__string_ { @inline(__always) get { - return { + return { () -> bridge.std__optional_std__string_ in if let actualValue = self.implementation.stringOrUndefined { return bridge.create_std__optional_std__string_(std.string(actualValue)) } else { @@ -117,7 +117,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { } @inline(__always) set { - self.implementation.stringOrUndefined = { + self.implementation.stringOrUndefined = { () -> String? in if let actualValue = newValue.value { return String(actualValue) } else { @@ -130,7 +130,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public var stringOrNull: bridge.std__optional_std__string_ { @inline(__always) get { - return { + return { () -> bridge.std__optional_std__string_ in if let actualValue = self.implementation.stringOrNull { return bridge.create_std__optional_std__string_(std.string(actualValue)) } else { @@ -140,7 +140,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { } @inline(__always) set { - self.implementation.stringOrNull = { + self.implementation.stringOrNull = { () -> String? in if let actualValue = newValue.value { return String(actualValue) } else { @@ -153,7 +153,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public var optionalString: bridge.std__optional_std__string_ { @inline(__always) get { - return { + return { () -> bridge.std__optional_std__string_ in if let actualValue = self.implementation.optionalString { return bridge.create_std__optional_std__string_(std.string(actualValue)) } else { @@ -163,7 +163,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { } @inline(__always) set { - self.implementation.optionalString = { + self.implementation.optionalString = { () -> String? in if let actualValue = newValue.value { return String(actualValue) } else { @@ -176,7 +176,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public var someMap: bridge.std__unordered_map_std__string__double_ { @inline(__always) get { - return { + return { () -> bridge.std__unordered_map_std__string__double_ in var map = bridge.create_std__unordered_map_std__string__double_(self.implementation.someMap.count) for (k, v) in self.implementation.someMap { map[std.string(k)] = v @@ -186,7 +186,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { } @inline(__always) set { - self.implementation.someMap = { + self.implementation.someMap = { () -> Dictionary in var dictionary = Dictionary(minimumCapacity: newValue.size()) let keys = bridge.get_std__unordered_map_std__string__double__keys(newValue) for key in keys { @@ -201,7 +201,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public var someArray: bridge.std__vector_std__string_ { @inline(__always) get { - return { + return { () -> bridge.std__vector_std__string_ in var vector = bridge.create_std__vector_std__string_(self.implementation.someArray.count) for item in self.implementation.someArray { vector.push_back(std.string(item)) @@ -218,7 +218,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public var someOptional: bridge.std__optional_std__string_ { @inline(__always) get { - return { + return { () -> bridge.std__optional_std__string_ in if let actualValue = self.implementation.someOptional { return bridge.create_std__optional_std__string_(std.string(actualValue)) } else { @@ -228,7 +228,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { } @inline(__always) set { - self.implementation.someOptional = { + self.implementation.someOptional = { () -> String? in if let actualValue = newValue.value { return String(actualValue) } else { @@ -241,7 +241,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public var car: bridge.std__optional_Car_ { @inline(__always) get { - return { + return { () -> bridge.std__optional_Car_ in if let actualValue = self.implementation.car { return bridge.create_std__optional_Car_(actualValue) } else { @@ -251,7 +251,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { } @inline(__always) set { - self.implementation.car = { + self.implementation.car = { () -> Car? in if let actualValue = newValue.value { return actualValue } else { @@ -343,7 +343,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public func getNumbers() -> bridge.std__vector_double_ { do { let result = try self.implementation.getNumbers() - return { + return { () -> bridge.std__vector_double_ in var vector = bridge.create_std__vector_double_(result.count) for item in result { vector.push_back(item) @@ -360,7 +360,7 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { public func getStrings() -> bridge.std__vector_std__string_ { do { let result = try self.implementation.getStrings() - return { + return { () -> bridge.std__vector_std__string_ in var vector = bridge.create_std__vector_std__string_(result.count) for item in result { vector.push_back(std.string(item)) @@ -418,16 +418,4 @@ public final class HybridSwiftKotlinTestObjectSpecCxx { fatalError("Swift errors can currently not be propagated to C++! See https://github.com/swiftlang/swift/issues/75290 (Error: \(message))") } } - - @inline(__always) - public func passVariant(val: bridge.std__variant_std__string__double_) -> Void { - do { - val.index() - try self.implementation.passVariant(val: val) - return - } catch { - let message = "\(error.localizedDescription)" - fatalError("Swift errors can currently not be propagated to C++! See https://github.com/swiftlang/swift/issues/75290 (Error: \(message))") - } - } } diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Variant_String_Double.swift b/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Variant_String_Double.swift deleted file mode 100644 index 0b0d0f3d3..000000000 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/swift/Variant_String_Double.swift +++ /dev/null @@ -1,17 +0,0 @@ -/// -/// Variant_String_Double.swift -/// Wed Aug 14 2024 -/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. -/// https://github.com/mrousavy/react-native-nitro -/// Copyright © 2024 Marc Rousavy @ Margelo -/// - -/** - * An Swift enum with associated values representing a Variant/Union type. - * JS type: `string | number` - */ -@frozen -public enum Variant_String_Double { - case someString(String) - case someDouble(Double) -} diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.cpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.cpp index 11f8a14c9..098e38f1d 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.cpp @@ -53,7 +53,6 @@ namespace margelo::nitro::image { prototype.registerHybridMethod("createNewBuffer", &HybridSwiftKotlinTestObjectSpec::createNewBuffer); prototype.registerHybridMethod("newTestObject", &HybridSwiftKotlinTestObjectSpec::newTestObject); prototype.registerHybridMethod("bounceBack", &HybridSwiftKotlinTestObjectSpec::bounceBack); - prototype.registerHybridMethod("passVariant", &HybridSwiftKotlinTestObjectSpec::passVariant); }); } diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.hpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.hpp index 842e8149c..46ee5ab43 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridSwiftKotlinTestObjectSpec.hpp @@ -36,7 +36,6 @@ namespace margelo::nitro::image { class HybridSwiftKotlinTestObjectSpec; } #include #include #include "HybridSwiftKotlinTestObjectSpec.hpp" -#include namespace margelo::nitro::image { @@ -103,7 +102,6 @@ namespace margelo::nitro::image { virtual std::shared_ptr createNewBuffer(double size) = 0; virtual std::shared_ptr newTestObject() = 0; virtual std::shared_ptr bounceBack(const std::shared_ptr& obj) = 0; - virtual void passVariant(const std::variant& val) = 0; protected: // Hybrid Setup diff --git a/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts b/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts index bd00c2af4..dee45091c 100644 --- a/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts +++ b/packages/react-native-nitro-image/src/specs/TestObject.nitro.ts @@ -135,6 +135,4 @@ export interface SwiftKotlinTestObject extends HybridObject<{ ios: 'swift' }> { createNewBuffer(size: number): ArrayBuffer newTestObject(): SwiftKotlinTestObject bounceBack(obj: SwiftKotlinTestObject): SwiftKotlinTestObject - - passVariant(val: string | number): void }