mirror of
https://github.com/bellard/quickjs.git
synced 2025-05-13 03:15:20 +08:00
fixed TypedArray.prototype.slice() when the buffers overlap
This commit is contained in:
parent
08a28c0cc3
commit
3bffe67e6b
18
quickjs.c
18
quickjs.c
@ -52747,6 +52747,18 @@ static JSValue js_typed_array_toReversed(JSContext *ctx, JSValueConst this_val,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void slice_memcpy(uint8_t *dst, const uint8_t *src, size_t len)
|
||||||
|
{
|
||||||
|
if (dst + len <= src || dst >= src + len) {
|
||||||
|
/* no overlap: can use memcpy */
|
||||||
|
memcpy(dst, src, len);
|
||||||
|
} else {
|
||||||
|
/* otherwise the spec mandates byte copy */
|
||||||
|
while (len-- != 0)
|
||||||
|
*dst++ = *src++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val,
|
static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val,
|
||||||
int argc, JSValueConst *argv)
|
int argc, JSValueConst *argv)
|
||||||
{
|
{
|
||||||
@ -52789,9 +52801,9 @@ static JSValue js_typed_array_slice(JSContext *ctx, JSValueConst this_val,
|
|||||||
if (p1 != NULL && p->class_id == p1->class_id &&
|
if (p1 != NULL && p->class_id == p1->class_id &&
|
||||||
typed_array_get_length(ctx, p1) >= count &&
|
typed_array_get_length(ctx, p1) >= count &&
|
||||||
typed_array_get_length(ctx, p) >= start + count) {
|
typed_array_get_length(ctx, p) >= start + count) {
|
||||||
memcpy(p1->u.array.u.uint8_ptr,
|
slice_memcpy(p1->u.array.u.uint8_ptr,
|
||||||
p->u.array.u.uint8_ptr + (start << shift),
|
p->u.array.u.uint8_ptr + (start << shift),
|
||||||
count << shift);
|
count << shift);
|
||||||
} else {
|
} else {
|
||||||
for (n = 0; n < count; n++) {
|
for (n = 0; n < count; n++) {
|
||||||
val = JS_GetPropertyValue(ctx, this_val, JS_NewInt32(ctx, start + n));
|
val = JS_GetPropertyValue(ctx, this_val, JS_NewInt32(ctx, start + n));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user