Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use runtime check instead compile time for Array#flat #1353

Merged
merged 4 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
MaxGraey marked this conversation as resolved.
Show resolved Hide resolved

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