Skip to content

Commit 7f156b6

Browse files
authored
Add typed array utility functions
1 parent be2db73 commit 7f156b6

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

quickjs.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52449,6 +52449,16 @@ static JSValue js_typed_array_get_byteOffset(JSContext *ctx, JSValue this_val)
5244952449
return js_uint32(ta->offset);
5245052450
}
5245152451

52452+
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValue *argv,
52453+
JSTypedArrayEnum type)
52454+
{
52455+
if (type < JS_TYPED_ARRAY_UINT8C || type > JS_TYPED_ARRAY_FLOAT64)
52456+
return JS_ThrowRangeError(ctx, "invalid typed array type");
52457+
52458+
return js_typed_array_constructor(ctx, JS_UNDEFINED, argc, argv,
52459+
JS_CLASS_UINT8C_ARRAY + type);
52460+
}
52461+
5245252462
/* Return the buffer associated to the typed array or an exception if
5245352463
it is not a typed array or if the buffer is detached. pbyte_offset,
5245452464
pbyte_length or pbytes_per_element can be NULL. */
@@ -54756,8 +54766,13 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
5475654766
return js_new_uint8array(ctx, buffer);
5475754767
}
5475854768

54759-
JS_BOOL JS_IsUint8Array(JSValue obj) {
54760-
return JS_GetClassID(obj) == JS_CLASS_UINT8_ARRAY;
54769+
int JS_GetTypedArrayType(JSValue obj)
54770+
{
54771+
JSClassID class_id = JS_GetClassID(obj);
54772+
if (class_id >= JS_CLASS_UINT8C_ARRAY && class_id <= JS_CLASS_FLOAT64_ARRAY)
54773+
return class_id - JS_CLASS_UINT8C_ARRAY;
54774+
else
54775+
return -1;
5476154776
}
5476254777

5476354778
/* Atomics */

quickjs.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,14 +775,33 @@ JS_EXTERN void JS_DetachArrayBuffer(JSContext *ctx, JSValue obj);
775775
JS_EXTERN uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValue obj);
776776
JS_EXTERN JS_BOOL JS_IsArrayBuffer(JSValue obj);
777777
JS_EXTERN uint8_t *JS_GetUint8Array(JSContext *ctx, size_t *psize, JSValue obj);
778+
779+
typedef enum JSTypedArrayEnum {
780+
JS_TYPED_ARRAY_UINT8C = 0,
781+
JS_TYPED_ARRAY_INT8,
782+
JS_TYPED_ARRAY_UINT8,
783+
JS_TYPED_ARRAY_INT16,
784+
JS_TYPED_ARRAY_UINT16,
785+
JS_TYPED_ARRAY_INT32,
786+
JS_TYPED_ARRAY_UINT32,
787+
JS_TYPED_ARRAY_BIG_INT64,
788+
JS_TYPED_ARRAY_BIG_UINT64,
789+
JS_TYPED_ARRAY_FLOAT16,
790+
JS_TYPED_ARRAY_FLOAT32,
791+
JS_TYPED_ARRAY_FLOAT64,
792+
} JSTypedArrayEnum;
793+
794+
JS_EXTERN JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValue *argv,
795+
JSTypedArrayEnum array_type);
778796
JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValue obj,
779797
size_t *pbyte_offset,
780798
size_t *pbyte_length,
781799
size_t *pbytes_per_element);
782800
JS_EXTERN JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len,
783801
JSFreeArrayBufferDataFunc *free_func, void *opaque,
784802
JS_BOOL is_shared);
785-
JS_EXTERN JS_BOOL JS_IsUint8Array(JSValue obj);
803+
/* returns -1 if not a typed array otherwise return a JSTypedArrayEnum value */
804+
JS_EXTERN int JS_GetTypedArrayType(JSValue obj);
786805
JS_EXTERN JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len);
787806
typedef struct {
788807
void *(*sab_alloc)(void *opaque, size_t size);

0 commit comments

Comments
 (0)