Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed array utility functions #760

Merged
merged 5 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52448,6 +52448,16 @@ static JSValue js_typed_array_get_byteOffset(JSContext *ctx, JSValue this_val)
return js_uint32(ta->offset);
}

JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
richarddd marked this conversation as resolved.
Show resolved Hide resolved
richarddd marked this conversation as resolved.
Show resolved Hide resolved
JSTypedArrayEnum type)
{
if (type < JS_TYPED_ARRAY_UINT8C || type > JS_TYPED_ARRAY_FLOAT64)
richarddd marked this conversation as resolved.
Show resolved Hide resolved
return JS_ThrowRangeError(ctx, "invalid typed array type");

return js_typed_array_constructor(ctx, JS_UNDEFINED, argc, argv,
JS_CLASS_UINT8C_ARRAY + type);
}

/* Return the buffer associated to the typed array or an exception if
it is not a typed array or if the buffer is detached. pbyte_offset,
pbyte_length or pbytes_per_element can be NULL. */
Expand Down Expand Up @@ -54755,10 +54765,59 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
return js_new_uint8array(ctx, buffer);
}

JS_BOOL JS_IsTypedArray(JSValue obj) {
saghul marked this conversation as resolved.
Show resolved Hide resolved
JSClassID class_id = JS_GetClassID(obj);
return class_id >= JS_CLASS_INT8_ARRAY && class_id <= JS_CLASS_FLOAT64_ARRAY;
richarddd marked this conversation as resolved.
Show resolved Hide resolved
}

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;
}

/* Atomics */
#ifdef CONFIG_ATOMICS

Expand Down
29 changes: 29 additions & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,14 +777,43 @@ JS_EXTERN void JS_DetachArrayBuffer(JSContext *ctx, JSValue obj);
JS_EXTERN uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValue obj);
JS_EXTERN JS_BOOL JS_IsArrayBuffer(JSValue obj);
JS_EXTERN uint8_t *JS_GetUint8Array(JSContext *ctx, size_t *psize, JSValue obj);

typedef enum JSTypedArrayEnum {
JS_TYPED_ARRAY_UINT8C = 0,
JS_TYPED_ARRAY_INT8,
JS_TYPED_ARRAY_UINT8,
JS_TYPED_ARRAY_INT16,
JS_TYPED_ARRAY_UINT16,
JS_TYPED_ARRAY_INT32,
JS_TYPED_ARRAY_UINT32,
JS_TYPED_ARRAY_BIG_INT64,
JS_TYPED_ARRAY_BIG_UINT64,
JS_TYPED_ARRAY_FLOAT32,
JS_TYPED_ARRAY_FLOAT64,
} JSTypedArrayEnum;
richarddd marked this conversation as resolved.
Show resolved Hide resolved

JS_EXTERN JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
JSTypedArrayEnum array_type);
JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValue obj,
size_t *pbyte_offset,
size_t *pbyte_length,
size_t *pbytes_per_element);
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);
richarddd marked this conversation as resolved.
Show resolved Hide resolved
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
Loading