Skip to content
/ nitro Public
forked from mrousavy/nitro

Commit

Permalink
chore: Rename BorrowingReference<T> -> WeakReference<T>, and `Own…
Browse files Browse the repository at this point in the history
…ingReference<T>` -> `BorrowingReference<T>` (mrousavy#523)

* chore: Rename `BorrowingReference<T>` -> `WeakReference<T>`, and `OwningReference<T>` -> `BorrowingReference<T>`

* chore: Remove unused flag

* chore: Lint

* fix: remove unnecessary nullptr compare
  • Loading branch information
mrousavy authored Feb 3, 2025
1 parent 1d6f8c3 commit 6998588
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 303 deletions.
4 changes: 2 additions & 2 deletions packages/react-native-nitro-modules/cpp/core/ArrayBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

#include "ArrayBuffer.hpp"
#include "OwningReference.hpp"
#include "BorrowingReference.hpp"
#include <functional>
#include <jsi/jsi.h>
#include <thread>
Expand Down Expand Up @@ -61,7 +61,7 @@ bool NativeArrayBuffer::isOwner() const noexcept {

// 3. JSArrayBuffer

JSArrayBuffer::JSArrayBuffer(jsi::Runtime* runtime, OwningReference<jsi::ArrayBuffer> jsReference)
JSArrayBuffer::JSArrayBuffer(jsi::Runtime* runtime, BorrowingReference<jsi::ArrayBuffer> jsReference)
: ArrayBuffer(), _runtime(runtime), _jsReference(jsReference), _initialThreadId(std::this_thread::get_id()) {}

JSArrayBuffer::~JSArrayBuffer() {}
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-nitro-modules/cpp/core/ArrayBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#pragma once

#include "OwningReference.hpp"
#include "BorrowingReference.hpp"
#include <jsi/jsi.h>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -122,7 +122,7 @@ class NativeArrayBuffer final : public ArrayBuffer {
*/
class JSArrayBuffer final : public ArrayBuffer {
public:
explicit JSArrayBuffer(jsi::Runtime* runtime, OwningReference<jsi::ArrayBuffer> jsReference);
explicit JSArrayBuffer(jsi::Runtime* runtime, BorrowingReference<jsi::ArrayBuffer> jsReference);
~JSArrayBuffer();

public:
Expand All @@ -141,7 +141,7 @@ class JSArrayBuffer final : public ArrayBuffer {

private:
jsi::Runtime* _runtime;
OwningReference<jsi::ArrayBuffer> _jsReference;
BorrowingReference<jsi::ArrayBuffer> _jsReference;
std::thread::id _initialThreadId;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class HybridObject : public virtual jsi::NativeState, public HybridObjectPrototy
private:
static constexpr auto TAG = "HybridObject";
const char* _name = TAG;
std::unordered_map<jsi::Runtime*, OwningReference<jsi::WeakObject>> _objectCache;
std::unordered_map<jsi::Runtime*, BorrowingReference<jsi::WeakObject>> _objectCache;
};

} // namespace margelo::nitro
4 changes: 2 additions & 2 deletions packages/react-native-nitro-modules/cpp/jsi/JSICache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace margelo::nitro {
static constexpr auto CACHE_PROP_NAME = "__nitroModulesJSICache";

template <typename T>
inline void destroyReferences(const std::vector<BorrowingReference<T>>& references) {
inline void destroyReferences(const std::vector<WeakReference<T>>& references) {
for (auto& func : references) {
OwningReference<T> owning = func.lock();
BorrowingReference<T> owning = func.lock();
if (owning) {
// Destroy all functions that we might still have in cache, some callbacks and Promises may now become invalid.
owning.destroy();
Expand Down
36 changes: 18 additions & 18 deletions packages/react-native-nitro-modules/cpp/jsi/JSICache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "BorrowingReference.hpp"
#include "NitroLogger.hpp"
#include "OwningReference.hpp"
#include "WeakReference.hpp"
#include <jsi/jsi.h>
#include <memory>
#include <mutex>
Expand All @@ -24,12 +24,12 @@ class JSICacheReference;

/**
* A `JSICache` can safely store `jsi::Value` instances (e.g. `jsi::Object` or
* `jsi::Function`) inside `OwningReference<T>`.
* `jsi::Function`) inside `BorrowingReference<T>`.
*
* `jsi::Value`s are managed by a `jsi::Runtime`, and will be deleted if the `jsi::Runtime`
* is deleted - even if there are still strong references to the `jsi::Value`.
*
* To access a `OwningReference<jsi::Value>` safely, use `lock()` to get an `OwningLock<jsi::Value>`.
* To access a `BorrowingReference<jsi::Value>` safely, use `lock()` to get an `OwningLock<jsi::Value>`.
* This will allow you to access the `jsi::Value` as long as the `OwningLock` is alive,
* and `JSICache` will hold any garbage collection calls until the `OwningLock` is destroyed.
*/
Expand Down Expand Up @@ -59,11 +59,11 @@ class JSICache final : public jsi::NativeState {

private:
std::mutex _mutex;
std::vector<BorrowingReference<jsi::Value>> _valueCache;
std::vector<BorrowingReference<jsi::Object>> _objectCache;
std::vector<BorrowingReference<jsi::Function>> _functionCache;
std::vector<BorrowingReference<jsi::WeakObject>> _weakObjectCache;
std::vector<BorrowingReference<jsi::ArrayBuffer>> _arrayBufferCache;
std::vector<WeakReference<jsi::Value>> _valueCache;
std::vector<WeakReference<jsi::Object>> _objectCache;
std::vector<WeakReference<jsi::Function>> _functionCache;
std::vector<WeakReference<jsi::WeakObject>> _weakObjectCache;
std::vector<WeakReference<jsi::ArrayBuffer>> _arrayBufferCache;

private:
static inline std::unordered_map<jsi::Runtime*, std::weak_ptr<JSICache>> _globalCache;
Expand All @@ -83,28 +83,28 @@ class JSICacheReference final {
}

public:
OwningReference<jsi::Value> makeShared(jsi::Value&& value) {
OwningReference<jsi::Value> owning(new jsi::Value(std::move(value)));
BorrowingReference<jsi::Value> makeShared(jsi::Value&& value) {
BorrowingReference<jsi::Value> owning(new jsi::Value(std::move(value)));
_strongCache->_valueCache.push_back(owning.weak());
return owning;
}
OwningReference<jsi::Object> makeShared(jsi::Object&& value) {
OwningReference<jsi::Object> owning(new jsi::Object(std::move(value)));
BorrowingReference<jsi::Object> makeShared(jsi::Object&& value) {
BorrowingReference<jsi::Object> owning(new jsi::Object(std::move(value)));
_strongCache->_objectCache.push_back(owning.weak());
return owning;
}
OwningReference<jsi::Function> makeShared(jsi::Function&& value) {
OwningReference<jsi::Function> owning(new jsi::Function(std::move(value)));
BorrowingReference<jsi::Function> makeShared(jsi::Function&& value) {
BorrowingReference<jsi::Function> owning(new jsi::Function(std::move(value)));
_strongCache->_functionCache.push_back(owning.weak());
return owning;
}
OwningReference<jsi::WeakObject> makeShared(jsi::WeakObject&& value) {
OwningReference<jsi::WeakObject> owning(new jsi::WeakObject(std::move(value)));
BorrowingReference<jsi::WeakObject> makeShared(jsi::WeakObject&& value) {
BorrowingReference<jsi::WeakObject> owning(new jsi::WeakObject(std::move(value)));
_strongCache->_weakObjectCache.push_back(owning.weak());
return owning;
}
OwningReference<jsi::ArrayBuffer> makeShared(jsi::ArrayBuffer&& value) {
OwningReference<jsi::ArrayBuffer> owning(new jsi::ArrayBuffer(std::move(value)));
BorrowingReference<jsi::ArrayBuffer> makeShared(jsi::ArrayBuffer&& value) {
BorrowingReference<jsi::ArrayBuffer> owning(new jsi::ArrayBuffer(std::move(value)));
_strongCache->_arrayBufferCache.push_back(owning.weak());
return owning;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct JSIConverter<std::function<ReturnType(Args...)>> final {
// Make function global - it'll be managed by the Runtime's memory, and we only have a weak_ref to it.
auto cache = JSICache::getOrCreateCache(runtime);
jsi::Function function = arg.asObject(runtime).asFunction(runtime);
OwningReference<jsi::Function> sharedFunction = cache.makeShared(std::move(function));
BorrowingReference<jsi::Function> sharedFunction = cache.makeShared(std::move(function));

std::shared_ptr<Dispatcher> strongDispatcher = Dispatcher::getRuntimeGlobalDispatcher(runtime);
std::weak_ptr<Dispatcher> weakDispatcher = strongDispatcher;
Expand Down Expand Up @@ -94,7 +94,8 @@ struct JSIConverter<std::function<ReturnType(Args...)>> final {
}

private:
static inline ResultingType callJSFunction(jsi::Runtime& runtime, const OwningReference<jsi::Function>& function, const Args&... args) {
static inline ResultingType callJSFunction(jsi::Runtime& runtime, const BorrowingReference<jsi::Function>& function,
const Args&... args) {
if (!function) {
if constexpr (std::is_void_v<ResultingType>) {
// runtime has already been deleted. since this returns void, we can just ignore it being deleted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jsi::Value HybridObjectPrototype::createPrototype(jsi::Runtime& runtime, const s
auto& prototypeCache = _prototypeCache[&runtime];
auto cachedPrototype = prototypeCache.find(prototype->getNativeInstanceId());
if (cachedPrototype != prototypeCache.end()) {
const OwningReference<jsi::Object>& cachedObject = cachedPrototype->second;
const BorrowingReference<jsi::Object>& cachedObject = cachedPrototype->second;
return jsi::Value(runtime, *cachedObject).getObject(runtime);
}

Expand Down Expand Up @@ -68,7 +68,7 @@ jsi::Value HybridObjectPrototype::createPrototype(jsi::Runtime& runtime, const s

// 6. Throw it into our cache so the next lookup can be cached and therefore faster
JSICacheReference jsiCache = JSICache::getOrCreateCache(runtime);
OwningReference<jsi::Object> cachedObject = jsiCache.makeShared(std::move(object));
BorrowingReference<jsi::Object> cachedObject = jsiCache.makeShared(std::move(object));
prototypeCache.emplace(prototype->getNativeInstanceId(), cachedObject);

// 7. In DEBUG, add a __type info to the prototype object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#pragma once

#include "BorrowingReference.hpp"
#include "HybridFunction.hpp"
#include "OwningReference.hpp"
#include "Prototype.hpp"
#include "PrototypeChain.hpp"
#include <functional>
Expand Down Expand Up @@ -46,7 +46,7 @@ class HybridObjectPrototype {

private:
static jsi::Value createPrototype(jsi::Runtime& runtime, const std::shared_ptr<Prototype>& prototype);
using PrototypeCache = std::unordered_map<NativeInstanceId, OwningReference<jsi::Object>>;
using PrototypeCache = std::unordered_map<NativeInstanceId, BorrowingReference<jsi::Object>>;
static std::unordered_map<jsi::Runtime*, PrototypeCache> _prototypeCache;

protected:
Expand Down
Loading

0 comments on commit 6998588

Please sign in to comment.