Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove hybridContext and memorySize from Swift HybridObjects (make them optional) #420

Merged
merged 5 commits into from
Dec 17, 2024
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
2 changes: 1 addition & 1 deletion packages/nitrogen/src/syntax/swift/SwiftCxxBridgedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ case ${i}:
const createFunc = `bridge.${bridge.funcName}`
return `
{ () -> bridge.${bridge.specializationName} in
class ClosureHolder {
final class ClosureHolder {
let closure: ${func.getCode('swift')}
init(wrappingClosure closure: @escaping ${func.getCode('swift')}) {
self.closure = closure
Expand Down
52 changes: 30 additions & 22 deletions packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ export function createSwiftHybridObject(spec: HybridObjectSpec): SourceFile[] {
const properties = spec.properties.map((p) => p.getCode('swift')).join('\n')
const methods = spec.methods.map((p) => p.getCode('swift')).join('\n')

const baseClasses = ['HybridObjectSpec']
const protocolBaseClasses = ['AnyObject']
const classBaseClasses: string[] = []
for (const base of spec.baseTypes) {
const baseName = getHybridObjectName(base.name)
baseClasses.push(baseName.HybridTSpec)
protocolBaseClasses.push(`${baseName.HybridTSpec}_protocol`)
classBaseClasses.push(`${baseName.HybridTSpec}_base`)
}

const baseMembers: string[] = []
if (classBaseClasses.length === 0) {
// It doesn't have a base class - implement hybridContext
classBaseClasses.push('HybridObjectSpec')
baseMembers.push(`public var hybridContext = margelo.nitro.HybridContext()`)
baseMembers.push(`public var memorySize: Int { return getSizeOf(self) }`)
}

const protocolCode = `
Expand All @@ -23,32 +33,30 @@ ${createFileMetadataString(`${protocolName}.swift`)}
import Foundation
import NitroModules

