Also lint stdlib

This commit is contained in:
dcodeIO
2018-02-25 23:21:32 +01:00
parent ae05006d21
commit 9ef8b162a9
30 changed files with 506 additions and 461 deletions

6
std/assembly.d.ts vendored
View File

@ -155,7 +155,7 @@ declare function sqrt<T = f32 | f64>(value: T): T;
/** Rounds to the nearest integer towards zero of a 32-bit or 64-bit float. */
declare function trunc<T = f32 | f64>(value: T): T;
/** Loads a value of the specified type from memory. Equivalent to dereferncing a pointer in other languages. */
declare function load<T>(ptr: usize, constantOffset?: usize): any;
declare function load<T>(ptr: usize, constantOffset?: usize): T;
/** Stores a value of the specified type to memory. Equivalent to dereferencing a pointer in other languages when assigning a value. */
declare function store<T>(ptr: usize, value: any, constantOffset?: usize): void;
/** Returns the current memory size in units of pages. One page is 64kb. */
@ -292,10 +292,10 @@ declare class Set<T> {
// Internal decorators
/** Annotates an element as a program global. */
declare function global(target: Function): any;
declare function global(target: Function, propertyKey: string, descriptor: any): void;
/** Annotates a method as an operator overload. */
declare function operator(token: string): any;
declare function operator(token: string): (target: any, propertyKey: string, descriptor: any) => void;
/** Annotates a class as being unmanaged with limited capabilities. */
declare function unmanaged(target: Function): any;

View File

@ -17,7 +17,6 @@ const SL_SIZE: usize = 1 << <usize>SL_BITS;
const SB_BITS: usize = <usize>(SL_BITS + AL_BITS);
const SB_SIZE: usize = 1 << <usize>SB_BITS;
const SB_MASK: usize = SB_SIZE - 1;
const FL_BITS: u32 = (sizeof<usize>() == sizeof<u32>()
? 30 // ^= up to 1GB per block
@ -267,10 +266,8 @@ class Root {
// link previous and next free block
var prev = block.prev;
var next = block.next;
if (prev)
prev.next = next;
if (next)
next.prev = prev;
if (prev) prev.next = next;
if (next) next.prev = prev;
// update head if we are removing it
if (block == this.getHead(fl, sl)) {
@ -282,8 +279,7 @@ class Root {
this.setSLMap(fl, slMap &= ~(1 << sl));
// clear first level map if second level is empty now
if (!slMap)
this.flMap &= ~(1 << fl);
if (!slMap) this.flMap &= ~(1 << fl);
}
}
}
@ -320,10 +316,10 @@ class Root {
slMap = assert(this.getSLMap(fl)); // can't be zero if fl points here
head = this.getHead(fl, ffs<u32>(slMap));
}
} else
} else {
head = this.getHead(fl, ffs<u32>(slMap));
return head;
}
return head;
}
/** Links a free left with its right block in memory. */
@ -386,13 +382,15 @@ class Root {
tailInfo = changetype<Block>(tailRef).info;
}
} else
} else {
assert(start >= changetype<usize>(this) + Root.SIZE); // starts after root
}
// check if size is large enough for a free block and the tail block
var size = end - start;
if (size < Block.INFO + Block.MIN_SIZE + Block.INFO)
if (size < Block.INFO + Block.MIN_SIZE + Block.INFO) {
return false;
}
// left size is total minus its own and the zero-length tail's header
var leftSize = size - 2 * Block.INFO;
@ -443,8 +441,9 @@ export function allocate_memory(size: usize): usize {
root.flMap = 0;
for (var fl: usize = 0; fl < FL_BITS; ++fl) {
root.setSLMap(fl, 0);
for (var sl: u32 = 0; sl < SL_SIZE; ++sl)
for (var sl: u32 = 0; sl < SL_SIZE; ++sl) {
root.setHead(fl, sl, null);
}
}
root.addMemory(rootOffset + Root.SIZE, current_memory() << 16);
}
@ -461,9 +460,11 @@ export function allocate_memory(size: usize): usize {
var pagesBefore = current_memory();
var pagesNeeded = ((size + 0xffff) & ~0xffff) >>> 16;
var pagesWanted = max(pagesBefore, pagesNeeded); // double memory
if (grow_memory(pagesWanted) < 0)
if (grow_memory(pagesNeeded) < 0)
if (grow_memory(pagesWanted) < 0) {
if (grow_memory(pagesNeeded) < 0) {
unreachable(); // out of memory
}
}
var pagesAfter = current_memory();
root.addMemory(<usize>pagesBefore << 16, <usize>pagesAfter << 16);
block = assert(root.search(size)); // must be found now

View File

@ -16,9 +16,12 @@ export class Array<T> {
}
constructor(capacity: i32 = 0) {
if (capacity < 0)
if (capacity < 0) {
throw new RangeError("Invalid array length");
this.__memory = capacity ? allocate_memory(<usize>capacity * sizeof<T>()) : 0;
}
this.__memory = capacity
? allocate_memory(<usize>capacity * sizeof<T>())
: 0;
this.__capacity = this.__length = capacity;
}
@ -27,72 +30,92 @@ export class Array<T> {
}
set length(length: i32) {
if (length < 0)
if (length < 0) {
throw new RangeError("Invalid array length");
if (length > this.__capacity)
}
if (length > this.__capacity) {
this.__grow(max(length, this.__capacity << 1));
}
this.__length = length;
}
@operator("[]")
private __get(index: i32): T {
if (<u32>index >= this.__capacity)
if (<u32>index >= this.__capacity) {
throw new Error("Index out of bounds"); // return changetype<T>(0) ?
}
return load<T>(this.__memory + <usize>index * sizeof<T>());
}
@operator("[]=")
private __set(index: i32, value: T): void {
if (index < 0)
if (index < 0) {
throw new Error("Index out of bounds");
if (index >= this.__capacity)
}
if (index >= this.__capacity) {
this.__grow(max(index + 1, this.__capacity << 1));
}
store<T>(this.__memory + <usize>index * sizeof<T>(), value);
}
indexOf(searchElement: T, fromIndex: i32 = 0): i32 {
if (fromIndex < 0)
if (fromIndex < 0) {
fromIndex = this.__length + fromIndex;
}
while (<u32>fromIndex < this.__length) {
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement)
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement) {
return fromIndex;
}
++fromIndex;
}
return -1;
}
lastIndexOf(searchElement: T, fromIndex: i32 = 0): i32 {
if (fromIndex < 0)
if (fromIndex < 0) {
fromIndex = this.__length + fromIndex;
else if (fromIndex >= this.__length)
} else if (fromIndex >= this.__length) {
fromIndex = this.__length - 1;
}
while (fromIndex >= 0) {
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement)
if (load<T>(this.__memory + fromIndex * sizeof<T>()) == searchElement) {
return fromIndex;
}
--fromIndex;
}
return -1;
}
push(element: T): i32 {
if (this.__length == this.__capacity)
if (this.__length == this.__capacity) {
this.__grow(this.__capacity ? this.__capacity << 1 : 1);
}
store<T>(this.__memory + this.__length * sizeof<T>(), element);
return ++this.__length;
}
pop(): T {
if (this.__length < 1)
if (this.__length < 1) {
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
}
return load<T>(this.__memory + --this.__length * sizeof<T>());
}
shift(): T {
if (this.__length < 1)
if (this.__length < 1) {
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
}
var element = load<T>(this.__memory);
move_memory(this.__memory, this.__memory + sizeof<T>(), (this.__capacity - 1) * sizeof<T>());
set_memory(this.__memory + (this.__capacity - 1) * sizeof<T>(), 0, sizeof<T>());
move_memory(
this.__memory,
this.__memory + sizeof<T>(),
(this.__capacity - 1) * sizeof<T>()
);
set_memory(
this.__memory + (this.__capacity - 1) * sizeof<T>(),
0,
sizeof<T>()
);
--this.__length;
return element;
}
@ -105,13 +128,22 @@ export class Array<T> {
assert(newCapacity > this.__capacity);
var newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
if (this.__memory) {
move_memory(newMemory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
move_memory(
newMemory + sizeof<T>(),
this.__memory,
oldCapacity * sizeof<T>()
);
free_memory(this.__memory);
}
this.__memory = newMemory;
this.__capacity = newCapacity;
} else
move_memory(this.__memory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
} else {
move_memory(
this.__memory + sizeof<T>(),
this.__memory,
oldCapacity * sizeof<T>()
);
}
store<T>(this.__memory, element);
return ++this.__length;
}
@ -119,37 +151,53 @@ export class Array<T> {
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
if (begin < 0) {
begin = this.__length + begin;
if (begin < 0)
if (begin < 0) {
begin = 0;
} else if (begin > this.__length)
}
} else if (begin > this.__length) {
begin = this.__length;
if (end < 0)
}
if (end < 0) {
end = this.__length + end;
else if (end > this.__length)
} else if (end > this.__length) {
end = this.__length;
if (end < begin)
}
if (end < begin) {
end = begin;
}
var capacity = end - begin;
assert(capacity >= 0);
var sliced = new Array<T>(capacity);
if (capacity)
move_memory(sliced.__memory, this.__memory + <usize>begin * sizeof<T>(), <usize>capacity * sizeof<T>());
if (capacity) {
move_memory(
sliced.__memory,
this.__memory + <usize>begin * sizeof<T>(),
<usize>capacity * sizeof<T>()
);
}
return sliced;
}
splice(start: i32, deleteCount: i32 = i32.MAX_VALUE): void {
if (deleteCount < 1)
if (deleteCount < 1) {
return;
}
if (start < 0) {
start = this.__length + start;
if (start < 0)
if (start < 0) {
start = 0;
else if (start >= this.__length)
} else if (start >= this.__length) {
return;
} else if (start >= this.__length)
}
} else if (start >= this.__length) {
return;
}
deleteCount = min(deleteCount, this.__length - start);
move_memory(this.__memory + <usize>start * sizeof<T>(), this.__memory + <usize>(start + deleteCount) * sizeof<T>(), deleteCount * sizeof<T>());
move_memory(
this.__memory + <usize>start * sizeof<T>(),
this.__memory + <usize>(start + deleteCount) * sizeof<T>(),
deleteCount * sizeof<T>()
);
this.__length -= deleteCount;
}
@ -170,15 +218,17 @@ export class CArray<T> {
@operator("[]")
private __get(index: i32): T {
if (index < 0)
if (index < 0) {
throw new RangeError("Index out of range");
}
return load<T>(changetype<usize>(this) + <usize>index * sizeof<T>());
}
@operator("[]=")
private __set(index: i32, value: T): void {
if (index < 0)
if (index < 0) {
throw new RangeError("Index out of range");
}
store<T>(changetype<usize>(this) + <usize>index * sizeof<T>(), value);
}
}

