Skip to content

Commit

Permalink
fix: Use runtime check in Array#flat (#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey authored Jun 25, 2020
1 parent d5a2959 commit 40440f7
Show file tree
Hide file tree
Showing 5 changed files with 10,460 additions and 1,548 deletions.
11 changes: 8 additions & 3 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BLOCK_MAXSIZE } from "./rt/common";
import { COMPARATOR, SORT } from "./util/sort";
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
import { idof, isArray as builtin_isArray } from "./builtins";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_ILLEGALGENTYPE, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";

/** Ensures that the given array has _at least_ the specified backing size. */
function ensureSize(array: usize, minSize: usize, alignLog2: u32): void {
Expand Down Expand Up @@ -367,7 +367,12 @@ export class Array<T> {
base + sizeof<T>(),
<usize>lastIndex << alignof<T>()
);
store<T>(base + (<usize>lastIndex << alignof<T>()), changetype<T>(0));
if (isReference<T>()) {
store<usize>(base + (<usize>lastIndex << alignof<T>()), 0);
} else {
// @ts-ignore
store<T>(base + (<usize>lastIndex << alignof<T>()), <T>0);
}
this.length_ = lastIndex;
return element; // no need to retain -> is moved
}
Expand Down Expand Up @@ -496,7 +501,7 @@ export class Array<T> {

flat(): T {
if (!isArray<T>()) {
ERROR("Cannot call flat() on Array<T> where T is not an Array.");
throw new TypeError(E_ILLEGALGENTYPE);
}
// Get the length and data start values
var length = this.length_;
Expand Down
4 changes: 4 additions & 0 deletions std/assembly/util/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const E_INDEXOUTOFRANGE: string = "Index out of range";
@lazy @inline
export const E_INVALIDLENGTH: string = "Invalid length";

// @ts-ignore: decorator
@lazy @inline
export const E_ILLEGALGENTYPE: string = "Illegal generic type";

// @ts-ignore: decorator
@lazy @inline
export const E_EMPTYARRAY: string = "Array is empty";
Expand Down
Loading

0 comments on commit 40440f7

Please sign in to comment.