Skip to content

Commit 9c2a19e

Browse files
committed
Update JS_NewTypedArray() to C level API
1 parent 97be5a3 commit 9c2a19e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

quickjs.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -53429,14 +53429,26 @@ static JSValue js_typed_array_get_byteOffset(JSContext *ctx,
5342953429
return JS_NewInt32(ctx, ta->offset);
5343053430
}
5343153431

53432-
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
53432+
JSValue JS_NewTypedArray(JSContext *ctx, size_t length, const void *init,
5343353433
JSTypedArrayEnum type)
5343453434
{
53435+
JSValue args[1], ret;
53436+
5343553437
if (type < JS_TYPED_ARRAY_UINT8C || type > JS_TYPED_ARRAY_FLOAT64)
5343653438
return JS_ThrowRangeError(ctx, "invalid typed array type");
5343753439

53438-
return js_typed_array_constructor(ctx, JS_UNDEFINED, argc, argv,
53439-
JS_CLASS_UINT8C_ARRAY + type);
53440+
args[0] = JS_NewInt64(ctx, length);
53441+
ret = js_typed_array_constructor(ctx, JS_UNDEFINED, 1, (JSValueConst *)args,
53442+
JS_CLASS_UINT8C_ARRAY + type);
53443+
JS_FreeValue(ctx, args[0]);
53444+
53445+
if (init != NULL && !JS_IsException(ret)) {
53446+
JSObject *p = JS_VALUE_GET_OBJ(ret);
53447+
size_t len = length * (1 << typed_array_size_log2(p->class_id));
53448+
memcpy(p->u.array.u.ptr, init, len);
53449+
}
53450+
53451+
return ret;
5344053452
}
5344153453

5344253454
/* Return the buffer associated to the typed array or an exception if

quickjs.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,8 @@ typedef enum JSTypedArrayEnum {
845845
JS_TYPED_ARRAY_FLOAT64,
846846
} JSTypedArrayEnum;
847847

848-
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
849-
JSTypedArrayEnum array_type);
848+
JSValue JS_NewTypedArray(JSContext *ctx, size_t length, const void *init,
849+
JSTypedArrayEnum type);
850850
JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
851851
size_t *pbyte_offset,
852852
size_t *pbyte_length,

0 commit comments

Comments
 (0)