Skip to content

Commit

Permalink
fix: Use correct data length in Array#flat (#2058)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Sep 10, 2021
1 parent ba270a8 commit 5dce4e4
Show file tree
Hide file tree
Showing 4 changed files with 1,138 additions and 615 deletions.
9 changes: 5 additions & 4 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ export class Array<T> {
}

// calculate the byteLength of the resulting backing ArrayBuffer
var byteLength = <usize>size << usize(alignof<valueof<T>>());
const align = alignof<valueof<T>>();
var byteLength = <usize>size << align;
var outBuffer = changetype<ArrayBuffer>(__new(byteLength, idof<ArrayBuffer>()));

// create the return value and initialize it
Expand All @@ -492,14 +493,14 @@ export class Array<T> {
let child = load<usize>(ptr + (<usize>i << alignof<T>()));

// ignore null arrays
if (child == 0) continue;
if (!child) continue;

// copy the underlying buffer data to the result buffer
let childDataLength = load<i32>(child, offsetof<T>("byteLength"));
let childDataLength = <usize>load<i32>(child, offsetof<T>("length_")) << align;
memory.copy(
changetype<usize>(outBuffer) + resultOffset,
load<usize>(child, offsetof<T>("dataStart")),
<usize>childDataLength
childDataLength
);

// advance the result length
Expand Down

0 comments on commit 5dce4e4

Please sign in to comment.