diff --git a/ReactCommon/jsi/JSCRuntime.cpp b/ReactCommon/jsi/JSCRuntime.cpp index b6e6062847cc87..adcf805db9266f 100644 --- a/ReactCommon/jsi/JSCRuntime.cpp +++ b/ReactCommon/jsi/JSCRuntime.cpp @@ -157,6 +157,8 @@ class JSCRuntime : public jsi::Runtime { jsi::String createStringFromUtf8(const uint8_t *utf8, size_t length) override; std::string utf8(const jsi::String &) override; + jsi::Object createArrayBufferFromBytes(const void* bytes, size_t length) override; + jsi::Object createObject() override; jsi::Object createObject(std::shared_ptr ho) override; virtual std::shared_ptr getHostObject( @@ -679,6 +681,17 @@ std::string JSCRuntime::utf8(const jsi::String &str) { return JSStringToSTLString(stringRef(str)); } +static void freePtr(void* ptr, void*) { + free(ptr); +} + +jsi::Object JSCRuntime::createArrayBufferFromBytes(const void* bytes, size_t length) { + void* buf = malloc(length); + memcpy(buf, bytes, length); + JSObjectRef ref = JSObjectMakeArrayBufferWithBytesNoCopy(ctx_, buf, length, freePtr, nullptr, nullptr); + return createObject(ref); +} + jsi::Object JSCRuntime::createObject() { return createObject(static_cast(nullptr)); } diff --git a/ReactCommon/jsi/jsi/decorator.h b/ReactCommon/jsi/jsi/decorator.h index 9110145e17788f..3317e8eb71c126 100644 --- a/ReactCommon/jsi/jsi/decorator.h +++ b/ReactCommon/jsi/jsi/decorator.h @@ -194,6 +194,10 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation { return plain_.utf8(s); } + Object createArrayBufferFromBytes(const void* bytes, size_t length) override { + return plain_.createArrayBufferFromBytes(bytes, length); + }; + Object createObject() override { return plain_.createObject(); }; @@ -565,6 +569,11 @@ class WithRuntimeDecorator : public RuntimeDecorator { return RD::utf8(s); } + Object createArrayBufferFromBytes(const void* bytes, size_t length) override { + Around around{with_}; + return RD::createArrayBufferFromBytes(bytes, length); + }; + Object createObject() override { Around around{with_}; return RD::createObject(); diff --git a/ReactCommon/jsi/jsi/jsi.h b/ReactCommon/jsi/jsi/jsi.h index 25c152227e6d57..6f06a10b339cb5 100644 --- a/ReactCommon/jsi/jsi/jsi.h +++ b/ReactCommon/jsi/jsi/jsi.h @@ -258,6 +258,10 @@ class JSI_EXPORT Runtime { // implementation creates a \c String and invokes JSON.parse. virtual Value createValueFromJsonUtf8(const uint8_t* json, size_t length); + virtual Object createArrayBufferFromBytes(const void* bytes, size_t length) { + throw std::runtime_error{"Not implemented."}; + } + virtual Object createObject() = 0; virtual Object createObject(std::shared_ptr ho) = 0; virtual std::shared_ptr getHostObject(const jsi::Object&) = 0; @@ -788,6 +792,11 @@ class JSI_EXPORT ArrayBuffer : public Object { return runtime.data(*this); } + static Object + createFromBytes(Runtime& runtime, const void* bytes, size_t length) { + return runtime.createArrayBufferFromBytes(bytes, length); + } + private: friend class Object; friend class Value;