Skip to content

Commit c32407e

Browse files
committed
Add JS_IsTypedArray()
Unlike JS_GetTypedArrayBuffer() it does not throw an exception if `val` is not a typed array.
1 parent 97be5a3 commit c32407e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

quickjs.c

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

53442+
/* return -1 if exception (proxy case) or TRUE/FALSE */
53443+
int JS_IsTypedArray(JSContext *ctx, JSValueConst val)
53444+
{
53445+
JSObject *p;
53446+
53447+
if (js_resolve_proxy(ctx, &val, TRUE))
53448+
return -1;
53449+
if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
53450+
return FALSE;
53451+
p = JS_VALUE_GET_OBJ(val);
53452+
return p->class_id >= JS_CLASS_UINT8C_ARRAY &&
53453+
p->class_id <= JS_CLASS_FLOAT64_ARRAY;
53454+
}
53455+
5344253456
/* Return the buffer associated to the typed array or an exception if
5344353457
it is not a typed array or if the buffer is detached. pbyte_offset,
5344453458
pbyte_length or pbytes_per_element can be NULL. */

quickjs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ typedef enum JSTypedArrayEnum {
847847

848848
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
849849
JSTypedArrayEnum array_type);
850+
int JS_IsTypedArray(JSContext *ctx, JSValueConst val);
850851
JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
851852
size_t *pbyte_offset,
852853
size_t *pbyte_length,

0 commit comments

Comments
 (0)