Skip to content

Commit 40a7281

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

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

quickjs.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53074,6 +53074,33 @@ 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_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
53083+
p = JS_VALUE_GET_OBJ(val);
53084+
if (unlikely(p->class_id == JS_CLASS_PROXY)) {
53085+
JSProxyData *s = JS_GetOpaque(val, JS_CLASS_PROXY);
53086+
if (!s)
53087+
return FALSE;
53088+
if (js_check_stack_overflow(ctx->rt, 0)) {
53089+
JS_ThrowStackOverflow(ctx);
53090+
return -1;
53091+
}
53092+
if (s->is_revoked) {
53093+
JS_ThrowTypeErrorRevokedProxy(ctx);
53094+
return -1;
53095+
}
53096+
return JS_IsArrayBuffer(ctx, s->target);
53097+
}
53098+
return p->class_id == JS_CLASS_ARRAY_BUFFER ||
53099+
p->class_id == JS_CLASS_SHARED_ARRAY_BUFFER;
53100+
}
53101+
return FALSE;
53102+
}
53103+
5307753104
static JSValue js_array_buffer_constructor(JSContext *ctx,
5307853105
JSValueConst new_target,
5307953106
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)