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

fix: Replace HybridObjectSpec (a protocol) with HybridObject (a base class) #486

Closed
wants to merge 6 commits into from
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
2 changes: 1 addition & 1 deletion docs/docs/nitrogen.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Math extends HybridObject {
<div className="side-by-side-block">

```swift title="HybridMathSpec.swift (generated)"
protocol HybridMathSpec: HybridObjectSpec {
protocol HybridMathSpec: HybridObject {
var pi: Double { get }
func add(a: Double, b: Double) -> Double
}
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/types/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ These are all the types Nitro supports out of the box:
<tr>
<td>..any <code><a href="https://github.com/mrousavy/nitro/blob/main/packages/react-native-nitro-modules/src/HybridObject.ts">HybridObject</a></code></td>
<td><code>std::shared_ptr&lt;<a href="https://github.com/mrousavy/nitro/blob/main/packages/react-native-nitro-modules/cpp/core/HybridObject.hpp">HybridObject</a>&gt;</code></td>
<td><code><a href="https://github.com/mrousavy/nitro/blob/main/packages/react-native-nitro-modules/ios/core/HybridObjectSpec.swift">HybridObjectSpec</a></code></td>
<td><code><a href="https://github.com/mrousavy/nitro/blob/main/packages/react-native-nitro-modules/ios/core/HybridObject.swift">HybridObject</a></code></td>
<td><code><a href="https://github.com/mrousavy/nitro/blob/main/packages/react-native-nitro-modules/android/src/main/java/com/margelo/nitro/core/HybridObject.kt">HybridObject</a></code></td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions example/ios/NitroExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../../node_modules/react-native";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
SWIFT_OBJC_INTEROP_MODE = objcxx;
Expand Down Expand Up @@ -705,7 +705,7 @@
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../../node_modules/react-native";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
SWIFT_OBJC_INTEROP_MODE = objcxx;
USE_HERMES = true;
Expand Down
100 changes: 50 additions & 50 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1894,70 +1894,70 @@ SPEC CHECKSUMS:
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
hermes-engine: 1949ca944b195a8bde7cbf6316b9068e19cf53c6
NitroImage: ff97c5986ea4619abd3d6399b886eac84f5a4b65
NitroModules: bbe10b61d6fefce0176ba06e6a34eea92efb7704
RCT-Folly: bf5c0376ffe4dd2cf438dcf86db385df9fdce648
NitroImage: 39c21a6d585fea258a1eab5cd8236457ad06fb62
NitroModules: 3a58d9bc70815a0d5de4476ed6a36eff05a6a0ae
RCT-Folly: 84578c8756030547307e4572ab1947de1685c599
RCTDeprecation: 063fc281b30b7dc944c98fe53a7e266dab1a8706
RCTRequired: 8eda2a5a745f6081157a4f34baac40b65fe02b31
RCTTypeSafety: 0f96bf6c99efc33eb43352212703854933f22930
React: 1d3d5bada479030961d513fb11e42659b30e97ff
React-callinvoker: 682c610b9e9d3b93bd8d0075eacb2e6aa304d3e0
React-Core: 10420b32e62acf6b3aa0a570e45566001175c777
React-CoreModules: aad977a7dbff83aa707c4045e5db81446a511cca
React-cxxreact: 1bee1b97e7d537f1a33d9eb68c9426c1fc1a4e3c
React-Core: 9f33c0fc7776a5796d4dae09c363bd58e6a27efe
React-CoreModules: 91afb654834f0a1f48fb26dd1f4d1a1460c44def
React-cxxreact: c7491114266a70f8215306f1d0c4b54a811e77cf
React-debug: 4ae2e95c2d392cca29939a3a2f2b4320ddff3e59
React-defaultsnativemodule: b585565214178c5780b54e4d56815d65782eac81
React-domnativemodule: 03fd1847e49505aa9024acbe4f0811e441dc89a2
React-Fabric: fc0898bb601b03ed41ab0df3e7b1a4acd05a6cff
React-FabricComponents: 13e78253b210d112b3ffddca5b7323db7f254358
React-FabricImage: a86ff938570a06c2a9fbf00ff0b00195f0bd4aba
React-defaultsnativemodule: 43d27f1844b4c18fc03fa4fa35ea2f1c48d64237
React-domnativemodule: bca178dd0ce1532f75be783f6f2923f675a778ae
React-Fabric: d6bc0222335270eb76c28dd5036c03a010c04d51
React-FabricComponents: 05eec9e2cf998be793daaee8fa8a8ea6d1187785
React-FabricImage: 3a12374b0aedda71c7ef6bd64b59479b8bb3fe05
React-featureflags: 5670e0dcdc17ba2515963d117dacc13d5b69c431
React-featureflagsnativemodule: 79dea40c60cdc0356aadc67a099bba0af8c34e4f
React-graphics: 04eed50a115e750e4644c1e955f32bec57f6a235
React-hermes: add932964f5ef024c86352dcc0dc427e6309642e
React-idlecallbacksnativemodule: 3e8d5085a21eb2f70ac64ff9817f8f8a603518a9
React-ImageManager: 3239badd14cc602baf836b5d7151ffa90393deae
React-jserrorhandler: 81ac36638e02c33a9df0bdbeec464d2e699ac8a9
React-jsi: 690f3742db66cab8d5219bcfbc19fee112c6bb0c
React-jsiexecutor: a060f7e989da21e2478f652d7799e3b5ae5db262
React-jsinspector: 0eb6ea6f6b1e42edeab4bcad092d37ef748e337a
React-jsitracing: 737a69a469e2bc821cf8ae11977bded522393719
React-logger: 162c09cc432b02d4a0db31b1d98f6df5243a2679
React-Mapbuffer: f760d2229640be48cb3c2d4832b5bbc3018123fc
React-microtasksnativemodule: 1364ae5354f51b3ecee8eb718b5b6d1686d2ff4d
react-native-safe-area-context: d6406c2adbd41b2e09ab1c386781dc1c81a90919
react-native-segmented-control: b92809e9111013dfa266e1168ba366d62898d9a4
React-featureflagsnativemodule: bb13129d1427327b2eb8cc13d3879363a4cd8326
React-graphics: 659968f797257c0071ddff28a2d094c8e5c5899c
React-hermes: 6eb81c6f72c25d9058b6030227d0fcc1f741a807
React-idlecallbacksnativemodule: 551b7a89b46041c746640fe13eacf39c1b169709
React-ImageManager: e3d0270c82bf39432da2aff2fcd60dd16b308689
React-jserrorhandler: f60c9b68b4d4ac1449bddc2553610708e939ddee
React-jsi: 47528a2928f38fe15e3d06a96de886e1a779ffc7
React-jsiexecutor: 88a141c4dc821e1b2aa7ecc7d6af7b186e8455a2
React-jsinspector: c26cf4118ea7c1aae721d2cde5acf3b2cdceb814
React-jsitracing: 810d0465c3455e352a71147c18332b1cba1d1410
React-logger: d42a53754a7252cc7a851315f0da2e46b450ea92
React-Mapbuffer: 89885d1518433a462fe64b68bf5e097997380090
React-microtasksnativemodule: 36341e09dcd1df535503e6ed2ddf88f10da56d52
react-native-safe-area-context: f145b8906585d648f9f9c7dfa3885bac54f38fcc
react-native-segmented-control: 5acfc346871654bd2272126159aaaae48f10fbe6
React-nativeconfig: 539ff4de6ce3b694e8e751080568c281c84903ce
React-NativeModulesApple: 771cc40b086281b820fe455fedebbe4341fd0c90
React-perflogger: 4e80a5b8d61dfb38024e7d5b8efaf9ce1037d31f
React-performancetimeline: 1dcacc31d81f790f43a2d434ec95b0f864582329
React-NativeModulesApple: 702246817c286d057e23fe4b1302019796e62521
React-perflogger: f260e2de95f9dbd002035251559c13bf1f0643d4
React-performancetimeline: 957075cead70fe9536a327eb4f842b3d8982f2ec
React-RCTActionSheet: ed5a845eae98fe455b9a1d71f057d626e2f3f1c5
React-RCTAnimation: 0cda303ef8ca5a2d0ee9e425f188cc9fc1d2e20f
React-RCTAppDelegate: 1edcdebdaebf5120bdaa9d54bc40789714be3719
React-RCTBlob: dab83a3c22210e5c7a8267834c68e6cf94bc1ce2
React-RCTFabric: 19ba31d6b913b8b4aa8b27e4d6f5dc8ebd93a438
React-RCTImage: b9c3d2cff3b8214322022cdf8afb92ff978bb92e
React-RCTLinking: e58c4fa216f9ac87ed3d4a0cce03905df67adec0
React-RCTNetwork: 9f206fa039e107f51ddfac133df79105643ea2bd
React-RCTSettings: c7663cfcb3531cd438b8f73e98cd2d982a4bbd72
React-RCTText: cfee29316f1049f016cbd81328a89a8a07410bba
React-RCTVibration: 20f5efc1b05cd3f5f7ea03489dd3766c890fb493
React-RCTAnimation: a49bd2c28c3f32b1d01ff1163603aee3d420ce42
React-RCTAppDelegate: f7aa2f938a6673cfd2a76e76fea8c4b38a4a5bec
React-RCTBlob: 8ddf30f97222f4d8227f64428349fd8252292cb5
React-RCTFabric: 51fb64f7ca7ca2fa334433ba6d4f12750a481cf1
React-RCTImage: 077a25f3a9a6b79938a01c2cfae05ea5f07fc584
React-RCTLinking: 0c8415c600942454d663c4c4dc0d3b00aa7ba5e5
React-RCTNetwork: 42a3c6fb5318dcc9f8796f43de081799fb905021
React-RCTSettings: 1028522e45192515bd8c5308752d3270ee95fd66
React-RCTText: 29ef786d78f69ec5b571634ef2ddd6ec177c958a
React-RCTVibration: 97859ed50816369f4830f623dfac8dc9877f3c5c
React-rendererconsistency: ccd50d5ee6544b26cd11fff5ad1112c5058a0064
React-rendererdebug: d8f43065459c2095f27a173121f03dcd1d1b08e5
React-rendererdebug: 2092a1207a0023ac30877f4f730b403bfaf5ccbe
React-rncore: bfe554cb773978e8b94898866964c9579cb0c70c
React-RuntimeApple: 89c319b1610d4ca8895642cf0eae1188bf864270
React-RuntimeCore: 30399cbd2368f7e031692875275984fa42142601
React-RuntimeApple: 80949ebe7e6a94296e0168a940832d2029b66982
React-RuntimeCore: f04b5d1eb0534a4f4f46bc76a938a9360ad91024
React-runtimeexecutor: 26a9d14619ec1359470df391be9abb7c80a21b2b
React-RuntimeHermes: c78f07b7a599c1c9a889189c02436600e72c8b27
React-runtimescheduler: 9f6b0b85154ed8a17a899cd1bab258a26c8db2cd
React-RuntimeHermes: 91c2a67a99f316f11a08d3d9280ab4c9fae59b56
React-runtimescheduler: 76bb85f5ba01e800b4970fbc84eeaf10756c50c4
React-timing: c9c7c0fe2fdfc433ef208889b6191dfb45457d68
React-utils: e6697b03f21c7ac57b075d848cda7882662cabf7
ReactCodegen: ea6042d14b12c4c5b17889270956ec2d508e154a
ReactCommon: 832cdd669aeecd430d9ca1975d15676b38d0b263
RNScreens: 2fe13c8d610ef2d9d5ace2e7d85b716ec0f5217c
React-utils: 1b14c41c3edf4d96db1247a78e0ad96e7ceea011
ReactCodegen: 51656188195fcd8f4025da822392840d390b3c74
ReactCommon: 3c1c8c6d777103c0e60e37c6c5f08e828e2a77c9
RNScreens: e6e9c91fc2507887016a184e00013a1240580b85
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Yoga: 2a5ae8f7db3c675ff5a781fb5d99c5f7a5d2fc11
Yoga: be6f55a028e86c83ae066f018e9b5d24ffc45436

PODFILE CHECKSUM: bd3f52fb9dca02f373eb874756f56fe0737bf773

COCOAPODS: 1.15.2
COCOAPODS: 1.16.2
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ ${includes.sort().join('\n')}
#include <NitroModules/RuntimeError.hpp>

// Forward declarations of Swift defined types
namespace NitroModules { class HybridObject; }
${swiftForwardDeclares.sort().join('\n')}

// Include Swift defined types
Expand Down
5 changes: 2 additions & 3 deletions packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public ${hasBaseClass ? 'override func' : 'func'} getCxxWrapper() -> ${name.Hybr
}`.trim()
)
if (!hasBaseClass) {
// It doesn't have a base class - implement the `HybridObjectSpec` base protocol
classBaseClasses.push('HybridObjectSpec')
baseMembers.push(`public var memorySize: Int { return 0 }`)
// It doesn't have a base class - inherit from the `HybridObject` base class
classBaseClasses.push('HybridObject')
}

