Skip to content

Commit 2df2bcf

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

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-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+
JSTypedArrayDescriptor *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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,40 @@ 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+
return 8;
864+
}
865+
}
866+
848867
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
849868
JSTypedArrayEnum array_type);
869+
870+
typedef struct JSTypedArrayDescriptor {
871+
JSTypedArrayEnum type;
872+
size_t length;
873+
void *data;
874+
} JSTypedArrayDescriptor;
875+
876+
/* Return value is -1 for proxy errors, 0 if `obj` is not a typed array,
877+
1 if it is a typed array.
878+
The structure pointed to by `desc` is filled on success unless `desc`
879+
is a null pointer. */
880+
int JS_GetTypedArray(JSContext *ctx, JSValueConst obj,
881+
JSTypedArrayDescriptor *desc);
850882
JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
851883
size_t *pbyte_offset,
852884
size_t *pbyte_length,

0 commit comments

Comments
 (0)