Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 40440f7

Browse files
authoredJun 25, 2020
fix: Use runtime check in Array#flat (#1353)
1 parent d5a2959 commit 40440f7

File tree

5 files changed

+10460
-1548
lines changed

5 files changed

+10460
-1548
lines changed
 

‎std/assembly/array.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BLOCK_MAXSIZE } from "./rt/common";
44
import { COMPARATOR, SORT } from "./util/sort";
55
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
66
import { idof, isArray as builtin_isArray } from "./builtins";
7-
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
7+
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_ILLEGALGENTYPE, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
88

99
/** Ensures that the given array has _at least_ the specified backing size. */
1010
function ensureSize(array: usize, minSize: usize, alignLog2: u32): void {
@@ -367,7 +367,12 @@ export class Array<T> {
367367
base + sizeof<T>(),
368368
<usize>lastIndex << alignof<T>()
369369
);
370-
store<T>(base + (<usize>lastIndex << alignof<T>()), changetype<T>(0));
370+
if (isReference<T>()) {
371+
store<usize>(base + (<usize>lastIndex << alignof<T>()), 0);
372+
} else {
373+
// @ts-ignore
374+
store<T>(base + (<usize>lastIndex << alignof<T>()), <T>0);
375+
}
371376
this.length_ = lastIndex;
372377
return element; // no need to retain -> is moved
373378
}
@@ -496,7 +501,7 @@ export class Array<T> {
496501

497502
flat(): T {
498503
if (!isArray<T>()) {
499-
ERROR("Cannot call flat() on Array<T> where T is not an Array.");
504+
throw new TypeError(E_ILLEGALGENTYPE);
500505
}
501506
// Get the length and data start values
502507
var length = this.length_;

‎std/assembly/util/error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const E_INDEXOUTOFRANGE: string = "Index out of range";
99
@lazy @inline
1010
export const E_INVALIDLENGTH: string = "Invalid length";
1111

12+
// @ts-ignore: decorator
13+
@lazy @inline
14+
export const E_ILLEGALGENTYPE: string = "Illegal generic type";
15+
1216
// @ts-ignore: decorator
1317
@lazy @inline
1418
export const E_EMPTYARRAY: string = "Array is empty";

‎tests/compiler/std/array.optimized.wat

Lines changed: 4530 additions & 929 deletions
Large diffs are not rendered by default.

‎tests/compiler/std/array.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,11 @@ __release(changetype<usize>(arr));
10601060
let testArray: i32[][] = [[], []];
10611061
assert(testArray.flat().length == 0);
10621062
}
1063+
1064+
// export extended arrays
1065+
1066+
export class ArrayU32 extends Array<u32> {}
1067+
export class ArrayU8 extends Array<u8> {}
1068+
export class ArrayStr extends Array<string> {}
1069+
// TODO:
1070+
// export class ArrayArrayI32 extends Array<Array<i32>> {}

‎tests/compiler/std/array.untouched.wat

Lines changed: 5910 additions & 616 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.