Skip to content

Commit 57266a0

Browse files
committed
Add methods to detect arrays
I have a use case where a user can hand me many different kinds of types, array buffer, uint8array, or a string, and I need to be able to distingush between them. Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
1 parent 33f7249 commit 57266a0

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

quickjs.c

+7
Original file line numberDiff line numberDiff line change
@@ -48195,6 +48195,10 @@ JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
4819548195
buf, free_func, opaque, FALSE);
4819648196
}
4819748197

48198+
JS_BOOL JS_IsArrayBuffer(JSValue obj) {
48199+
return JS_GetClassID(obj) == JS_CLASS_ARRAY_BUFFER;
48200+
}
48201+
4819848202
/* create a new ArrayBuffer of length 'len' and copy 'buf' to it */
4819948203
JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len)
4820048204
{
@@ -50691,6 +50695,9 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
5069150695
return js_new_uint8array(ctx, buffer);
5069250696
}
5069350697

50698+
JS_BOOL JS_IsUint8Array(JSValue obj) {
50699+
return JS_GetClassID(obj) == JS_CLASS_UINT8_ARRAY;
50700+
}
5069450701

5069550702
/* Atomics */
5069650703
#ifdef CONFIG_ATOMICS

quickjs.h

+2
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ JS_EXTERN JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
748748
JS_EXTERN JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len);
749749
JS_EXTERN void JS_DetachArrayBuffer(JSContext *ctx, JSValue obj);
750750
JS_EXTERN uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValue obj);
751+
JS_EXTERN JS_BOOL JS_IsArrayBuffer(JSValue obj);
751752
JS_EXTERN uint8_t *JS_GetUint8Array(JSContext *ctx, size_t *psize, JSValue obj);
752753
JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValue obj,
753754
size_t *pbyte_offset,
@@ -756,6 +757,7 @@ JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValue obj,
756757
JS_EXTERN JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len,
757758
JSFreeArrayBufferDataFunc *free_func, void *opaque,
758759
JS_BOOL is_shared);
760+
JS_EXTERN JS_BOOL JS_IsUint8Array(JSValue obj);
759761
JS_EXTERN JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len);
760762
typedef struct {
761763
void *(*sab_alloc)(void *opaque, size_t size);

0 commit comments

Comments
 (0)