This commit is contained in:
dcode
2019-03-18 00:40:55 +01:00
parent edb2299f13
commit ba4c00efbd
20 changed files with 933 additions and 1050 deletions

View File

@ -4,13 +4,13 @@ import { COMPARATOR, SORT } from "./util/sort";
import { itoa, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./util/number";
import { isArray as builtin_isArray } from "./builtins";
/** Ensures that the given array has _at least_ the specified length. */
function ensureLength(array: ArrayBufferView, length: i32, alignLog2: u32): void {
/** Ensures that the given array has _at least_ the specified capacity. */
function ensureCapacity(array: ArrayBufferView, minCapacity: i32, alignLog2: u32): void {
var oldData = array.data;
var oldCapacity = oldData.byteLength >>> alignLog2;
if (<u32>length > <u32>oldCapacity) {
if (<u32>length > <u32>(MAX_BYTELENGTH >>> alignLog2)) throw new RangeError("Invalid array length");
let newByteLength = length << alignLog2;
if (<u32>minCapacity > <u32>oldCapacity) {
if (<u32>minCapacity > <u32>(MAX_BYTELENGTH >>> alignLog2)) throw new RangeError("Invalid array length");
let newByteLength = minCapacity << alignLog2;
let newData = REALLOCATE(changetype<usize>(oldData), <usize>newByteLength); // registers on move
if (newData !== changetype<usize>(oldData)) {
array.data = changetype<ArrayBuffer>(newData); // links
@ -41,7 +41,7 @@ export class Array<T> extends ArrayBufferView {
}
set length(length: i32) {
ensureLength(changetype<ArrayBufferView>(this), length, alignof<T>());
ensureCapacity(this, length, alignof<T>());
this.length_ = length;
}
@ -61,7 +61,7 @@ export class Array<T> extends ArrayBufferView {
@operator("[]=")
private __set(index: i32, value: T): void { // unchecked is built-in
ensureLength(changetype<ArrayBufferView>(this), index + 1, alignof<T>());
ensureCapacity(this, index + 1, alignof<T>());
store<T>(this.dataStart + (<usize>index << alignof<T>()), value);
if (isManaged<T>()) LINK(value, this);
if (index >= this.length_) this.length_ = index + 1;
@ -119,7 +119,7 @@ export class Array<T> extends ArrayBufferView {
push(element: T): i32 {
var newLength = this.length_ + 1;
ensureLength(changetype<ArrayBufferView>(this), newLength, alignof<T>());
ensureCapacity(this, newLength, alignof<T>());
this.length_ = newLength;
store<T>(this.dataStart + (<usize>(newLength - 1) << alignof<T>()), element);
if (isManaged<T>()) LINK(element, this);
@ -266,7 +266,7 @@ export class Array<T> extends ArrayBufferView {
unshift(element: T): i32 {
var newLength = this.length_ + 1;
ensureLength(changetype<ArrayBufferView>(this), newLength, alignof<T>());
ensureCapacity(this, newLength, alignof<T>());
var base = this.dataStart;
memory.copy(
base + sizeof<T>(),

View File

@ -41,13 +41,11 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
}
charCodeAt(pos: i32): i32 {
assert(this !== null);
if (<u32>pos >= <u32>this.length) return -1; // (NaN)
return load<u16>(changetype<usize>(this) + (<usize>pos << 1));
}
codePointAt(pos: i32): i32 {
assert(this !== null);
if (<u32>pos >= <u32>this.length) return -1; // (undefined)
var first = <i32>load<u16>(changetype<usize>(this) + (<usize>pos << 1));
if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length) return first;
@ -56,13 +54,11 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
}
@operator("+") static concat(left: String, right: String): String {
if (!changetype<usize>(left)) left = changetype<String>("null");
return left.concat(right);
@operator("+") private static __concat(left: string, right: string): string {
return select<string>(left, "null", left !== null).concat(right);
}
concat(other: String): String {
assert(this !== null);
if (other === null) other = changetype<String>("null");
var thisSize: isize = this.length << 1;
var otherSize: isize = other.length << 1;
@ -85,7 +81,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
return !compareImpl(this, start, searchString, 0, searchLength);
}
@operator("==") static eq(left: String, right: String): bool {
@operator("==") private static __eq(left: String, right: String): bool {
if (left === right) return true;
if (left === null || right === null) return false;
var leftLength = left.length;
@ -94,11 +90,11 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
return !compareImpl(left, 0, right, 0, leftLength);
}
@operator("!=") static ne(left: String, right: String): bool {
return !this.eq(left, right);
@operator("!=") private static __ne(left: String, right: String): bool {
return !this.__eq(left, right);
}
@operator(">") static gt(left: String, right: String): bool {
@operator(">") private static __gt(left: String, right: String): bool {
if (left === right || left === null || right === null) return false;
var leftLength = left.length;
var rightLength = right.length;
@ -108,11 +104,11 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) > 0;
}
@operator(">=") static gte(left: String, right: String): bool {
return !this.lt(left, right);
@operator(">=") private static __gte(left: String, right: String): bool {
return !this.__lt(left, right);
}
@operator("<") static lt(left: String, right: String): bool {
@operator("<") private static __lt(left: String, right: String): bool {
if (left === right || left === null || right === null) return false;
var leftLength = left.length;
var rightLength = right.length;
@ -122,8 +118,8 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) < 0;
}
@operator("<=") static lte(left: String, right: String): bool {
return !this.gt(left, right);
@operator("<=") private static __lte(left: String, right: String): bool {
return !this.__gt(left, right);
}
@inline includes(searchString: String, position: i32 = 0): bool {
@ -366,8 +362,8 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
charStr,
load<u16>(changetype<usize>(this) + (<usize>i << 1))
);
store<usize>(resultStart + (<usize>i << alignof<usize>()), charStr); // result[i] = charStr
LINK(REGISTER<String>(charStr), result);
store<usize>(resultStart + (<usize>i << alignof<usize>()), REGISTER<String>(charStr)); // result[i] = charStr
if (isManaged<String>()) LINK(changetype<String>(charStr), result);
}
return result;
} else if (!length) {

View File

@ -1,4 +1,4 @@
import { ALLOCATE, REGISTER, LINK, ArrayBufferView } from "./runtime";
import { ALLOCATE, REGISTER, ArrayBufferView } from "./runtime";
import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort";
// function clampToByte(value: i32): i32 {