Files
nitro/packages/react-native-nitro-modules/cpp/core/ArrayBuffer.cpp
T
Marc RousavyandGitHub a06c7d44af feat: Add new ArrayBuffer APIs (copy(), wrap(), allocate(), toData()) (#463)
* feat: Add `ArrayBuffer::copyBuffer(..)` API

* feat: Add `wrap` API for Kotlin

* feat: Add `ArrayBuffer.copy(data:)`

* perf: Copy Data a bit more effectively

* fix: Rename a bunch of things

* docs: Add creating docs

* feat: Add `ArrayBufferHolder.toData()`

* feat: Add `ArrayBuffer::copy(std::vector&)`

* perf: Change type of `.data` to `uint8_t*` to avoid upcast

* feat: Add `copyIfNeeded` flag

* Update array-buffers.md
2025-01-07 16:35:59 +01:00

104 lines
3.0 KiB
C++

//
// ArrayBuffer.cpp
// react-native-nitro
//
// Created by Marc Rousavy on 14.07.24.
//
#include "ArrayBuffer.hpp"
#include "OwningReference.hpp"
#include <functional>
#include <jsi/jsi.h>
#include <thread>
namespace margelo::nitro {
using namespace facebook;
// 1. ArrayBuffer
std::shared_ptr<ArrayBuffer> ArrayBuffer::wrap(uint8_t* data, size_t size, DeleteFn&& deleteFunc) {
return std::make_shared<NativeArrayBuffer>(data, size, std::move(deleteFunc));
}
std::shared_ptr<ArrayBuffer> ArrayBuffer::copy(uint8_t* data, size_t size) {
uint8_t* copy = new uint8_t[size];
std::memcpy(copy, data, size);
return ArrayBuffer::wrap(copy, size, [=]() { delete[] copy; });
}
std::shared_ptr<ArrayBuffer> ArrayBuffer::copy(std::vector<uint8_t>& data) {
return ArrayBuffer::copy(data.data(), data.size());
}
std::shared_ptr<ArrayBuffer> ArrayBuffer::allocate(size_t size) {
uint8_t* data = new uint8_t[size];
return ArrayBuffer::wrap(data, size, [=]() { delete[] data; });
}
// 2. NativeArrayBuffer
NativeArrayBuffer::NativeArrayBuffer(uint8_t* data, size_t size, DeleteFn&& deleteFunc)
: ArrayBuffer(), _data(data), _size(size), _deleteFunc(std::move(deleteFunc)) {}
NativeArrayBuffer::~NativeArrayBuffer() {
if (_deleteFunc != nullptr) {
_deleteFunc();
}
}
uint8_t* NativeArrayBuffer::data() {
return _data;
}
size_t NativeArrayBuffer::size() const {
return _size;
}
bool NativeArrayBuffer::isOwner() const noexcept {
return _deleteFunc != nullptr;
}
// 3. JSArrayBuffer
JSArrayBuffer::JSArrayBuffer(jsi::Runtime* runtime, OwningReference<jsi::ArrayBuffer> jsReference)
: ArrayBuffer(), _runtime(runtime), _jsReference(jsReference), _initialThreadId(std::this_thread::get_id()) {}
JSArrayBuffer::~JSArrayBuffer() {}
uint8_t* JSArrayBuffer::data() {
if (_initialThreadId != std::this_thread::get_id()) [[unlikely]] {
throw std::runtime_error("`data()` can only be accessed synchronously on the JS Thread! "
"If you want to access it elsewhere, copy it first.");
}
OwningLock<jsi::ArrayBuffer> lock = _jsReference.lock();
if (!_jsReference) [[unlikely]] {
// JS Part has been deleted - data is now nullptr.
return nullptr;
}
// JS Part is still alive - we can assume that the jsi::Runtime is safe to access here too.
return _jsReference->data(*_runtime);
}
size_t JSArrayBuffer::size() const {
if (_initialThreadId != std::this_thread::get_id()) [[unlikely]] {
throw std::runtime_error("`size()` can only be accessed synchronously on the JS Thread! "
"If you want to access it elsewhere, copy it first.");
}
OwningLock<jsi::ArrayBuffer> lock = _jsReference.lock();
if (!_jsReference) [[unlikely]] {
// JS Part has been deleted - size is now 0.
return 0;
}
// JS Part is still alive - we can assume that the jsi::Runtime is safe to access here too.
return _jsReference->size(*_runtime);
}
bool JSArrayBuffer::isOwner() const noexcept {
return false;
}
} // namespace margelo::nitro