|
3 | 3 | #include <set> |
4 | 4 | #include <type_traits> |
5 | 5 |
|
| 6 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
6 | 7 | #include <jsi/jsi.h> |
| 8 | +#include <rnexecutorch/utils/JsiTensorView.h> |
7 | 9 |
|
8 | 10 | namespace rnexecutorch::jsiconversion { |
9 | 11 |
|
@@ -34,6 +36,93 @@ inline std::string getValue<std::string>(const jsi::Value &val, |
34 | 36 | return val.getString(runtime).utf8(runtime); |
35 | 37 | } |
36 | 38 |
|
| 39 | +template <> |
| 40 | +inline JsiTensorView getValue<JsiTensorView>(const jsi::Value &val, |
| 41 | + jsi::Runtime &runtime) { |
| 42 | + jsi::Object obj = val.asObject(runtime); |
| 43 | + JsiTensorView tensorView; |
| 44 | + |
| 45 | + int scalarTypeInt = |
| 46 | + getValue<int>(obj.getProperty(runtime, "scalarType"), runtime); |
| 47 | + tensorView.scalarType = static_cast<ScalarType>(scalarTypeInt); |
| 48 | + |
| 49 | + jsi::Value shapeValue = obj.getProperty(runtime, "shape"); |
| 50 | + jsi::Array shapeArray = shapeValue.asObject(runtime).asArray(runtime); |
| 51 | + size_t shapeDims = shapeArray.size(runtime); |
| 52 | + tensorView.shape.reserve(shapeDims); |
| 53 | + |
| 54 | + for (size_t i = 0; i < shapeDims; ++i) { |
| 55 | + int dim = getValue<int>(shapeArray.getValueAtIndex(runtime, i), runtime); |
| 56 | + tensorView.shape.push_back(static_cast<int32_t>(dim)); |
| 57 | + } |
| 58 | + |
| 59 | + // On JS side, TensorPtr objects hold a 'data' property which should be either |
| 60 | + // an ArrayBuffer or TypedArray |
| 61 | + jsi::Value dataValue = obj.getProperty(runtime, "data"); |
| 62 | + if (!dataValue.isObject()) { |
| 63 | + throw jsi::JSError(runtime, "Data must be a typed array or ArrayBuffer"); |
| 64 | + } |
| 65 | + |
| 66 | + jsi::Object dataObj = dataValue.asObject(runtime); |
| 67 | + |
| 68 | + // Check if it's an ArrayBuffer or TypedArray |
| 69 | + if (dataObj.isArrayBuffer(runtime)) { |
| 70 | + jsi::ArrayBuffer arrayBuffer = dataObj.getArrayBuffer(runtime); |
| 71 | + tensorView.dataPtr = arrayBuffer.data(runtime); |
| 72 | + |
| 73 | + // Get the array size in bytes |
| 74 | + size_t arrayBytes = arrayBuffer.size(runtime); |
| 75 | + size_t elementBytes = |
| 76 | + executorch::runtime::elementSize(tensorView.scalarType); |
| 77 | + tensorView.numel = arrayBytes / elementBytes; |
| 78 | + |
| 79 | + } else { |
| 80 | + // Handle typed arrays (Float32Array, Int32Array, etc.) |
| 81 | + if (dataObj.hasProperty(runtime, "buffer") && |
| 82 | + dataObj.hasProperty(runtime, "byteOffset") && |
| 83 | + dataObj.hasProperty(runtime, "byteLength") && |
| 84 | + dataObj.hasProperty(runtime, "length")) { |
| 85 | + |
| 86 | + tensorView.numel = |
| 87 | + getValue<int>(dataObj.getProperty(runtime, "length"), runtime); |
| 88 | + |
| 89 | + jsi::Value bufferValue = dataObj.getProperty(runtime, "buffer"); |
| 90 | + if (!bufferValue.isObject() || |
| 91 | + !bufferValue.asObject(runtime).isArrayBuffer(runtime)) { |
| 92 | + throw jsi::JSError(runtime, |
| 93 | + "TypedArray buffer property must be an ArrayBuffer"); |
| 94 | + } |
| 95 | + |
| 96 | + jsi::ArrayBuffer arrayBuffer = |
| 97 | + bufferValue.asObject(runtime).getArrayBuffer(runtime); |
| 98 | + size_t byteOffset = |
| 99 | + getValue<int>(dataObj.getProperty(runtime, "byteOffset"), runtime); |
| 100 | + |
| 101 | + tensorView.dataPtr = |
| 102 | + static_cast<uint8_t *>(arrayBuffer.data(runtime)) + byteOffset; |
| 103 | + } else { |
| 104 | + throw jsi::JSError(runtime, "Data must be an ArrayBuffer or TypedArray"); |
| 105 | + } |
| 106 | + } |
| 107 | + return std::move(tensorView); |
| 108 | +} |
| 109 | + |
| 110 | +template <> |
| 111 | +inline std::vector<JsiTensorView> |
| 112 | +getValue<std::vector<JsiTensorView>>(const jsi::Value &val, |
| 113 | + jsi::Runtime &runtime) { |
| 114 | + jsi::Array array = val.asObject(runtime).asArray(runtime); |
| 115 | + size_t length = array.size(runtime); |
| 116 | + std::vector<JsiTensorView> result; |
| 117 | + result.reserve(length); |
| 118 | + |
| 119 | + for (size_t i = 0; i < length; ++i) { |
| 120 | + jsi::Value element = array.getValueAtIndex(runtime, i); |
| 121 | + result.push_back(getValue<JsiTensorView>(element, runtime)); |
| 122 | + } |
| 123 | + return result; |
| 124 | +} |
| 125 | + |
37 | 126 | template <> |
38 | 127 | inline std::vector<std::string> |
39 | 128 | getValue<std::vector<std::string>>(const jsi::Value &val, |
@@ -88,6 +177,10 @@ inline jsi::Value getJsiValue(const std::vector<int32_t> &vec, |
88 | 177 | return jsi::Value(runtime, array); |
89 | 178 | } |
90 | 179 |
|
| 180 | +inline jsi::Value getJsiValue(int val, jsi::Runtime &runtime) { |
| 181 | + return jsi::Value(runtime, val); |
| 182 | +} |
| 183 | + |
91 | 184 | inline jsi::Value getJsiValue(const std::string &str, jsi::Runtime &runtime) { |
92 | 185 | return jsi::String::createFromAscii(runtime, str); |
93 | 186 | } |
|
0 commit comments