Skip to content

Commit

Permalink
perf: Mark all types that are used only in std::shared_ptr<T> as `S…
Browse files Browse the repository at this point in the history
…WIFT_NONCOPYABLE` (#435)

* fix: Mark all types that are used only in `std::shared_ptr<T>` as `SWIFT_UNSAFE_REFERENCE`

* Update AnyMap.hpp

* fix: Add `SWIFT_PRIVATE`

* fix: Use `SWIFT_NONCOPYABLE` instead!! THAT WORKS
  • Loading branch information
mrousavy authored Dec 20, 2024
1 parent 08f3c6c commit 196fc9d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/nitrogen/src/syntax/swift/SwiftCxxTypeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public:
}
private:
${actualType} _function;
};
} SWIFT_NONCOPYABLE;
inline ${name} create_${name}(void* _Nonnull closureHolder, ${functionPointerParam}, void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return ${name}([sharedClosureHolder = std::move(sharedClosureHolder), call](${paramsSignature.join(', ')}) -> ${type.returnType.getCode('c++')} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(const std::string& /* path */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_std__string create_Func_void_std__string(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, std::string), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_std__string([sharedClosureHolder = std::move(sharedClosureHolder), call](const std::string& path) -> void {
Expand Down Expand Up @@ -271,7 +271,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(const std::vector<Powertrain>& /* array */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_std__vector_Powertrain_ create_Func_void_std__vector_Powertrain_(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, std::vector<Powertrain>), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_std__vector_Powertrain_([sharedClosureHolder = std::move(sharedClosureHolder), call](const std::vector<Powertrain>& array) -> void {
Expand Down Expand Up @@ -308,7 +308,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void()> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void create_Func_void(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void([sharedClosureHolder = std::move(sharedClosureHolder), call]() -> void {
Expand Down Expand Up @@ -336,7 +336,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(const std::exception_ptr& /* error */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_std__exception_ptr create_Func_void_std__exception_ptr(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, std::exception_ptr), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_std__exception_ptr([sharedClosureHolder = std::move(sharedClosureHolder), call](const std::exception_ptr& error) -> void {
Expand Down Expand Up @@ -411,7 +411,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(int64_t /* result */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_int64_t create_Func_void_int64_t(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, int64_t), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_int64_t([sharedClosureHolder = std::move(sharedClosureHolder), call](int64_t result) -> void {
Expand Down Expand Up @@ -448,7 +448,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(double /* result */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_double create_Func_void_double(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, double), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_double([sharedClosureHolder = std::move(sharedClosureHolder), call](double result) -> void {
Expand Down Expand Up @@ -494,7 +494,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(const Car& /* result */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_Car create_Func_void_Car(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, Car), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_Car([sharedClosureHolder = std::move(sharedClosureHolder), call](const Car& result) -> void {
Expand Down Expand Up @@ -531,7 +531,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(std::optional<double> /* maybe */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_std__optional_double_ create_Func_void_std__optional_double_(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, std::optional<double>), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_std__optional_double_([sharedClosureHolder = std::move(sharedClosureHolder), call](std::optional<double> maybe) -> void {
Expand Down Expand Up @@ -560,7 +560,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<std::shared_ptr<Promise<double>>()> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_std__shared_ptr_Promise_double__ create_Func_std__shared_ptr_Promise_double__(void* _Nonnull closureHolder, std::shared_ptr<Promise<double>>(* _Nonnull call)(void* _Nonnull /* closureHolder */), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_std__shared_ptr_Promise_double__([sharedClosureHolder = std::move(sharedClosureHolder), call]() -> std::shared_ptr<Promise<double>> {
Expand Down Expand Up @@ -590,7 +590,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<std::shared_ptr<Promise<std::string>>()> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_std__shared_ptr_Promise_std__string__ create_Func_std__shared_ptr_Promise_std__string__(void* _Nonnull closureHolder, std::shared_ptr<Promise<std::string>>(* _Nonnull call)(void* _Nonnull /* closureHolder */), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_std__shared_ptr_Promise_std__string__([sharedClosureHolder = std::move(sharedClosureHolder), call]() -> std::shared_ptr<Promise<std::string>> {
Expand Down Expand Up @@ -637,7 +637,7 @@ namespace margelo::nitro::image::bridge::swift {
}
private:
std::function<void(const std::shared_ptr<ArrayBuffer>& /* result */)> _function;
};
} SWIFT_NONCOPYABLE;
inline Func_void_std__shared_ptr_ArrayBuffer_ create_Func_void_std__shared_ptr_ArrayBuffer_(void* _Nonnull closureHolder, void(* _Nonnull call)(void* _Nonnull /* closureHolder */, ArrayBufferHolder), void(* _Nonnull destroy)(void* _Nonnull)) {
std::shared_ptr<void> sharedClosureHolder(closureHolder, destroy);
return Func_void_std__shared_ptr_ArrayBuffer_([sharedClosureHolder = std::move(sharedClosureHolder), call](const std::shared_ptr<ArrayBuffer>& result) -> void {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native-nitro-modules/cpp/core/AnyMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include "NitroDefines.hpp"
#include <map>
#include <memory>
#include <string>
Expand Down Expand Up @@ -199,6 +200,6 @@ class AnyMap final {

private:
std::unordered_map<std::string, AnyValue> _map;
};
} SWIFT_NONCOPYABLE;

} // namespace margelo::nitro
4 changes: 2 additions & 2 deletions packages/react-native-nitro-modules/cpp/core/Promise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class Promise final {
std::vector<OnResolvedFunc> _onResolvedListeners;
std::vector<OnRejectedFunc> _onRejectedListeners;
std::unique_ptr<std::mutex> _mutex;
};
} SWIFT_NONCOPYABLE;

// Specialization for void
template <>
Expand Down Expand Up @@ -405,6 +405,6 @@ class Promise<void> final {
std::exception_ptr _error;
std::vector<OnResolvedFunc> _onResolvedListeners;
std::vector<OnRejectedFunc> _onRejectedListeners;
};
} SWIFT_NONCOPYABLE;

} // namespace margelo::nitro
25 changes: 12 additions & 13 deletions packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,23 @@
#define _CXX_INTEROP_HAS_ATTRIBUTE(x) 0
#endif

#if _CXX_INTEROP_HAS_ATTRIBUTE(swift_attr)
// Rename Type for Swift
#define SWIFT_NAME(_name) __attribute__((swift_name(#_name)))
// Make Swift type private
#define SWIFT_PRIVATE __attribute__((swift_private))
// Make getter + setter a computed property
#define SWIFT_COMPUTED_PROPERTY __attribute__((swift_attr("import_computed_property")))
#else
#define SWIFT_NAME(_name)
#define SWIFT_PRIVATE
#define SWIFT_COMPUTED_PROPERTY
#endif

#if _CXX_INTEROP_HAS_ATTRIBUTE(enum_extensibility)
// Enum is marked as closed/not extensible
#define CLOSED_ENUM __attribute__((enum_extensibility(closed)))
#else
#define CLOSED_ENUM
#endif

#if __has_include(<swift/bridging>)
// Swift's bridging header defines those things
#include <swift/bridging>
#define SWIFT_PRIVATE __attribute__((swift_private))
#else
// If we don't have Swift bridging header, those macros do nothing
#define SWIFT_NAME(_name)
#define SWIFT_PRIVATE
#define SWIFT_COMPUTED_PROPERTY
#define SWIFT_NONCOPYABLE
#endif

#endif /* NitroDefines_h */
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#pragma once

#include "ArrayBuffer.hpp"
#include "NitroDefines.hpp"
#include "SwiftClosure.hpp"
#include <memory>
#include <swift/bridging>

namespace margelo::nitro {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public extension ArrayBufferHolder {
/**
* Copy the given `ArrayBufferHolder` into a new **owning** `ArrayBufferHolder`.
*/
static func copy(of other: ArrayBufferHolder) -> ArrayBufferHolder {
static func copy(of other: borrowing ArrayBufferHolder) -> ArrayBufferHolder {
let data = UnsafeMutablePointer<UInt8>.allocate(capacity: other.size)
let pointer = other.data.assumingMemoryBound(to: UInt8.self)
data.initialize(from: pointer, count: other.size)
Expand Down

0 comments on commit 196fc9d

Please sign in to comment.