Files
nitro/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+ArrayBuffer.hpp
T
Marc RousavyandGitHub c101faf948 feat: Cache toObject(...) using jsi::WeakObject and correctly set externalExternalMemoryPressure (#13)
* feat: Cache `toObject(...)` using `jsi::WeakObject` and set `externalMemoryPressure` properly

* feat: Support caching different JSI types - strongly typed
2024-08-08 12:28:33 +02:00

61 lines
1.8 KiB
C++

//
// Created by Marc Rousavy on 21.02.24.
//
#pragma once
// Forward declare a few of the common types that might have cyclic includes.
namespace margelo::nitro {
class ArrayBuffer;
class JSICache;
template <typename T, typename Enable>
struct JSIConverter;
} // namespace margelo::nitro
#include "JSIConverter.hpp"
#include "ArrayBuffer.hpp"
#include "IsSharedPtrTo.hpp"
#include "JSICache.hpp"
#include <jsi/jsi.h>
#include <memory>
#include <type_traits>
namespace margelo::nitro {
using namespace facebook;
// MutableBuffer <> ArrayBuffer
template <typename T>
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer>>> {
static inline std::shared_ptr<ArrayBuffer> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
jsi::Object object = arg.asObject(runtime);
#if DEBUG
if (!object.isArrayBuffer(runtime)) [[unlikely]] {
throw std::runtime_error("Object \"" + arg.toString(runtime).utf8(runtime) +
"\" is not an ArrayBuffer! "
"Are you maybe passing a TypedArray (e.g. Uint8Array)? Try to pass it's `.buffer` value.");
}
#endif
JSICacheReference cache = JSICache::getOrCreateCache(runtime);
auto borrowingArrayBuffer = cache.makeShared(object.getArrayBuffer(runtime));
return std::make_shared<JSArrayBuffer>(&runtime, borrowingArrayBuffer);
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::shared_ptr<jsi::MutableBuffer>& buffer) {
return jsi::ArrayBuffer(runtime, buffer);
}
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
if (value.isObject()) {
jsi::Object object = value.getObject(runtime);
return object.isArrayBuffer(runtime);
}
return false;
}
};
} // namespace margelo::nitro