Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to create ArrayBuffers from byte arrays #30445

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ReactCommon/jsi/JSCRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<jsi::HostObject> ho) override;
virtual std::shared_ptr<jsi::HostObject> getHostObject(
Expand Down Expand Up @@ -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<JSObjectRef>(nullptr));
}
Expand Down
9 changes: 9 additions & 0 deletions ReactCommon/jsi/jsi/decorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down Expand Up @@ -565,6 +569,11 @@ class WithRuntimeDecorator : public RuntimeDecorator<Plain, Base> {
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();
Expand Down
9 changes: 9 additions & 0 deletions ReactCommon/jsi/jsi/jsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<HostObject> ho) = 0;
virtual std::shared_ptr<HostObject> getHostObject(const jsi::Object&) = 0;
Expand Down Expand Up @@ -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;
Expand Down