Files
nitro/packages/react-native-nitro-modules/cpp/core/HybridObject.cpp
T
Marc RousavyandGitHub 86c3702ffc feat: Make HybridObject a NativeState (#11)
* feat: Make `HybridObject` a `NativeState`

* feat: Get `NativeState` inside `callMethod(...)`, no more passing `this*`

* Update HybridObject.hpp

* fix: Convert NativeState to `jsi::Value` again

* Use `Object.create`

* fix broken test

* feat: Add `__type` hidden state to objects

* chore: Format

* `makeGlobal` -> `makeShared`

* feat: Extract to `HybridObjectPrototype`

* Update HybridObject.hpp

* feat: Properly build Prototype chain

* Cache prototypes

* fix: Fix prototype caching

* Extract to separate file

* fix: Some comments

* chore: Format

* feat: Freeze prototypes

* Remove freeze

* feat: Optimize `NativeState` performance, make it 56x faster (#12)

* fix: Fix `HybridFunction` usage

* Update HybridFunction.hpp

* mark final

* feat: Add new `registerHybrids(...)` API

* fix: Remove logs

* perf: Make `registerHybrids` faster by using function pointer instead of `std::function<...>`

* fix: Turn off memory allocations debugger

* refactor

* Format

* fix: Fix cyclic includes for `JSIConverter+HybridObject`

* DO_NULL_CHECKS

* fix: Use `jsi::Value` directly to avoid down and up casting again

* Add `__type` to `HybridObject` declaration

* feat: Throw `jsi::JSError` again

* feat: Add prototype tests

* feat: Set `__type` on Prototypes as well

* Delete PointerHolder.hpp

* Delete `JSIPrototypeCache`

* fix: Use `jsi::Value` directly
2024-08-08 11:39:17 +02:00

105 lines
3.0 KiB
C++

//
// Created by Marc Rousavy on 21.02.24.
//
#include "HybridObject.hpp"
#include "HybridContext.hpp"
#include "JSIConverter.hpp"
#include "NitroLogger.hpp"
#define LOG_MEMORY_ALLOCATIONS false
namespace margelo::nitro {
#if LOG_MEMORY_ALLOCATIONS
static std::mutex _instanceCounterMutex;
static std::unordered_map<const char*, uint32_t> _aliveInstances;
static std::unordered_map<const char*, int> _instanceIds;
static uint32_t incrementAliveInstancesAndGet(const char* name) {
std::unique_lock lock(_instanceCounterMutex);
return ++_aliveInstances[name];
}
static uint32_t decrementAliveInstancesAndGet(const char* name) {
std::unique_lock lock(_instanceCounterMutex);
return --_aliveInstances[name];
}
static uint32_t getTotalAliveInstances() {
std::unique_lock lock(_instanceCounterMutex);
uint32_t total = 0;
for (const auto& iter : _aliveInstances) {
total += iter.second;
}
return total;
}
static int getId(const char* name) {
std::unique_lock lock(_instanceCounterMutex);
if (_instanceIds.find(name) == _instanceIds.end()) {
_instanceIds.insert({name, 1});
}
auto iterator = _instanceIds.find(name);
return iterator->second++;
}
#endif
HybridObject::HybridObject(const char* name) : HybridObjectPrototype(), _name(name) {
#if LOG_MEMORY_ALLOCATIONS
_instanceId = getId(name);
uint32_t alive = incrementAliveInstancesAndGet(_name);
uint32_t totalObjects = getTotalAliveInstances();
Logger::log(TAG, "(MEMORY) ✅ Creating %s (#%i)... (Total %s(s): %i | Total HybridObjects: %i)", _name, _instanceId, _name, alive,
totalObjects);
#endif
}
HybridObject::~HybridObject() {
#if LOG_MEMORY_ALLOCATIONS
uint32_t alive = decrementAliveInstancesAndGet(_name);
uint32_t totalObjects = getTotalAliveInstances();
Logger::log(TAG, "(MEMORY) ❌ Deleting %s (#%i)... (Total %s(s): %i | Total HybridObjects: %i) ", _name, _instanceId, _name, alive,
totalObjects);
#endif
}
std::string HybridObject::toString() {
return "[HybridObject " + std::string(_name) + "]";
}
std::string HybridObject::getName() {
return _name;
}
bool HybridObject::equals(std::shared_ptr<HybridObject> other) {
return this == other.get();
}
void HybridObject::loadHybridMethods() {
registerHybrids(this, [](Prototype& prototype) {
prototype.registerHybridGetter("name", &HybridObject::getName);
prototype.registerHybridMethod("toString", &HybridObject::toString);
prototype.registerHybridMethod("equals", &HybridObject::equals);
});
}
jsi::Value HybridObject::toObject(jsi::Runtime& runtime) {
jsi::Value prototype = getPrototype(runtime);
jsi::Object objectConstructor = runtime.global().getPropertyAsObject(runtime, "Object");
jsi::Function create = objectConstructor.getPropertyAsFunction(runtime, "create");
jsi::Object object = create.call(runtime, prototype).asObject(runtime);
object.setNativeState(runtime, shared_from_this());
#if DEBUG
object.setProperty(runtime, "__type", jsi::String::createFromUtf8(runtime, "NativeState<" + std::string(_name) + ">"));
#endif
return object;
}
} // namespace margelo::nitro