Skip to content

Commit c02bd3a

Browse files
committed
Add JS_IsArrayBuffer()
Unlike JS_GetArrayBuffer() it does not throw an exception if `val` is not an array buffer.
1 parent 1fec6b3 commit c02bd3a

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
@@ -53074,6 +53074,20 @@ JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len)
5307453074
TRUE);
5307553075
}
5307653076

53077+
/* return -1 if exception (proxy case) or TRUE/FALSE */
53078+
int JS_IsArrayBuffer(JSContext *ctx, JSValueConst val)
53079+
{
53080+
JSObject *p;
53081+
53082+
if (js_resolve_proxy(ctx, &val, TRUE))
53083+
return -1;
53084+
if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
53085+
return FALSE;
53086+
p = JS_VALUE_GET_OBJ(val);
53087+
return p->class_id == JS_CLASS_ARRAY_BUFFER ||
53088+
p->class_id == JS_CLASS_SHARED_ARRAY_BUFFER;
53089+
}
53090+
5307753091
static JSValue js_array_buffer_constructor(JSContext *ctx,
5307853092
JSValueConst new_target,
5307953093
int argc, JSValueConst *argv)

quickjs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
828828
JSFreeArrayBufferDataFunc *free_func, void *opaque,
829829
JS_BOOL is_shared);
830830
JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len);
831+
int JS_IsArrayBuffer(JSContext *ctx, JSValueConst val);
831832
void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj);
832833
uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj);
833834

0 commit comments

Comments
 (0)