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

View File

@ -5,22 +5,18 @@ jobs:
- stage: lint
node_js: node
script: npm run lint
env: DESC="Checks the sources with TSLint"
env: Checks the sources with TSLint
- stage: test
node_js: lts/*
script: npm run clean && node bin/asc -v && npm test
env: DESC="Tests the sources on latest node.js LTS"
- stage: test
node_js: node
script: npm run clean && node bin/asc -v && npm test
env: DESC="Tests the sources on latest stable node.js"
env: Tests the sources on latest node.js LTS
- node_js: node
env: Tests the sources on latest stable node.js
- stage: build
node_js: lts/*
script: npm run build && node bin/asc -v && npm test
env: DESC="Builds and tests the bundle on latest node.js LTS"
- stage: build
node_js: node
script: npm run build && node bin/asc -v && npm test
env: DESC="Builds and tests the bundle on latest stable node.js"
env: Builds and tests the bundle on latest node.js LTS
- node_js: node
env: Builds and tests the bundle on latest stable node.js

2
dist/asc.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -40,13 +40,14 @@
"scripts": {
"build": "webpack",
"clean": "node scripts/clean",
"lint": "tslint -c tslint.json --project src --formatters-dir lib/tslint --format as",
"test:config": "npm run test:config:portable --scripts-prepend-node-path && npm run test:config:src --scripts-prepend-node-path",
"test:config:portable": "tsc --noEmit -p std/portable --diagnostics --listFiles",
"test:config:src": "tsc --noEmit -p src --diagnostics --listFiles",
"lint": "npm run lint:compiler && npm run lint:library",
"lint:compiler": "tslint -c tslint.json --project src --formatters-dir lib/tslint --format as",
"lint:library": "tslint -c tslint.json --project std/assembly --formatters-dir lib/tslint --format as",
"test:config": "tsc --noEmit -p src --diagnostics --listFiles",
"test:parser": "node tests/parser",
"test:compiler": "node tests/compiler",
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path"
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path",
"all": "npm run lint && npm run clean && npm test && npm run build && npm test"
},
"files": [
"bin/",

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,9 +316,9 @@ 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;
}
@ -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,9 +441,10 @@ 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"
]
}

View File

@ -69,12 +69,6 @@
(i32.const 1)
)
)
(set_global $binary/i
(i32.mul
(get_global $binary/i)
(i32.const 1)
)
)
(set_global $binary/i
(i32.div_s
(get_global $binary/i)
@ -135,12 +129,6 @@
(i32.const 1)
)
)
(set_global $binary/i
(i32.mul
(get_global $binary/i)
(i32.const 1)
)
)
(set_global $binary/i
(i32.rem_s
(get_global $binary/i)

View File

@ -18,13 +18,13 @@
)
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(i32.rem_u
(select
(i32.and
(get_local $1)
(i32.const 4)
(i32.const 3)
)
(get_local $2)
(get_local $2)
)
(block
(set_local $0
@ -63,9 +63,9 @@
)
(if
(i32.eqz
(i32.rem_u
(i32.and
(get_local $0)
(i32.const 4)
(i32.const 3)
)
)
(block
@ -269,9 +269,9 @@
(block $tablify|0
(br_table $case0|2 $case1|2 $case2|2 $tablify|0
(i32.sub
(i32.rem_u
(i32.and
(get_local $0)
(i32.const 4)
(i32.const 3)
)
(i32.const 1)
)

View File

@ -31,21 +31,21 @@
(block
(if
(i32.eq
(i32.rem_u
(i32.and
(get_local $1)
(i32.const 8)
(i32.const 7)
)
(i32.rem_u
(i32.and
(get_local $0)
(i32.const 8)
(i32.const 7)
)
)
(block
(loop $continue|0
(if
(i32.rem_u
(i32.and
(get_local $0)
(i32.const 8)
(i32.const 7)
)
(block
(if
@ -169,24 +169,24 @@
(block
(if
(i32.eq
(i32.rem_u
(i32.and
(get_local $1)
(i32.const 8)
(i32.const 7)
)
(i32.rem_u
(i32.and
(get_local $0)
(i32.const 8)
(i32.const 7)
)
)
(block
(loop $continue|3
(if
(i32.rem_u
(i32.and
(i32.add
(get_local $0)
(get_local $2)
)
(i32.const 8)
(i32.const 7)
)
(block
(if

View File

@ -92,13 +92,13 @@
)
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(i32.rem_u
(select
(i32.and
(get_local $1)
(i32.const 4)
(i32.const 3)
)
(get_local $2)
(get_local $2)
)
(block
(set_local $0
@ -137,9 +137,9 @@
)
(if
(i32.eqz
(i32.rem_u
(i32.and
(get_local $0)
(i32.const 4)
(i32.const 3)
)
)
(block
@ -343,9 +343,9 @@
(block $tablify|0
(br_table $case0|2 $case1|2 $case2|2 $tablify|0
(i32.sub
(i32.rem_u
(i32.and
(get_local $0)
(i32.const 4)
(i32.const 3)
)
(i32.const 1)
)
@ -2858,12 +2858,6 @@
(i32.const 1)
)
)
(set_global $binary/i
(i32.mul
(get_global $binary/i)
(i32.const 1)
)
)
(set_global $binary/i
(i32.div_s
(get_global $binary/i)
@ -2924,12 +2918,6 @@
(i32.const 1)
)
)
(set_global $binary/i
(i32.mul
(get_global $binary/i)
(i32.const 1)
)
)
(set_global $binary/i
(i32.rem_s
(get_global $binary/i)

View File

@ -1994,9 +1994,9 @@
)
(set_local $2
(call "$(lib)/allocator/arena/allocate_memory"
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2010,11 +2010,11 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(i32.load offset=4
(get_local $0)
)
(i32.const 4)
(i32.const 2)
)
)
(call "$(lib)/allocator/arena/free_memory"
@ -2065,11 +2065,11 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(i32.load offset=8
(get_local $0)
)
(i32.const 4)
(i32.const 2)
)
)
(get_local $1)
@ -2102,9 +2102,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2137,9 +2137,9 @@
)
)
)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2181,7 +2181,7 @@
(call $abort
(i32.const 0)
(i32.const 32)
(i32.const 105)
(i32.const 128)
(i32.const 6)
)
(unreachable)
@ -2189,9 +2189,9 @@
)
(set_local $4
(call "$(lib)/allocator/arena/allocate_memory"
(i32.mul
(i32.shl
(get_local $3)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2208,9 +2208,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
(call "$(lib)/allocator/arena/free_memory"
@ -2239,9 +2239,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2625,14 +2625,14 @@
)
(i32.const 4)
)
(i32.mul
(i32.shl
(i32.sub
(i32.load offset=4
(get_local $0)
)
(i32.const 1)
)
(i32.const 4)
(i32.const 2)
)
)
(call "$(lib)/memory/set_memory"
@ -2640,14 +2640,14 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(i32.sub
(i32.load offset=4
(get_local $0)
)
(i32.const 1)
)
(i32.const 4)
(i32.const 2)
)
)
(i32.const 0)
@ -2689,9 +2689,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2701,9 +2701,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
(i32.load
@ -2711,9 +2711,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2723,9 +2723,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
(get_local $3)
@ -2779,9 +2779,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2856,16 +2856,16 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
(i32.add
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(i32.add
(get_local $1)
(tee_local $2
@ -2886,12 +2886,12 @@
)
)
)
(i32.const 4)
(i32.const 2)
)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
(i32.store offset=8

View File

@ -2479,7 +2479,7 @@
(call $abort
(i32.const 0)
(i32.const 32)
(i32.const 105)
(i32.const 128)
(i32.const 6)
)
(unreachable)

View File

@ -21,9 +21,9 @@
(i32.load
(i32.add
(get_local $0)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
)
@ -39,9 +39,9 @@
(i32.store
(i32.add
(get_local $0)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
(get_local $2)

View File

@ -49,7 +49,6 @@
(i32.const 1)
)
(return
(i32.or
(i32.or
(i32.and
(i32.shl
@ -66,11 +65,6 @@
(i32.const 255)
)
)
(i32.and
(get_local $0)
(i32.const 0)
)
)
)
)
(get_local $0)
@ -158,7 +152,6 @@
(return
(i32.shr_s
(i32.shl
(i32.or
(i32.or
(i32.shr_s
(i32.shl
@ -184,11 +177,6 @@
(i32.const 255)
)
)
(i32.and
(get_local $0)
(i32.const 0)
)
)
(i32.const 16)
)
(i32.const 16)

View File

@ -1981,7 +1981,7 @@
(call $abort
(i32.const 0)
(i32.const 28)
(i32.const 31)
(i32.const 33)
(i32.const 4)
)
(unreachable)
@ -1999,7 +1999,7 @@
(block
(set_local $2
(call "$(lib)/allocator/arena/allocate_memory"
(i32.mul
(i32.shl
(tee_local $3
(select
(tee_local $2
@ -2019,7 +2019,7 @@
)
)
)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2033,11 +2033,11 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(i32.load offset=4
(get_local $0)
)
(i32.const 4)
(i32.const 2)
)
)
(call "$(lib)/allocator/arena/free_memory"
@ -2062,11 +2062,11 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(i32.load offset=8
(get_local $0)
)
(i32.const 4)
(i32.const 2)
)
)
(get_local $1)
@ -2118,9 +2118,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2153,7 +2153,7 @@
(call $abort
(i32.const 0)
(i32.const 28)
(i32.const 49)
(i32.const 51)
(i32.const 4)
)
(unreachable)
@ -2178,9 +2178,9 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
)
@ -2202,21 +2202,21 @@
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(get_local $2)
(i32.const 4)
(i32.const 2)
)
)
(i32.add
(i32.load
(get_local $0)
)
(i32.mul
(i32.shl
(i32.add
(get_local $2)
(i32.const 1)
)
(i32.const 4)
(i32.const 2)
)
)
(i32.sub
@ -2265,7 +2265,7 @@
(call $abort
(i32.const 0)
(i32.const 28)
(i32.const 62)
(i32.const 70)
(i32.const 4)
)
(unreachable)

View File

@ -2267,7 +2267,7 @@
(call $abort
(i32.const 0)
(i32.const 28)
(i32.const 31)
(i32.const 33)
(i32.const 4)
)
(unreachable)
@ -2408,6 +2408,7 @@
(get_local $2)
(get_local $3)
)
(block
(block
(if
(i32.eq
@ -2428,6 +2429,7 @@
(i32.const 1)
)
)
)
(set_local $2
(i32.add
(get_local $2)
@ -2457,7 +2459,7 @@
(call $abort
(i32.const 0)
(i32.const 28)
(i32.const 49)
(i32.const 51)
(i32.const 4)
)
(unreachable)
@ -2480,6 +2482,7 @@
(get_local $2)
(get_local $3)
)
(block
(block
(if
(i32.eq
@ -2554,6 +2557,7 @@
)
)
)
)
(set_local $2
(i32.add
(get_local $2)
@ -2581,7 +2585,7 @@
(call $abort
(i32.const 0)
(i32.const 28)
(i32.const 62)
(i32.const 70)
(i32.const 4)
)
(unreachable)

View File

@ -41,7 +41,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 38)
(i32.const 39)
(i32.const 4)
)
(unreachable)
@ -140,7 +140,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 166)
(i32.const 168)
(i32.const 4)
)
(unreachable)
@ -227,7 +227,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 103)
(i32.const 105)
(i32.const 4)
)
(unreachable)
@ -314,7 +314,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 144)
(i32.const 145)
(i32.const 4)
)
(unreachable)
@ -957,7 +957,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 469)
(i32.const 493)
(i32.const 10)
)
(unreachable)

View File

@ -71,7 +71,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 38)
(i32.const 39)
(i32.const 4)
)
(unreachable)
@ -187,7 +187,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 166)
(i32.const 168)
(i32.const 4)
)
(unreachable)
@ -215,7 +215,7 @@
(tee_local $5
(select
(tee_local $5
(get_local $2)
(get_local $3)
)
(tee_local $6
(i32.const 0)
@ -294,7 +294,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 103)
(i32.const 105)
(i32.const 4)
)
(unreachable)
@ -400,7 +400,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 144)
(i32.const 145)
(i32.const 4)
)
(unreachable)
@ -466,6 +466,7 @@
)
(get_local $4)
)
(block
(block
(if
(i32.eqz
@ -494,6 +495,7 @@
(get_local $9)
)
)
)
(set_local $9
(i32.add
(get_local $9)
@ -1142,7 +1144,7 @@
(call $abort
(i32.const 0)
(i32.const 72)
(i32.const 469)
(i32.const 493)
(i32.const 10)
)
(unreachable)

View File

@ -81,9 +81,9 @@
(get_local $0)
(i32.const 20)
)
(i32.mul
(i32.shl
(get_local $1)
(i32.const 4)
(i32.const 2)
)
)
(get_local $2)
@ -126,15 +126,15 @@
(get_local $0)
(i32.const 112)
)
(i32.mul
(i32.shl
(i32.add
(i32.mul
(i32.shl
(get_local $1)
(i32.const 32)
(i32.const 5)
)
(get_local $2)
)
(i32.const 4)
(i32.const 2)
)
)
(get_local $3)

View File

@ -45,7 +45,7 @@
"no-trailing-whitespace": true,
"no-unbound-method": true,
"no-unsafe-any": true,
"no-unused-variable": true,
"no-unused-variable": [true, {"ignore-pattern": "^_"}],
"no-void-expression": true,
"object-literal-shorthand": [ true, "never" ],
"prefer-method-signature": true,