react-native-nitro-modules 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,7 +48,6 @@ Pod::Spec.new do |s|
48
48
  # Public iOS-specific headers that will be exposed in modulemap (for Swift)
49
49
  "ios/core/ArrayBufferHolder.hpp",
50
50
  "ios/core/AnyMapHolder.hpp",
51
- "ios/core/HybridContext.hpp",
52
51
  "ios/core/PromiseHolder.hpp",
53
52
  "ios/utils/Result.hpp",
54
53
  "ios/utils/RuntimeError.hpp",
@@ -7,7 +7,7 @@ buildscript {
7
7
  }
8
8
 
9
9
  dependencies {
10
- classpath "com.android.tools.build:gradle:8.8.1"
10
+ classpath "com.android.tools.build:gradle:8.9.0"
11
11
  }
12
12
  }
13
13
 
@@ -3,7 +3,6 @@
3
3
  #include "JAnyMap.hpp"
4
4
  #include "JAnyValue.hpp"
5
5
  #include "JArrayBuffer.hpp"
6
- #include "JHybridObjectRegistry.hpp"
7
6
  #include "JNitroModules.hpp"
8
7
  #include "JPromise.hpp"
9
8
  #include <fbjni/fbjni.h>
@@ -14,7 +13,6 @@ using namespace margelo::nitro;
14
13
  JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
