Skip to content

Commit

Permalink
Merge pull request #67 from OP-Engineering/osp/scalar-error-msg
Browse files Browse the repository at this point in the history
Add a more descriptive error when trying to cast JS value to Variant
  • Loading branch information
ospfranco authored Mar 9, 2024
2 parents c4682e0 + 9727348 commit cd5b939
Showing 1 changed file with 14 additions and 19 deletions.
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

0 comments on commit cd5b939

Please sign in to comment.