@@ -199,6 +199,8 @@ class Instrumentation;
199199class Scope ;
200200class JSIException ;
201201class JSError ;
202+ class TypedArray ;
203+ class Uint8Array ;
202204
203205// / A function which has this type can be registered as a function
204206// / callable from JavaScript using Function::createFromHostFunction().
@@ -549,6 +551,8 @@ class JSI_EXPORT IRuntime : public ICast {
549551
550552 virtual bool isArray (const Object&) const = 0;
551553 virtual bool isArrayBuffer (const Object&) const = 0;
554+ virtual bool isTypedArray (const Object&) const = 0;
555+ virtual bool isUint8Array (const Object&) const = 0;
552556 virtual bool isFunction (const Object&) const = 0;
553557 virtual bool isHostObject (const jsi::Object&) const = 0;
554558 virtual bool isHostFunction (const jsi::Function&) const = 0;
@@ -637,6 +641,22 @@ class JSI_EXPORT IRuntime : public ICast {
637641 virtual std::shared_ptr<MutableBuffer> tryGetMutableBuffer (
638642 const jsi::ArrayBuffer& arrayBuffer) = 0;
639643
644+ // / \return the underlying buffer of the \p typedArray.
645+ virtual ArrayBuffer buffer (const TypedArray& typedArray) = 0;
646+ // / \return the 'byteOffset' property of the \p typedArray.
647+ virtual size_t byteOffset (const TypedArray& typedArray) = 0;
648+ // / \return the 'byteLength' property of the \p typedArray.
649+ virtual size_t byteLength (const TypedArray& typedArray) = 0;
650+ // / \return the 'length; property of the \p typedArray.
651+ virtual size_t length (const TypedArray& typedArray) = 0;
652+
653+ // / Create a JS UInt8Array with length \p length.
654+ virtual Uint8Array createUint8Array (size_t length) = 0;
655+ // / Create a JS UInt8Array using the ArrayBuffer \p buffer starting at byte
656+ // / offset \p offset and length \p length.
657+ virtual Uint8Array
658+ createUint8Array (const ArrayBuffer& buffer, size_t offset, size_t length) = 0;
659+
640660 protected:
641661 virtual ~IRuntime () = default ;
642662};
@@ -727,6 +747,19 @@ class JSI_EXPORT Runtime : public IRuntime {
727747
728748 bool detached (const ArrayBuffer&) override ;
729749
750+ ArrayBuffer buffer (const TypedArray& typedArray) override ;
751+ size_t byteOffset (const TypedArray& typedArray) override ;
752+ size_t byteLength (const TypedArray& typedArray) override ;
753+ size_t length (const TypedArray& typedArray) override ;
754+
755+ bool isTypedArray (const Object&) const override ;
756+ bool isUint8Array (const Object&) const override ;
757+ Uint8Array createUint8Array (size_t length) override ;
758+ Uint8Array createUint8Array (
759+ const ArrayBuffer& buffer,
760+ size_t offset,
761+ size_t length) override ;
762+
730763 protected:
731764 friend class Pointer ;
732765 friend class PropNameID ;
@@ -1214,6 +1247,18 @@ class JSI_EXPORT Object : public Pointer {
12141247 return runtime.isArrayBuffer (*this );
12151248 }
12161249
1250+ // / \return true iff the Object is a TypedArray (Uint8Array, Int32Array,
1251+ // / Float64Array, etc.). If so, then \c getTypedArray() will succeed.
1252+ bool isTypedArray (IRuntime& runtime) const {
1253+ return runtime.isTypedArray (*this );
1254+ }
1255+
1256+ // / \return true iff the Object is an Uint8Array. If so, then \c
1257+ // / getUint8Array() will succeed
1258+ bool isUint8Array (IRuntime& runtime) const {
1259+ return runtime.isUint8Array (*this );
1260+ }
1261+
12171262 // / \return true iff the Object is callable. If so, then \c
12181263 // / getFunction will succeed.
12191264 bool isFunction (IRuntime& runtime) const {
@@ -1244,6 +1289,16 @@ class JSI_EXPORT Object : public Pointer {
12441289 // / JSIException.
12451290 Array asArray (IRuntime& runtime) &&;
12461291
1292+ // / \return a TypedArray instance which refers to the same underlying
1293+ // / object. If \c isTypedArray() would return false, this will throw
1294+ // / JSIException.
1295+ TypedArray asTypedArray (IRuntime& runtime) const &;
1296+
1297+ // / \return an Uint8Array instance which refers to the same underlying
1298+ // / object. If \c isUint8Array() would return false, this will throw
1299+ // / JSIException.
1300+ Uint8Array asUint8Array (IRuntime& runtime) const &;
1301+
12471302 // / \return an ArrayBuffer instance which refers to the same underlying
12481303 // / object. If \c isArrayBuffer() would return false, this will assert.
12491304 ArrayBuffer getArrayBuffer (IRuntime& runtime) const &;
@@ -1252,6 +1307,14 @@ class JSI_EXPORT Object : public Pointer {
12521307 // / object. If \c isArrayBuffer() would return false, this will assert.
12531308 ArrayBuffer getArrayBuffer (IRuntime& runtime) &&;
12541309
1310+ // / \return a TypedArray instance which refers to the same underlying
1311+ // / object. If \c isTypedArray() would return false, this will assert.
1312+ TypedArray getTypedArray (IRuntime& runtime) const &;
1313+
1314+ // / \return an Uint8Array instance which refers to the same underlying
1315+ // / object. If \c isUint8Array() would return false, this will assert.
1316+ Uint8Array getUint8Array (IRuntime& runtime) const &;
1317+
12551318 // / \return a Function instance which refers to the same underlying
12561319 // / object. If \c isFunction() would return false, this will assert.
12571320 Function getFunction (IRuntime& runtime) const &;
@@ -1601,6 +1664,64 @@ class JSI_EXPORT Function : public Object {
16011664 Function (Runtime::PointerValue* value) : Object(value) {}
16021665};
16031666
1667+ // / Represents a JS TypedArray
1668+ class JSI_EXPORT TypedArray : public Object {
1669+ public:
1670+ TypedArray (TypedArray&&) = default ;
1671+ TypedArray& operator =(TypedArray&&) = default ;
1672+
1673+ // Gets the buffer of this TypedArray
1674+ ArrayBuffer buffer (IRuntime& runtime) {
1675+ return runtime.buffer (*this );
1676+ }
1677+
1678+ // Gets the byte offset of this TypedArray
1679+ size_t byteOffset (IRuntime& runtime) {
1680+ return runtime.byteOffset (*this );
1681+ }
1682+
1683+ // Gets the byte length of this TypedArray
1684+ size_t byteLength (IRuntime& runtime) {
1685+ return runtime.byteLength (*this );
1686+ }
1687+
1688+ // Gets the element length of this TypedArray
1689+ size_t length (IRuntime& runtime) {
1690+ return runtime.length (*this );
1691+ }
1692+
1693+ private:
1694+ friend class Object ;
1695+ friend class Value ;
1696+ friend class Runtime ;
1697+ friend class Uint8Array ;
1698+
1699+ explicit TypedArray (Runtime::PointerValue* value) : Object(value) {}
1700+ };
1701+
1702+ // Represents a JS Uint8Array
1703+ class JSI_EXPORT Uint8Array : public TypedArray {
1704+ public:
1705+ Uint8Array (Uint8Array&&) = default ;
1706+ Uint8Array& operator =(Uint8Array&&) = default ;
1707+
1708+ Uint8Array (IRuntime& runtime, size_t length)
1709+ : Uint8Array(runtime.createUint8Array(length)) {}
1710+ Uint8Array (
1711+ IRuntime& runtime,
1712+ const ArrayBuffer& buffer,
1713+ size_t offset,
1714+ size_t length)
1715+ : Uint8Array(runtime.createUint8Array(buffer, offset, length)) {}
1716+
1717+ private:
1718+ friend class Object ;
1719+ friend class Value ;
1720+ friend class Runtime ;
1721+
1722+ explicit Uint8Array (Runtime::PointerValue* value) : TypedArray(value) {}
1723+ };
1724+
16041725// / Represents any JS Value (undefined, null, boolean, number, symbol,
16051726// / string, or object). Movable, or explicitly copyable (has no copy
16061727// / ctor).
0 commit comments