Skip to content

Commit

Permalink
Make typed array utils more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
richarddavison committed Dec 17, 2024
1 parent f584c0c commit 8fa4a89
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 63 deletions.
55 changes: 5 additions & 50 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -54765,57 +54765,12 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
return js_new_uint8array(ctx, buffer);
}

JS_BOOL JS_IsTypedArray(JSValue obj) {
int JS_GetTypedArrayType(JSValue obj)
{
JSClassID class_id = JS_GetClassID(obj);
return class_id >= JS_CLASS_INT8_ARRAY && class_id <= JS_CLASS_FLOAT64_ARRAY;
}

JS_BOOL JS_isUint8ClampedArray(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_UINT8C_ARRAY;
}

JS_BOOL JS_IsInt8Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_INT8_ARRAY;
}

JS_BOOL JS_IsUint8Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_UINT8_ARRAY;
}

JS_BOOL JS_IsInt16Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_INT16_ARRAY;
}

JS_BOOL JS_IsUint16Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_UINT16_ARRAY;
}

JS_BOOL JS_IsInt32Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_INT32_ARRAY;
}

JS_BOOL JS_IsUint32Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_UINT32_ARRAY;
}

JS_BOOL JS_IsBigInt64Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_BIG_INT64_ARRAY;
}

JS_BOOL JS_IsBigUint64Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_BIG_UINT64_ARRAY;
}

JS_BOOL JS_IsFloat16Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_FLOAT16_ARRAY;
}

JS_BOOL JS_IsFloat32Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_FLOAT32_ARRAY;
}

JS_BOOL JS_IsFloat64Array(JSValue obj) {
return JS_GetClassID(obj) == JS_CLASS_FLOAT64_ARRAY;
int mask = -((class_id >= JS_CLASS_UINT8C_ARRAY) & (class_id <= JS_CLASS_FLOAT64_ARRAY));
int offset = (class_id - JS_CLASS_UINT8C_ARRAY) & mask;
return offset | (~mask & -1);
}

/* Atomics */
Expand Down
16 changes: 3 additions & 13 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ typedef enum JSTypedArrayEnum {
JS_TYPED_ARRAY_UINT32,
JS_TYPED_ARRAY_BIG_INT64,
JS_TYPED_ARRAY_BIG_UINT64,
JS_TYPED_ARRAY_FLOAT16,
JS_TYPED_ARRAY_FLOAT32,
JS_TYPED_ARRAY_FLOAT64,
} JSTypedArrayEnum;
Expand All @@ -801,19 +802,8 @@ JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValue obj,
JS_EXTERN JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len,
JSFreeArrayBufferDataFunc *free_func, void *opaque,
JS_BOOL is_shared);
JS_EXTERN JS_BOOL JS_IsTypedArray(JSValue obj);
JS_EXTERN JS_BOOL JS_IsUint8ClampedArray(JSValue obj);
JS_EXTERN JS_BOOL JS_IsInt8Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsUint8Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsInt16Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsUint16Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsInt32Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsUint32Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsBigInt64Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsBigUint64Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsFloat16Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsFloat32Array(JSValue obj);
JS_EXTERN JS_BOOL JS_IsFloat64Array(JSValue obj);
/* returns -1 if not a typed array otherwise return a JSTypedArrayEnum value */
JS_EXTERN int JS_GetTypedArrayType(JSValue obj);
JS_EXTERN JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len);
typedef struct {
void *(*sab_alloc)(void *opaque, size_t size);
Expand Down

0 comments on commit 8fa4a89

Please sign in to comment.