@@ -38,6 +38,18 @@ class JSI_EXPORT Buffer {
38
38
virtual const uint8_t * data () const = 0;
39
39
};
40
40
41
+ // / Base class for buffers of data that need to be passed to the runtime. The
42
+ // / result of size() and data() must not change after construction. However, the
43
+ // / region pointed to by data() may be modified by the user or the runtime. The
44
+ // / user must ensure that access to the contents of the buffer is properly
45
+ // / synchronised.
46
+ class JSI_EXPORT MutableBuffer {
47
+ public:
48
+ virtual ~MutableBuffer ();
49
+ virtual size_t size () const = 0;
50
+ virtual uint8_t * data () = 0;
51
+ };
52
+
41
53
class JSI_EXPORT StringBuffer : public Buffer {
42
54
public:
43
55
StringBuffer (std::string s) : s_(std::move(s)) {}
@@ -80,6 +92,7 @@ class Instrumentation;
80
92
class Scope ;
81
93
class JSIException ;
82
94
class JSError ;
95
+ class TypedArray ;
83
96
84
97
// / A function which has this type can be registered as a function
85
98
// / callable from JavaScript using Function::createFromHostFunction().
@@ -251,6 +264,8 @@ class JSI_EXPORT Runtime {
251
264
friend class Value ;
252
265
friend class Scope ;
253
266
friend class JSError ;
267
+ friend class TypedArray ;
268
+
254
269
255
270
// Potential optimization: avoid the cloneFoo() virtual dispatch,
256
271
// and instead just fix the number of fields, and copy them, since
@@ -307,18 +322,40 @@ class JSI_EXPORT Runtime {
307
322
308
323
virtual bool isArray (const Object&) const = 0;
309
324
virtual bool isArrayBuffer (const Object&) const = 0;
325
+ virtual bool isArrayBufferView (const Object&) const = 0;
326
+ virtual bool isTypedArray (const Object&) const = 0;
327
+ virtual bool isInt8Array (const Object&) const = 0;
328
+ virtual bool isUint8Array (const Object&) const = 0;
329
+ virtual bool isUint8ClampedArray (const Object&) const = 0;
330
+ virtual bool isInt16Array (const Object&) const = 0;
331
+ virtual bool isUint16Array (const Object&) const = 0;
332
+ virtual bool isInt32Array (const Object&) const = 0;
333
+ virtual bool isUint32Array (const Object&) const = 0;
334
+ virtual bool isFloat32Array (const Object&) const = 0;
335
+ virtual bool isBigInt64Array (const Object&) const = 0;
336
+ virtual bool isBigUint64Array (const Object&) const = 0;
337
+ virtual bool isFloat64Array (const Object&) const = 0;
310
338
virtual bool isFunction (const Object&) const = 0;
311
339
virtual bool isHostObject (const jsi::Object&) const = 0;
312
340
virtual bool isHostFunction (const jsi::Function&) const = 0;
313
341
virtual Array getPropertyNames (const Object&) = 0;
314
342
315
343
virtual WeakObject createWeakObject (const Object&) = 0;
316
344
virtual Value lockWeakObject (WeakObject&) = 0;
345
+
346
+ virtual uint64_t uint64Value (const BigInt&, bool *lossless) const = 0;
347
+ virtual int64_t int64Value (const BigInt&, bool *lossless) const = 0;
348
+
317
349
318
350
virtual Array createArray (size_t length) = 0;
351
+ virtual ArrayBuffer createArrayBuffer (
352
+ std::shared_ptr<MutableBuffer> buffer) = 0;
319
353
virtual size_t size (const Array&) = 0;
320
354
virtual size_t size (const ArrayBuffer&) = 0;
355
+ virtual size_t size (const TypedArray&) = 0;
321
356
virtual uint8_t * data (const ArrayBuffer&) = 0;
357
+ virtual uint8_t * data (const TypedArray&) = 0;
358
+ virtual size_t offset (const TypedArray&) = 0;
322
359
virtual Value getValueAtIndex (const Array&, size_t i) = 0;
323
360
virtual void setValueAtIndexImpl (Array&, size_t i, const Value& value) = 0;
324
361
@@ -493,6 +530,26 @@ class JSI_EXPORT BigInt : public Pointer {
493
530
494
531
BigInt (BigInt&& other) = default ;
495
532
BigInt& operator =(BigInt&& other) = default ;
533
+
534
+
535
+ /* *
536
+ * Returns the value of this BigInt as an unsigned 64-bit integer.
537
+ * If `lossless` is provided, it will reflect whether the return value was
538
+ * truncated or wrapped around. In particular, it is set to `false` if this
539
+ * BigInt is negative.
540
+ */
541
+ uint64_t Uint64Value (Runtime& runtime, bool * lossless = nullptr ) const {
542
+ return runtime.uint64Value (*this , lossless);
543
+ }
544
+
545
+ /* *
546
+ * Returns the value of this BigInt as a signed 64-bit integer.
547
+ * If `lossless` is provided, it will reflect whether this BigInt was
548
+ * truncated or not.
549
+ */
550
+ int64_t Int64Value (Runtime& runtime, bool * lossless = nullptr ) const {
551
+ return runtime.int64Value (*this , lossless);
552
+ }
496
553
497
554
friend class Runtime ;
498
555
friend class Value ;
@@ -641,6 +698,86 @@ class JSI_EXPORT Object : public Pointer {
641
698
bool isArrayBuffer (Runtime& runtime) const {
642
699
return runtime.isArrayBuffer (*this );
643
700
}
701
+
702
+
703
+ // / \return true iff the Object is an isArrayBufferView. If so, then \c
704
+ // / getArrayBuffer() will succeed.
705
+ bool isArrayBufferView (Runtime& runtime) const {
706
+ return runtime.isArrayBufferView (*this );
707
+ }
708
+
709
+ // / \return true iff the Object is an isTypedArray. If so, then \c
710
+ // / getArrayBuffer() will succeed.
711
+ bool isTypedArray (Runtime& runtime) const {
712
+ return runtime.isTypedArray (*this );
713
+ }
714
+
715
+
716
+ // / \return true iff the Object is an isInt8Array. If so, then \c
717
+ // / getArrayBuffer() will succeed.
718
+ bool isInt8Array (Runtime& runtime) const {
719
+ return runtime.isInt8Array (*this );
720
+ }
721
+
722
+ // / \return true iff the Object is an isUint8Array. If so, then \c
723
+ // / getArrayBuffer() will succeed.
724
+ bool isUint8Array (Runtime& runtime) const {
725
+ return runtime.isUint8Array (*this );
726
+ }
727
+
728
+ // / \return true iff the Object is an isUint8ClampedArray. If so, then \c
729
+ // / getArrayBuffer() will succeed.
730
+ bool isUint8ClampedArray (Runtime& runtime) const {
731
+ return runtime.isUint8ClampedArray (*this );
732
+ }
733
+
734
+ // / \return true iff the Object is an isInt16Array. If so, then \c
735
+ // / getArrayBuffer() will succeed.
736
+ bool isInt16Array (Runtime& runtime) const {
737
+ return runtime.isInt16Array (*this );
738
+ }
739
+
740
+ // / \return true iff the Object is an isUint16Array. If so, then \c
741
+ // / getArrayBuffer() will succeed.
742
+ bool isUint16Array (Runtime& runtime) const {
743
+ return runtime.isUint16Array (*this );
744
+ }
745
+
746
+ // / \return true iff the Object is an isInt32Array. If so, then \c
747
+ // / getArrayBuffer() will succeed.
748
+ bool isInt32Array (Runtime& runtime) const {
749
+ return runtime.isInt32Array (*this );
750
+ }
751
+
752
+ // / \return true iff the Object is an isUint32Array. If so, then \c
753
+ // / getArrayBuffer() will succeed.
754
+ bool isUint32Array (Runtime& runtime) const {
755
+ return runtime.isUint32Array (*this );
756
+ }
757
+
758
+ // / \return true iff the Object is an isFloat32Array. If so, then \c
759
+ // / getArrayBuffer() will succeed.
760
+ bool isFloat32Array (Runtime& runtime) const {
761
+ return runtime.isFloat32Array (*this );
762
+ }
763
+
764
+ // / \return true iff the Object is an isBigInt64Array. If so, then \c
765
+ // / getArrayBuffer() will succeed.
766
+ bool isBigInt64Array (Runtime& runtime) const {
767
+ return runtime.isBigInt64Array (*this );
768
+ }
769
+
770
+ // / \return true iff the Object is an isBigUint64Array. If so, then \c
771
+ // / getArrayBuffer() will succeed.
772
+ bool isBigUint64Array (Runtime& runtime) const {
773
+ return runtime.isBigUint64Array (*this );
774
+ }
775
+
776
+ // / \return true iff the Object is an isFloat64Array. If so, then \c
777
+ // / getArrayBuffer() will succeed.
778
+ bool isFloat64Array (Runtime& runtime) const {
779
+ return runtime.isFloat64Array (*this );
780
+ }
644
781
645
782
// / \return true iff the Object is callable. If so, then \c
646
783
// / getFunction will succeed.
@@ -679,6 +816,16 @@ class JSI_EXPORT Object : public Pointer {
679
816
// / \return an ArrayBuffer instance which refers to the same underlying
680
817
// / object. If \c isArrayBuffer() would return false, this will assert.
681
818
ArrayBuffer getArrayBuffer (Runtime& runtime) &&;
819
+
820
+
821
+ // / \return an TypedArray instance which refers to the same underlying
822
+ // / object. If \c isTypedArray() would return false, this will assert.
823
+ TypedArray getTypedArray (Runtime& runtime) const &;
824
+
825
+ // / \return an TypedArray instance which refers to the same underlying
826
+ // / object. If \c isTypedArray() would return false, this will assert.
827
+ TypedArray getTypedArray (Runtime& runtime) &&;
828
+
682
829
683
830
// / \return a Function instance which refers to the same underlying
684
831
// / object. If \c isFunction() would return false, this will assert.
@@ -828,6 +975,8 @@ class JSI_EXPORT ArrayBuffer : public Object {
828
975
public:
829
976
ArrayBuffer (ArrayBuffer&&) = default ;
830
977
ArrayBuffer& operator =(ArrayBuffer&&) = default ;
978
+ ArrayBuffer (Runtime& runtime, std::shared_ptr<MutableBuffer> buffer)
979
+ : ArrayBuffer(runtime.createArrayBuffer(std::move(buffer))) {}
831
980
832
981
// / \return the size of the ArrayBuffer, according to its byteLength property.
833
982
// / (C++ naming convention)
@@ -850,6 +999,41 @@ class JSI_EXPORT ArrayBuffer : public Object {
850
999
ArrayBuffer (Runtime::PointerValue* value) : Object(value) {}
851
1000
};
852
1001
1002
+
1003
+ // / Represents a TypedArray
1004
+ class JSI_EXPORT TypedArray : public Object {
1005
+ public:
1006
+ TypedArray (TypedArray &&) = default ;
1007
+
1008
+ TypedArray &operator =(TypedArray &&) = default ;
1009
+
1010
+ // / \return the size of the TypedArray ArrayBuffer, according to its byteLength property.
1011
+ // / (C++ naming convention)
1012
+ size_t size (Runtime &runtime) {
1013
+ return runtime.size (*this );
1014
+ }
1015
+
1016
+ size_t offset (Runtime &runtime) const {
1017
+ return runtime.offset (*this );
1018
+ }
1019
+
1020
+ size_t length (Runtime &runtime) const {
1021
+ return runtime.size (*this );
1022
+ }
1023
+
1024
+ uint8_t *data (Runtime &runtime) {
1025
+ return runtime.data (*this );
1026
+ }
1027
+
1028
+ private:
1029
+ friend class Object ;
1030
+
1031
+ friend class Value ;
1032
+
1033
+ TypedArray (Runtime::PointerValue *value) : Object(value) {}
1034
+ };
1035
+
1036
+
853
1037
// / Represents a JS Object which is guaranteed to be Callable.
854
1038
class JSI_EXPORT Function : public Object {
855
1039
public:
0 commit comments