15
14
  return facebook::jni::initialize(vm, [] {
16
15
  // 1. Initialize all core Nitro Java bindings
17
- JHybridObjectRegistry::registerNatives();
18
16
  JArrayBuffer::registerNatives();
19
17
  JAnyMap::registerNatives();
20
18
  JAnyValue::registerNatives();
@@ -18,7 +18,7 @@ using namespace facebook;
18
18
  struct JHybridObjectInitializer : public jni::JavaClass<JHybridObjectInitializer> {
19
19
  public:
20
20
  jni::local_ref<JHybridObject::javaobject> call() const {
21
- const auto method = this->getClass()->getMethod<JHybridObject::javaobject()>("initialize");
21
+ const auto method = this->javaClassStatic()->getMethod<JHybridObject::javaobject()>("initialize");
22
22
  return method(self());
23
23
  }
24
24
 
@@ -4,6 +4,8 @@
4
4
 
5
5
  #pragma once
6
6
 
7
+ #include "NitroDefines.hpp"
8
+ #include "NitroTypeInfo.hpp"
7
9
  #include <fbjni/fbjni.h>
8
10
  #include <memory>
9
11
 
@@ -13,19 +15,21 @@ using namespace facebook;
13
15
 
14
16
  template <typename T>
15
17
  struct GlobalRefDeleter {
16
- explicit GlobalRefDeleter(jni::global_ref<typename T::javaobject> ref) : _ref(ref) {}
18
+ explicit GlobalRefDeleter(const jni::global_ref<typename T::javaobject>& ref) : _ref(ref) {}
19
+
20
+ GlobalRefDeleter(const GlobalRefDeleter& copy) = delete;
21
+ GlobalRefDeleter(GlobalRefDeleter&& move) = default;
17
22
 
18
23
  void operator()(T* /* cthis */) {
19
- if (_ref) {
20
- _ref.release();
21
- }
24
+ // It's RAII - once `GlobalRefDeleter` goes out of scope, `jni::global_ref` will too.
25
+ _ref = nullptr;
22
26
  }
23
27
 
24
28
  private:
25
29
  jni::global_ref<typename T::javaobject> _ref;
26
30
  };
27
31
 
28
- class JNISharedPtr {
32
+ class JNISharedPtr final {
29
33
  private:
30
34
  template <typename T, template <typename, typename...> class Base>
31
35
  struct is_base_template_of {
@@ -39,9 +43,23 @@ private:
39
43
  };
40
44
 
41
45
  public:
46
+ JNISharedPtr() = delete;
47
+ ~JNISharedPtr() = delete;
48
+
49
+ public:
50
+ /**
51
+ * Creates a new `std::shared_ptr<T>` from the given `jni::global_ref<T::javaobject>`.
52
+ */
42
53
  template <typename T, typename std::enable_if<is_base_template_of<T, jni::HybridClass>::value, int>::type = 0>
43
- static std::shared_ptr<T> make_shared_from_jni(jni::global_ref<typename T::javaobject> ref) {
44
- return std::shared_ptr<T>(ref->cthis(), GlobalRefDeleter<T>{ref});
54
+ static std::shared_ptr<T> make_shared_from_jni(const jni::global_ref<typename T::javaobject>& ref) {
55
+ #ifdef NITRO_DEBUG
56
+ if (ref == nullptr || ref->cthis() == nullptr) [[unlikely]] {
57
+ std::string typeName = TypeInfo::getFriendlyTypename<T>(true);
58
+ throw std::runtime_error("Failed to wrap jni::global_ref<" + typeName + "::javaobject> in std::shared_ptr<" + typeName +
59
+ "> - it's null!");
60
+ }
61
+ #endif
62
+ return std::shared_ptr<T>(ref->cthis(), GlobalRefDeleter<T>(ref));
45
63
  }
46
64
  };
47
65
 
@@ -137,10 +137,5 @@ class ArrayBuffer {
137
137
  byteBuffer.rewind()
138
138
  return ArrayBuffer(byteBuffer)
139
139
  }
140
-
141
- @Deprecated("Use copy(...) instead", level = DeprecationLevel.WARNING)
142
- fun copyOf(other: ArrayBuffer): ArrayBuffer {
143
- return copy(other)
144
- }
145
140
  }
146
141
  }
@@ -21,13 +21,13 @@ std::shared_ptr<ArrayBuffer> ArrayBuffer::wrap(uint8_t* data, size_t size, Delet
21
21
  return std::make_shared<NativeArrayBuffer>(data, size, std::move(deleteFunc));
22
22
  }
23
23
 
24
- std::shared_ptr<ArrayBuffer> ArrayBuffer::copy(uint8_t* data, size_t size) {
24
+ std::shared_ptr<ArrayBuffer> ArrayBuffer::copy(const uint8_t* data, size_t size) {
25
25
  uint8_t* copy = new uint8_t[size];
26
26
  std::memcpy(copy, data, size);
27
27
  return ArrayBuffer::wrap(copy, size, [=]() { delete[] copy; });
28
28
  }
29
29
 
30
- std::shared_ptr<ArrayBuffer> ArrayBuffer::copy(std::vector<uint8_t>& data) {
30
+ std::shared_ptr<ArrayBuffer> ArrayBuffer::copy(const std::vector<uint8_t>& data) {
31
31
  return ArrayBuffer::copy(data.data(), data.size());
32
32
  }
33
33
 
@@ -58,20 +58,15 @@ public:
58
58
  * Create a new `NativeArrayBuffer` that copies the given data of the given size
59
59
  * into a newly allocated buffer.
60
60
  */
61
- static std::shared_ptr<ArrayBuffer> copy(uint8_t* data, size_t size);
61
+ static std::shared_ptr<ArrayBuffer> copy(const uint8_t* data, size_t size);
62
62
  /**
63
63
  * Create a new `NativeArrayBuffer` that copies the given `std::vector`.
64
64
  */
65
- static std::shared_ptr<ArrayBuffer> copy(std::vector<uint8_t>& data);
65
+ static std::shared_ptr<ArrayBuffer> copy(const std::vector<uint8_t>& data);
66
66
  /**
67
67
  * Create a new `NativeArrayBuffer` that allocates a new buffer of the given size.
68
68
  */
69
69
  static std::shared_ptr<ArrayBuffer> allocate(size_t size);
70
-
71
- [[deprecated("Use wrapBuffer(...) instead.")]]
72
- static std::shared_ptr<ArrayBuffer> makeBuffer(uint8_t* data, size_t size, DeleteFn&& deleteFunc) {
73
- return ArrayBuffer::wrap(data, size, std::move(deleteFunc));
74
- }
75
70
  };
76
71
 
77
72
  /**
@@ -79,11 +79,17 @@ jsi::Value HybridObject::toObject(jsi::Runtime& runtime) {
79
79
  object.setProperty(runtime, "__type", jsi::String::createFromUtf8(runtime, "NativeState<" + std::string(_name) + ">"));
80
80
  #endif
81
81
 
82
- // 8. Throw a jsi::WeakObject pointing to our object into cache so subsequent calls can use it from cache
82
+ #ifdef NITRO_DEBUG
83
+ // 8. Freeze the object to prevent accidentally setting wrong props on it that do nothing.
84
+ jsi::Function freeze = objectConstructor.getPropertyAsFunction(runtime, "freeze");
85
+ freeze.call(runtime, object);
86
+ #endif
87
+
88
+ // 9. Throw a jsi::WeakObject pointing to our object into cache so subsequent calls can use it from cache
83
89
  JSICacheReference cache = JSICache::getOrCreateCache(runtime);
84
- _objectCache.emplace(&runtime, cache.makeShared(jsi::WeakObject(runtime, object)));
90
+ _objectCache[&runtime] = cache.makeShared(jsi::WeakObject(runtime, object));
85
91
 
86
- // 9. Return it!
92
+ // 10. Return it!
87
93
  return object;
88
94
  }
89
95
 
@@ -161,10 +161,6 @@ public:
161
161
  _onResolvedListeners.push_back(onResolved);
162
162
  }
163
163
  }
164
- [[deprecated("Upgrade Nitro to use PromiseHolder<T> instead.")]]
165
- void addOnResolvedListenerCopy(const std::function<void(TResult)>& onResolved) {
166
- addOnResolvedListener([=](const TResult& value) { onResolved(value); });
167
- }
168
164
 
169
165
  /**
170
166
  * Add a listener that will be called when the Promise gets rejected.
@@ -57,7 +57,7 @@ JSICacheReference JSICache::getOrCreateCache(jsi::Runtime& runtime) {
57
57
  // Cache doesn't exist yet.
58
58
  Logger::log(LogLevel::Info, TAG, "Creating new JSICache<T> for runtime %s..", getRuntimeId(runtime).c_str());
59
59
  // Create new cache
60
- auto nativeState = std::shared_ptr<JSICache>(new JSICache());
60
+ std::shared_ptr<JSICache> nativeState(new JSICache());
61
61
  // Wrap it in a jsi::Value using NativeState
62
62
  jsi::Object cache(runtime);
63
63
  cache.setNativeState(runtime, nativeState);
@@ -185,7 +185,6 @@ struct JSIConverter<std::string> final {
185
185
  #include "JSIConverter+ArrayBuffer.hpp"
186
186
  #include "JSIConverter+Exception.hpp"
187
187
  #include "JSIConverter+Function.hpp"
188
- #include "JSIConverter+Future.hpp"
189
188
  #include "JSIConverter+HostObject.hpp"
190
189
  #include "JSIConverter+HybridObject.hpp"
191
190
  #include "JSIConverter+Optional.hpp"
@@ -78,7 +78,8 @@ jsi::Value HybridObjectPrototype::createPrototype(jsi::Runtime& runtime, const s
78
78
  // 7. Throw it into our cache so the next lookup can be cached and therefore faster
79
79
  JSICacheReference jsiCache = JSICache::getOrCreateCache(runtime);
80
80
  BorrowingReference<jsi::Object> sharedObject = jsiCache.makeShared(std::move(object));
81
- prototypeCache.emplace(prototype->getNativeInstanceId(), sharedObject);
81
+ auto instanceId = prototype->getNativeInstanceId();
82
+ prototypeCache[instanceId] = sharedObject;
82
83
 
83
84
  // 8. Return it!
84
85
  return jsi::Value(runtime, *sharedObject);
@@ -22,7 +22,7 @@ void Dispatcher::installRuntimeGlobalDispatcher(jsi::Runtime& runtime, std::shar
22
22
  Logger::log(LogLevel::Info, TAG, "Installing global Dispatcher Holder into Runtime \"%s\"...", getRuntimeId(runtime).c_str());
23
23
 
24
24
  // Store a weak reference in global cache
25
- _globalCache[&runtime] = std::weak_ptr(dispatcher);
25
+ _globalCache[&runtime] = dispatcher;
26
26
 
27
27
  // Inject the dispatcher into Runtime global (runtime will hold a strong reference)
28
28
  jsi::Object dispatcherHolder(runtime);
@@ -31,9 +31,10 @@ void Dispatcher::installRuntimeGlobalDispatcher(jsi::Runtime& runtime, std::shar
31
31
  }
32
32
 
33
33
  std::shared_ptr<Dispatcher> Dispatcher::getRuntimeGlobalDispatcher(jsi::Runtime& runtime) {
34
- if (_globalCache.contains(&runtime)) [[likely]] {
34
+ auto found = _globalCache.find(&runtime);
35
+ if (found != _globalCache.end()) [[likely]] {
35
36
  // the runtime is known - we have something in cache
36
- std::weak_ptr<Dispatcher> weakDispatcher = _globalCache[&runtime];
37
+ std::weak_ptr<Dispatcher> weakDispatcher = found->second;
37
38
  std::shared_ptr<Dispatcher> strongDispatcher = weakDispatcher.lock();
38
39
  if (strongDispatcher) {
39
40
  // the weak reference we cached is still valid - return it!
@@ -9,7 +9,7 @@
9
9
  #define NitroDefines_h
10
10
 
11
11
  // Sets the version of the native Nitro core library
12
- #define NITRO_VERSION "0.24.0"
12
+ #define NITRO_VERSION "0.25.0"
13
13
 
14
14
  // Sets whether to use debug or optimized production build flags
15
15
  #ifdef DEBUG
@@ -33,13 +33,3 @@ public extension HybridObject {
33
33
  // By default, this returns `0`.
34
34
  var memorySize: Int { return 0 }
35
35
  }
36
-
37
- @available(*, deprecated, message: "HybridObjectSpec has been renamed to HybridObject. Update Nitrogen and re-generate your specs.")
38
- public typealias HybridObjectSpec = HybridObject
39
-
40
- public extension HybridObjectSpec {
41
- @available(*, deprecated, message: "getSizeOf(...) will now be default-computed. Please remove getSizeOf() from your code.")
42
- func getSizeOf<T: AnyObject>(_ instance: T) -> Int {
43
- return 0
44
- }
45
- }
@@ -0,0 +1,46 @@
1
+ //
2
+ // ThreadUtils.mm
3
+ // react-native-nitro
4
+ //
5
+ // Created by Marc Rousavy on 14.07.24.
6
+ //
7
+
8
+ #include "ThreadUtils.hpp"
9
+ #include <pthread.h>
10
+ #include <sstream>
11
+ #include <thread>
12
+
13
+ // ObjC import
14
+ #import <Foundation/Foundation.h>
15
+
16
+ namespace margelo::nitro {
17
+
18
+ std::string ThreadUtils::getThreadName() {
19
+ // Try using NSThread APIs
20
+ NSString* threadName = NSThread.currentThread.name;
21
+ if (threadName != nil && threadName.length > 0) {
22
+ return threadName.UTF8String;
23
+ }
24
+
25
+ // Try using DispatchQueue APIs as fallback
26
+ #pragma clang diagnostic push
27
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
28
+ dispatch_queue_t queue = dispatch_get_current_queue();
29
+ #pragma clang diagnostic pop
30
+ const char* label = dispatch_queue_get_label(queue);
31
+ if (label != nullptr) {
32
+ return label;
33
+ }
34
+
35
+ // Fall back to this_thread ID
36
+ std::stringstream stream;
37
+ stream << std::this_thread::get_id();
38
+ std::string threadId = stream.str();
39
+ return "Thread #" + threadId;
40
+ }
41
+
42
+ void ThreadUtils::setThreadName(const std::string& name) {
43
+ pthread_setname_np(name.c_str());
44
+ }
45
+
46
+ } // namespace margelo::nitro
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-modules",
3
- "version": "0.24.0",
3
+ "version": "0.25.0",
4
4
  "description": "Insanely fast native C++, Swift or Kotlin modules with a statically compiled binding layer to JSI.",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -1,45 +0,0 @@
1
- //
2
- // JHybridObjectRegistry.cpp
3
- // react-native-nitro
4
- //
5
- // Created by Marc Rousavy on 22.07.24.
6
- //
7
-
8
- #include "JHybridObjectRegistry.hpp"
9
- #include "HybridObjectRegistry.hpp"
10
- #include "JNISharedPtr.hpp"
11
- #include "NitroDefines.hpp"
12
-
13
- namespace margelo::nitro {
14
-
15
- void JHybridObjectRegistry::registerHybridObjectConstructor(jni::alias_ref<jni::JClass>, std::string hybridObjectName,
16
- jni::alias_ref<JHybridObjectInitializer> constructorFn) {
17
- auto sharedInitializer = jni::make_global(constructorFn);
18
- HybridObjectRegistry::registerHybridObjectConstructor(
19
- hybridObjectName,
20
- [sharedInitializer = std::move(sharedInitializer),
21
- hybridObjectName = std::move(hybridObjectName)]() -> std::shared_ptr<HybridObject> {
22
- // 1. Call the Java initializer function
23
- jni::local_ref<JHybridObject::javaobject> hybridObject = sharedInitializer->call();
24
- #ifdef NITRO_DEBUG
25
- if (hybridObject == nullptr) [[unlikely]] {
26
- throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName + "\" - the constructor returned null!");
27
- }
28
- #endif
29
-
30
- // 2. Make the resulting HybridObject a global (shared) reference
31
- jni::global_ref<JHybridObject::javaobject> globalHybridObject = jni::make_global(hybridObject);
32
- // 3. Create a shared_ptr from the JNI global reference
33
- std::shared_ptr<JHybridObject> sharedCppPart = JNISharedPtr::make_shared_from_jni<JHybridObject>(globalHybridObject);
34
- // 4. Up-cast to a HybridObject (kinda unsafe)
35
- std::shared_ptr<HybridObject> cast = std::static_pointer_cast<HybridObject>(sharedCppPart);
36
- return cast;
37
- });
38
- }
39
-
40
- void JHybridObjectRegistry::registerNatives() {
41
- javaClassStatic()->registerNatives({
42
- makeNativeMethod("registerHybridObjectConstructor", JHybridObjectRegistry::registerHybridObjectConstructor),
43
- });
44
- }
45
- } // namespace margelo::nitro
@@ -1,32 +0,0 @@
1
- //
2
- // JHybridObjectRegistry.hpp
3
- // react-native-nitro
4
- //
5
- // Created by Marc Rousavy on 22.07.24.
6
- //
7
-
8
- #pragma once
9
-
10
- #include "HybridObject.hpp"
11
- #include "JHybridObjectInitializer.hpp"
12
- #include <fbjni/fbjni.h>
13
-
14
- namespace margelo::nitro {
15
-
16
- using namespace facebook;
17
-
18
- struct JHybridObjectRegistry : public jni::JavaClass<JHybridObjectRegistry> {
19
- public:
20
- static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/core/HybridObjectRegistry;";
21
-
22
- public:
23
- [[deprecated("HybridObjects should be registered from C++ instead. Either autolink them using `nitro.json`, "
24
- "or add them manually in the C++ HybridObjectRegistry.")]]
25
- static void registerHybridObjectConstructor(jni::alias_ref<jni::JClass> clazz, std::string hybridObjectName,
26
- jni::alias_ref<JHybridObjectInitializer> constructorFn);
27
-
28
- public:
29
- static void registerNatives();
30
- };
31
-
32
- } // namespace margelo::nitro
@@ -1,17 +0,0 @@
1
- package com.margelo.nitro.core;
2
-
3
- import androidx.annotation.Keep;
4
-
5
- import com.facebook.proguard.annotations.DoNotStrip;
6
-
7
- /**
8
- * @deprecated HybridObjects should be registered from C++ instead. Either autolink them using `nitro.json`, or add them manually in the C++ `HybridObjectRegistry`.
9
- */
10
- @Keep
11
- @DoNotStrip
12
- @Deprecated(forRemoval = true)
13
- public interface HybridObjectInitializer {
14
- @Keep
15
- @DoNotStrip
16
- HybridObject initialize();
17
- }
@@ -1,27 +0,0 @@
1
- package com.margelo.nitro.core;
2
-
3
- import androidx.annotation.Keep;
4
- import com.facebook.proguard.annotations.DoNotStrip;
5
- import com.margelo.nitro.JNIOnLoad;
6
-
7
- /**
8
- * A registry that holds initializers for HybridObjects.
9
- * This will be used to initialize them from JS using `NitroModules.createHybridObject<T>(name)`.
10
- * @noinspection JavaJniMissingFunction
11
- */
12
- @Keep
13
- @DoNotStrip
14
- public class HybridObjectRegistry {
15
- static {
16
- JNIOnLoad.initializeNativeNitro();
17
- }
18
-
19
- /**
20
- * Registers the given HybridObject in the `HybridObjectRegistry`.
21
- * It will be uniquely identified via it's `hybridObjectName`, and can be initialized from
22
- * JS using `NitroModules.createHybridObject<T>(name)` - which will call the `constructorFn` here.
23
- * @deprecated HybridObjects should be registered from C++ instead. Either autolink them using `nitro.json`, or add them manually in the C++ `HybridObjectRegistry`.
24
- */
25
- @Deprecated(forRemoval = true)
26
- public static native void registerHybridObjectConstructor(String hybridObjectName, HybridObjectInitializer initializer);
27
- }
@@ -1,46 +0,0 @@
1
- //
2
- // Created by Marc Rousavy on 21.02.24.
3
- //
4
-
5
- #pragma once
6
-
7
- // Forward declare a few of the common types that might have cyclic includes.
8
- namespace margelo::nitro {
9
-
10
- template <typename T, typename Enable>
11
- struct JSIConverter;
12
- } // namespace margelo::nitro
13
-
14
- #include "JSIConverter.hpp"
15
-
16
- #include "Promise.hpp"
17
- #include <future>
18
- #include <jsi/jsi.h>
19
- #include <memory>
20
-
21
- namespace margelo::nitro {
22
-
23
- using namespace facebook;
24
-
25
- // std::future<T> <> Promise<T>
26
- template <typename TResult>
27
- struct JSIConverter<std::future<TResult>> final {
28
- [[deprecated("Use JSIConverter<std::shared_ptr<Promise<T>>> instead.")]]
29
- static inline std::future<TResult> fromJSI(jsi::Runtime& runtime, const jsi::Value& value) {
30
- auto promise = JSIConverter<std::shared_ptr<Promise<TResult>>>::fromJSI(runtime, value);
31
- return promise->await();
32
- }
33
-
34
- [[deprecated("Use JSIConverter<std::shared_ptr<Promise<T>>> instead.")]]
35
- static inline jsi::Value toJSI(jsi::Runtime& runtime, std::future<TResult>&& arg) {
36
- auto promise = Promise<TResult>::awaitFuture(std::move(arg));
37
- return JSIConverter<std::shared_ptr<Promise<TResult>>>::toJSI(runtime, promise);
38
- }
39
-
40
- [[deprecated("Use JSIConverter<std::shared_ptr<Promise<T>>> instead.")]]
41
- static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
42
- return JSIConverter<std::shared_ptr<Promise<TResult>>>::canConvert(runtime, value);
43
- }
44
- };
45
-
46
- } // namespace margelo::nitro
@@ -1,55 +0,0 @@
1
- //
2
- // HybridContext.hpp
3
- // NitroModules
4
- //
5
- // Created by Marc Rousavy on 17.07.24.
6
- //
7
-
8
- #pragma once
9
-
10
- namespace margelo::nitro {
11
- class HybridObject;
12
- }
13
-
14
- #include "HybridObject.hpp"
15
- #include "NitroLogger.hpp"
16
- #include "NitroTypeInfo.hpp"
17
- #include <memory>
18
-
19
- namespace margelo::nitro {
20
-
21
- /**
22
- * Represents contextual state for a `HybridObject`.
23
- *
24
- * This can be used in remote implementations, e.g. in a Swift implementation
25
- * to properly (weak-)reference the `HybridObject` instead of re-creating it each time.
26
- */
27
- struct [[deprecated("Update Nitrogen and re-generate your specs.")]]
28
- HybridContext final {
29
- public:
30
- std::weak_ptr<HybridObject> cppPart;
31
-
32
- public:
33
- template <typename THybridObject, typename TSwiftPart>
34
- static inline std::shared_ptr<THybridObject> getOrCreate(TSwiftPart swiftPart) noexcept {
35
- auto hybridContext = swiftPart.getHybridContext();
36
- auto hybridObject = std::dynamic_pointer_cast<THybridObject>(hybridContext.cppPart.lock());
37
- if (hybridObject != nullptr) [[likely]] {
38
- // Fast path - an existing HybridObject is still in cache! (HybridContext)
39
- return hybridObject;
40
- }
41
-
42
- // Slow path - we need to create a new HybridObject that wraps our Swift implementation.
43
- Logger::log(LogLevel::Info, TAG, "Creating new HybridObject<%s> for %s...", TypeInfo::getFriendlyTypename<THybridObject>().c_str(),
44
- TypeInfo::getFriendlyTypename<TSwiftPart>().c_str());
45
- hybridObject = std::make_shared<THybridObject>(std::forward<decltype(swiftPart)>(swiftPart));
46
- hybridContext.cppPart = hybridObject;
47
- swiftPart.setHybridContext(hybridContext);
48
- return hybridObject;
49
- }
50
-
51
- private:
52
- static constexpr auto TAG = "HybridContext";
53
- };
54
-
55
- } // namespace margelo::nitro
@@ -1,33 +0,0 @@
1
- //
2
- // ThreadUtils.cpp
3
- // react-native-nitro
4
- //
5
- // Created by Marc Rousavy on 14.07.24.
6
- //
7
-
8
- #include "ThreadUtils.hpp"
9
- #include <pthread.h>
10
- #include <sstream>
11
- #include <thread>
12
-
13
- namespace margelo::nitro {
14
-
15
- std::string ThreadUtils::getThreadName() {
16
- // Try using pthread APIs
17
- char name[256];
18
- if (pthread_getname_np(pthread_self(), name, sizeof(name)) == 0) {
19
- return std::string(name);
20
- }
21
-
22
- // Fall back to this_thread ID
23
- std::stringstream stream;
24
- stream << std::this_thread::get_id();
25
- std::string threadId = stream.str();
26
- return "Thread #" + threadId;
27
- }
28
-
29
- void ThreadUtils::setThreadName(const std::string& name) {
30
- pthread_setname_np(name.c_str());
31
- }
32
-
33
- } // namespace margelo::nitro