diff --git a/ReactCommon/jsi/JSCRuntime.cpp b/ReactCommon/jsi/JSCRuntime.cpp index ae907bbd16f373..bbc606561ee353 100644 --- a/ReactCommon/jsi/JSCRuntime.cpp +++ b/ReactCommon/jsi/JSCRuntime.cpp @@ -206,6 +206,8 @@ class JSCRuntime : public jsi::Runtime { jsi::Value lockWeakObject(jsi::WeakObject &) override; jsi::Array createArray(size_t length) override; + jsi::ArrayBuffer createArrayBuffer( + std::shared_ptr buffer) override; size_t size(const jsi::Array &) override; size_t size(const jsi::ArrayBuffer &) override; uint8_t *data(const jsi::ArrayBuffer &) override; @@ -1087,6 +1089,11 @@ jsi::Array JSCRuntime::createArray(size_t length) { return createObject(obj).getArray(*this); } +jsi::ArrayBuffer JSCRuntime::createArrayBuffer( + std::shared_ptr buffer) { + throw std::logic_error("Not implemented"); +} + size_t JSCRuntime::size(const jsi::Array &arr) { return static_cast( getProperty(arr, createPropNameID(getLengthString())).getNumber()); diff --git a/ReactCommon/jsi/jsi/decorator.h b/ReactCommon/jsi/jsi/decorator.h index 075cb8aa666699..9df3a2bc8f505c 100644 --- a/ReactCommon/jsi/jsi/decorator.h +++ b/ReactCommon/jsi/jsi/decorator.h @@ -302,6 +302,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation { Array createArray(size_t length) override { return plain_.createArray(length); }; + ArrayBuffer createArrayBuffer( + std::shared_ptr buffer) override { + return plain_.createArrayBuffer(std::move(buffer)); + }; size_t size(const Array& a) override { return plain_.size(a); }; @@ -701,6 +705,10 @@ class WithRuntimeDecorator : public RuntimeDecorator { Around around{with_}; return RD::createArray(length); }; + ArrayBuffer createArrayBuffer( + std::shared_ptr buffer) override { + return RD::createArrayBuffer(std::move(buffer)); + }; size_t size(const Array& a) override { Around around{with_}; return RD::size(a); diff --git a/ReactCommon/jsi/jsi/jsi.cpp b/ReactCommon/jsi/jsi/jsi.cpp index 815189d2443182..2831571537ded0 100644 --- a/ReactCommon/jsi/jsi/jsi.cpp +++ b/ReactCommon/jsi/jsi/jsi.cpp @@ -66,6 +66,8 @@ Value callGlobalFunction(Runtime& runtime, const char* name, const Value& arg) { Buffer::~Buffer() = default; +MutableBuffer::~MutableBuffer() = default; + PreparedJavaScript::~PreparedJavaScript() = default; Value HostObject::get(Runtime&, const PropNameID&) { diff --git a/ReactCommon/jsi/jsi/jsi.h b/ReactCommon/jsi/jsi/jsi.h index 85c0626905c974..b50780964ad23a 100644 --- a/ReactCommon/jsi/jsi/jsi.h +++ b/ReactCommon/jsi/jsi/jsi.h @@ -31,6 +31,10 @@ class FBJSRuntime; namespace facebook { namespace jsi { +/// Base class for buffers of data or bytecode that need to be passed to the +/// runtime. The buffer is expected to be fully immutable, so the result of +/// size(), data(), and the contents of the pointer returned by data() must not +/// change after construction. class JSI_EXPORT Buffer { public: virtual ~Buffer(); @@ -52,6 +56,18 @@ class JSI_EXPORT StringBuffer : public Buffer { std::string s_; }; +/// Base class for buffers of data that need to be passed to the runtime. The +/// result of size() and data() must not change after construction. However, the +/// region pointed to by data() may be modified by the user or the runtime. The +/// user must ensure that access to the contents of the buffer is properly +/// synchronised. +class JSI_EXPORT MutableBuffer { + public: + virtual ~MutableBuffer(); + virtual size_t size() const = 0; + virtual uint8_t* data() = 0; +}; + /// PreparedJavaScript is a base class representing JavaScript which is in a /// form optimized for execution, in a runtime-specific way. Construct one via /// jsi::Runtime::prepareJavaScript(). @@ -336,6 +352,8 @@ class JSI_EXPORT Runtime { virtual Value lockWeakObject(WeakObject&) = 0; virtual Array createArray(size_t length) = 0; + virtual ArrayBuffer createArrayBuffer( + std::shared_ptr buffer) = 0; virtual size_t size(const Array&) = 0; virtual size_t size(const ArrayBuffer&) = 0; virtual uint8_t* data(const ArrayBuffer&) = 0; @@ -915,6 +933,9 @@ class JSI_EXPORT ArrayBuffer : public Object { ArrayBuffer(ArrayBuffer&&) = default; ArrayBuffer& operator=(ArrayBuffer&&) = default; + ArrayBuffer(Runtime& runtime, std::shared_ptr buffer) + : ArrayBuffer(runtime.createArrayBuffer(std::move(buffer))) {} + /// \return the size of the ArrayBuffer, according to its byteLength property. /// (C++ naming convention) size_t size(Runtime& runtime) const {