Skip to content

Commit

Permalink
feat: Don't expose header, fix cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 17, 2024
1 parent 444337e commit ec2be05
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 92 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ SPEC CHECKSUMS:
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f
hermes-engine: 01d3e052018c2a13937aca1860fbedbccd4a41b7
NitroModules: e4e20daccdf4e90215bfbbac9e331bca7d09da98
NitroModules: f7fd03ec90747fdb3ccabea4e7568aeedf5fc495
RCT-Folly: 02617c592a293bd6d418e0a88ff4ee1f88329b47
RCTDeprecation: b03c35057846b685b3ccadc9bfe43e349989cdb2
RCTRequired: 194626909cfa8d39ca6663138c417bc6c431648c
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-nitro-modules/NitroModules.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Pod::Spec.new do |s|
"cpp/turbomodule/RegisterNativeNitroModules.hpp",
"cpp/utils/**/*.{h,hpp}",
# iOS C++ bindings
"ios/**/*.{h,hpp}",
"ios/core/**/*.{h,hpp}",
]

s.pod_target_xcconfig = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jsi::Object NativeNitroModules::createTestHybridObject(jsi::Runtime &runtime) {

jsi::Object NativeNitroModules::createSwiftTestHybridObject(jsi::Runtime &runtime) {
// 1. Create Swift instance
auto swiftPart = NitroModules::SwiftTestHybridObjectSwift::init();
auto swiftPart = NitroModules::SwiftTestHybridObject::init();
// 2. Get or create C++ HybridObject instance wrapping Swift instance
auto hybrid = SwiftTestHybridObject::getHybridPart(swiftPart);
// 3. Return jsi::Object to JS
Expand Down
26 changes: 26 additions & 0 deletions packages/react-native-nitro-modules/cpp/utils/DoesClassExist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// DoesClassExist.hpp
// Pods
//
// Created by Marc Rousavy on 17.07.24.
//

#pragma once

#include <type_traits>

namespace margelo {

// By default, it is false (class does not exist)
template<typename T, typename = std::void_t<>>
struct does_class_exist : std::false_type {};

// If sizeof(T) can be a type (number), the type exists - so it is true
template<typename T>
struct does_class_exist<T, std::void_t<decltype(sizeof(T))>> : std::true_type {};

// Direct value accessor for does_class_exist
template<typename T>
inline constexpr bool does_class_exist_v = does_class_exist<T>::value;

} // namespace margelo
49 changes: 0 additions & 49 deletions packages/react-native-nitro-modules/ios/core/HybridSwiftObject.hpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// SwiftHybridObject.cpp
// NitroModules
//
// Created by Marc Rousavy on 17.07.24.
//

#include "SwiftHybridObject.hpp"
16 changes: 16 additions & 0 deletions packages/react-native-nitro-modules/ios/core/SwiftHybridObject.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// SwiftHybridObject.hpp
// NitroModules
//
// Created by Marc Rousavy on 17.07.24.
//

#pragma once

#include "HybridObject.hpp"

namespace margelo {



} // namespace margelo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace margelo {

std::shared_ptr<SwiftTestHybridObject> SwiftTestHybridObject::getHybridPart(NitroModules::SwiftTestHybridObjectSwift swiftPart) {
std::shared_ptr<SwiftTestHybridObject> SwiftTestHybridObject::getHybridPart(NitroModules::SwiftTestHybridObject swiftPart) {
{
// 1. Check if we have a C++ context already created and stored in Swift
HybridContext hybridContext = swiftPart.getHybridContext();
Expand All @@ -38,7 +38,7 @@ std::shared_ptr<SwiftTestHybridObject> SwiftTestHybridObject::getHybridPart(Nitr
return hybridObject;
}

SwiftTestHybridObject::SwiftTestHybridObject(NitroModules::SwiftTestHybridObjectSwift swiftPart):
SwiftTestHybridObject::SwiftTestHybridObject(NitroModules::SwiftTestHybridObject swiftPart):
HybridObject("SwiftTestHybridObject"), _swiftPart(swiftPart) {
Logger::log("SwiftTestHybridObject", "Created a new SwiftTestHybridObject (C++)!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@

#if __has_include("NitroModules-Swift.h")
#include "NitroModules-Swift.h"
#include "DoesClassExist.hpp"

namespace margelo {

static_assert(does_class_exist_v<NitroModules::SwiftTestHybridObject>,
"Swift class \"SwiftTestHybridObject\" does not exist! Does the class exist in Swift, and is it marked as public?");

// Generated by Nitrogen (C++ HybridObject bindings)
class SwiftTestHybridObject: public HybridObject {
private:
explicit SwiftTestHybridObject(NitroModules::SwiftTestHybridObjectSwift swiftPart);
explicit SwiftTestHybridObject(NitroModules::SwiftTestHybridObject swiftPart);

public:
static std::shared_ptr<SwiftTestHybridObject> getHybridPart(NitroModules::SwiftTestHybridObjectSwift swiftPart);
static std::shared_ptr<SwiftTestHybridObject> getHybridPart(NitroModules::SwiftTestHybridObject swiftPart);

public:

Expand All @@ -30,7 +35,7 @@ class SwiftTestHybridObject: public HybridObject {
void loadHybridMethods() override;

private:
NitroModules::SwiftTestHybridObjectSwift _swiftPart;
NitroModules::SwiftTestHybridObject _swiftPart;
};

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// SwiftTestHybridObjectSwift.swift
// NitroModules
//
// Created by Marc Rousavy on 23.06.24.
//

import Foundation

public enum SomeError: Error {
case error
}

// Implemented by the user
public class SwiftTestHybridObject: SwiftTestHybridObjectSpec {
public func throwError() throws {
throw SomeError.error
}

public var hybridContext = margelo.HybridContext()

private var _int: Int = 5
public var int: Int {
get {
return _int
}
set {
_int = newValue
}
}

public init() {
print("Initialized a new SwiftTestHybridObject!")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TestHybridObjectSpec.swift
// NitroModules
//
// Created by Marc Rousavy on 17.07.24.
//

import Foundation

// Generated by Nitrogen (Swift protocol spec)
public protocol SwiftTestHybridObjectSpec {
var hybridContext: margelo.HybridContext { get set }

var int: Int { get set }

func throwError() throws
}

This file was deleted.

0 comments on commit ec2be05

Please sign in to comment.