Skip to content

Commit 6c43013

Browse files
authored
Add JS_NewTypedArray() (bellard#272)
1 parent 01454ca commit 6c43013

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

quickjs.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53393,6 +53393,16 @@ static JSValue js_typed_array_get_byteOffset(JSContext *ctx,
5339353393
return JS_NewInt32(ctx, ta->offset);
5339453394
}
5339553395

53396+
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
53397+
JSTypedArrayEnum type)
53398+
{
53399+
if (type < JS_TYPED_ARRAY_UINT8C || type > JS_TYPED_ARRAY_FLOAT64)
53400+
return JS_ThrowRangeError(ctx, "invalid typed array type");
53401+
53402+
return js_typed_array_constructor(ctx, JS_UNDEFINED, argc, argv,
53403+
JS_CLASS_UINT8C_ARRAY + type);
53404+
}
53405+
5339653406
/* Return the buffer associated to the typed array or an exception if
5339753407
it is not a typed array or if the buffer is detached. pbyte_offset,
5339853408
pbyte_length or pbytes_per_element can be NULL. */

quickjs.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,23 @@ JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
824824
JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len);
825825
void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj);
826826
uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj);
827+
828+
typedef enum JSTypedArrayEnum {
829+
JS_TYPED_ARRAY_UINT8C = 0,
830+
JS_TYPED_ARRAY_INT8,
831+
JS_TYPED_ARRAY_UINT8,
832+
JS_TYPED_ARRAY_INT16,
833+
JS_TYPED_ARRAY_UINT16,
834+
JS_TYPED_ARRAY_INT32,
835+
JS_TYPED_ARRAY_UINT32,
836+
JS_TYPED_ARRAY_BIG_INT64,
837+
JS_TYPED_ARRAY_BIG_UINT64,
838+
JS_TYPED_ARRAY_FLOAT32,
839+
JS_TYPED_ARRAY_FLOAT64,
840+
} JSTypedArrayEnum;
841+
842+
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
843+
JSTypedArrayEnum array_type);
827844
JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
828845
size_t *pbyte_offset,
829846
size_t *pbyte_length,

0 commit comments

Comments
 (0)