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 a more descriptive error when trying to cast JS value to Variant #67

Merged
merged 1 commit into from
Mar 9, 2024
Merged
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
33 changes: 14 additions & 19 deletions cpp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ jsi::Value toJSI(jsi::Runtime &rt, JSVariant value) {
jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)jsBuffer.size)
.getObject(rt);
jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
// You cannot share raw memory between native and JS
// always copy the data
// see https://github.com/facebook/hermes/pull/419 and
// https://github.com/facebook/hermes/issues/564.
memcpy(buf.data(rt), jsBuffer.data.get(), jsBuffer.size);
return o;
}
Expand Down Expand Up @@ -62,25 +58,24 @@ JSVariant toVariant(jsi::Runtime &rt, const jsi::Value &value) {
return JSVariant(strVal);
} else if (value.isObject()) {
auto obj = value.asObject(rt);
if (obj.isArrayBuffer(rt)) {
auto buffer = obj.getArrayBuffer(rt);

uint8_t *data = new uint8_t[buffer.size(rt)];
// You cannot share raw memory between native and JS
// always copy the data
// see https://github.com/facebook/hermes/pull/419 and
// https://github.com/facebook/hermes/issues/564.
memcpy(data, buffer.data(rt), buffer.size(rt));

return JSVariant(ArrayBuffer{.data = std::shared_ptr<uint8_t>{data},
.size = buffer.size(rt)});
} else {
if (!obj.isArrayBuffer(rt)) {
throw std::invalid_argument(
"Unknown JSI ArrayBuffer to variant value conversion, received "
"object instead of ArrayBuffer");
"Objects returned by OP-SQLite, are C++ HostObjects and thus cannot "
"store any object, only scalar "
"properties (int, long, double, string, bool) and ArrayBuffers.");
}

auto buffer = obj.getArrayBuffer(rt);
uint8_t *data = new uint8_t[buffer.size(rt)];
memcpy(data, buffer.data(rt), buffer.size(rt));

return JSVariant(ArrayBuffer{.data = std::shared_ptr<uint8_t>{data},
.size = buffer.size(rt)});

} else {
throw std::invalid_argument("Unknown JSI to variant value conversion");
throw std::invalid_argument(
"Cannot convert JSI value to C++ Variant value");
}
}

Expand Down
Loading