const protocolCode = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace margelo::nitro::image { enum class Powertrain; }
#include <NitroModules/RuntimeError.hpp>

// Forward declarations of Swift defined types
namespace NitroModules { class HybridObject; }
// Forward declaration of `HybridBaseSpec_cxx` to properly resolve imports.
namespace NitroImage { class HybridBaseSpec_cxx; }
// Forward declaration of `HybridChildSpec_cxx` to properly resolve imports.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public protocol HybridBaseSpec_protocol: AnyObject {
}

/// See ``HybridBaseSpec``
public class HybridBaseSpec_base: HybridObjectSpec {
public class HybridBaseSpec_base: HybridObject {
private weak var cxxWrapper: HybridBaseSpec_cxx? = nil
public func getCxxWrapper() -> HybridBaseSpec_cxx {
#if DEBUG
Expand All @@ -34,7 +34,6 @@ public class HybridBaseSpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol HybridImageFactorySpec_protocol: AnyObject {
}

/// See ``HybridImageFactorySpec``
public class HybridImageFactorySpec_base: HybridObjectSpec {
public class HybridImageFactorySpec_base: HybridObject {
private weak var cxxWrapper: HybridImageFactorySpec_cxx? = nil
public func getCxxWrapper() -> HybridImageFactorySpec_cxx {
#if DEBUG
Expand All @@ -37,7 +37,6 @@ public class HybridImageFactorySpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol HybridImageSpec_protocol: AnyObject {
}

/// See ``HybridImageSpec``
public class HybridImageSpec_base: HybridObjectSpec {
public class HybridImageSpec_base: HybridObject {
private weak var cxxWrapper: HybridImageSpec_cxx? = nil
public func getCxxWrapper() -> HybridImageSpec_cxx {
#if DEBUG
Expand All @@ -37,7 +37,6 @@ public class HybridImageSpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public protocol HybridTestObjectSwiftKotlinSpec_protocol: AnyObject {
}

/// See ``HybridTestObjectSwiftKotlinSpec``
public class HybridTestObjectSwiftKotlinSpec_base: HybridObjectSpec {
public class HybridTestObjectSwiftKotlinSpec_base: HybridObject {
private weak var cxxWrapper: HybridTestObjectSwiftKotlinSpec_cxx? = nil
public func getCxxWrapper() -> HybridTestObjectSwiftKotlinSpec_cxx {
#if DEBUG
Expand All @@ -95,7 +95,6 @@ public class HybridTestObjectSwiftKotlinSpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return 0 }
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-nitro-modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ The following C++ / JS types are supported out of the box:
<tr>
<td>..any <code><a href="./src/HybridObject.ts">HybridObject</a></code></td>
<td><code>std::shared_ptr&lt;<a href="./cpp/core/HybridObject.hpp">HybridObject</a>&gt;</code></td>
<td><code><a href="./ios/core/HybridObjectSpec.swift">HybridObjectSpec</a></code></td>
<td><code><a href="./ios/core/HybridObject.swift">HybridObject</a></code></td>
<td><code><a href="./android/src/main/java/com/margelo/nitro/core/HybridObject.kt">HybridObject</a></code></td>
</tr>
<tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
//
// HybridObjectSpec.swift
// HybridObject.swift
// NitroModules
//
// Created by Marc Rousavy on 23.07.24.
//

import Foundation

@available(*, deprecated, message: "HybridObjectSpec (a protocol) has been replaced with HybridObject (a class).")
public protocol HybridObjectSpec: AnyObject {
var memorySize: Int { get }
}

/**
* A base protocol for all Swift-based Hybrid Objects.
* The base class for all Swift-based HybridObjects.
*/
public protocol HybridObjectSpec: AnyObject {
open class HybridObject: HybridObjectSpec {
/**
* Get the memory size of the Swift instance (plus any external heap allocations),
* in bytes.
* Get the memory size of any external heap allocations in bytes.
*
* Override this to allow tracking heap allocations such as buffers or images,
* which will help the JS GC be more efficient in deleting large unused objects.
*
* @default 0
* @example
* ```swift
* var memorySize: Int {
* override public var memorySize: Int {
* let imageSize = self.uiImage.bytesPerRow * self.uiImage.height
* return imageSize
* }
* ```
*/
var memorySize: Int { get }
open var memorySize: Int { return 0 }
}

public extension HybridObjectSpec {
Expand Down
Loading