From 696fadb3d5583854d4b5bb490f9237b82a32e241 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Wed, 8 May 2024 15:01:35 -0700 Subject: [PATCH] Add `JS_IsArrayBuffer()` Unlike JS_GetArrayBuffer() it does not throw an exception if `val` is not an array buffer. --- quickjs.c | 14 ++++++++++++++ quickjs.h | 1 + 2 files changed, 15 insertions(+) diff --git a/quickjs.c b/quickjs.c index bf5989d..702c4d3 100644 --- a/quickjs.c +++ b/quickjs.c @@ -53074,6 +53074,20 @@ JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len) TRUE); } +/* return -1 if exception (proxy case) or TRUE/FALSE */ +int JS_IsArrayBuffer(JSContext *ctx, JSValueConst val) +{ + JSObject *p; + + if (js_resolve_proxy(ctx, &val, TRUE)) + return -1; + if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT) + return FALSE; + p = JS_VALUE_GET_OBJ(val); + return p->class_id == JS_CLASS_ARRAY_BUFFER || + p->class_id == JS_CLASS_SHARED_ARRAY_BUFFER; +} + static JSValue js_array_buffer_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv) diff --git a/quickjs.h b/quickjs.h index 84a7e0b..f608b5c 100644 --- a/quickjs.h +++ b/quickjs.h @@ -828,6 +828,7 @@ JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len, JSFreeArrayBufferDataFunc *free_func, void *opaque, JS_BOOL is_shared); JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len); +int JS_IsArrayBuffer(JSContext *ctx, JSValueConst val); void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj); uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj);