mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-14 07:21:30 +00:00
Polyfill move_memory and set_memory and remove Heap
This commit is contained in:
40
std/assembly.d.ts
vendored
40
std/assembly.d.ts
vendored
@ -162,6 +162,16 @@ declare function store<T>(offset: usize, value: T): void;
|
||||
declare function current_memory(): i32;
|
||||
/** Grows linear memory by a given unsigned delta of pages. One page is 64kb. Returns the previous memory size in units of pages or `-1` on failure. */
|
||||
declare function grow_memory(value: i32): i32;
|
||||
/** Copies n bytes from the specified source to the specified destination in memory. These regions may overlap. */
|
||||
declare function move_memory(destination: usize, source: usize, n: usize): void;
|
||||
/** Sets n bytes beginning at the specified destination in memory to the specified byte value. */
|
||||
declare function set_memory(destination: usize, value: u8, count: usize): void;
|
||||
/** Compares two chunks of memory. Returns `0` if equal, otherwise the difference of the first differing bytes. */
|
||||
declare function compare_memory(vl: usize, vr: usize, n: usize): i32;
|
||||
/** Allocates a chunk of memory of the specified size and returns a pointer to it. */
|
||||
declare function allocate_memory(size: usize): usize;
|
||||
/** Disposes a chunk of memory by its pointer. */
|
||||
declare function free_memory(ptr: usize): void;
|
||||
/** Emits an unreachable operation that results in a runtime error when executed. Both a statement and an expression of any type. */
|
||||
declare function unreachable(): any; // sic
|
||||
|
||||
@ -250,36 +260,6 @@ declare class Error {
|
||||
/** Class for indicating an error when a value is not in the set or range of allowed values. */
|
||||
declare class RangeError extends Error { }
|
||||
|
||||
/** A static class representing the heap. */
|
||||
declare class Heap {
|
||||
|
||||
/** Gets the amount of used heap space, in bytes. */
|
||||
static readonly used: usize;
|
||||
|
||||
/** Gets the amount of free heap space, in bytes. */
|
||||
static readonly free: usize;
|
||||
|
||||
/** Gets the size of the heap, in bytes. */
|
||||
static readonly size: usize;
|
||||
|
||||
/** Allocates a chunk of memory and returns a pointer to it. */
|
||||
static allocate(size: usize): usize;
|
||||
|
||||
/** Disposes a chunk of memory by its pointer. */
|
||||
static dispose(ptr: usize): void;
|
||||
|
||||
/** Copies a chunk of memory from one location to another. */
|
||||
static copy(dest: usize, src: usize, n: usize): usize;
|
||||
|
||||
/** Fills a chunk of memory with the specified byte value. */
|
||||
static fill(dest: usize, c: u8, n: usize): usize;
|
||||
|
||||
/** Compares two chunks of memory. Returns `0` if equal, otherwise the difference of the first differing bytes. */
|
||||
static compare(vl: usize, vr: usize, n: usize): i32;
|
||||
|
||||
private constructor();
|
||||
}
|
||||
|
||||
interface Boolean {}
|
||||
interface Function {}
|
||||
interface IArguments {}
|
||||
|
@ -8,7 +8,7 @@ export class Array<T> {
|
||||
if (capacity < 0)
|
||||
throw new RangeError("invalid array length");
|
||||
this.__capacity = this.length = capacity;
|
||||
this.__memory = capacity > 0 ? Heap.allocate(<usize>capacity * sizeof<T>()) : 0;
|
||||
this.__memory = capacity > 0 ? allocate_memory(<usize>capacity * sizeof<T>()) : 0;
|
||||
}
|
||||
|
||||
@operator("[]")
|
||||
@ -34,19 +34,20 @@ export class Array<T> {
|
||||
return -1;
|
||||
}
|
||||
|
||||
private __grow(capacity: i32): void {
|
||||
assert(capacity > this.__capacity);
|
||||
var newMemory = Heap.allocate(<usize>(capacity * sizeof<T>()));
|
||||
if (this.__memory)
|
||||
Heap.copy(newMemory, this.__memory, this.__capacity * sizeof<T>());
|
||||
Heap.dispose(this.__memory);
|
||||
private __grow(newCapacity: i32): void {
|
||||
assert(newCapacity > this.__capacity);
|
||||
var newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
|
||||
if (this.__memory) {
|
||||
move_memory(newMemory, this.__memory, this.__capacity * sizeof<T>());
|
||||
free_memory(this.__memory);
|
||||
}
|
||||
this.__memory = newMemory;
|
||||
this.__capacity = capacity;
|
||||
this.__capacity = newCapacity;
|
||||
}
|
||||
|
||||
push(element: T): i32 {
|
||||
if (<u32>this.length >= this.__capacity)
|
||||
this.__grow(max(this.length + 1, this.__capacity * 2));
|
||||
this.__grow(max(this.length + 1, this.__capacity << 1));
|
||||
store<T>(this.__memory + <usize>this.length * sizeof<T>(), element);
|
||||
return ++this.length;
|
||||
}
|
||||
@ -62,23 +63,22 @@ export class Array<T> {
|
||||
if (this.length < 1 || <u32>this.length > this.__capacity)
|
||||
throw new RangeError("index out of range");
|
||||
var element = load<T>(this.__memory);
|
||||
Heap.copy(this.__memory, this.__memory + sizeof<T>(), (this.__capacity - 1) * sizeof<T>());
|
||||
Heap.fill(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;
|
||||
}
|
||||
|
||||
unshift(element: T): i32 {
|
||||
var capacity = this.__capacity;
|
||||
if (<u32>this.length >= capacity)
|
||||
this.__grow(max(this.length + 1, capacity * 2));
|
||||
var oldCapacity = this.__capacity;
|
||||
if (<u32>this.length >= oldCapacity)
|
||||
this.__grow(max(this.length + 1, oldCapacity * 2));
|
||||
|
||||
// FIXME: needs memmove (Heap.copy is just memcpy). it's also inefficient because
|
||||
// __grow copies and then unshift copies again.
|
||||
// Heap.copy(this.__memory + sizeof<T>(), this.__memory, capacity * sizeof<T>());
|
||||
// FIXME: this is inefficient because of two copies, one in __grow and one here
|
||||
// move_memory(this.__memory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
|
||||
|
||||
if (capacity)
|
||||
for (var index: usize = capacity; index > 0; --index)
|
||||
if (oldCapacity)
|
||||
for (var index: usize = oldCapacity; index > 0; --index)
|
||||
store<T>(this.__memory + index * sizeof<T>(), load<T>(this.__memory + (index - 1) * sizeof<T>()));
|
||||
|
||||
store<T>(this.__memory, element);
|
||||
@ -92,12 +92,16 @@ export class CArray<T> {
|
||||
private constructor() {}
|
||||
|
||||
@operator("[]")
|
||||
private __get(index: usize): T {
|
||||
return load<T>(changetype<usize>(this) + index * sizeof<T>());
|
||||
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: usize, value: T): void {
|
||||
store<T>(changetype<usize>(this) + index * sizeof<T>(), value);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -4,245 +4,289 @@ const ALIGN_MASK: usize = ALIGN_SIZE - 1;
|
||||
|
||||
var HEAP_OFFSET: usize = HEAP_BASE; // HEAP_BASE is a constant generated by the compiler
|
||||
|
||||
// TODO: maybe tlsf
|
||||
export function allocate_memory(size: usize): usize {
|
||||
if (!size) return 0;
|
||||
var len: i32 = current_memory();
|
||||
if (HEAP_OFFSET + size > <usize>len << 16)
|
||||
if(grow_memory(max<i32>(<i32>ceil<f64>(<f64>size / 65536), len * 2 - len)) < 0)
|
||||
unreachable();
|
||||
var ptr: usize = HEAP_OFFSET;
|
||||
if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset
|
||||
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
export class Heap {
|
||||
export function free_memory(ptr: usize): void {
|
||||
// just a big chunk of non-disposable memory for now
|
||||
}
|
||||
|
||||
static get used(): usize { return HEAP_OFFSET - HEAP_BASE; }
|
||||
static get free(): usize { return (<usize>current_memory() << 16) - HEAP_OFFSET; }
|
||||
static get size(): usize { return (<usize>current_memory() << 16) - HEAP_BASE; }
|
||||
function copy_memory(dest: usize, src: usize, n: usize): void {
|
||||
// based on musl's implementation of memcpy
|
||||
// not a future instruction and sufficiently covered by the upcoming move_memory intrinsic
|
||||
|
||||
static allocate(size: usize): usize {
|
||||
if (!size) return 0;
|
||||
var len: i32 = current_memory();
|
||||
if (HEAP_OFFSET + size > <usize>len << 16)
|
||||
if(grow_memory(max<i32>(<i32>ceil<f64>(<f64>size / 65536), len * 2 - len)) < 0)
|
||||
unreachable();
|
||||
var ptr: usize = HEAP_OFFSET;
|
||||
if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset
|
||||
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
|
||||
return ptr;
|
||||
var w: u32, x: u32;
|
||||
|
||||
// copy 1 byte each until src is aligned to 4 bytes
|
||||
while (n && src % 4) {
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
n--;
|
||||
}
|
||||
|
||||
static dispose(ptr: usize): void {
|
||||
// just a big chunk of non-disposable memory for now
|
||||
}
|
||||
|
||||
static copy(dest: usize, src: usize, n: usize): usize { // TODO: use move_memory op once available
|
||||
assert(dest >= HEAP_BASE);
|
||||
|
||||
// the following is based on musl's implementation of memcpy
|
||||
var dst: usize = dest;
|
||||
var w: u32, x: u32;
|
||||
|
||||
// copy 1 byte each until src is aligned to 4 bytes
|
||||
while (n && src % 4) {
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
n--;
|
||||
}
|
||||
|
||||
// if dst is aligned to 4 bytes as well, copy 4 bytes each
|
||||
if (dst % 4 == 0) {
|
||||
while (n >= 16) {
|
||||
store<u32>(dst , load<u32>(src ));
|
||||
store<u32>(dst + 4, load<u32>(src + 4));
|
||||
store<u32>(dst + 8, load<u32>(src + 8));
|
||||
store<u32>(dst + 12, load<u32>(src + 12));
|
||||
src += 16; dst += 16; n -= 16;
|
||||
}
|
||||
if (n & 8) {
|
||||
store<u32>(dst , load<u32>(src ));
|
||||
store<u32>(dst + 4, load<u32>(src + 4));
|
||||
dst += 8; src += 8;
|
||||
}
|
||||
if (n & 4) {
|
||||
store<u32>(dst, load<u32>(src));
|
||||
dst += 4; src += 4;
|
||||
}
|
||||
if (n & 2) { // drop to 2 bytes each
|
||||
store<u16>(dst, load<u16>(src));
|
||||
dst += 2; src += 2;
|
||||
}
|
||||
if (n & 1) { // drop to 1 byte
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
// if dst is not aligned to 4 bytes, use alternating shifts to copy 4 bytes each
|
||||
// doing shifts if faster when copying enough bytes (here: 32 or more)
|
||||
if (n >= 32) {
|
||||
switch (dst % 4) {
|
||||
// known to be != 0
|
||||
case 1:
|
||||
w = load<u32>(src);
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
n -= 3;
|
||||
while (n >= 17) {
|
||||
x = load<u32>(src + 1);
|
||||
store<u32>(dst, w >> 24 | x << 8);
|
||||
w = load<u32>(src + 5);
|
||||
store<u32>(dst + 4, x >> 24 | w << 8);
|
||||
x = load<u32>(src + 9);
|
||||
store<u32>(dst + 8, w >> 24 | x << 8);
|
||||
w = load<u32>(src + 13);
|
||||
store<u32>(dst + 12, x >> 24 | w << 8);
|
||||
src += 16; dst += 16; n -= 16;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
w = load<u32>(src);
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
n -= 2;
|
||||
while (n >= 18) {
|
||||
x = load<u32>(src + 2);
|
||||
store<u32>(dst, w >> 16 | x << 16);
|
||||
w = load<u32>(src + 6);
|
||||
store<u32>(dst + 4, x >> 16 | w << 16);
|
||||
x = load<u32>(src + 10);
|
||||
store<u32>(dst + 8, w >> 16 | x << 16);
|
||||
w = load<u32>(src + 14);
|
||||
store<u32>(dst + 12, x >> 16 | w << 16);
|
||||
src += 16; dst += 16; n -= 16;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
w = load<u32>(src);
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
n -= 1;
|
||||
while (n >= 19) {
|
||||
x = load<u32>(src + 3);
|
||||
store<u32>(dst, w >> 8 | x << 24);
|
||||
w = load<u32>(src + 7);
|
||||
store<u32>(dst + 4, x >> 8 | w << 24);
|
||||
x = load<u32>(src + 11);
|
||||
store<u32>(dst + 8, w >> 8 | x << 24);
|
||||
w = load<u32>(src + 15);
|
||||
store<u32>(dst + 12, x >> 8 | w << 24);
|
||||
src += 16; dst += 16; n -= 16;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// copy remaining bytes one by one
|
||||
if (n & 16) {
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
// if dst is aligned to 4 bytes as well, copy 4 bytes each
|
||||
if (dest % 4 == 0) {
|
||||
while (n >= 16) {
|
||||
store<u32>(dest , load<u32>(src ));
|
||||
store<u32>(dest + 4, load<u32>(src + 4));
|
||||
store<u32>(dest + 8, load<u32>(src + 8));
|
||||
store<u32>(dest + 12, load<u32>(src + 12));
|
||||
src += 16; dest += 16; n -= 16;
|
||||
}
|
||||
if (n & 8) {
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u32>(dest , load<u32>(src ));
|
||||
store<u32>(dest + 4, load<u32>(src + 4));
|
||||
dest += 8; src += 8;
|
||||
}
|
||||
if (n & 4) {
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u32>(dest, load<u32>(src));
|
||||
dest += 4; src += 4;
|
||||
}
|
||||
if (n & 2) {
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
if (n & 2) { // drop to 2 bytes each
|
||||
store<u16>(dest, load<u16>(src));
|
||||
dest += 2; src += 2;
|
||||
}
|
||||
if (n & 1) {
|
||||
store<u8>(dst++, load<u8>(src++));
|
||||
if (n & 1) { // drop to 1 byte
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
}
|
||||
return dest;
|
||||
return;
|
||||
}
|
||||
|
||||
static fill(dest: usize, c: u8, n: usize): usize { // TODO: use set_memory op once available
|
||||
assert(dest >= HEAP_BASE);
|
||||
|
||||
// the following is based on musl's implementation of memset
|
||||
if (!n) return dest;
|
||||
|
||||
var s: usize = dest;
|
||||
|
||||
// Fill head and tail with minimal branching
|
||||
store<u8>(s, c); store<u8>(s + n - 1, c);
|
||||
if (n <= 2) return dest;
|
||||
store<u8>(s + 1, c); store<u8>(s + n - 2, c);
|
||||
store<u8>(s + 2, c); store<u8>(s + n - 3, c);
|
||||
if (n <= 6) return dest;
|
||||
store<u8>(s + 3, c); store<u8>(s + n - 4, c);
|
||||
if (n <= 8) return dest;
|
||||
|
||||
// Align to 4 bytes
|
||||
var k: usize = -s & 3;
|
||||
s += k;
|
||||
n -= k;
|
||||
n &= -4;
|
||||
|
||||
var c32: u32 = -1 / 255 * c;
|
||||
|
||||
// Fill head and tail in preparation of setting 32 bytes at a time
|
||||
store<u32>(s, c32);
|
||||
store<u32>(s + n - 4, c32);
|
||||
if (n <= 8) return dest;
|
||||
store<u32>(s + 4, c32);
|
||||
store<u32>(s + 8, c32);
|
||||
store<u32>(s + n - 12, c32);
|
||||
store<u32>(s + n - 8, c32);
|
||||
if (n <= 24) return dest;
|
||||
store<u32>(s + 12, c32);
|
||||
store<u32>(s + 16, c32);
|
||||
store<u32>(s + 20, c32);
|
||||
store<u32>(s + 24, c32);
|
||||
store<u32>(s + n - 28, c32);
|
||||
store<u32>(s + n - 24, c32);
|
||||
store<u32>(s + n - 20, c32);
|
||||
store<u32>(s + n - 16, c32);
|
||||
|
||||
// Align to 8 bytes
|
||||
k = 24 + (s & 4);
|
||||
s += k;
|
||||
n -= k;
|
||||
|
||||
// Set 32 bytes at a time
|
||||
var c64: u64 = <u64>c32 | (<u64>c32 << 32);
|
||||
while (n >= 32) {
|
||||
store<u64>(s, c64);
|
||||
store<u64>(s + 8, c64);
|
||||
store<u64>(s + 16, c64);
|
||||
store<u64>(s + 24, c64);
|
||||
n -= 32; s += 32;
|
||||
// if dst is not aligned to 4 bytes, use alternating shifts to copy 4 bytes each
|
||||
// doing shifts if faster when copying enough bytes (here: 32 or more)
|
||||
if (n >= 32) {
|
||||
switch (dest % 4) {
|
||||
// known to be != 0
|
||||
case 1:
|
||||
w = load<u32>(src);
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
n -= 3;
|
||||
while (n >= 17) {
|
||||
x = load<u32>(src + 1);
|
||||
store<u32>(dest, w >> 24 | x << 8);
|
||||
w = load<u32>(src + 5);
|
||||
store<u32>(dest + 4, x >> 24 | w << 8);
|
||||
x = load<u32>(src + 9);
|
||||
store<u32>(dest + 8, w >> 24 | x << 8);
|
||||
w = load<u32>(src + 13);
|
||||
store<u32>(dest + 12, x >> 24 | w << 8);
|
||||
src += 16; dest += 16; n -= 16;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
w = load<u32>(src);
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
n -= 2;
|
||||
while (n >= 18) {
|
||||
x = load<u32>(src + 2);
|
||||
store<u32>(dest, w >> 16 | x << 16);
|
||||
w = load<u32>(src + 6);
|
||||
store<u32>(dest + 4, x >> 16 | w << 16);
|
||||
x = load<u32>(src + 10);
|
||||
store<u32>(dest + 8, w >> 16 | x << 16);
|
||||
w = load<u32>(src + 14);
|
||||
store<u32>(dest + 12, x >> 16 | w << 16);
|
||||
src += 16; dest += 16; n -= 16;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
w = load<u32>(src);
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
n -= 1;
|
||||
while (n >= 19) {
|
||||
x = load<u32>(src + 3);
|
||||
store<u32>(dest, w >> 8 | x << 24);
|
||||
w = load<u32>(src + 7);
|
||||
store<u32>(dest + 4, x >> 8 | w << 24);
|
||||
x = load<u32>(src + 11);
|
||||
store<u32>(dest + 8, w >> 8 | x << 24);
|
||||
w = load<u32>(src + 15);
|
||||
store<u32>(dest + 12, x >> 8 | w << 24);
|
||||
src += 16; dest += 16; n -= 16;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
static compare(vl: usize, vr: usize, n: usize): i32 {
|
||||
if (vl == vr) return 0;
|
||||
|
||||
// the following is based on musl's implementation of memcmp
|
||||
while (n && load<u8>(vl) == load<u8>(vr)) {
|
||||
n--; vl++; vr++;
|
||||
}
|
||||
return n ? <i32>load<u8>(vl) - <i32>load<u8>(vr) : 0;
|
||||
// copy remaining bytes one by one
|
||||
if (n & 16) {
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
}
|
||||
if (n & 8) {
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
}
|
||||
if (n & 4) {
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
}
|
||||
if (n & 2) {
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
}
|
||||
if (n & 1) {
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
}
|
||||
|
||||
private constructor() {}
|
||||
}
|
||||
|
||||
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 (src + n <= dest || dest + n <= src) {
|
||||
copy_memory(dest, src, n);
|
||||
return;
|
||||
}
|
||||
if (dest < src) {
|
||||
if (src % 8 == dest % 8) {
|
||||
while (dest % 8) {
|
||||
if (!n)
|
||||
return;
|
||||
--n;
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
}
|
||||
while (n >= 8) {
|
||||
store<u64>(dest, load<u64>(src));
|
||||
n -= 8;
|
||||
dest += 8;
|
||||
src += 8;
|
||||
}
|
||||
}
|
||||
while (n) {
|
||||
store<u8>(dest++, load<u8>(src++));
|
||||
--n;
|
||||
}
|
||||
} else {
|
||||
if (src % 8 == dest % 8) {
|
||||
while ((dest + n) % 8) {
|
||||
if (!n)
|
||||
return;
|
||||
store<u8>(dest + --n, load<u8>(src + n));
|
||||
}
|
||||
while (n >= 8) {
|
||||
n -= 8;
|
||||
store<u64>(dest + n, load<u64>(src + n));
|
||||
}
|
||||
}
|
||||
while (n) {
|
||||
store<u8>(dest + --n, load<u8>(src + n));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function set_memory(dest: usize, c: u8, n: usize): void {
|
||||
// based on musl's implementation of memset
|
||||
// becomes obsolete once https://github.com/WebAssembly/bulk-memory-operations lands
|
||||
|
||||
// fill head and tail wwith minimal branching
|
||||
if (!n)
|
||||
return;
|
||||
store<u8>(dest, c);
|
||||
store<u8>(dest + n - 1, c);
|
||||
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;
|
||||
store<u8>(dest + 3, c);
|
||||
store<u8>(dest + n - 4, c);
|
||||
if (n <= 8)
|
||||
return;
|
||||
|
||||
// advance pointer to align it at 4-byte boundary
|
||||
var k: usize = -dest & 3;
|
||||
dest += k;
|
||||
n -= k;
|
||||
n &= -4;
|
||||
|
||||
var c32: u32 = -1 / 255 * c;
|
||||
|
||||
// fill head/tail up to 28 bytes each in preparation
|
||||
store<u32>(dest, c32);
|
||||
store<u32>(dest + n - 4, c32);
|
||||
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;
|
||||
store<u32>(dest + 12, c32);
|
||||
store<u32>(dest + 16, c32);
|
||||
store<u32>(dest + 20, c32);
|
||||
store<u32>(dest + 24, c32);
|
||||
store<u32>(dest + n - 28, c32);
|
||||
store<u32>(dest + n - 24, c32);
|
||||
store<u32>(dest + n - 20, c32);
|
||||
store<u32>(dest + n - 16, c32);
|
||||
|
||||
// align to a multiple of 8
|
||||
k = 24 + (dest & 4);
|
||||
dest += k;
|
||||
n -= k;
|
||||
|
||||
// copy 32 bytes each
|
||||
var c64: u64 = <u64>c32 | (<u64>c32 << 32);
|
||||
while (n >= 32) {
|
||||
store<u64>(dest, c64);
|
||||
store<u64>(dest + 8, c64);
|
||||
store<u64>(dest + 16, c64);
|
||||
store<u64>(dest + 24, c64);
|
||||
n -= 32;
|
||||
dest += 32;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
while (n && load<u8>(vl) == load<u8>(vr)) {
|
||||
n--;
|
||||
vl++;
|
||||
vr++;
|
||||
}
|
||||
return n ? <i32>load<u8>(vl) - <i32>load<u8>(vr) : 0;
|
||||
}
|
||||
|
@ -1,38 +1,37 @@
|
||||
const EMPTY: String = changetype<String>("");
|
||||
|
||||
export class String {
|
||||
// [key: number]: string;
|
||||
|
||||
private ptr: usize;
|
||||
|
||||
private __memory: usize;
|
||||
readonly length: i32;
|
||||
|
||||
constructor(ptr: usize, lenght: i32) {
|
||||
this.ptr = ptr;
|
||||
this.length = lenght;
|
||||
constructor(ptr: usize, length: i32) {
|
||||
if (length < 0)
|
||||
throw new RangeError("invalid length");
|
||||
this.__memory = ptr;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
@operator("[]")
|
||||
charAt(pos: i32): String {
|
||||
assert(this != null);
|
||||
return pos < 0 || pos >= this.length ? EMPTY
|
||||
: new String(this.ptr + (<usize>pos << 1), 1);
|
||||
if (<u32>pos >= this.length)
|
||||
return changetype<String>("");
|
||||
return new String(this.__memory + (<usize>pos << 1), 1);
|
||||
}
|
||||
|
||||
charCodeAt(pos: i32): i32 {
|
||||
assert(this != null);
|
||||
return pos < 0 || pos >= this.length ? -1 // NaN
|
||||
: load<u16>(this.ptr + (<usize>pos << 1));
|
||||
if (<u32>pos >= this.length)
|
||||
return -1; // NaN
|
||||
return load<u16>(this.__memory + (<usize>pos << 1));
|
||||
}
|
||||
|
||||
codePointAt(pos: i32): i32 {
|
||||
assert(this != null);
|
||||
if (pos < 0 || pos >= this.length)
|
||||
if (<u32>pos >= this.length)
|
||||
return -1; // undefined
|
||||
var first = <i32>load<u16>(this.ptr + (<usize>pos << 1));
|
||||
var first = <i32>load<u16>(this.__memory + (<usize>pos << 1));
|
||||
if (first < 0xD800 || first > 0xDBFF || pos + 1 == this.length)
|
||||
return first;
|
||||
var second = <i32>load<u16>(this.ptr + ((<usize>pos + 1) << 1));
|
||||
var second = <i32>load<u16>(this.__memory + ((<usize>pos + 1) << 1));
|
||||
if (second < 0xDC00 || second > 0xDFFF)
|
||||
return first;
|
||||
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
|
||||
@ -45,37 +44,31 @@ export class String {
|
||||
var thisLen: isize = this.length;
|
||||
var otherLen: isize = other.length;
|
||||
var len: usize = thisLen + otherLen;
|
||||
return new String(
|
||||
Heap.copy(
|
||||
Heap.copy(
|
||||
Heap.allocate(len << 1),
|
||||
this.ptr,
|
||||
thisLen << 1
|
||||
) + (thisLen << 1),
|
||||
other.ptr,
|
||||
otherLen << 1
|
||||
),
|
||||
<i32>len
|
||||
);
|
||||
var newMemory = allocate_memory(len << 1);
|
||||
move_memory(newMemory, this.__memory, thisLen << 1);
|
||||
move_memory(newMemory + (thisLen << 1), other.__memory, otherLen << 1);
|
||||
return new String(newMemory, <i32>len);
|
||||
}
|
||||
|
||||
endsWith(searchString: this, endPosition: i32 = 0x7fffffff): bool {
|
||||
assert(this != null);
|
||||
assert(searchString != null);
|
||||
var end: isize = <isize>min<i32>(max<i32>(endPosition, 0), this.length);
|
||||
var searchLength: isize = searchString.length;
|
||||
var start: isize = end - searchLength;
|
||||
if (start < 0)
|
||||
return false;
|
||||
return !Heap.compare(this.ptr + (start << 1), searchString.ptr, searchLength << 1);
|
||||
return !compare_memory(this.__memory + (start << 1), searchString.__memory, searchLength << 1);
|
||||
}
|
||||
|
||||
@operator("==")
|
||||
private __eq(other: this): bool {
|
||||
assert(this != null);
|
||||
assert(other != null);
|
||||
return this.length != other.length ? false
|
||||
: !Heap.compare(this.ptr, other.ptr, <usize>this.length);
|
||||
if (this == null)
|
||||
return other == null;
|
||||
else if (other == null)
|
||||
return false;
|
||||
if (this.length != other.length)
|
||||
return false;
|
||||
return !compare_memory(this.__memory, other.__memory, <usize>this.length);
|
||||
}
|
||||
|
||||
includes(searchString: this, position: i32 = 0): bool {
|
||||
@ -83,14 +76,13 @@ export class String {
|
||||
}
|
||||
|
||||
indexOf(searchString: this, position: i32 = 0): i32 {
|
||||
assert(this != null);
|
||||
assert(searchString != null);
|
||||
var pos: isize = position;
|
||||
var len: isize = this.length;
|
||||
var start: isize = min<isize>(max<isize>(pos, 0), len);
|
||||
var searchLen: isize = searchString.length;
|
||||
for (var k: usize = start; <isize>k + searchLen <= len; ++k)
|
||||
if (!Heap.compare(this.ptr + (k << 1), searchString.ptr, searchLen << 1))
|
||||
if (!compare_memory(this.__memory + (k << 1), searchString.__memory, searchLen << 1))
|
||||
return <i32>k;
|
||||
return -1;
|
||||
}
|
||||
@ -104,7 +96,7 @@ export class String {
|
||||
var searchLength: isize = searchString.length;
|
||||
if (searchLength + start > len)
|
||||
return false;
|
||||
return !Heap.compare(this.ptr + (start << 1), searchString.ptr, searchLength << 1);
|
||||
return !compare_memory(this.__memory + (start << 1), searchString.__memory, searchLength << 1);
|
||||
}
|
||||
|
||||
substr(start: i32, length: i32 = i32.MAX_VALUE): String {
|
||||
@ -117,7 +109,7 @@ export class String {
|
||||
var resultLength: isize = min<isize>(max<isize>(end, 0), size - intStart);
|
||||
if (resultLength < 0)
|
||||
return EMPTY;
|
||||
return new String(this.ptr + (intStart << 1), <i32>resultLength);
|
||||
return new String(this.__memory + (intStart << 1), <i32>resultLength);
|
||||
}
|
||||
|
||||
substring(start: i32, end: i32 = i32.MAX_VALUE): String {
|
||||
@ -132,46 +124,46 @@ export class String {
|
||||
return EMPTY;
|
||||
if (!from && to == this.length)
|
||||
return this;
|
||||
return new String(this.ptr + (from << 1), len);
|
||||
return new String(this.__memory + (from << 1), len);
|
||||
}
|
||||
|
||||
trim(): String {
|
||||
assert(this != null);
|
||||
var length: usize = this.length;
|
||||
while (length && isWhiteSpaceOrLineTerminator(load<u16>(this.ptr + (length << 1))))
|
||||
while (length && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (length << 1))))
|
||||
--length;
|
||||
var start: usize = 0;
|
||||
while (start < length && isWhiteSpaceOrLineTerminator(load<u16>(this.ptr + (start << 1)))) {
|
||||
while (start < length && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (start << 1)))) {
|
||||
++start; --length;
|
||||
}
|
||||
if (!length)
|
||||
return EMPTY;
|
||||
if (!start && length == this.length)
|
||||
return this;
|
||||
return new String(this.ptr + (start << 1), length);
|
||||
return new String(this.__memory + (start << 1), length);
|
||||
}
|
||||
|
||||
trimLeft(): String {
|
||||
assert(this != null);
|
||||
var start: isize = 0;
|
||||
var len: isize = this.length;
|
||||
while (start < len && isWhiteSpaceOrLineTerminator(load<u16>(this.ptr + (start << 1))))
|
||||
while (start < len && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (start << 1))))
|
||||
++start;
|
||||
if (!start)
|
||||
return this;
|
||||
return new String(this.ptr + (start << 1), <i32>(len - start));
|
||||
return new String(this.__memory + (start << 1), <i32>(len - start));
|
||||
}
|
||||
|
||||
trimRight(): String {
|
||||
assert(this != null);
|
||||
var len: isize = this.length;
|
||||
while (len > 0 && isWhiteSpaceOrLineTerminator(load<u16>(this.ptr + (len << 1))))
|
||||
while (len > 0 && isWhiteSpaceOrLineTerminator(load<u16>(this.__memory + (len << 1))))
|
||||
--len;
|
||||
if (len <= 0)
|
||||
return EMPTY;
|
||||
if (<i32>len == this.length)
|
||||
return this;
|
||||
return new String(this.ptr, <i32>len);
|
||||
return new String(this.__memory, <i32>len);
|
||||
}
|
||||
}
|
||||
|
||||
|
6
std/portable.d.ts
vendored
6
std/portable.d.ts
vendored
@ -116,6 +116,12 @@ declare function select<T>(ifTrue: T, ifFalse: T, condition: bool): T;
|
||||
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;
|
||||
/** Allocates a chunk of memory of the specified size and returns a pointer to it. */
|
||||
declare function allocate_memory(size: usize): usize;
|
||||
/** Disposes a chunk of memory by its pointer. */
|
||||
declare function free_memory(ptr: usize): void;
|
||||
/** Copies n bytes from the specified source to the specified destination in memory. These regions may overlap. */
|
||||
declare function move_memory(destination: usize, source: usize, n: usize): void;
|
||||
/** Loads a value of the specified type from memory. Type must be `u8`. */
|
||||
declare function load<T = u8>(offset: usize): T;
|
||||
/** Stores a value of the specified type to memory. Type must be `u8`. */
|
||||
|
@ -15,7 +15,6 @@
|
||||
"files": [
|
||||
"./portable.d.ts",
|
||||
"./portable.js",
|
||||
"./portable/heap.d.ts",
|
||||
"./portable/heap.js"
|
||||
]
|
||||
}
|
||||
|
21
std/portable/heap.d.ts
vendored
21
std/portable/heap.d.ts
vendored
@ -1,21 +0,0 @@
|
||||
/** A static class representing the heap. */
|
||||
declare class Heap {
|
||||
|
||||
/** Allocates a chunk of memory and returns a pointer to it. */
|
||||
static allocate(size: usize): usize;
|
||||
|
||||
/** Disposes a chunk of memory by its pointer. */
|
||||
static dispose(ptr: usize): void;
|
||||
|
||||
/** Gets the amount of used heap space, in bytes. */
|
||||
static readonly used: usize;
|
||||
|
||||
/** Gets the amount of free heap space, in bytes. */
|
||||
static readonly free: usize;
|
||||
|
||||
/** Gets the size of the heap, in bytes. */
|
||||
static readonly size: usize;
|
||||
|
||||
/** Copies a chunk of memory from one location to another. */
|
||||
static copy(dest: usize, src: usize, n: usize): usize;
|
||||
}
|
@ -3,29 +3,37 @@ var globalScope = typeof window !== "undefined" && window || typeof global !== "
|
||||
var HEAP = new Uint8Array(0);
|
||||
var HEAP_OFFSET = 0;
|
||||
|
||||
Object.defineProperties(globalScope["Heap"] = {
|
||||
allocate: function allocate(size) {
|
||||
if (!(size >>>= 0)) return 0;
|
||||
if (HEAP_OFFSET + size > HEAP.length) {
|
||||
var oldHeap = HEAP;
|
||||
HEAP = new Uint8Array(Math.max(65536, HEAP.length + size, HEAP.length * 2));
|
||||
HEAP.set(oldHeap);
|
||||
}
|
||||
var ptr = HEAP_OFFSET;
|
||||
if ((HEAP_OFFSET += size) & 7)
|
||||
HEAP_OFFSET = (HEAP_OFFSET | 7) + 1;
|
||||
return ptr;
|
||||
},
|
||||
dispose: function dispose() { },
|
||||
copy: function copy(dest, src, n) {
|
||||
HEAP.set(HEAP.subarray(src >>> 0, (src + n) >>> 0), dest >>> 0);
|
||||
return dest;
|
||||
globalScope["allocate_memory"] =
|
||||
function allocate_memory(size) {
|
||||
if (!(size >>>= 0))
|
||||
return 0;
|
||||
if (HEAP_OFFSET + size > HEAP.length) {
|
||||
var oldHeap = HEAP;
|
||||
HEAP = new Uint8Array(Math.max(65536, HEAP.length + size, HEAP.length * 2));
|
||||
HEAP.set(oldHeap);
|
||||
}
|
||||
}, {
|
||||
used: { get: function get_used() { return HEAP_OFFSET; } },
|
||||
free: { get: function get_free() { return HEAP.length - HEAP_OFFSET; } },
|
||||
size: { get: function get_size() { return HEAP.length; } }
|
||||
});
|
||||
var ptr = HEAP_OFFSET;
|
||||
if ((HEAP_OFFSET += size) & 7)
|
||||
HEAP_OFFSET = (HEAP_OFFSET | 7) + 1;
|
||||
return ptr;
|
||||
};
|
||||
|
||||
globalScope["store"] = function store(ptr, val) { HEAP[ptr] = val; };
|
||||
globalScope["load"] = function load(ptr) { return HEAP[ptr]; };
|
||||
globalScope["free_memory"] =
|
||||
function free_memory(ptr) {
|
||||
// TODO
|
||||
};
|
||||
|
||||
globalScope["move_memory"] =
|
||||
function move_memory(dest, src, n) {
|
||||
HEAP.copyWithin(dest, src, src + n);
|
||||
};
|
||||
|
||||
globalScope["store"] =
|
||||
function store(ptr, val) {
|
||||
HEAP[ptr] = val;
|
||||
};
|
||||
|
||||
globalScope["load"] =
|
||||
function load(ptr) {
|
||||
return HEAP[ptr];
|
||||
};
|
||||
|
Reference in New Issue
Block a user