This commit is contained in:
dcode
2019-03-14 12:46:36 +01:00
parent cdf3e2cf12
commit 3a60638f72
12 changed files with 627 additions and 662 deletions

View File

@ -1,4 +1,5 @@
import { runtime } from "./runtime";
import { gc } from "./gc";
import { MAX_SIZE_32 } from "./util/allocator";
import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string";
@ -14,15 +15,15 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
// TODO Add and handle second argument
static fromCharCode(code: i32): String {
var out = runtime.allocRaw(2);
var out = runtime.alloc(2);
store<u16>(out, <u16>code);
return runtime.register<String>(out);
return gc.register<String>(out);
}
static fromCodePoint(code: i32): String {
assert(<u32>code <= 0x10FFFF);
var sur = code > 0xFFFF;
var out = runtime.allocRaw((i32(sur) + 1) << 1);
var out = runtime.alloc((i32(sur) + 1) << 1);
if (!sur) {
store<u16>(out, <u16>code);
} else {
@ -31,15 +32,15 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
let lo: u32 = (code & 0x3FF) + 0xDC00;
store<u32>(out, (hi << 16) | lo);
}
return runtime.register<String>(out);
return gc.register<String>(out);
}
@operator("[]") charAt(pos: i32): String {
assert(this !== null);
if (<u32>pos >= <u32>this.length) return changetype<String>("");
var out = runtime.allocRaw(2);
var out = runtime.alloc(2);
store<u16>(out, load<u16>(changetype<usize>(this) + (<usize>pos << 1)));
return runtime.register<String>(out);
return gc.register<String>(out);
}
charCodeAt(pos: i32): i32 {
@ -70,10 +71,10 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
var otherSize: isize = other.length << 1;
var outSize: usize = thisSize + otherSize;
if (outSize == 0) return changetype<String>("");
var out = runtime.allocRaw(outSize);
var out = runtime.alloc(outSize);
memory.copy(out, changetype<usize>(this), thisSize);
memory.copy(out + thisSize, changetype<usize>(other), otherSize);
return runtime.register<String>(out);
return gc.register<String>(out);
}
endsWith(searchString: String, endPosition: i32 = String.MAX_LENGTH): bool {
@ -183,9 +184,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
if (intStart < 0) intStart = max(size + intStart, 0);
var resultLength = min(max(end, 0), size - intStart);
if (resultLength <= 0) return changetype<String>("");
var out = runtime.allocRaw(resultLength << 1);
var out = runtime.alloc(resultLength << 1);
memory.copy(out, changetype<usize>(this) + intStart, resultLength);
return runtime.register<String>(out);
return gc.register<String>(out);
}
substring(start: i32, end: i32 = i32.MAX_VALUE): String {
@ -198,9 +199,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
len = toPos - fromPos;
if (!len) return changetype<String>("");
if (!fromPos && toPos == this.length << 1) return this;
var out = runtime.allocRaw(len);
var out = runtime.alloc(len);
memory.copy(out, changetype<usize>(this) + fromPos, len);
return runtime.register<String>(out);
return gc.register<String>(out);
}
trim(): String {
@ -226,9 +227,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
}
if (!size) return changetype<String>("");
if (!start && size == length << 1) return this;
var out = runtime.allocRaw(size);
var out = runtime.alloc(size);
memory.copy(out, changetype<usize>(this) + offset, size);
return runtime.register<String>(out);
return gc.register<String>(out);
}
@inline
@ -256,9 +257,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
if (!offset) return this;
size -= offset;
if (!size) return changetype<String>("");
var out = runtime.allocRaw(size);
var out = runtime.alloc(size);
memory.copy(out, changetype<usize>(this) + offset, size);
return runtime.register<String>(out);
return gc.register<String>(out);
}
trimEnd(): String {
@ -275,9 +276,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
}
if (!size) return changetype<String>("");
if (size == originalSize) return this;
var out = runtime.allocRaw(size);
var out = runtime.alloc(size);
memory.copy(out, changetype<usize>(this), size);
return runtime.register<String>(out);
return gc.register<String>(out);
}
padStart(targetLength: i32, padString: string = " "): String {
@ -287,7 +288,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
var padSize = <usize>padString.length << 1;
if (targetSize < thisSize || !padSize) return this;
var prependSize = targetSize - thisSize;
var out = runtime.allocRaw(targetSize);
var out = runtime.alloc(targetSize);
if (prependSize > padSize) {
let repeatCount = (prependSize - 2) / padSize;
let restBase = repeatCount * padSize;
@ -298,7 +299,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
memory.copy(out, changetype<usize>(padString), prependSize);
}
memory.copy(out + prependSize, changetype<usize>(this), thisSize);
return runtime.register<String>(out);
return gc.register<String>(out);
}
padEnd(targetLength: i32, padString: string = " "): String {
@ -308,7 +309,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
var padSize = <usize>padString.length << 1;
if (targetSize < thisSize || !padSize) return this;
var appendSize = targetSize - thisSize;
var out = runtime.allocRaw(targetSize);
var out = runtime.alloc(targetSize);
memory.copy(out, changetype<usize>(this), thisSize);
if (appendSize > padSize) {
let repeatCount = (appendSize - 2) / padSize;
@ -319,7 +320,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
} else {
memory.copy(out + thisSize, changetype<usize>(padString), appendSize);
}
return runtime.register<String>(out);
return gc.register<String>(out);
}
repeat(count: i32 = 0): String {
@ -333,9 +334,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
if (count == 0 || !length) return changetype<String>("");
if (count == 1) return this;
var out = runtime.allocRaw((length * count) << 1);
var out = runtime.alloc((length * count) << 1);
memory.repeat(out, changetype<usize>(this), <usize>length << 1, count);
return runtime.register<String>(out);
return gc.register<String>(out);
}
slice(beginIndex: i32, endIndex: i32 = i32.MAX_VALUE): String {
@ -344,9 +345,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
var end = endIndex < 0 ? max(endIndex + len, 0) : min(endIndex, len);
len = end - begin;
if (len <= 0) return changetype<String>("");
var out = runtime.allocRaw(len << 1);
var out = runtime.alloc(len << 1);
memory.copy(out, changetype<usize>(this) + (<usize>begin << 1), <usize>len << 1);
return runtime.register<String>(out);
return gc.register<String>(out);
}
split(separator: String | null = null, limit: i32 = i32.MAX_VALUE): String[] {
@ -364,7 +365,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
let buffer = unreachable(); // TODO
// let buffer = <ArrayBuffer>result.buffer_;
for (let i: isize = 0; i < length; ++i) {
let char = runtime.allocRaw(2);
let char = runtime.alloc(2);
store<u16>(
changetype<usize>(char),
load<u16>(
@ -384,9 +385,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
while ((end = this.indexOf(separator!, start)) != -1) {
let len = end - start;
if (len > 0) {
let out = runtime.allocRaw(<usize>len << 1);
let out = runtime.alloc(<usize>len << 1);
memory.copy(out, changetype<usize>(this) + (<usize>start << 1), <usize>len << 1);
result.push(runtime.register<String>(out));
result.push(gc.register<String>(out));
} else {
result.push(changetype<String>(""));
}
@ -400,9 +401,9 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
}
var len = length - start;
if (len > 0) {
let out = runtime.allocRaw(<usize>len << 1);
let out = runtime.alloc(<usize>len << 1);
memory.copy(out, changetype<usize>(this) + (<usize>start << 1), <usize>len << 1);
result.push(runtime.register<String>(out));
result.push(gc.register<String>(out));
} else {
result.push(changetype<String>(""));
}
@ -474,10 +475,10 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
}
}
assert(ptrPos == len);
var out = runtime.allocRaw(bufPos);
var out = runtime.alloc(bufPos);
memory.copy(changetype<usize>(out), buf, bufPos);
memory.free(buf);
return runtime.register<string>(out);
return gc.register<string>(out);
}
toUTF8(): usize {