Skip to content

Commit 1fec6b3

Browse files
committed
Add JS_GetTypedArray()
1 parent 97be5a3 commit 1fec6b3

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

quickjs.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53439,6 +53439,32 @@ JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
5343953439
JS_CLASS_UINT8C_ARRAY + type);
5344053440
}
5344153441

53442+
/* Return value is -1 for proxy errors, 0 if `obj` is not a typed array,
53443+
1 if it is a typed array.
53444+
The structure pointed to by `desc` is filled on success unless `desc`
53445+
is a null pointer. */
53446+
int JS_GetTypedArray(JSContext *ctx, JSValueConst obj,
53447+
struct JSTypedArrayDescription *desc)
53448+
{
53449+
int class_id;
53450+
JSObject *p;
53451+
53452+
if (js_resolve_proxy(ctx, &obj, TRUE))
53453+
return -1;
53454+
if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
53455+
return 0;
53456+
p = JS_VALUE_GET_OBJ(obj);
53457+
class_id = p->class_id;
53458+
if (class_id < JS_CLASS_UINT8C_ARRAY || class_id > JS_CLASS_FLOAT64_ARRAY)
53459+
return 0;
53460+
if (desc) {
53461+
desc->type = JS_TYPED_ARRAY_UINT8C + (class_id - JS_CLASS_UINT8C_ARRAY);
53462+
desc->length = p->u.typed_array->length;
53463+
desc->data = p->u.array.u.ptr;
53464+
}
53465+
return 1;
53466+
}
53467+
5344253468
/* Return the buffer associated to the typed array or an exception if
5344353469
it is not a typed array or if the buffer is detached. pbyte_offset,
5344453470
pbyte_length or pbytes_per_element can be NULL. */

quickjs.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,41 @@ typedef enum JSTypedArrayEnum {
845845
JS_TYPED_ARRAY_FLOAT64,
846846
} JSTypedArrayEnum;
847847

848+
static inline int JS_BytesPerElement(JSTypedArrayEnum type)
849+
{
850+
switch (type) {
851+
case JS_TYPED_ARRAY_UINT8C:
852+
case JS_TYPED_ARRAY_INT8:
853+
case JS_TYPED_ARRAY_UINT8:
854+
return 1;
855+
case JS_TYPED_ARRAY_INT16:
856+
case JS_TYPED_ARRAY_UINT16:
857+
return 2;
858+
case JS_TYPED_ARRAY_INT32:
859+
case JS_TYPED_ARRAY_UINT32:
860+
case JS_TYPED_ARRAY_FLOAT32:
861+
return 4;
862+
default:
863+
break;
864+
}
865+
return 8;
866+
}
867+
848868
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
849869
JSTypedArrayEnum array_type);
870+
871+
typedef struct JSTypedArrayDescription {
872+
JSTypedArrayEnum type;
873+
size_t length;
874+
void *data;
875+
} JSTypedArrayDescription;
876+
877+
/* Return value is -1 for proxy errors, 0 if `obj` is not a typed array,
878+
1 if it is a typed array.
879+
The structure pointed to by `desc` is filled on success unless `desc`
880+
is a null pointer. */
881+
int JS_GetTypedArray(JSContext *ctx, JSValueConst obj,
882+
JSTypedArrayDescription *desc);
850883
JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
851884
size_t *pbyte_offset,
852885
size_t *pbyte_length,

0 commit comments

Comments
 (0)