commit efbe0400603308baf9dc55907fc85f564739eece
parent 088d5beeacb1a4274e520e285f14737e7a6a634a
Author: Florian Dold <florian@dold.me>
Date: Wed, 9 Aug 2023 22:22:44 +0200
handle array views in JS_GetArrayBuffer
Diffstat:
4 files changed, 40 insertions(+), 55 deletions(-)
diff --git a/prelude.js b/prelude.js
@@ -13,11 +13,7 @@ class TextEncoder {
class TextDecoder {
decode(bytes) {
- if (ArrayBuffer.isView(bytes)) {
- return tart.decodeUtf8(bytes.buffer, bytes.byteOffset, bytes.byteLength);
- }
- // Assume it is an ArrayBuffer
- return tart.decodeUtf8(bytes, 0, bytes.byteLength);
+ return tart.decodeUtf8(bytes);
}
}
diff --git a/quickjs/quickjs.c b/quickjs/quickjs.c
@@ -51917,41 +51917,41 @@ JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
buffer and render the returned pointer invalid */
uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj)
{
- if (js_is_array_buffer(ctx, obj)) {
- JSArrayBuffer *abuf = js_get_array_buffer(ctx, obj);
- if (!abuf)
- goto fail;
- if (abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- *psize = abuf->byte_length;
- return abuf->data;
- } else {
- JSObject *p;
- JSTypedArray *ta;
- JSArrayBuffer *abuf;
- p = get_typed_array(ctx, obj, FALSE);
- if (!p) {
- goto fail;
- }
- if (typed_array_is_detached(ctx, p)) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
- }
- ta = p->u.typed_array;
- abuf = ta->buffer->u.array_buffer;
- if (!abuf)
- goto fail;
- if (abuf->detached) {
- JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
- goto fail;
+ if (js_is_array_buffer(ctx, obj)) {
+ JSArrayBuffer* abuf = js_get_array_buffer(ctx, obj);
+ if (!abuf)
+ goto fail;
+ if (abuf->detached) {
+ JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
+ goto fail;
+ }
+ *psize = abuf->byte_length;
+ return abuf->data;
+ } else {
+ JSObject* p;
+ JSTypedArray* ta;
+ JSArrayBuffer* abuf;
+ p = get_typed_array(ctx, obj, FALSE);
+ if (!p) {
+ goto fail;
+ }
+ if (typed_array_is_detached(ctx, p)) {
+ JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
+ goto fail;
+ }
+ ta = p->u.typed_array;
+ abuf = ta->buffer->u.array_buffer;
+ if (!abuf)
+ goto fail;
+ if (abuf->detached) {
+ JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
+ goto fail;
+ }
+ *psize = ta->length;
+ return abuf->data + ta->offset;
}
- *psize = abuf->byte_length;
- return abuf->data;
- }
- JS_ThrowTypeError(ctx, "expected ArrayBuffer or ArrayBufferView");
- fail:
+ JS_ThrowTypeError(ctx, "expected ArrayBuffer or ArrayBufferView");
+fail:
*psize = 0;
return NULL;
}
diff --git a/tart_module.c b/tart_module.c
@@ -1265,21 +1265,6 @@ static JSValue js_decode_utf8(JSContext *ctx, JSValueConst this_val,
if (NULL == utf8_buf) {
goto exception;
}
-
- if (0 != JS_ToUint32(ctx, &offset, argv[1])) {
- goto exception;
- }
-
- if (0 != JS_ToUint32(ctx, &len, argv[2])) {
- goto exception;
- }
-
- if (offset + len > psize) {
- JS_ThrowRangeError(ctx, "offset %llu len %llu out of range (%llu)",
- offset, len, psize);
- goto exception;
- }
-
return JS_NewStringLen(ctx, (char *) utf8_buf, psize);
done:
return ret_val;
@@ -1409,7 +1394,7 @@ static JSValue js_talercrypto_hash_state_finish(JSContext *ctx, JSValue this_val
static const JSCFunctionListEntry tart_talercrypto_funcs[] = {
JS_CFUNC_DEF("structuredClone", 1, js_structured_clone),
JS_CFUNC_DEF("encodeUtf8", 1, js_encode_utf8),
- JS_CFUNC_DEF("decodeUtf8", 3, js_decode_utf8),
+ JS_CFUNC_DEF("decodeUtf8", 1, js_decode_utf8),
JS_CFUNC_DEF("randomBytes", 1, js_random_bytes),
JS_CFUNC_DEF("encodeCrock", 1, js_talercrypto_encode_crock),
JS_CFUNC_DEF("decodeCrock", 1, js_talercrypto_decode_crock),
diff --git a/test_prelude.js b/test_prelude.js
@@ -17,6 +17,10 @@ const buf2 = new Uint8Array(buf.byteLength + 8);
buf2.set(buf, 4);
+buf2[0] = 42;
+
+console.log(JSON.stringify(dec.decode(buf2.slice(3, buf2.byteLength - 4))))
+
const str2 = dec.decode(buf2.slice(4, buf2.byteLength - 4))
if (str2 != "Hello, World") {
throw Error("test 2 failed");