some cleanup

This commit is contained in:
dcode
2019-04-01 22:23:11 +02:00
parent 7b99e44343
commit 1ada854830
74 changed files with 6072 additions and 3909 deletions

View File

@ -1,7 +1,8 @@
/// <reference path="./collector/index.d.ts" />
import { ALLOCATE, REGISTER, HEADER, HEADER_SIZE, MAKEARRAY, ArrayBufferView } from "./runtime";
import { MAX_SIZE_32 } from "./util/allocator";
import { runtime, HEADER, HEADER_SIZE, classId } from "./runtime";
import { ArrayBufferView } from "./arraybuffer";
import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string";
import { E_INVALIDLENGTH } from "./util/error";
@ -15,15 +16,15 @@ import { E_INVALIDLENGTH } from "./util/error";
// TODO Add and handle second argument
static fromCharCode(code: i32): String {
var out = ALLOCATE(2);
var out = runtime.allocate(2);
store<u16>(out, <u16>code);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
static fromCodePoint(code: i32): String {
assert(<u32>code <= 0x10FFFF);
var sur = code > 0xFFFF;
var out = ALLOCATE((i32(sur) + 1) << 1);
var out = runtime.allocate((i32(sur) + 1) << 1);
if (!sur) {
store<u16>(out, <u16>code);
} else {
@ -32,15 +33,15 @@ import { E_INVALIDLENGTH } from "./util/error";
let lo: u32 = (code & 0x3FF) + 0xDC00;
store<u32>(out, (hi << 16) | lo);
}
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
@operator("[]") charAt(pos: i32): String {
assert(this !== null);
if (<u32>pos >= <u32>this.length) return changetype<String>("");
var out = ALLOCATE(2);
var out = runtime.allocate(2);
store<u16>(out, load<u16>(changetype<usize>(this) + (<usize>pos << 1)));
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
charCodeAt(pos: i32): i32 {
@ -57,8 +58,8 @@ import { E_INVALIDLENGTH } from "./util/error";
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
}
@operator("+") private static __concat(left: string, right: string): string {
return select<string>(left, "null", left !== null).concat(right);
@operator("+") private static __concat(left: String, right: String): String {
return select<String>(left, changetype<String>("null"), left !== null).concat(right);
}
concat(other: String): String {
@ -67,10 +68,10 @@ import { E_INVALIDLENGTH } from "./util/error";
var otherSize: isize = other.length << 1;
var outSize: usize = thisSize + otherSize;
if (outSize == 0) return changetype<String>("");
var out = ALLOCATE(outSize);
var out = runtime.allocate(outSize);
memory.copy(out, changetype<usize>(this), thisSize);
memory.copy(out + thisSize, changetype<usize>(other), otherSize);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
endsWith(searchString: String, endPosition: i32 = String.MAX_LENGTH): bool {
@ -180,9 +181,9 @@ import { E_INVALIDLENGTH } from "./util/error";
if (intStart < 0) intStart = max(size + intStart, 0);
var resultLength = min(max(end, 0), size - intStart);
if (resultLength <= 0) return changetype<String>("");
var out = ALLOCATE(resultLength << 1);
var out = runtime.allocate(resultLength << 1);
memory.copy(out, changetype<usize>(this) + intStart, resultLength);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
substring(start: i32, end: i32 = i32.MAX_VALUE): String {
@ -195,9 +196,9 @@ import { E_INVALIDLENGTH } from "./util/error";
len = toPos - fromPos;
if (!len) return changetype<String>("");
if (!fromPos && toPos == this.length << 1) return this;
var out = ALLOCATE(len);
var out = runtime.allocate(len);
memory.copy(out, changetype<usize>(this) + fromPos, len);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
trim(): String {
@ -223,9 +224,9 @@ import { E_INVALIDLENGTH } from "./util/error";
}
if (!size) return changetype<String>("");
if (!start && size == length << 1) return this;
var out = ALLOCATE(size);
var out = runtime.allocate(size);
memory.copy(out, changetype<usize>(this) + offset, size);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
@inline
@ -253,9 +254,9 @@ import { E_INVALIDLENGTH } from "./util/error";
if (!offset) return this;
size -= offset;
if (!size) return changetype<String>("");
var out = ALLOCATE(size);
var out = runtime.allocate(size);
memory.copy(out, changetype<usize>(this) + offset, size);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
trimEnd(): String {
@ -272,9 +273,9 @@ import { E_INVALIDLENGTH } from "./util/error";
}
if (!size) return changetype<String>("");
if (size == originalSize) return this;
var out = ALLOCATE(size);
var out = runtime.allocate(size);
memory.copy(out, changetype<usize>(this), size);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
padStart(targetLength: i32, padString: string = " "): String {
@ -284,7 +285,7 @@ import { E_INVALIDLENGTH } from "./util/error";
var padSize = <usize>padString.length << 1;
if (targetSize < thisSize || !padSize) return this;
var prependSize = targetSize - thisSize;
var out = ALLOCATE(targetSize);
var out = runtime.allocate(targetSize);
if (prependSize > padSize) {
let repeatCount = (prependSize - 2) / padSize;
let restBase = repeatCount * padSize;
@ -295,7 +296,7 @@ import { E_INVALIDLENGTH } from "./util/error";
memory.copy(out, changetype<usize>(padString), prependSize);
}
memory.copy(out + prependSize, changetype<usize>(this), thisSize);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
padEnd(targetLength: i32, padString: string = " "): String {
@ -305,7 +306,7 @@ import { E_INVALIDLENGTH } from "./util/error";
var padSize = <usize>padString.length << 1;
if (targetSize < thisSize || !padSize) return this;
var appendSize = targetSize - thisSize;
var out = ALLOCATE(targetSize);
var out = runtime.allocate(targetSize);
memory.copy(out, changetype<usize>(this), thisSize);
if (appendSize > padSize) {
let repeatCount = (appendSize - 2) / padSize;
@ -316,7 +317,7 @@ import { E_INVALIDLENGTH } from "./util/error";
} else {
memory.copy(out + thisSize, changetype<usize>(padString), appendSize);
}
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
repeat(count: i32 = 0): String {
@ -330,9 +331,9 @@ import { E_INVALIDLENGTH } from "./util/error";
if (count == 0 || !length) return changetype<String>("");
if (count == 1) return this;
var out = ALLOCATE((length * count) << 1);
var out = runtime.allocate((length * count) << 1);
memory.repeat(out, changetype<usize>(this), <usize>length << 1, count);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
slice(beginIndex: i32, endIndex: i32 = i32.MAX_VALUE): String {
@ -341,48 +342,48 @@ import { E_INVALIDLENGTH } from "./util/error";
var end = endIndex < 0 ? max(endIndex + len, 0) : min(endIndex, len);
len = end - begin;
if (len <= 0) return changetype<String>("");
var out = ALLOCATE(len << 1);
var out = runtime.allocate(len << 1);
memory.copy(out, changetype<usize>(this) + (<usize>begin << 1), <usize>len << 1);
return REGISTER<String>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
split(separator: String | null = null, limit: i32 = i32.MAX_VALUE): String[] {
assert(this !== null);
if (!limit) return MAKEARRAY<String>(0);
if (!limit) return changetype<String[]>(runtime.makeArray(0, classId<String[]>(), alignof<String>()));
if (separator === null) return <String[]>[this];
var length: isize = this.length;
var sepLen: isize = separator.length;
if (limit < 0) limit = i32.MAX_VALUE;
if (!sepLen) {
if (!length) return MAKEARRAY<String>(0);
if (!length) return changetype<String[]>(runtime.makeArray(0, classId<String>(), alignof<String>()));
// split by chars
length = min<isize>(length, <isize>limit);
let result = MAKEARRAY<String>(length);
let result = changetype<String[]>(runtime.makeArray(length, classId<String[]>(), alignof<String>()));
let resultStart = changetype<ArrayBufferView>(result).dataStart;
for (let i: isize = 0; i < length; ++i) {
let charStr = REGISTER<String>(ALLOCATE(2));
store<u16>(changetype<usize>(charStr), load<u16>(changetype<usize>(this) + (<usize>i << 1)));
store<String>(resultStart + (<usize>i << alignof<usize>()), charStr); // result[i] = charStr
let charStr = runtime.allocate(2);
store<u16>(charStr, load<u16>(changetype<usize>(this) + (<usize>i << 1)));
store<usize>(resultStart + (<usize>i << alignof<usize>()), charStr); // result[i] = charStr
runtime.register(charStr, classId<String>());
if (isManaged<String>()) {
if (isDefined(__ref_link)) __ref_link(changetype<usize>(charStr), changetype<usize>(result));
else if (isDefined(__ref_retain)) __ref_retain(changetype<usize>(charStr));
else assert(false);
if (isDefined(__ref_retain)) __ref_retain(changetype<usize>(charStr));
}
}
return result;
} else if (!length) {
let result = MAKEARRAY<String>(1);
let result = changetype<String[]>(runtime.makeArray(1, classId<String[]>(), alignof<String>()));
store<string>(changetype<ArrayBufferView>(result).dataStart, ""); // no need to register/link
return result;
}
var result = MAKEARRAY<String>(0);
var result = changetype<String[]>(runtime.makeArray(0, classId<String[]>(), alignof<String>()));
var end = 0, start = 0, i = 0;
while ((end = this.indexOf(separator!, start)) != -1) {
let len = end - start;
if (len > 0) {
let out = ALLOCATE(<usize>len << 1);
let out = runtime.allocate(<usize>len << 1);
memory.copy(out, changetype<usize>(this) + (<usize>start << 1), <usize>len << 1);
result.push(REGISTER<String>(out));
result.push(changetype<String>(runtime.register(out, classId<String>())));
} else {
result.push(changetype<String>(""));
}
@ -390,15 +391,15 @@ import { E_INVALIDLENGTH } from "./util/error";
start = end + sepLen;
}
if (!start) {
let result = MAKEARRAY<String>(1);
let result = changetype<String[]>(runtime.makeArray(1, classId<String[]>(), alignof<String>()));
unchecked(result[0] = this);
return result;
}
var len = length - start;
if (len > 0) {
let out = ALLOCATE(<usize>len << 1);
let out = runtime.allocate(<usize>len << 1);
memory.copy(out, changetype<usize>(this) + (<usize>start << 1), <usize>len << 1);
result.push(REGISTER<String>(out));
result.push(changetype<String>(runtime.register(out, classId<String>())));
} else {
result.push(changetype<String>(""));
}
@ -433,8 +434,8 @@ import { E_INVALIDLENGTH } from "./util/error";
return len;
}
static fromUTF8(ptr: usize, len: usize): string {
if (len < 1) return changetype<string>("");
static fromUTF8(ptr: usize, len: usize): String {
if (len < 1) return changetype<String>("");
var ptrPos = <usize>0;
var buf = memory.allocate(<usize>len << 1);
var bufPos = <usize>0;
@ -470,10 +471,10 @@ import { E_INVALIDLENGTH } from "./util/error";
}
}
assert(ptrPos == len);
var out = ALLOCATE(bufPos);
var out = runtime.allocate(bufPos);
memory.copy(changetype<usize>(out), buf, bufPos);
memory.free(buf);
return REGISTER<string>(out);
return changetype<String>(runtime.register(out, classId<String>()));
}
toUTF8(): usize {