/**
* A Swift protocol representing the ${spec.name} HybridObject.
* Implement this protocol to create Swift-based instances of ${spec.name}.
*
* When implementing this protocol, make sure to initialize \`hybridContext\` - example:
* \`\`\`
* public class ${name.HybridT} : ${protocolName} {
* // Initialize HybridContext
* var hybridContext = margelo.nitro.HybridContext()
*
* // Return size of the instance to inform JS GC about memory pressure
* var memorySize: Int {
* return getSizeOf(self)
* }
*
* // ...
* }
* \`\`\`
*/
public protocol ${protocolName}: AnyObject, ${baseClasses.join(', ')} {
/// See \`\`${protocolName}\`\`
public protocol ${protocolName}_protocol: ${protocolBaseClasses.join(', ')} {
// Properties
${indent(properties, ' ')}

// Methods
${indent(methods, ' ')}
}

/// See \`\`${protocolName}\`\`
public class ${protocolName}_base: ${classBaseClasses.join(', ')} {
${baseMembers.length > 0 ? indent(baseMembers.join('\n'), ' ') : `/* inherited */`}
}

/**
* A Swift base-protocol representing the ${spec.name} HybridObject.
* Implement this protocol to create Swift-based instances of ${spec.name}.
* \`\`\`swift
* class ${name.HybridT} : ${protocolName} {
* // ...
* }
* \`\`\`
*/
public typealias ${protocolName} = ${protocolName}_protocol & ${protocolName}_base
`

const swiftBridge = createSwiftHybridObjectCxxBridge(spec)
Expand Down
5 changes: 0 additions & 5 deletions packages/react-native-nitro-image/ios/HybridBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
import Foundation

class HybridBase : HybridBaseSpec {
var hybridContext = margelo.nitro.HybridContext()
var memorySize: Int {
return getSizeOf(self)
}

var baseValue: Double {
return 10
}
Expand Down
5 changes: 0 additions & 5 deletions packages/react-native-nitro-image/ios/HybridChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
import Foundation

class HybridChild : HybridChildSpec {
var hybridContext = margelo.nitro.HybridContext()
var memorySize: Int {
return getSizeOf(self)
}

var baseValue: Double {
return 20
}
Expand Down
6 changes: 1 addition & 5 deletions packages/react-native-nitro-image/ios/HybridImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ class HybridImage : HybridImageSpec {
* The actual implementation uses an iOS `UIImage`.
*/
private let uiImage: UIImage
/**
* Just default initialize HybridContext - this holds the C++ state.
*/
public var hybridContext = margelo.nitro.HybridContext()
/**
* Get the memory size of the Swift class, and the `UIImage` we allocated so JS
* can efficiently garbage collect it when needed.
*/
public var memorySize: Int {
public override var memorySize: Int {
return getSizeOf(self) + uiImage.memorySize
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import Foundation
import NitroModules

class HybridImageFactory : HybridImageFactorySpec {
var hybridContext = margelo.nitro.HybridContext()

var memorySize: Int {
return getSizeOf(self)
}

func loadImageFromFile(path: String) throws -> any HybridImageSpec {
guard let uiImage = UIImage(contentsOfFile: path) else {
throw RuntimeError.error(withMessage: "Failed to load UIImage from \(path)!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import Foundation
import NitroModules

class HybridTestObjectSwift : HybridTestObjectSwiftKotlinSpec {
var hybridContext: margelo.nitro.HybridContext = .init()
var memorySize: Int {
return 0
}

var optionalArray: [String]? = []

var someVariant: Variant_String_Double = .someDouble(55)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,28 @@
import Foundation
import NitroModules

/**
* A Swift protocol representing the Base HybridObject.
* Implement this protocol to create Swift-based instances of Base.
*
* When implementing this protocol, make sure to initialize `hybridContext` - example:
* ```
* public class HybridBase : HybridBaseSpec {
* // Initialize HybridContext
* var hybridContext = margelo.nitro.HybridContext()
*
* // Return size of the instance to inform JS GC about memory pressure
* var memorySize: Int {
* return getSizeOf(self)
* }
*
* // ...
* }
* ```
*/
public protocol HybridBaseSpec: AnyObject, HybridObjectSpec {
/// See ``HybridBaseSpec``
public protocol HybridBaseSpec_protocol: AnyObject {
// Properties
var baseValue: Double { get }

// Methods

}

/// See ``HybridBaseSpec``
public class HybridBaseSpec_base: HybridObjectSpec {
public var hybridContext = margelo.nitro.HybridContext()
public var memorySize: Int { return getSizeOf(self) }
}

/**
* A Swift base-protocol representing the Base HybridObject.
* Implement this protocol to create Swift-based instances of Base.
* ```swift
* class HybridBase : HybridBaseSpec {
* // ...
* }
* ```
*/
public typealias HybridBaseSpec = HybridBaseSpec_protocol & HybridBaseSpec_base
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@
import Foundation
import NitroModules

/**
* A Swift protocol representing the Child HybridObject.
* Implement this protocol to create Swift-based instances of Child.
*
* When implementing this protocol, make sure to initialize `hybridContext` - example:
* ```
* public class HybridChild : HybridChildSpec {
* // Initialize HybridContext
* var hybridContext = margelo.nitro.HybridContext()
*
* // Return size of the instance to inform JS GC about memory pressure
* var memorySize: Int {
* return getSizeOf(self)
* }
*
* // ...
* }
* ```
*/
public protocol HybridChildSpec: AnyObject, HybridObjectSpec, HybridBaseSpec {
/// See ``HybridChildSpec``
public protocol HybridChildSpec_protocol: AnyObject, HybridBaseSpec_protocol {
// Properties
var childValue: Double { get }

// Methods

}

/// See ``HybridChildSpec``
public class HybridChildSpec_base: HybridBaseSpec_base {
/* inherited */
}

/**
* A Swift base-protocol representing the Child HybridObject.
* Implement this protocol to create Swift-based instances of Child.
* ```swift
* class HybridChild : HybridChildSpec {
* // ...
* }
* ```
*/
public typealias HybridChildSpec = HybridChildSpec_protocol & HybridChildSpec_base
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,8 @@
import Foundation
import NitroModules

/**
* A Swift protocol representing the ImageFactory HybridObject.
* Implement this protocol to create Swift-based instances of ImageFactory.
*
* When implementing this protocol, make sure to initialize `hybridContext` - example:
* ```
* public class HybridImageFactory : HybridImageFactorySpec {
* // Initialize HybridContext
* var hybridContext = margelo.nitro.HybridContext()
*
* // Return size of the instance to inform JS GC about memory pressure
* var memorySize: Int {
* return getSizeOf(self)
* }
*
* // ...
* }
* ```
*/
public protocol HybridImageFactorySpec: AnyObject, HybridObjectSpec {
/// See ``HybridImageFactorySpec``
public protocol HybridImageFactorySpec_protocol: AnyObject {
// Properties


Expand All @@ -37,3 +19,20 @@ public protocol HybridImageFactorySpec: AnyObject, HybridObjectSpec {
func loadImageFromSystemName(path: String) throws -> (any HybridImageSpec)
func bounceBack(image: (any HybridImageSpec)) throws -> (any HybridImageSpec)
}

/// See ``HybridImageFactorySpec``
public class HybridImageFactorySpec_base: HybridObjectSpec {
public var hybridContext = margelo.nitro.HybridContext()
public var memorySize: Int { return getSizeOf(self) }
}

/**
* A Swift base-protocol representing the ImageFactory HybridObject.
* Implement this protocol to create Swift-based instances of ImageFactory.
* ```swift
* class HybridImageFactory : HybridImageFactorySpec {
* // ...
* }
* ```
*/
public typealias HybridImageFactorySpec = HybridImageFactorySpec_protocol & HybridImageFactorySpec_base
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,8 @@
import Foundation
import NitroModules

/**
* A Swift protocol representing the Image HybridObject.
* Implement this protocol to create Swift-based instances of Image.
*
* When implementing this protocol, make sure to initialize `hybridContext` - example:
* ```
* public class HybridImage : HybridImageSpec {
* // Initialize HybridContext
* var hybridContext = margelo.nitro.HybridContext()
*
* // Return size of the instance to inform JS GC about memory pressure
* var memorySize: Int {
* return getSizeOf(self)
* }
*
* // ...
* }
* ```
*/
public protocol HybridImageSpec: AnyObject, HybridObjectSpec {
/// See ``HybridImageSpec``
public protocol HybridImageSpec_protocol: AnyObject {
// Properties
var size: ImageSize { get }
var pixelFormat: PixelFormat { get }
Expand All @@ -37,3 +19,20 @@ public protocol HybridImageSpec: AnyObject, HybridObjectSpec {
func toArrayBuffer(format: ImageFormat) throws -> Double
func saveToFile(path: String, onFinished: @escaping ((_ path: String) -> Void)) throws -> Void
}

/// See ``HybridImageSpec``
public class HybridImageSpec_base: HybridObjectSpec {
public var hybridContext = margelo.nitro.HybridContext()
public var memorySize: Int { return getSizeOf(self) }
}

/**
* A Swift base-protocol representing the Image HybridObject.
* Implement this protocol to create Swift-based instances of Image.
* ```swift
* class HybridImage : HybridImageSpec {
* // ...
* }
* ```
*/
public typealias HybridImageSpec = HybridImageSpec_protocol & HybridImageSpec_base
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,8 @@
import Foundation
import NitroModules

/**
* A Swift protocol representing the TestObjectSwiftKotlin HybridObject.
* Implement this protocol to create Swift-based instances of TestObjectSwiftKotlin.
*
* When implementing this protocol, make sure to initialize `hybridContext` - example:
* ```
* public class HybridTestObjectSwiftKotlin : HybridTestObjectSwiftKotlinSpec {
* // Initialize HybridContext
* var hybridContext = margelo.nitro.HybridContext()
*
* // Return size of the instance to inform JS GC about memory pressure
* var memorySize: Int {
* return getSizeOf(self)
* }
*
* // ...
* }
* ```
*/
public protocol HybridTestObjectSwiftKotlinSpec: AnyObject, HybridObjectSpec {
/// See ``HybridTestObjectSwiftKotlinSpec``
public protocol HybridTestObjectSwiftKotlinSpec_protocol: AnyObject {
// Properties
var thisObject: (any HybridTestObjectSwiftKotlinSpec) { get }
var optionalHybrid: (any HybridTestObjectSwiftKotlinSpec)? { get set }
Expand Down Expand Up @@ -88,3 +70,20 @@ public protocol HybridTestObjectSwiftKotlinSpec: AnyObject, HybridObjectSpec {
func bounceChildBase(child: (any HybridChildSpec)) throws -> (any HybridBaseSpec)
func castBase(base: (any HybridBaseSpec)) throws -> (any HybridChildSpec)
}

/// See ``HybridTestObjectSwiftKotlinSpec``
public class HybridTestObjectSwiftKotlinSpec_base: HybridObjectSpec {
public var hybridContext = margelo.nitro.HybridContext()
public var memorySize: Int { return getSizeOf(self) }
}

/**
* A Swift base-protocol representing the TestObjectSwiftKotlin HybridObject.
* Implement this protocol to create Swift-based instances of TestObjectSwiftKotlin.
* ```swift
* class HybridTestObjectSwiftKotlin : HybridTestObjectSwiftKotlinSpec {
* // ...
* }
* ```
*/
public typealias HybridTestObjectSwiftKotlinSpec = HybridTestObjectSwiftKotlinSpec_protocol & HybridTestObjectSwiftKotlinSpec_base
Loading
Loading