New ArrayBuffer/TypedArray; Stdlib restructure; Fix importing stdlib in stdlib; Traverse constructors; Allow initialization of readonly instance fields in constructors

This commit is contained in:
dcodeIO
2018-04-07 03:27:22 +02:00
parent 6268b92eba
commit 8770f7b548
44 changed files with 5200 additions and 2165 deletions

40
std/assembly.d.ts vendored
View File

@ -273,7 +273,7 @@ declare class ArrayBuffer {
}
/** Interface for a typed view on an array buffer. */
declare interface ArrayBufferView<T> {
interface ArrayBufferView<T> {
[key: number]: T;
/** The {@link ArrayBuffer} referenced by this view. */
readonly buffer: ArrayBuffer;
@ -283,6 +283,44 @@ declare interface ArrayBufferView<T> {
readonly byteLength: i32;
}
/* @internal */
declare abstract class TypedArray<T> implements ArrayBufferView<T> {
[key: number]: T;
/** Number of bytes per element. */
static readonly BYTES_PER_ELEMENT: usize;
/** Constructs a new typed array. */
constructor(length: i32);
/** The {@link ArrayBuffer} referenced by this view. */
readonly buffer: ArrayBuffer;
/** The offset in bytes from the start of the referenced {@link ArrayBuffer}. */
readonly byteOffset: i32;
/** The length in bytes from the start of the referenced {@link ArrayBuffer}. */
readonly byteLength: i32;
/** The length (in elements). */
readonly length: i32;
}
/** An array of twos-complement 8-bit signed integers. */
declare class Int8Array extends TypedArray<i8> {}
/** An array of 8-bit unsigned integers. */
declare class Uint8Array extends TypedArray<u8> {}
/** An array of twos-complement 16-bit signed integers. */
declare class Int16Array extends TypedArray<i16> {}
/** An array of 16-bit unsigned integers. */
declare class Uint16Array extends TypedArray<u16> {}
/** An array of twos-complement 32-bit signed integers. */
declare class Int32Array extends TypedArray<i32> {}
/** An array of 32-bit unsigned integers. */
declare class Uint32Array extends TypedArray<u32> {}
/** An array of twos-complement 64-bit signed integers. */
declare class Int64Array extends TypedArray<i64> {}
/** An array of 64-bit unsigned integers. */
declare class Uint64Array extends TypedArray<u64> {}
/** An array of 32-bit floating point numbers. */
declare class Float32Array extends TypedArray<f32> {}
/** An array of 64-bit floating point numbers. */
declare class Float64Array extends TypedArray<f64> {}
/** Class representing a sequence of values of type `T`. */
declare class Array<T> {
[key: number]: T;

View File

@ -7,7 +7,7 @@
* @module std/assembly/allocator/arena
*//***/
import { AL_MASK } from "./common";
import { AL_MASK } from "../internal/allocator";
var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
var offset: usize = startOffset;

View File

@ -1,6 +1,6 @@
/**
* Buddy Memory Allocator.
* @module stdd/assembly/allocator/buddy
* @module std/assembly/allocator/buddy
*//***/
/*

View File

@ -19,7 +19,7 @@ import {
AL_BITS,
AL_SIZE,
AL_MASK
} from "./common";
} from "../internal/allocator";
const SL_BITS: u32 = 5;
const SL_SIZE: usize = 1 << <usize>SL_BITS;

View File

@ -1,29 +1,35 @@
import {
defaultComparator,
insertionSort,
weakHeapSort
} from "./internal/array";
export class Array<T> {
private __memory: usize;
private __capacity: i32; // capped to [0, 0x7fffffff]
private __length: i32; // capped to [0, __capacity]
__memory: usize;
__capacity: i32; // capped to [0, 0x7fffffff]
__length: i32; // capped to [0, __capacity]
private __grow(newCapacity: i32): void {
assert(newCapacity > this.__capacity);
var oldMemory = this.__memory;
var oldCapacity = this.__capacity;
assert(newCapacity > oldCapacity);
var newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
if (this.__memory) {
move_memory(newMemory, this.__memory, <usize>this.__capacity * sizeof<T>());
free_memory(this.__memory);
if (oldMemory) {
move_memory(newMemory, oldMemory, <usize>oldCapacity * sizeof<T>());
free_memory(oldMemory);
}
this.__memory = newMemory;
this.__capacity = newCapacity;
}
constructor(capacity: i32 = 0) {
if (capacity < 0) {
throw new RangeError("Invalid array length");
}
if (capacity < 0) throw new RangeError("Invalid array length");
this.__memory = capacity
? allocate_memory(<usize>capacity * sizeof<T>())
: 0;
this.__capacity = this.__length = capacity;
this.__capacity = capacity;
this.__length = capacity;
}
every(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
@ -55,103 +61,93 @@ export class Array<T> {
}
set length(length: i32) {
if (length < 0) {
throw new RangeError("Invalid array length");
}
if (length > this.__capacity) {
this.__grow(max(length, this.__capacity << 1));
}
if (length < 0) throw new RangeError("Invalid array length");
if (length > this.__capacity) this.__grow(max(length, this.__capacity << 1));
this.__length = length;
}
@operator("[]")
private __get(index: i32): T {
if (<u32>index >= <u32>this.__capacity) {
throw new Error("Index out of bounds"); // return changetype<T>(0) ?
}
if (<u32>index >= <u32>this.__capacity) throw new Error("Index out of bounds");
return load<T>(this.__memory + <usize>index * sizeof<T>());
}
@operator("[]=")
private __set(index: i32, value: T): void {
if (index < 0) {
throw new Error("Index out of bounds");
}
if (index >= this.__capacity) {
this.__grow(max(index + 1, this.__capacity << 1));
}
if (index < 0) throw new Error("Index out of bounds");
var capacity = this.__capacity;
if (index >= capacity) this.__grow(max(index + 1, capacity << 1));
store<T>(this.__memory + <usize>index * sizeof<T>(), value);
}
includes(searchElement: T, fromIndex: i32 = 0): bool {
if (this.__length == 0 || fromIndex >= this.__length) {
return false;
}
var length = this.__length;
if (length == 0 || fromIndex >= length) return false;
if (fromIndex < 0) {
fromIndex = this.__length + fromIndex;
fromIndex = length + fromIndex;
if (fromIndex < 0) {
fromIndex = 0;
}
}
while (<u32>fromIndex < <u32>this.__length) {
if (load<T>(this.__memory + <usize>fromIndex * sizeof<T>()) == searchElement) {
return true;
}
while (fromIndex < length) {
if (load<T>(this.__memory + <usize>fromIndex * sizeof<T>()) == searchElement) return true;
++fromIndex;
}
return false;
}
indexOf(searchElement: T, fromIndex: i32 = 0): i32 {
if (this.__length == 0 || fromIndex >= this.__length) {
var length = this.__length;
if (length == 0 || fromIndex >= length) {
return -1;
}
if (fromIndex < 0) {
fromIndex = this.__length + fromIndex;
fromIndex = length + fromIndex;
if (fromIndex < 0) {
fromIndex = 0;
}
}
while (<u32>fromIndex < <u32>this.__length) {
if (load<T>(this.__memory + <usize>fromIndex * sizeof<T>()) == searchElement) {
return fromIndex;
}
var memory = this.__memory;
while (fromIndex < length) {
if (load<T>(memory + <usize>fromIndex * sizeof<T>()) == searchElement) return fromIndex;
++fromIndex;
}
return -1;
}
lastIndexOf(searchElement: T, fromIndex: i32 = this.__length): i32 {
if (this.__length == 0) {
return -1;
}
var length = this.__length;
if (length == 0) return -1;
if (fromIndex < 0) {
fromIndex = this.__length + fromIndex;
} else if (fromIndex >= this.__length) {
fromIndex = this.__length - 1;
fromIndex = length + fromIndex;
} else if (fromIndex >= length) {
fromIndex = length - 1;
}
var memory = this.__memory;
while (fromIndex >= 0) {
if (load<T>(this.__memory + <usize>fromIndex * sizeof<T>()) == searchElement) {
return fromIndex;
}
if (load<T>(memory + <usize>fromIndex * sizeof<T>()) == searchElement) return fromIndex;
--fromIndex;
}
return -1;
}
push(element: T): i32 {
if (this.__length == this.__capacity) {
this.__grow(this.__capacity ? this.__capacity << 1 : 1);
var capacity = this.__capacity;
var length = this.__length;
if (length == capacity) {
this.__grow(capacity ? capacity << 1 : 1);
}
store<T>(this.__memory + <usize>this.__length * sizeof<T>(), element);
return ++this.__length;
store<T>(this.__memory + <usize>length * sizeof<T>(), element);
this.__length = ++length;
return length;
}
pop(): T {
if (this.__length < 1) {
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
}
return load<T>(this.__memory + <usize>--this.__length * sizeof<T>());
var length = this.__length;
if (length < 1) throw new RangeError("Array is empty");
var element = load<T>(this.__memory + <usize>--length * sizeof<T>());
this.__length = length;
return element;
}
reduce<U>(
@ -161,7 +157,7 @@ export class Array<T> {
var accumulator: U = initialValue;
var toIndex: i32 = this.__length;
var i: i32 = 0;
while (i < toIndex && i < this.__length) {
while (i < toIndex && i < /* might change */ this.__length) {
accumulator = callbackfn(accumulator, load<T>(this.__memory + <usize>i * sizeof<T>()), i, this);
i += 1;
}
@ -169,77 +165,81 @@ export class Array<T> {
}
shift(): T {
if (this.__length < 1) {
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
}
var element = load<T>(this.__memory);
var length = this.__length;
if (length < 1) throw new RangeError("Array is empty");
var memory = this.__memory;
var capacity = this.__capacity;
var element = load<T>(memory);
move_memory(
this.__memory,
this.__memory + sizeof<T>(),
<usize>(this.__capacity - 1) * sizeof<T>()
memory,
memory + sizeof<T>(),
<usize>(capacity - 1) * sizeof<T>()
);
set_memory(
this.__memory + <usize>(this.__capacity - 1) * sizeof<T>(),
memory + <usize>(capacity - 1) * sizeof<T>(),
0,
sizeof<T>()
);
--this.__length;
this.__length = length - 1;
return element;
}
some(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
var toIndex: i32 = this.__length;
var i: i32 = 0;
while (i < toIndex && i < this.__length) {
if (callbackfn(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) {
return true;
}
while (i < toIndex && i < /* might change */ this.__length) {
if (callbackfn(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) return true;
i += 1;
}
return false;
}
unshift(element: T): i32 {
var oldCapacity = this.__capacity;
if (this.__length == oldCapacity) {
var memory = this.__memory;
var capacity = this.__capacity;
var length = this.__length;
if (this.__length == capacity) {
// inlined __grow (avoids moving twice)
let newCapacity: i32 = oldCapacity ? oldCapacity << 1 : 1;
assert(newCapacity > this.__capacity);
let newCapacity: i32 = capacity ? capacity << 1 : 1;
assert(newCapacity > capacity);
let newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
if (this.__memory) {
if (memory) {
move_memory(
newMemory + sizeof<T>(),
this.__memory,
<usize>oldCapacity * sizeof<T>()
memory,
<usize>capacity * sizeof<T>()
);
free_memory(this.__memory);
free_memory(memory);
}
this.__memory = newMemory;
this.__capacity = newCapacity;
memory = newMemory;
} else {
move_memory(
this.__memory + sizeof<T>(),
this.__memory,
<usize>oldCapacity * sizeof<T>()
memory + sizeof<T>(),
memory,
<usize>capacity * sizeof<T>()
);
}
store<T>(this.__memory, element);
return ++this.__length;
store<T>(memory, element);
this.__length = ++length;
return length;
}
slice(begin: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
var length = this.__length;
if (begin < 0) {
begin = this.__length + begin;
begin = length + begin;
if (begin < 0) {
begin = 0;
}
} else if (begin > this.__length) {
begin = this.__length;
} else if (begin > length) {
begin = length;
}
if (end < 0) {
end = this.__length + end;
} else if (end > this.__length) {
end = this.__length;
end = length + end;
} else if (end > length) {
end = length;
}
if (end < begin) {
end = begin;
@ -261,194 +261,52 @@ export class Array<T> {
if (deleteCount < 1) {
return;
}
var length = this.__length;
if (start < 0) {
start = this.__length + start;
start = length + start;
if (start < 0) {
start = 0;
} else if (start >= this.__length) {
} else if (start >= length) {
return;
}
} else if (start >= this.__length) {
} else if (start >= length) {
return;
}
deleteCount = min(deleteCount, this.__length - start);
deleteCount = min(deleteCount, length - start);
var memory = this.__memory;
move_memory(
this.__memory + <usize>start * sizeof<T>(),
this.__memory + <usize>(start + deleteCount) * sizeof<T>(),
memory + <usize>start * sizeof<T>(),
memory + <usize>(start + deleteCount) * sizeof<T>(),
<usize>deleteCount * sizeof<T>()
);
this.__length -= deleteCount;
this.__length = length - deleteCount;
}
reverse(): Array<T> {
var memory = this.__memory;
for (let front: usize = 0, back: usize = <usize>this.__length - 1; front < back; ++front, --back) {
let temp = load<T>(this.__memory + front * sizeof<T>());
store<T>(this.__memory + front * sizeof<T>(), load<T>(this.__memory + back * sizeof<T>()));
store<T>(this.__memory + back * sizeof<T>(), temp);
let temp = load<T>(memory + front * sizeof<T>());
store<T>(memory + front * sizeof<T>(), load<T>(memory + back * sizeof<T>()));
store<T>(memory + back * sizeof<T>(), temp);
}
return this;
}
sort(comparator: (a: T, b: T) => i32 = createDefaultComparator<T>()): Array<T> {
return sort<T>(this, comparator);
}
}
@unmanaged
@sealed
export class CArray<T> {
private constructor() {}
@operator("[]")
private __get(index: i32): T {
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) {
throw new RangeError("Index out of range");
}
store<T>(changetype<usize>(this) + <usize>index * sizeof<T>(), value);
}
}
/*
* Internal methods
*/
// TODO remove this wrapper when indirect table landed
function createDefaultComparator<T>(): (a: T, b: T) => i32 {
return (a: T, b: T): i32 => (
<i32>(a > b) - <i32>(a < b)
);
}
function insertionSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32): Array<T> {
var a: T, b: T, j: i32;
const typeShift = alignof<T>();
for (let i: i32 = 0, len: i32 = arr.length; i < len; i++) {
a = load<T>(arr.__memory + (i << typeShift)); // a = <T>arr[i];
j = i - 1;
while (j >= 0) {
b = load<T>(arr.__memory + (j << typeShift)); // b = <T>arr[j];
sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): Array<T> {
var len = this.length;
if (len <= 1) return this;
if (len == 2) {
let memory = this.__memory;
let a = load<T>(memory, sizeof<T>()); // var a = <T>arr[1];
let b = load<T>(memory, 0); // var b = <T>arr[0];
if (comparator(a, b) < 0) {
store<T>(arr.__memory + ((j + 1) << typeShift), b); // arr[j + 1] = b;
j--;
} else break;
}
store<T>(arr.__memory + ((j + 1) << typeShift), a); // arr[j + 1] = a;
}
return arr;
}
/* Weak Heap Sort implementation based on paper:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.21.1863&rep=rep1&type=pdf
*/
function weakHeapSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32): Array<T> {
var len: i32 = arr.length;
var i: i32, j: i32, y: i32, p: i32, a: T, b: T;
const typeShift = alignof<T>();
const intShift = alignof<i32>();
var blen = (len + 7) >> 3;
var bitset = allocate_memory(blen << intShift);
set_memory(bitset, 0, blen << intShift);
for (i = len - 1; i > 0; i--) {
j = i;
while ((j & 1) == ((load<i32>(bitset + ((j >> 4) << intShift)) >> ((j >> 1) & 7)) & 1)) {
j >>= 1;
}
p = j >> 1;
a = load<T>(arr.__memory + (p << typeShift)); // a = <T>arr[p];
b = load<T>(arr.__memory + (i << typeShift)); // b = <T>arr[i];
if (comparator(a, b) < 0) {
store<i32>(
bitset + ((i >> 3) << intShift),
load<i32>(bitset + ((i >> 3) << intShift)) ^ (1 << (i & 7))
);
store<T>(arr.__memory + (i << typeShift), a); // arr[i] = a;
store<T>(arr.__memory + (p << typeShift), b); // arr[p] = b;
}
}
for (i = len - 1; i >= 2; i--) {
/*
a = <T>arr[0];
arr[0] = <T>arr[i];
arr[i] = a;
*/
a = load<T>(arr.__memory, 0);
store<T>(arr.__memory, load<T>(arr.__memory + (i << typeShift)), 0);
store<T>(arr.__memory + (i << typeShift), a);
let x = 1;
while ((y = (x << 1) + ((load<i32>(bitset + ((x >> 3) << intShift)) >> (x & 7)) & 1)) < i) {
x = y;
}
while (x > 0) {
a = load<T>(arr.__memory, 0); // a = <T>arr[0];
b = load<T>(arr.__memory + (x << typeShift)); // b = <T>arr[x];
if (comparator(a, b) < 0) {
store<i32>(
bitset + ((x >> 3) << intShift),
load<i32>(bitset + ((x >> 3) << intShift)) ^ (1 << (x & 7))
);
store<T>(arr.__memory + (x << typeShift), a); // arr[x] = a;
store<T>(arr.__memory, b, 0); // arr[0] = b;
store<T>(memory, b, sizeof<T>()); // arr[1] = b;
store<T>(memory, a, 0); // arr[0] = a;
}
x >>= 1;
return this;
}
return len <= 256
? insertionSort<T>(this, comparator)
: weakHeapSort<T>(this, comparator);
}
free_memory(bitset);
/*
let t = <T>arr[1];
arr[1] = <T>arr[0];
arr[0] = t;
*/
var t = load<T>(arr.__memory, sizeof<T>());
store<T>(arr.__memory, load<T>(arr.__memory, 0), sizeof<T>());
store<T>(arr.__memory, t, 0);
return arr;
}
function sort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32): Array<T> {
var len = arr.length;
if (len <= 1) return arr;
if (len == 2) {
let a = load<T>(arr.__memory, sizeof<T>()); // var a = <T>arr[1];
let b = load<T>(arr.__memory, 0); // var b = <T>arr[0];
if (comparator(a, b) < 0) {
store<T>(arr.__memory, b, sizeof<T>()); // arr[1] = b;
store<T>(arr.__memory, a, 0); // arr[0] = a;
}
return arr;
}
if (len <= 256) {
return insertionSort<T>(arr, comparator);
}
return weakHeapSort<T>(arr, comparator);
}

View File

@ -1,38 +1,30 @@
const HEADER_SIZE: usize = sizeof<i32>();
import {
HEADER_SIZE,
MAX_BLENGTH,
allocate
} from "./internal/arraybuffer";
@sealed
export class ArrayBuffer {
readonly byteLength: i32; // capped to [0, 0x7fffffff]
readonly byteLength: i32; // capped to [0, MAX_LENGTH]
constructor(length: i32) {
if (<u32>length > 0x7fffffff) throw new RangeError("Invalid array buffer length");
var buffer = allocate_memory(HEADER_SIZE + <usize>length);
store<i32>(buffer, length);
return changetype<ArrayBuffer>(buffer);
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
var buffer = allocate(length);
set_memory(changetype<usize>(buffer) + HEADER_SIZE, 0, <usize>length);
return buffer;
}
slice(begin: i32 = 0, end: i32 = 0x7fffffff): ArrayBuffer {
slice(begin: i32 = 0, end: i32 = MAX_BLENGTH): ArrayBuffer {
var len = this.byteLength;
if (begin < 0) begin = max(len + begin, 0);
else begin = min(begin, len);
if (end < 0) end = max(len + end, 0);
else end = min(end, len);
var newLen = max(end - begin, 0);
var buffer = allocate_memory(HEADER_SIZE + <usize>newLen);
store<i32>(buffer, newLen);
move_memory(buffer + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
return changetype<ArrayBuffer>(buffer);
var buffer = allocate(newLen);
move_memory(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
return buffer;
}
// TODO: built-in isView?
// TODO: built-in transfer?
}
/** @internal */
export declare interface ArrayBufferView<T> {
readonly buffer: ArrayBuffer;
readonly byteOffset: i32;
readonly byteLength: i32;
readonly length: i32;
}

View File

@ -1,6 +1,5 @@
export class Error {
name: string = "Error";
message: string;
stack: string = ""; // TODO
@ -9,10 +8,5 @@ export class Error {
}
}
export class RangeError extends Error {
name: string = "RangeError";
}
export class TypeError extends Error {
name: string = "TypeError";
}
export class RangeError extends Error {}
export class TypeError extends Error {}

View File

@ -1,8 +1,3 @@
/**
* Shared allocator constants.
* @module std/assembly/allocator/common
*//***/
/** Number of alignment bits. */
export const AL_BITS: u32 = 3;

View File

@ -0,0 +1,109 @@
import { Array } from "../array";
/** Obtains the default comparator for the specified type. */
export function defaultComparator<T>(): (a: T, b: T) => i32 {
return (a: T, b: T): i32 => (<i32>(a > b) - <i32>(a < b)); // compiles to a constant function index
}
/** Sorts an Array with the 'Insertion Sort' algorithm. */
export function insertionSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32): Array<T> {
var a: T, b: T, j: i32;
const typeShift = alignof<T>();
for (let i: i32 = 0, len: i32 = arr.length; i < len; i++) {
a = load<T>(arr.__memory + (i << typeShift)); // a = <T>arr[i];
j = i - 1;
while (j >= 0) {
b = load<T>(arr.__memory + (j << typeShift)); // b = <T>arr[j];
if (comparator(a, b) < 0) {
store<T>(arr.__memory + ((j + 1) << typeShift), b); // arr[j + 1] = b;
j--;
} else break;
}
store<T>(arr.__memory + ((j + 1) << typeShift), a); // arr[j + 1] = a;
}
return arr;
}
/** Sorts an Array with the 'Weak Heap Sort' algorithm. */
export function weakHeapSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32): Array<T> {
// see: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.21.1863&rep=rep1&type=pdf
var len: i32 = arr.length;
var i: i32, j: i32, y: i32, p: i32, a: T, b: T;
const typeShift = alignof<T>();
const intShift = alignof<i32>();
var blen = (len + 7) >> 3;
var bitset = allocate_memory(blen << intShift);
set_memory(bitset, 0, blen << intShift);
for (i = len - 1; i > 0; i--) {
j = i;
while ((j & 1) == ((load<i32>(bitset + ((j >> 4) << intShift)) >> ((j >> 1) & 7)) & 1)) {
j >>= 1;
}
p = j >> 1;
a = load<T>(arr.__memory + (p << typeShift)); // a = <T>arr[p];
b = load<T>(arr.__memory + (i << typeShift)); // b = <T>arr[i];
if (comparator(a, b) < 0) {
store<i32>(
bitset + ((i >> 3) << intShift),
load<i32>(bitset + ((i >> 3) << intShift)) ^ (1 << (i & 7))
);
store<T>(arr.__memory + (i << typeShift), a); // arr[i] = a;
store<T>(arr.__memory + (p << typeShift), b); // arr[p] = b;
}
}
for (i = len - 1; i >= 2; i--) {
/*
a = <T>arr[0];
arr[0] = <T>arr[i];
arr[i] = a;
*/
a = load<T>(arr.__memory, 0);
store<T>(arr.__memory, load<T>(arr.__memory + (i << typeShift)), 0);
store<T>(arr.__memory + (i << typeShift), a);
let x = 1;
while ((y = (x << 1) + ((load<i32>(bitset + ((x >> 3) << intShift)) >> (x & 7)) & 1)) < i) {
x = y;
}
while (x > 0) {
a = load<T>(arr.__memory, 0); // a = <T>arr[0];
b = load<T>(arr.__memory + (x << typeShift)); // b = <T>arr[x];
if (comparator(a, b) < 0) {
store<i32>(
bitset + ((x >> 3) << intShift),
load<i32>(bitset + ((x >> 3) << intShift)) ^ (1 << (x & 7))
);
store<T>(arr.__memory + (x << typeShift), a); // arr[x] = a;
store<T>(arr.__memory, b, 0); // arr[0] = b;
}
x >>= 1;
}
}
free_memory(bitset);
/*
let t = <T>arr[1];
arr[1] = <T>arr[0];
arr[0] = t;
*/
var t = load<T>(arr.__memory, sizeof<T>());
store<T>(arr.__memory, load<T>(arr.__memory, 0), sizeof<T>());
store<T>(arr.__memory, t, 0);
return arr;
}

View File

@ -0,0 +1,34 @@
import { AL_MASK, MAX_SIZE_32 } from "./allocator";
/** Size of an ArrayBuffer header. */
export const HEADER_SIZE: usize = (offsetof<ArrayBuffer>() + AL_MASK) & ~AL_MASK;
/** Maximum byte length of an ArrayBuffer. */
export const MAX_BLENGTH: i32 = <i32>MAX_SIZE_32 - HEADER_SIZE;
/** Computes an ArrayBuffer's size in memory. */
export function computeSize(byteLength: i32): usize {
// round up to power of 2, with HEADER_SIZE=8:
// 0 -> 2^3 = 8
// 1..8 -> 2^4 = 16
// 9..24 -> 2^5 = 32
// ...
// MAX_LENGTH -> 2^30 = 0x40000000 (MAX_SIZE_32)
return <usize>1 << <usize>(<u32>32 - clz<u32>(byteLength + HEADER_SIZE - 1));
}
/** Allocates a raw ArrayBuffer with uninitialized contents. */
export function allocate(byteLength: i32): ArrayBuffer {
assert(<u32>byteLength <= <u32>MAX_BLENGTH);
var buffer = allocate_memory(computeSize(byteLength));
store<i32>(buffer, byteLength, offsetof<ArrayBuffer>("byteLength"));
return changetype<ArrayBuffer>(buffer);
}
/** Common typed array interface. Not a global object. */
// export declare interface ArrayBufferView<T> {
// readonly buffer: ArrayBuffer;
// readonly byteOffset: i32;
// readonly byteLength: i32;
// readonly length: i32;
// }

View File

@ -0,0 +1,149 @@
import {
MAX_SIZE_32
} from "./allocator";
import {
String
} from "../string";
/** Size of a String header. */
export const HEADER_SIZE = (offsetof<String>() + 1) & ~1; // 2 byte aligned
/** Maximum length of a String. */
export const MAX_LENGTH = (<i32>MAX_SIZE_32 - HEADER_SIZE) >>> 1;
/** Singleton empty String. */
export const EMPTY = changetype<String>(""); // TODO: is this a bad idea with '===' in place?
/** Allocates a raw String with uninitialized contents. */
export function allocate(length: i32): String {
assert(length > 0 && length <= MAX_LENGTH);
var buffer = allocate_memory(HEADER_SIZE + (<usize>length << 1));
store<i32>(buffer, length);
return changetype<String>(buffer);
}
export 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;
}
}
export const enum CharCode {
PLUS = 0x2B,
MINUS = 0x2D,
DOT = 0x2E,
_0 = 0x30,
_1 = 0x31,
_2 = 0x32,
_3 = 0x33,
_4 = 0x34,
_5 = 0x35,
_6 = 0x36,
_7 = 0x37,
_8 = 0x38,
_9 = 0x39,
A = 0x41,
B = 0x42,
E = 0x45,
O = 0x4F,
X = 0x58,
Z = 0x5a,
a = 0x61,
b = 0x62,
e = 0x65,
o = 0x6F,
x = 0x78,
z = 0x7A
}
export function parse<T>(str: String, radix: i32 = 0): T {
var len: i32 = str.length;
if (!len) {
return <T>NaN;
}
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEADER_SIZE);
// determine sign
var sign: T;
if (code == CharCode.MINUS) {
if (!--len) {
return <T>NaN;
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = -1;
} else if (code == CharCode.PLUS) {
if (!--len) {
return <T>NaN;
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = 1;
} else {
sign = 1;
}
// determine radix
if (!radix) {
if (code == CharCode._0 && len > 2) {
switch (<i32>load<u16>(ptr + 2, HEADER_SIZE)) {
case CharCode.B:
case CharCode.b: {
ptr += 4; len -= 2;
radix = 2;
break;
}
case CharCode.O:
case CharCode.o: {
ptr += 4; len -= 2;
radix = 8;
break;
}
case CharCode.X:
case CharCode.x: {
ptr += 4; len -= 2;
radix = 16;
break;
}
default: {
radix = 10;
}
}
} else radix = 10;
} else if (radix < 2 || radix > 36) {
return <T>NaN;
}
// calculate value
var num: T = 0;
while (len--) {
code = <i32>load<u16>(ptr, HEADER_SIZE);
if (code >= CharCode._0 && code <= CharCode._9) {
code -= CharCode._0;
} else if (code >= CharCode.A && code <= CharCode.Z) {
code -= CharCode.A - 10;
} else if (code >= CharCode.a && code <= CharCode.z) {
code -= CharCode.a - 10;
} else {
break;
}
if (code >= radix) {
break;
}
num = (num * radix) + code;
ptr += 2;
}
return sign * num;
}

View File

@ -0,0 +1,34 @@
import {
MAX_BLENGTH,
allocate
// ArrayBufferView
} from "./arraybuffer";
/** Typed array base class. Not a global object. */
export abstract class TypedArray<T> /* implements ArrayBufferView<T> */ {
readonly buffer: ArrayBuffer;
readonly byteOffset: i32;
readonly byteLength: i32;
constructor(length: i32) {
const MAX_LENGTH = <u32>MAX_BLENGTH / sizeof<T>();
if (<u32>length > MAX_LENGTH) throw new RangeError("Invalid typed array length");
var byteLength = length << alignof<T>();
this.buffer = allocate(byteLength);
this.byteOffset = 0;
this.byteLength = byteLength;
}
get length(): i32 {
return this.byteLength >> alignof<T>();
}
// @operator("[]") - maybe injected through ArrayBufferView?
// @operator("[]=") - maybe injected through ArrayBufferView?
// copyWithin(target: i32, start: i32, end: i32 = 0x7fffffff): TypedArray<T>
// subarray(begin: i32 = 0, end: i32 = 0x7fffffff): TypedArray<T>
}

View File

@ -1,20 +1,17 @@
// singleton empty string
const EMPTY: String = changetype<String>("");
// number of bytes preceeding string data
const HEADER_SIZE: usize = 4;
function allocate(length: i32): String {
assert(length > 0); // 0 -> EMPTY
var ptr = allocate_memory(HEADER_SIZE + (<usize>length << 1));
store<i32>(ptr, length);
return changetype<String>(ptr);
}
import {
HEADER_SIZE,
MAX_LENGTH,
EMPTY,
allocate,
isWhiteSpaceOrLineTerminator,
CharCode,
parse
} from "./internal/string";
@sealed
export class String {
readonly length: i32; // capped to [0, 0x7fffffff]
readonly length: i32; // capped to [0, MAX_LENGTH]
@operator("[]")
charAt(pos: i32): String {
@ -97,7 +94,7 @@ export class String {
return out;
}
endsWith(searchString: String, endPosition: i32 = 0x7fffffff): bool {
endsWith(searchString: String, endPosition: i32 = MAX_LENGTH): bool {
assert(this !== null);
if (searchString === null) return false;
var end: isize = <isize>min(max(endPosition, 0), this.length);
@ -382,52 +379,6 @@ 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;
}
}
const enum CharCode {
PLUS = 0x2B,
MINUS = 0x2D,
DOT = 0x2E,
_0 = 0x30,
_1 = 0x31,
_2 = 0x32,
_3 = 0x33,
_4 = 0x34,
_5 = 0x35,
_6 = 0x36,
_7 = 0x37,
_8 = 0x38,
_9 = 0x39,
A = 0x41,
B = 0x42,
E = 0x45,
O = 0x4F,
X = 0x58,
Z = 0x5a,
a = 0x61,
b = 0x62,
e = 0x65,
o = 0x6F,
x = 0x78,
z = 0x7A
}
export function parseInt(str: String, radix: i32 = 0): f64 {
return parse<f64>(str, radix);
}
@ -440,85 +391,6 @@ export function parseI64(str: String, radix: i32 = 0): i64 {
return parse<i64>(str, radix);
}
function parse<T>(str: String, radix: i32 = 0): T {
var len: i32 = str.length;
if (!len) {
return <T>NaN;
}
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEADER_SIZE);
// determine sign
var sign: T;
if (code == CharCode.MINUS) {
if (!--len) {
return <T>NaN;
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = -1;
} else if (code == CharCode.PLUS) {
if (!--len) {
return <T>NaN;
}
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = 1;
} else {
sign = 1;
}
// determine radix
if (!radix) {
if (code == CharCode._0 && len > 2) {
switch (<i32>load<u16>(ptr + 2, HEADER_SIZE)) {
case CharCode.B:
case CharCode.b: {
ptr += 4; len -= 2;
radix = 2;
break;
}
case CharCode.O:
case CharCode.o: {
ptr += 4; len -= 2;
radix = 8;
break;
}
case CharCode.X:
case CharCode.x: {
ptr += 4; len -= 2;
radix = 16;
break;
}
default: {
radix = 10;
}
}
} else radix = 10;
} else if (radix < 2 || radix > 36) {
return <T>NaN;
}
// calculate value
var num: T = 0;
while (len--) {
code = <i32>load<u16>(ptr, HEADER_SIZE);
if (code >= CharCode._0 && code <= CharCode._9) {
code -= CharCode._0;
} else if (code >= CharCode.A && code <= CharCode.Z) {
code -= CharCode.A - 10;
} else if (code >= CharCode.a && code <= CharCode.z) {
code -= CharCode.a - 10;
} else {
break;
}
if (code >= radix) {
break;
}
num = (num * radix) + code;
ptr += 2;
}
return sign * num;
}
// FIXME: naive implementation
export function parseFloat(str: String): f64 {
var len: i32 = str.length;

View File

@ -1,67 +1,43 @@
/** @internal */
abstract class TypedArray<T> /* implements ArrayBufferView<T> */ {
import {
TypedArray
} from "./internal/typedarray";
readonly buffer: ArrayBuffer;
readonly byteOffset: i32;
readonly byteLength: i32;
get length(): i32 { return this.byteLength >> alignof<T>(); }
constructor(length: i32) {
const maxLength = <u32>0x7fffffff >> alignof<T>();
if (<u32>length > maxLength) throw new RangeError("Invalid typed array length");
var byteLength = length << alignof<T>();
this.buffer = new ArrayBuffer(byteLength);
this.byteOffset = 0;
this.byteLength = byteLength;
}
export class Int8Array extends TypedArray<i8> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
}
// export class Int8Array extends TypedArray<i8> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
// static readonly name: string = "Int8Array";
// }
export class Uint8Array extends TypedArray<u8> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
}
// export class Uint8Array extends TypedArray<u8> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
// static readonly name: string = "Uint8Array";
// }
export class Int16Array extends TypedArray<i16> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();
}
// export class Int16Array extends TypedArray<i16> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();
// static readonly name: string = "Int16Array";
// }
export class Uint16Array extends TypedArray<u16> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>();
}
// export class Uint16Array extends TypedArray<u16> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>();
// static readonly name: string = "Uint16Array";
// }
export class Int32Array extends TypedArray<i32> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i32>();
}
// export class Int32Array extends TypedArray<i32> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i32>();
// static readonly name: string = "Int32Array";
// }
export class Uint32Array extends TypedArray<u32> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u32>();
}
// export class Uint32Array extends TypedArray<u32> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u32>();
// static readonly name: string = "Uint32Array";
// }
export class Int64Array extends TypedArray<i64> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i64>();
}
// export class Int64Array extends TypedArray<i64> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i64>();
// static readonly name: string = "Int64Array";
// }
export class Uint64Array extends TypedArray<u64> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u64>();
}
// export class Uint64Array extends TypedArray<u64> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u64>();
// static readonly name: string = "Uint64Array";
// }
export class Float32Array extends TypedArray<f32> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<f32>();
}
// export class Float32Array extends TypedArray<f32> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<f32>();
// static readonly name: string = "Float32Array";
// }
// export class Float64Array extends TypedArray<f64> {
// static readonly BYTES_PER_ELEMENT: usize = sizeof<f64>();
// static readonly name: string = "Float64Array";
// }
export class Float64Array extends TypedArray<f64> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<f64>();
}