From 9727348b8e01dd54d958795def196d9ab5a5921f Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sat, 9 Mar 2024 08:48:48 +0100 Subject: [PATCH] Add a more descriptive error when trying to cast JS value to Variant --- cpp/utils.cpp | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/cpp/utils.cpp b/cpp/utils.cpp index b952320b..e19411ab 100644 --- a/cpp/utils.cpp +++ b/cpp/utils.cpp @@ -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; } @@ -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{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{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"); } }