mirror of
https://github.com/DoneJS-Runtime/quickjs-done-nextgen.git
synced 2025-01-09 17:43:15 +00:00
Add typed array utility functions
This commit is contained in:
parent
be2db73000
commit
7f156b6ef9
2 changed files with 37 additions and 3 deletions
19
quickjs.c
19
quickjs.c
|
@ -52449,6 +52449,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, JSValue *argv,
|
||||
JSTypedArrayEnum type)
|
||||
{
|
||||
if (type < JS_TYPED_ARRAY_UINT8C || type > JS_TYPED_ARRAY_FLOAT64)
|
||||
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. */
|
||||
|
@ -54756,8 +54766,13 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
|
|||
return js_new_uint8array(ctx, buffer);
|
||||
}
|
||||
|
||||
JS_BOOL JS_IsUint8Array(JSValue obj) {
|
||||
return JS_GetClassID(obj) == JS_CLASS_UINT8_ARRAY;
|
||||
int JS_GetTypedArrayType(JSValue obj)
|
||||
{
|
||||
JSClassID class_id = JS_GetClassID(obj);
|
||||
if (class_id >= JS_CLASS_UINT8C_ARRAY && class_id <= JS_CLASS_FLOAT64_ARRAY)
|
||||
return class_id - JS_CLASS_UINT8C_ARRAY;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Atomics */
|
||||
|
|
21
quickjs.h
21
quickjs.h
|
@ -775,6 +775,24 @@ 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_FLOAT16,
|
||||
JS_TYPED_ARRAY_FLOAT32,
|
||||
JS_TYPED_ARRAY_FLOAT64,
|
||||
} JSTypedArrayEnum;
|
||||
|
||||
JS_EXTERN JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValue *argv,
|
||||
JSTypedArrayEnum array_type);
|
||||
JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValue obj,
|
||||
size_t *pbyte_offset,
|
||||
size_t *pbyte_length,
|
||||
|
@ -782,7 +800,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_IsUint8Array(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);
|
||||
|
|
Loading…
Reference in a new issue