View File

@ -89,7 +89,12 @@ export declare function changetype<T>(value: void): T;
export declare function assert<T>(isTrueish: T, message?: string): T;
@builtin
export declare function abort(message?: string | null, fileName?: string | null, lineNumber?: u32, columnNumber?: u32): void;
export declare function abort(
message?: string | null,
fileName?: string | null,
lineNumber?: u32,
columnNumber?: u32
): void;
@builtin
declare function i8(value: void): i8;
@ -126,8 +131,12 @@ export { i64 };
@builtin
declare function isize(value: void): isize;
namespace isize {
export const MIN_VALUE: isize = sizeof<i32>() == sizeof<isize>() ? -2147483648 : <usize>-9223372036854775808;
export const MAX_VALUE: isize = sizeof<i32>() == sizeof<isize>() ? 2147483647 : <usize>9223372036854775807;
export const MIN_VALUE: isize = sizeof<i32>() == sizeof<isize>()
? -2147483648
: <usize>-9223372036854775808;
export const MAX_VALUE: isize = sizeof<i32>() == sizeof<isize>()
? 2147483647
: <usize>9223372036854775807;
}
export { isize };
@ -167,7 +176,9 @@ export { u64 };
declare function usize(value: void): usize;
namespace usize {
export const MIN_VALUE: usize = 0;
export const MAX_VALUE: usize = sizeof<u32>() == sizeof<usize>() ? 4294967295 : <usize>18446744073709551615;
export const MAX_VALUE: usize = sizeof<u32>() == sizeof<usize>()
? 4294967295
: <usize>18446744073709551615;
}
export { usize };

