Skip to content

Commit 40440f7

Browse files
authored
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

Diff for: std/assembly/array.ts

+8-3
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_;

Diff for: std/assembly/util/error.ts

+4
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";

0 commit comments

Comments
 (0)