mirror of
https://github.com/zoriya/nitro.git
synced 2026-10-13 00:05:23 +00:00
* feat: Try bridge more Swift
* Make all Promises `async` in swift
* Rename `Promise` to `JSPromise`
* Update JSPromise.hpp
* Update JSICache.cpp
* fix import
* fix: Init props
* fix: Remove AnyMap and ArrayBuffer apis for now
* fix import
* no more async? idk
* feat: Convert `std::optional<T>` to `swift::Optional<T>`
* Remove all funcs except hello
* fix: Generate Swift Umbrella header
* fix: Organize
* fix: Hardcode forward-decl
* feat: Properly generate umbrella header from known types
* fix: Fix indentation
* order
* feat: Try bridging some primitives over...
* Update NitroDefines.hpp
* Update Property.ts
* fallthrough looks better
* better error
* fix: Fix wrong enum `maxLength`
* feat: Try `CarSwift` experiment
* Revert "feat: Try `CarSwift` experiment"
This reverts commit 7794f38271.
71 lines
2.2 KiB
C++
71 lines
2.2 KiB
C++
//
|
|
// JSICache.cpp
|
|
// NitroModules
|
|
//
|
|
// Created by Marc Rousavy on 20.06.24.
|
|
//
|
|
|
|
#include "JSICache.hpp"
|
|
#include "JSIHelpers.hpp"
|
|
|
|
namespace margelo::nitro {
|
|
|
|
static constexpr auto CACHE_PROP_NAME = "__nitroModulesJSICache";
|
|
|
|
template <typename T>
|
|
inline void destroyReferences(const std::vector<BorrowingReference<T>>& references) {
|
|
for (auto& func : references) {
|
|
OwningReference<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();
|
|
}
|
|
}
|
|
}
|
|
|
|
JSICache::~JSICache() {
|
|
Logger::log(TAG, "Destroying JSICache...");
|
|
std::unique_lock lock(_mutex);
|
|
|
|
destroyReferences(_objectCache);
|
|
destroyReferences(_functionCache);
|
|
destroyReferences(_weakObjectCache);
|
|
destroyReferences(_arrayBufferCache);
|
|
}
|
|
|
|
JSICacheReference JSICache::getOrCreateCache(jsi::Runtime& runtime) {
|
|
auto found = _globalCache.find(&runtime);
|
|
if (found != _globalCache.end()) [[likely]] {
|
|
// Fast path: get weak_ptr to JSICache from our global list.
|
|
std::weak_ptr<JSICache> weak = found->second;
|
|
std::shared_ptr<JSICache> strong = weak.lock();
|
|
if (strong) {
|
|
// It's still alive! Return it
|
|
return JSICacheReference(strong);
|
|
}
|
|
Logger::log(TAG, "JSICache was created, but it is no longer strong!");
|
|
}
|
|
|
|
#if DEBUG
|
|
if (runtime.global().hasProperty(runtime, CACHE_PROP_NAME)) [[unlikely]] {
|
|
throw std::runtime_error("The Runtime \"" + getRuntimeId(runtime) + "\" already has a global cache! (\"" + CACHE_PROP_NAME + "\")");
|
|
}
|
|
#endif
|
|
|
|
// Cache doesn't exist yet.
|
|
Logger::log(TAG, "Creating new JSICache<T> for runtime %s..", getRuntimeId(runtime));
|
|
// Create new cache
|
|
auto nativeState = std::shared_ptr<JSICache>(new JSICache(&runtime));
|
|
// Wrap it in a jsi::Value using NativeState
|
|
jsi::Object cache(runtime);
|
|
cache.setNativeState(runtime, nativeState);
|
|
// Inject it into the jsi::Runtime's global so it's memory is managed by it
|
|
runtime.global().setProperty(runtime, CACHE_PROP_NAME, std::move(cache));
|
|
// Add it to our map of caches
|
|
_globalCache[&runtime] = nativeState;
|
|
// Return it
|
|
return JSICacheReference(nativeState);
|
|
}
|
|
|
|
} // namespace margelo::nitro
|