View File

@ -11,17 +11,21 @@ export class Map<K,V> {
get(key: K): V | null {
var keys = this.__keys;
for (var i = 0, k = keys.length; i < k; ++i)
if (keys[i] == key)
for (var i = 0, k = keys.length; i < k; ++i) {
if (keys[i] == key) {
return this.__values[i];
}
}
return null;
}
has(key: K): bool {
var keys = this.__keys;
for (var i = 0, k = keys.length; i < k; ++i)
if (keys[i] == key)
for (var i = 0, k = keys.length; i < k; ++i) {
if (keys[i] == key) {
return true;
}
}
return false;
}

View File

@ -145,8 +145,7 @@ export function move_memory(dest: usize, src: usize, n: usize): void {
// based on musl's implementation of memmove
// becomes obsolete once https://github.com/WebAssembly/bulk-memory-operations lands
if (dest == src)
return;
if (dest == src) return;
if (src + n <= dest || dest + n <= src) {
copy_memory(dest, src, n);
return;
@ -154,8 +153,7 @@ export function move_memory(dest: usize, src: usize, n: usize): void {
if (dest < src) {
if ((src & 7) == (dest & 7)) {
while (dest & 7) {
if (!n)
return;
if (!n) return;
--n;
store<u8>(dest++, load<u8>(src++));
}
@ -173,8 +171,7 @@ export function move_memory(dest: usize, src: usize, n: usize): void {
} else {
if ((src & 7) == (dest & 7)) {
while ((dest + n) & 7) {
if (!n)
return;
if (!n) return;
store<u8>(dest + --n, load<u8>(src + n));
}
while (n >= 8) {
@ -193,23 +190,19 @@ export function set_memory(dest: usize, c: u8, n: usize): void {
// becomes obsolete once https://github.com/WebAssembly/bulk-memory-operations lands
// fill head and tail with minimal branching
if (!n)
return;
if (!n) return;
store<u8>(dest, c);
store<u8>(dest + n - 1, c);
if (n <= 2)
return;
if (n <= 2) return;
store<u8>(dest + 1, c);
store<u8>(dest + 2, c);
store<u8>(dest + n - 2, c);
store<u8>(dest + n - 3, c);
if (n <= 6)
return;
if (n <= 6) return;
store<u8>(dest + 3, c);
store<u8>(dest + n - 4, c);
if (n <= 8)
return;
if (n <= 8) return;
// advance pointer to align it at 4-byte boundary
var k: usize = -dest & 3;
@ -222,14 +215,12 @@ export function set_memory(dest: usize, c: u8, n: usize): void {
// fill head/tail up to 28 bytes each in preparation
store<u32>(dest, c32);
store<u32>(dest + n - 4, c32);
if (n <= 8)
return;
if (n <= 8) return;
store<u32>(dest + 4, c32);
store<u32>(dest + 8, c32);
store<u32>(dest + n - 12, c32);
store<u32>(dest + n - 8, c32);
if (n <= 24)
return;
if (n <= 24) return;
store<u32>(dest + 12, c32);
store<u32>(dest + 16, c32);
store<u32>(dest + 20, c32);
@ -259,8 +250,7 @@ export function set_memory(dest: usize, c: u8, n: usize): void {
export function compare_memory(vl: usize, vr: usize, n: usize): i32 {
// based on musl's implementation of memcmp
// provided because there's no proposed alternative
if (vl == vr)
return 0;
if (vl == vr) return 0;
while (n && load<u8>(vl) == load<u8>(vr)) {
n--;
vl++;

View File

@ -21,9 +21,11 @@ export class Set<T> {
has(value: T): bool {
assert(this != null);
for (var index: usize = 0, limit: usize = this.__size; index < limit; ++index)
if (load<T>(this.__memory + index * sizeof<T>()) == value)
for (var index: usize = 0, limit: usize = this.__size; index < limit; ++index) {
if (load<T>(this.__memory + index * sizeof<T>()) == value) {
return true;
}
}
return false;
}
@ -48,13 +50,19 @@ export class Set<T> {
delete(value: T): bool {
assert(this != null);
for (var index: usize = 0, limit: usize = this.__size; index < limit; ++index)
for (var index: usize = 0, limit: usize = this.__size; index < limit; ++index) {
if (load<T>(this.__memory + index * sizeof<T>()) == value) {
if (index + 1 < this.__size)
move_memory(this.__memory + index * sizeof<T>(), this.__memory + (index + 1) * sizeof<T>(), this.__size - index - 1);
if (index + 1 < this.__size) {
move_memory(
this.__memory + index * sizeof<T>(),
this.__memory + (index + 1) * sizeof<T>(),
this.__size - index - 1
);
}
--this.__size;
return true;
}
}
return false;
}

View File

@ -19,8 +19,9 @@ export class String {
charAt(pos: i32): String {
assert(this != null);
if (<u32>pos >= this.length)
if (<u32>pos >= this.length) {
return EMPTY;
}
var out = allocate(1);
store<u16>(
@ -36,10 +37,9 @@ export class String {
charCodeAt(pos: i32): i32 {
assert(this != null);
if (<u32>pos >= this.length)
if (<u32>pos >= this.length) {
return -1; // (NaN)
}
return load<u16>(
changetype<usize>(this) + (<usize>pos << 1),
HEAD
@ -48,43 +48,45 @@ export class String {
codePointAt(pos: i32): i32 {
assert(this != null);
if (<u32>pos >= this.length)
if (<u32>pos >= this.length) {
return -1; // (undefined)
}
var first = <i32>load<u16>(
changetype<usize>(this) + (<usize>pos << 1),
HEAD
);
if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length)
if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length) {
return first;
}
var second = <i32>load<u16>(
changetype<usize>(this) + ((<usize>pos + 1) << 1),
HEAD
);
if (second < 0xDC00 || second > 0xDFFF)
if (second < 0xDC00 || second > 0xDFFF) {
return first;
}
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
}
@operator("+")
private static __concat(left: String, right: String): String {
if (left == null)
if (left == null) {
left = changetype<String>("null");
}
return left.concat(right);
}
concat(other: String): String {
assert(this != null);
if (other == null)
if (other == null) {
other = changetype<String>("null");
}
var thisLen: isize = this.length;
var otherLen: isize = other.length;
var outLen: usize = thisLen + otherLen;
if (outLen == 0)
if (outLen == 0) {
return EMPTY;
}
var out = allocate(outLen);
move_memory(
changetype<usize>(out) + HEAD,
@ -101,16 +103,15 @@ export class String {
endsWith(searchString: String, endPosition: i32 = 0x7fffffff): bool {
assert(this != null);
if (searchString == null)
if (searchString == null) {
return false;
}
var end: isize = <isize>min(max(endPosition, 0), this.length);
var searchLength: isize = searchString.length;
var start: isize = end - searchLength;
if (start < 0)
if (start < 0) {
return false;
}
return !compare_memory(
changetype<usize>(this) + HEAD + (start << 1),
changetype<usize>(searchString) + HEAD,
@ -120,15 +121,15 @@ export class String {
@operator("==")
private static __eq(left: String, right: String): bool {
if (left == null)
if (left == null) {
return right == null;
else if (right == null)
} else if (right == null) {
return false;
}
var leftLength = left.length;
if (leftLength != right.length)
if (leftLength != right.length) {
return false;
}
return !compare_memory(
changetype<usize>(left) + HEAD,
changetype<usize>(right) + HEAD,
@ -142,39 +143,39 @@ export class String {
indexOf(searchString: String, position: i32 = 0): i32 {
assert(this != null);
if (searchString == null)
if (searchString == null) {
searchString = changetype<String>("null");
}
var pos: isize = position;
var len: isize = this.length;
var start: isize = min<isize>(max<isize>(pos, 0), len);
var searchLen: isize = <isize>searchString.length;
// TODO: two-way, multiple char codes
for (var k: usize = start; <isize>k + searchLen <= len; ++k)
for (var k: usize = start; <isize>k + searchLen <= len; ++k) {
if (!compare_memory(
changetype<usize>(this) + HEAD + (k << 1),
changetype<usize>(searchString) + HEAD,
searchLen << 1)
)
) {
return <i32>k;
}
}
return -1;
}
startsWith(searchString: String, position: i32 = 0): bool {
assert(this != null);
if (searchString == null)
if (searchString == null) {
searchString = changetype<String>("null");
}
var pos: isize = position;
var len: isize = this.length;
var start: isize = min<isize>(max<isize>(position, 0), len);
var start: isize = min<isize>(max<isize>(pos, 0), len);
var searchLength: isize = <isize>searchString.length;
if (searchLength + start > len)
if (searchLength + start > len) {
return false;
}
return !compare_memory(
changetype<usize>(this) + HEAD + (start << 1),
changetype<usize>(searchString) + HEAD,
@ -184,17 +185,16 @@ export class String {
substr(start: i32, length: i32 = i32.MAX_VALUE): String {
assert(this != null);
var intStart: isize = start;
var end: isize = length;
var size: isize = this.length;
if (intStart < 0)
if (intStart < 0) {
intStart = max<isize>(size + intStart, 0);
}
var resultLength: isize = min<isize>(max<isize>(end, 0), size - intStart);
if (resultLength <= 0)
if (resultLength <= 0) {
return EMPTY;
}
var out = allocate(resultLength);
move_memory(
changetype<usize>(out) + HEAD,
@ -206,19 +206,18 @@ export class String {
substring(start: i32, end: i32 = i32.MAX_VALUE): String {
assert(this != null);
var len = this.length;
var finalStart = min<i32>(max<i32>(start, 0), len);
var finalEnd = min<i32>(max<i32>(end, 0), len);
var from = min<i32>(finalStart, finalEnd);
var to = max<i32>(finalStart, finalEnd);
len = to - from;
if (!len)
if (!len) {
return EMPTY;
if (!from && to == this.length)
}
if (!from && to == this.length) {
return this;
}
var out = allocate(len);
move_memory(
changetype<usize>(out) + HEAD,
@ -230,21 +229,30 @@ export class String {
trim(): String {
assert(this != null);
var length: usize = this.length;
while (length && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (length << 1), HEAD)))
while (
length &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (length << 1), HEAD)
)
) {
--length;
}
var start: usize = 0;
while (start < length && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (start << 1), HEAD)))
while (
start < length &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (start << 1), HEAD)
)
) {
++start, --length;
if (!length)
}
if (!length) {
return EMPTY;
if (!start && length == this.length)
}
if (!start && length == this.length) {
return this;
}
var out = allocate(length);
move_memory(
changetype<usize>(out) + HEAD,
@ -256,19 +264,23 @@ export class String {
trimLeft(): String {
assert(this != null);
var start: isize = 0;
var len: isize = this.length;
while (start < len && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (start << 1), HEAD)))
while (
start < len &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (start << 1), HEAD)
)
) {
++start;
if (!start)
}
if (!start) {
return this;
}
var outLen = len - start;
if (!outLen)
if (!outLen) {
return EMPTY;
}
var out = allocate(outLen);
move_memory(
changetype<usize>(out) + HEAD,
@ -280,17 +292,21 @@ export class String {
trimRight(): String {
assert(this != null);
var len: isize = this.length;
while (len > 0 && isWhiteSpaceOrLineTerminator(load<u16>(changetype<usize>(this) + (len << 1), HEAD)))
while (
len > 0 &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + (len << 1), HEAD)
)
) {
--len;
if (len <= 0)
}
if (len <= 0) {
return EMPTY;
if (<i32>len == this.length)
}
if (<i32>len == this.length) {
return this;
}
var out = allocate(len);
move_memory(
changetype<usize>(out) + HEAD,
@ -303,19 +319,16 @@ export class String {
function isWhiteSpaceOrLineTerminator(c: u16): bool {
switch (c) {
case 10: // <LF>
case 13: // <CR>
case 8232: // <LS>
case 8233: // <PS>
case 9: // <TAB>
case 11: // <VT>
case 12: // <FF>
case 32: // <SP>
case 160: // <NBSP>
case 65279: // <ZWNBSP>
return true;
default:
return false;
@ -364,25 +377,29 @@ export function parseI64(str: String, radix: i32 = 0): i64 {
function parse<T>(str: String, radix: i32 = 0): T {
var len: i32 = str.length;
if (!len)
if (!len) {
return <T>NaN;
}
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEAD);
// determine sign
var sign: T;
if (code == CharCode.MINUS) {
if (!--len)
if (!--len) {
return <T>NaN;
}
code = <i32>load<u16>(ptr += 2, HEAD);
sign = -1;
} else if (code == CharCode.PLUS) {
if (!--len)
if (!--len) {
return <T>NaN;
}
code = <i32>load<u16>(ptr += 2, HEAD);
sign = 1;
} else
} else {
sign = 1;
}
// determine radix
if (!radix) {
@ -411,23 +428,26 @@ function parse<T>(str: String, radix: i32 = 0): T {
radix = 10;
}
} else radix = 10;
} else if (radix < 2 || radix > 36)
} else if (radix < 2 || radix > 36) {
return <T>NaN;
}
// calculate value
var num: T = 0;
while (len--) {
code = <i32>load<u16>(ptr, HEAD);
if (code >= CharCode._0 && code <= CharCode._9)
if (code >= CharCode._0 && code <= CharCode._9) {
code -= CharCode._0;
else if (code >= CharCode.A && code <= CharCode.Z)
} else if (code >= CharCode.A && code <= CharCode.Z) {
code -= CharCode.A - 10;
else if (code >= CharCode.a && code <= CharCode.z)
} else if (code >= CharCode.a && code <= CharCode.z) {
code -= CharCode.a - 10;
else
} else {
break;
if (code >= radix)
}
if (code >= radix) {
break;
}
num = (num * radix) + code;
ptr += 2;
}
@ -436,25 +456,29 @@ function parse<T>(str: String, radix: i32 = 0): T {
export function parseFloat(str: String): f64 {
var len: i32 = str.length;
if (!len)
if (!len) {
return NaN;
}
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEAD);
// determine sign
var sign: f64;
if (code == CharCode.MINUS) {
if (!--len)
if (!--len) {
return NaN;
}
code = <i32>load<u16>(ptr += 2, HEAD);
sign = -1;
} else if (code == CharCode.PLUS) {
if (!--len)
if (!--len) {
return NaN;
}
code = <i32>load<u16>(ptr += 2, HEAD);
sign = 1;
} else
} else {
sign = 1;
}
// calculate value
var num: f64 = 0;
@ -465,11 +489,13 @@ export function parseFloat(str: String): f64 {
var fac: f64 = 0.1; // precision :(
while (len--) {
code = <i32>load<u16>(ptr, HEAD);
if (code == CharCode.E || code == CharCode.e)
if (code == CharCode.E || code == CharCode.e) {
assert(false); // TODO
}
code -= CharCode._0;
if (<u32>code > 9)
if (<u32>code > 9) {
break;
}
num += <f64>code * fac;
fac *= 0.1;
ptr += 2;
@ -477,8 +503,9 @@ export function parseFloat(str: String): f64 {
break;
}
code -= CharCode._0;
if (<u32>code >= 10)
if (<u32>code >= 10) {
break;
}
num = (num * 10) + code;
ptr += 2;
}

View File

@ -144,6 +144,7 @@ globalScope["parseI32"] = function parseI32(str, radix) {
String["fromCharCodes"] = function fromCharCodes(arr) {
return String.fromCharCode.apply(String, arr);
};
String["fromCodePoints"] = function fromCodePoints(arr) {
return String.fromCodePoint.apply(String, arr);
};

View File

@ -15,6 +15,6 @@
"files": [
"./portable.d.ts",
"./portable.js",
"./portable/heap.js"
"./portable/memory.js"
]
}

View File

@ -3,8 +3,7 @@ var globalScope = typeof window !== "undefined" && window || typeof global !== "
var HEAP = new Uint8Array(0);
var HEAP_OFFSET = 0;
globalScope["allocate_memory"] =
function allocate_memory(size) {
globalScope["allocate_memory"] = function allocate_memory(size) {
if (!(size >>>= 0))
return 0;
if (HEAP_OFFSET + size > HEAP.length) {
@ -18,25 +17,21 @@ function allocate_memory(size) {
return ptr;
};
globalScope["free_memory"] =
function free_memory(ptr) {
globalScope["free_memory"] = function free_memory(ptr) {
// TODO
};
globalScope["move_memory"] =
function move_memory(dest, src, n) {
globalScope["move_memory"] = function move_memory(dest, src, n) {
HEAP.copyWithin(dest, src, src + n);
};
globalScope["store"] =
function store(ptr, val, off) {
globalScope["store"] = function store(ptr, val, off) {
if (typeof off === "number")
ptr += off;
HEAP[ptr] = val;
};
globalScope["load"] =
function load(ptr) {
globalScope["load"] = function load(ptr) {
if (typeof off === "number")
ptr += off;
return HEAP[ptr];

View File

@ -1,9 +0,0 @@
{
"extends": "../portable.json",
"compilerOptions": {
"diagnostics": true
},
"include": [
"./**/*.ts"
]
}