Skip to content

Commit

Permalink
Add JS_IsArrayBuffer()
Browse files Browse the repository at this point in the history
 Unlike JS_GetArrayBuffer() it does not throw an exception
 if `val` is not an array buffer.
  • Loading branch information
xeioex committed May 9, 2024
1 parent 1fec6b3 commit c02bd3a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c02bd3a

Please sign in to comment.