Skip to content

Commit ec4f957

Browse files
authored
Add methods to detect arrays (#282)
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 <[email protected]>
1 parent 377e6a6 commit ec4f957

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

quickjs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48224,6 +48224,10 @@ JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
4822448224
buf, free_func, opaque, FALSE);
4822548225
}
4822648226

48227+
JS_BOOL JS_IsArrayBuffer(JSValue obj) {
48228+
return JS_GetClassID(obj) == JS_CLASS_ARRAY_BUFFER;
48229+
}
48230+
4822748231
/* create a new ArrayBuffer of length 'len' and copy 'buf' to it */
4822848232
JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len)
4822948233
{
@@ -50720,6 +50724,9 @@ JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len)
5072050724
return js_new_uint8array(ctx, buffer);
5072150725
}
5072250726

50727+
JS_BOOL JS_IsUint8Array(JSValue obj) {
50728+
return JS_GetClassID(obj) == JS_CLASS_UINT8_ARRAY;
50729+
}
5072350730

5072450731
/* Atomics */
5072550732
#ifdef CONFIG_ATOMICS

quickjs.h

Lines changed: 2 additions & 0 deletions
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)