This commit is contained in:
dcode 2019-03-13 03:09:24 +01:00
parent 36d54d63d5
commit e78c4c7f54
5 changed files with 149 additions and 90 deletions

View File

@ -40,6 +40,10 @@ export class Array<T> extends ArrayBufferView {
this.length_ = length; this.length_ = length;
} }
@unsafe get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 { get length(): i32 {
return this.length_; return this.length_;
} }
@ -49,18 +53,18 @@ export class Array<T> extends ArrayBufferView {
this.length_ = length; this.length_ = length;
} }
resize(length: i32): void { private resize(length: i32): void {
var buffer = this.buffer; var oldData = this.data;
var oldCapacity = buffer.byteLength >>> alignof<T>(); var oldCapacity = oldData.byteLength >>> alignof<T>();
if (<u32>length > <u32>oldCapacity) { if (<u32>length > <u32>oldCapacity) {
const MAX_LENGTH = ArrayBuffer.MAX_BYTELENGTH >>> alignof<T>(); const MAX_LENGTH = ArrayBuffer.MAX_BYTELENGTH >>> alignof<T>();
if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length"); if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length");
let newCapacity = length << alignof<T>(); let newCapacity = <usize>length << alignof<T>();
let newBuffer = REALLOC(changetype<usize>(buffer), newCapacity); let newData = REALLOC(changetype<usize>(oldData), newCapacity); // registers on move
if (newBuffer !== changetype<usize>(buffer)) { if (newData !== changetype<usize>(oldData)) {
this.buffer = changetype<ArrayBuffer>(newBuffer); // links this.data = changetype<ArrayBuffer>(newData); // links
this.dataStart = newBuffer; this.dataStart = newData;
this.dataEnd = newBuffer + newCapacity; this.dataEnd = newData + newCapacity;
} }
} }
} }
@ -114,23 +118,21 @@ export class Array<T> extends ArrayBufferView {
// } // }
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this { fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
var base = this.dataStart; var dataStart = this.dataStart;
var length = this.length_; var length = this.length_;
start = start < 0 ? max(length + start, 0) : min(start, length); start = start < 0 ? max(length + start, 0) : min(start, length);
end = end < 0 ? max(length + end, 0) : min(end, length); end = end < 0 ? max(length + end, 0) : min(end, length);
if (sizeof<T>() == 1) { if (sizeof<T>() == 1) {
if (start < end) { if (start < end) {
memory.fill( memory.fill(
base + <usize>start, dataStart + <usize>start,
<u8>value, <u8>value,
<usize>(end - start) <usize>(end - start)
); );
} }
} else { } else {
for (; start < end; ++start) { for (; start < end; ++start) {
store<T>(base + (<usize>start << alignof<T>()), value); store<T>(dataStart + (<usize>start << alignof<T>()), value);
} }
} }
return this; return this;
@ -144,9 +146,9 @@ export class Array<T> extends ArrayBufferView {
var length = this.length_; var length = this.length_;
if (length == 0 || fromIndex >= length) return -1; if (length == 0 || fromIndex >= length) return -1;
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0); if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
var base = this.dataStart; var dataStart = this.dataStart;
while (fromIndex < length) { while (fromIndex < length) {
if (load<T>(base + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex; if (load<T>(dataStart + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex;
++fromIndex; ++fromIndex;
} }
return -1; return -1;
@ -155,11 +157,11 @@ export class Array<T> extends ArrayBufferView {
lastIndexOf(searchElement: T, fromIndex: i32 = this.length_): i32 { lastIndexOf(searchElement: T, fromIndex: i32 = this.length_): i32 {
var length = this.length_; var length = this.length_;
if (length == 0) return -1; if (length == 0) return -1;
if (fromIndex < 0) fromIndex = length + fromIndex; // no need to clamp if (fromIndex < 0) fromIndex = length + fromIndex;
else if (fromIndex >= length) fromIndex = length - 1; else if (fromIndex >= length) fromIndex = length - 1;
var base = this.dataStart; var dataStart = this.dataStart;
while (fromIndex >= 0) { // ^ while (fromIndex >= 0) {
if (load<T>(base + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex; if (load<T>(dataStart + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex;
--fromIndex; --fromIndex;
} }
return -1; return -1;
@ -177,19 +179,32 @@ export class Array<T> extends ArrayBufferView {
concat(other: Array<T>): Array<T> { concat(other: Array<T>): Array<T> {
var thisLen = this.length_; var thisLen = this.length_;
var otherLen = select(0, other.length_, other === null); var otherLen = select(0, other.length_, other === null);
var outLen = thisLen + otherLen; var out = new Array<T>(thisLen + otherLen);
var out = new Array<T>(outLen); var outStart = out.dataStart;
if (thisLen) { var thisSize = <usize>thisLen << alignof<T>();
memory.copy(out.dataStart, this.dataStart, <usize>thisLen << alignof<T>()); if (isManaged<T>()) {
} let thisStart = this.dataStart;
if (otherLen) { for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
memory.copy(out.dataStart + (<usize>thisLen << alignof<T>()), other.dataStart, <usize>otherLen << alignof<T>()); let element = load<T>(thisStart + offset);
store<T>(outStart + offset, element);
LINK(changetype<usize>(element), changetype<usize>(out));
}
let otherStart = other.dataStart;
let otherSize = <usize>otherLen << alignof<T>();
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
let element = load<T>(otherStart + offset);
store<T>(outStart + thisSize + offset, element);
LINK(changetype<usize>(element), changetype<usize>(out));
}
} else {
memory.copy(outStart, this.dataStart, thisSize);
memory.copy(outStart + thisSize, other.dataStart, <usize>otherLen << alignof<T>());
} }
return out; return out;
} }
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this { copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this {
var buffer = this.buffer_; var dataStart = this.dataStart;
var len = this.length_; var len = this.length_;
end = min<i32>(end, len); end = min<i32>(end, len);
@ -202,13 +217,13 @@ export class Array<T> extends ArrayBufferView {
from += count - 1; from += count - 1;
to += count - 1; to += count - 1;
while (count) { while (count) {
STORE<T>(buffer, to, LOAD<T>(buffer, from)); store<T>(dataStart + (<usize>to << alignof<T>()), load<T>(dataStart + (<usize>from << alignof<T>())));
--from, --to, --count; --from, --to, --count;
} }
} else { } else {
memory.copy( memory.copy(
changetype<usize>(buffer) + HEADER_SIZE + (<usize>to << alignof<T>()), dataStart + (<usize>to << alignof<T>()),
changetype<usize>(buffer) + HEADER_SIZE + (<usize>from << alignof<T>()), dataStart + (<usize>from << alignof<T>()),
<usize>count << alignof<T>() <usize>count << alignof<T>()
); );
} }
@ -231,14 +246,15 @@ export class Array<T> extends ArrayBufferView {
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U> { map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U> {
var length = this.length_; var length = this.length_;
var result = new Array<U>(length); var out = new Array<U>(length);
var resultStart = result.dataStart; var outStart = out.dataStart;
for (let index = 0; index < min(length, this.length_); ++index) { for (let index = 0; index < min(length, this.length_); ++index) {
let element = load<T>(this.dataStart + (<usize>index << alignof<T>())); let value = load<T>(this.dataStart + (<usize>index << alignof<T>()));
store<U>(resultStart + (<usize>index << alignof<T>()), element); let result = callbackfn(value, index, this);
if (isManaged<U>()) LINK(changetype<usize>(element), changetype<usize>(result)); store<U>(outStart + (<usize>index << alignof<U>()), result);
if (isManaged<U>()) LINK(changetype<usize>(result), changetype<usize>(out));
} }
return result; return out;
} }
filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T> { filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T> {
@ -360,9 +376,10 @@ export class Array<T> extends ArrayBufferView {
reverse(): Array<T> { reverse(): Array<T> {
var base = this.dataStart; var base = this.dataStart;
for (let front = 0, back = this.length_ - 1; front < back; ++front, --back) { for (let front = 0, back = this.length_ - 1; front < back; ++front, --back) {
let temp: T = load<T>(base, front); let temp = load<T>(base, front);
store<T>(base + (<usize>front << alignof<T>()), load<T>(base + (<usize>back << alignof<T>()))); let dest = base + (<usize>back << alignof<T>());
store<T>(base + (<usize>back << alignof<T>()), temp); store<T>(base + (<usize>front << alignof<T>()), load<T>(dest));
store<T>(dest, temp);
} }
return this; return this;
} }

View File

@ -1,7 +1,4 @@
import { import { AL_MASK, MAX_SIZE_32 } from "./util/allocator";
AL_MASK,
MAX_SIZE_32
} from "./internal/allocator";
@builtin export declare const HEAP_BASE: usize; @builtin export declare const HEAP_BASE: usize;
@ -151,9 +148,9 @@ export abstract class ArrayBufferBase {
export abstract class ArrayBufferView { export abstract class ArrayBufferView {
[key: number]: number; [key: number]: number;
protected data: ArrayBuffer; @unsafe data: ArrayBuffer;
protected dataStart: usize; @unsafe dataStart: usize;
protected dataEnd: usize; @unsafe dataEnd: usize;
constructor(length: i32, alignLog2: i32) { constructor(length: i32, alignLog2: i32) {
if (<u32>length > <u32>ArrayBufferBase.MAX_BYTELENGTH >>> alignLog2) throw new RangeError("Invalid length"); if (<u32>length > <u32>ArrayBufferBase.MAX_BYTELENGTH >>> alignLog2) throw new RangeError("Invalid length");
@ -161,11 +158,7 @@ export abstract class ArrayBufferView {
var buffer = new ArrayBuffer(byteLength); var buffer = new ArrayBuffer(byteLength);
this.data = buffer; this.data = buffer;
this.dataStart = changetype<usize>(buffer); this.dataStart = changetype<usize>(buffer);
this.dataEnd = changetype<usize>(buffer) + length; this.dataEnd = changetype<usize>(buffer) + <usize>length;
}
get buffer(): ArrayBuffer {
return this.data;
} }
get byteOffset(): i32 { get byteOffset(): i32 {
@ -175,6 +168,11 @@ export abstract class ArrayBufferView {
get byteLength(): i32 { get byteLength(): i32 {
return <i32>(this.dataEnd - this.dataStart); return <i32>(this.dataEnd - this.dataStart);
} }
get length(): i32 {
ERROR("not implemented");
return unreachable();
}
} }
export abstract class StringBase { export abstract class StringBase {
@ -185,7 +183,7 @@ export abstract class StringBase {
} }
} }
import { memcmp, memmove, memset } from "./internal/memory"; import { memcmp, memmove, memset } from "./util/memory";
export namespace memory { export namespace memory {
@builtin export declare function size(): i32; @builtin export declare function size(): i32;

View File

@ -9,7 +9,7 @@ import {
parse, parse,
CharCode, CharCode,
isWhiteSpaceOrLineTerminator isWhiteSpaceOrLineTerminator
} from "./internal/string"; } from "./util/string";
@sealed export class String extends StringBase { @sealed export class String extends StringBase {
@ -431,8 +431,8 @@ import {
return len; return len;
} }
static fromUTF8(ptr: usize, len: usize): String { static fromUTF8(ptr: usize, len: usize): string {
if (len < 1) return changetype<String>(""); if (len < 1) return changetype<string>("");
var ptrPos = <usize>0; var ptrPos = <usize>0;
var buf = memory.allocate(<usize>len << 1); var buf = memory.allocate(<usize>len << 1);
var bufPos = <usize>0; var bufPos = <usize>0;
@ -468,10 +468,10 @@ import {
} }
} }
assert(ptrPos == len); assert(ptrPos == len);
var out = ALLOC(<u32>(bufPos >> 1)); var out = ALLOC(bufPos);
memory.copy(changetype<usize>(out), buf, bufPos); memory.copy(changetype<usize>(out), buf, bufPos);
memory.free(buf); memory.free(buf);
return REGISTER<String>(out); return REGISTER<string>(out);
} }
toUTF8(): usize { toUTF8(): usize {

View File

@ -1,14 +1,5 @@
import { import { ALLOC, REGISTER, LINK, ArrayBufferView } from "./runtime";
ALLOC, import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort";
REGISTER,
LINK,
ArrayBufferView
} from "./runtime";
import {
COMPARATOR,
SORT as SORT_IMPL
} from "./internal/sort";
function clampToByte(value: i32): i32 { function clampToByte(value: i32): i32 {
return ~(value >> 31) & (((255 - value) >> 31) | value); // & 255 return ~(value >> 31) & (((255 - value) >> 31) | value); // & 255
@ -18,10 +9,16 @@ export class Int8Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
constructor(length: i32) { constructor(length: i32) {
super(length, 0); super(length, alignof<i8>());
} }
get length(): i32 { return this.byteLength; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength;
}
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array { fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
return FILL<Int8Array, i8>(this, value, start, end); return FILL<Int8Array, i8>(this, value, start, end);
@ -74,7 +71,7 @@ export class Uint8Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
constructor(length: i32) { constructor(length: i32) {
super(length, 0); super(length, alignof<u8>());
} }
get length(): i32 { return this.byteLength; } get length(): i32 { return this.byteLength; }
@ -180,10 +177,16 @@ export class Int16Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();
constructor(length: i32) { constructor(length: i32) {
super(length, 1); super(length, alignof<i16>());
} }
get length(): i32 { return this.byteLength >>> 1; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 1;
}
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array { fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
return FILL<Int16Array, i16>(this, value, start, end); return FILL<Int16Array, i16>(this, value, start, end);
@ -236,10 +239,16 @@ export class Uint16Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>();
constructor(length: i32) { constructor(length: i32) {
super(length, 1); super(length, alignof<u16>());
} }
get length(): i32 { return this.byteLength >>> 1; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 1;
}
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array { fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
return FILL<Uint16Array, u16>(this, value, start, end); return FILL<Uint16Array, u16>(this, value, start, end);
@ -292,10 +301,16 @@ export class Int32Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i32>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i32>();
constructor(length: i32) { constructor(length: i32) {
super(length, 2); super(length, alignof<i32>());
} }
get length(): i32 { return this.byteLength >>> 2; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 2;
}
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array { fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
return FILL<Int32Array, i32>(this, value, start, end); return FILL<Int32Array, i32>(this, value, start, end);
@ -348,10 +363,16 @@ export class Uint32Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u32>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u32>();
constructor(length: i32) { constructor(length: i32) {
super(length, 2); super(length, alignof<u32>());
} }
get length(): i32 { return this.byteLength >>> 2; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 2;
}
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array { fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
return FILL<Uint32Array, u32>(this, value, start, end); return FILL<Uint32Array, u32>(this, value, start, end);
@ -404,10 +425,16 @@ export class Int64Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i64>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i64>();
constructor(length: i32) { constructor(length: i32) {
super(length, 3); super(length, alignof<i64>());
} }
get length(): i32 { return this.byteLength >>> 3; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 3;
}
fill(value: i64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array { fill(value: i64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
return FILL<Int64Array, i64>(this, value, start, end); return FILL<Int64Array, i64>(this, value, start, end);
@ -460,10 +487,16 @@ export class Uint64Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u64>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u64>();
constructor(length: i32) { constructor(length: i32) {
super(length, 3); super(length, alignof<u64>());
} }
get length(): i32 { return this.byteLength >>> 3; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 3;
}
fill(value: u64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array { fill(value: u64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
return FILL<Uint64Array, u64>(this, value, start, end); return FILL<Uint64Array, u64>(this, value, start, end);
@ -516,10 +549,16 @@ export class Float32Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<f32>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<f32>();
constructor(length: i32) { constructor(length: i32) {
super(length, 2); super(length, alignof<f32>());
} }
get length(): i32 { return this.byteLength >>> 2; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 2;
}
fill(value: f32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array { fill(value: f32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
return FILL<Float32Array, f32>(this, value, start, end); return FILL<Float32Array, f32>(this, value, start, end);
@ -572,10 +611,16 @@ export class Float64Array extends ArrayBufferView {
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<f64>(); @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<f64>();
constructor(length: i32) { constructor(length: i32) {
super(length, 3); super(length, alignof<f64>());
} }
get length(): i32 { return this.byteLength >>> 3; } get buffer(): ArrayBuffer {
return this.data;
}
get length(): i32 {
return this.byteLength >>> 3;
}
fill(value: f64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array { fill(value: f64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
return FILL<Float64Array, f64>(this, value, start, end); return FILL<Float64Array, f64>(this, value, start, end);

View File

@ -1,3 +1,2 @@
@sealed @sealed export class V128 {
export abstract class V128 {
} }