diff --git a/std/assembly/array.ts b/std/assembly/array.ts index 2ef05f70..ce13b79a 100644 --- a/std/assembly/array.ts +++ b/std/assembly/array.ts @@ -32,22 +32,22 @@ export class Array extends ArrayBufferView { return builtin_isArray(value) && value !== null; } + static create(capacity: i32): Array { + if (capacity > MAX_BYTELENGTH >>> alignof()) throw new RangeError("Invalid length"); + var buffer = new ArrayBuffer(capacity = capacity << alignof()); + var out = REGISTER>(ALLOCATE(offsetof>())); + out.data = buffer; // links + out.dataStart = changetype(buffer); + out.dataLength = capacity; + out.length_ = 0; + return out; + } + constructor(length: i32 = 0) { super(length, alignof()); if (isReference()) { if (!isNullable()) { - let cur = this.dataStart; - let end = cur + (length << alignof()); - while (cur < end) { - // TODO: probably a common reason for complaints of T not having a default ctor. what if - // the array ctor would also take default arguments, like `new Array(10, ...args)`? - store(cur, - isString() - ? "" // no need to instantiate - : RETAIN(instantiate(), this) - ); - cur += sizeof(); - } + if (length) throw new Error("T must be nullable if length > 0"); } } this.length_ = length;