diff --git a/std/assembly/allocator/arena.ts b/std/assembly/allocator/arena.ts index 94c2a232..55fd770e 100644 --- a/std/assembly/allocator/arena.ts +++ b/std/assembly/allocator/arena.ts @@ -1,9 +1,13 @@ import { HEAP_BASE, memory } from "../memory"; import { AL_MASK, MAX_SIZE_32 } from "../util/allocator"; +// @ts-ignore: decorator @lazy var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK; + +// @ts-ignore: decorator @lazy var offset: usize = startOffset; +// @ts-ignore: decorator @unsafe @global function __memory_allocate(size: usize): usize { if (size > MAX_SIZE_32) unreachable(); var ptr = offset; @@ -22,9 +26,11 @@ import { AL_MASK, MAX_SIZE_32 } from "../util/allocator"; return ptr; } +// @ts-ignore: decorator @unsafe @global function __memory_free(ptr: usize): void { } +// @ts-ignore: decorator @unsafe @global function __memory_reset(): void { offset = startOffset; } diff --git a/std/assembly/allocator/emscripten.ts b/std/assembly/allocator/emscripten.ts index 3ab1d083..9fc3cc7e 100644 --- a/std/assembly/allocator/emscripten.ts +++ b/std/assembly/allocator/emscripten.ts @@ -1,10 +1,15 @@ +// @ts-ignore: decorator @unsafe declare function _malloc(size: usize): usize; + +// @ts-ignore: decorator @unsafe declare function _free(ptr: usize): void; +// @ts-ignore: decorator @unsafe @global function __memory_allocate(size: usize): usize { return _malloc(size); } +// @ts-ignore: decorator @unsafe @global function __memory_free(ptr: usize): void { _free(ptr); } diff --git a/std/assembly/allocator/system.ts b/std/assembly/allocator/system.ts index 1c88cbe2..70d3ba12 100644 --- a/std/assembly/allocator/system.ts +++ b/std/assembly/allocator/system.ts @@ -1,10 +1,15 @@ +// @ts-ignore: decorator @unsafe declare function malloc(size: usize): usize; + +// @ts-ignore: decorator @unsafe declare function free(ptr: usize): void; +// @ts-ignore: decorator @unsafe @global function __memory_allocate(size: usize): usize { return malloc(size); } +// @ts-ignore: decorator @unsafe @global function __memory_free(ptr: usize): void { free(ptr); } diff --git a/std/assembly/allocator/tlsf.ts b/std/assembly/allocator/tlsf.ts index ec766ffb..1146fa6b 100644 --- a/std/assembly/allocator/tlsf.ts +++ b/std/assembly/allocator/tlsf.ts @@ -44,8 +44,7 @@ const LEFT_FREE: usize = 1 << 1; const TAGS: usize = FREE | LEFT_FREE; /** Block structure. */ -@unmanaged -class Block { +@unmanaged class Block { /** Info field holding this block's size and tags. */ info: usize; @@ -110,8 +109,7 @@ class Block { assert((1 << SL_BITS) <= 32); // second level must fit into 32 bits /** Root structure. */ -@unmanaged -class Root { +@unmanaged class Root { /** First level bitmap. */ flMap: usize = 0; @@ -406,15 +404,17 @@ class Root { } /** Determines the first (LSB to MSB) set bit's index of a word. */ -function ffs(word: T): T { +function ffs(word: T): T { assert(word != 0); // word cannot be 0 return ctz(word); // differs from ffs only for 0 } /** Determines the last (LSB to MSB) set bit's index of a word. */ -function fls(word: T): T { +function fls(word: T): T { assert(word != 0); // word cannot be 0 + // @ts-ignore: type const inv: T = (sizeof() << 3) - 1; + // @ts-ignore: type return inv - clz(word); } @@ -422,6 +422,7 @@ function fls(word: T): T { var ROOT: Root = changetype(0); /** Allocates a chunk of memory. */ +// @ts-ignore: decorator @unsafe @global function __memory_allocate(size: usize): usize { // initialize if necessary var root = ROOT; @@ -470,6 +471,7 @@ var ROOT: Root = changetype(0); } /** Frees the chunk of memory at the specified address. */ +// @ts-ignore @unsafe @global function __memory_free(data: usize): void { if (data) { let root = ROOT; diff --git a/std/assembly/array.ts b/std/assembly/array.ts index b56cad5e..635465de 100644 --- a/std/assembly/array.ts +++ b/std/assembly/array.ts @@ -95,6 +95,7 @@ export class Array extends ArrayBufferView { if (start < end) { memory.fill( dataStart + start, + // @ts-ignore: cast value, (end - start) ); @@ -268,6 +269,7 @@ export class Array extends ArrayBufferView { base + sizeof(), lastIndex << alignof() ); + // @ts-ignore: cast store(base + (lastIndex << alignof()), null); this.length_ = lastIndex; return element; @@ -375,6 +377,7 @@ export class Array extends ArrayBufferView { join(separator: string = ","): string { if (isInteger()) { + // @ts-ignore: type if (value instanceof bool) return this.join_bool(separator); return this.join_int(separator); } @@ -400,6 +403,7 @@ export class Array extends ArrayBufferView { var value: bool; for (let i = 0; i < lastIndex; ++i) { value = load(dataStart + i); + // @ts-ignore: cast valueLen = 4 + (!value); memory.copy( result + (offset << 1), @@ -417,6 +421,7 @@ export class Array extends ArrayBufferView { } } value = load(dataStart + lastIndex); + // @ts-ignore: cast valueLen = 4 + (!value); memory.copy( result + (offset << 1), @@ -440,6 +445,7 @@ export class Array extends ArrayBufferView { if (!lastIndex) return changetype(itoa(load(dataStart))); var sepLen = separator.length; + // @ts-ignore: cast const valueLen = (sizeof() <= 4 ? 10 : 20) + isSigned(); var estLen = (valueLen + sepLen) * lastIndex + valueLen; var result = runtime.alloc(estLen << 1); @@ -471,6 +477,7 @@ export class Array extends ArrayBufferView { var lastIndex = this.length_ - 1; if (lastIndex < 0) return ""; var dataStart = this.dataStart; + // @ts-ignore: type if (!lastIndex) return changetype(dtoa(load(dataStart))); const valueLen = MAX_DOUBLE_LENGTH; @@ -481,6 +488,7 @@ export class Array extends ArrayBufferView { var value: T; for (let i = 0; i < lastIndex; ++i) { value = load(dataStart + (i << alignof())); + // @ts-ignore: type offset += dtoa_stream(result, offset, value); if (sepLen) { memory.copy( @@ -492,6 +500,7 @@ export class Array extends ArrayBufferView { } } value = load(dataStart + (lastIndex << alignof())); + // @ts-ignore: type offset += dtoa_stream(result, offset, value); if (estLen > offset) { let trimmed = changetype(result).substring(0, offset); @@ -557,14 +566,17 @@ export class Array extends ArrayBufferView { var value: T; if (!lastIndex) { value = load(base); + // @ts-ignore: type return value ? value.join(separator) : ""; } for (let i = 0; i < lastIndex; ++i) { value = load(base + (i << alignof())); + // @ts-ignore: type if (value) result += value.join(separator); if (sepLen) result += separator; } value = load(base + (lastIndex << alignof())); + // @ts-ignore: type if (value) result += value.join(separator); return result; // registered by concatenation (FIXME: lots of garbage) } diff --git a/std/assembly/builtins.ts b/std/assembly/builtins.ts index 75190b5a..1349e1b7 100644 --- a/std/assembly/builtins.ts +++ b/std/assembly/builtins.ts @@ -1,501 +1,898 @@ -/* tslint:disable */ - +// @ts-ignore: decorator @builtin @inline export const NaN: f64 = 0 / 0; +// @ts-ignore: decorator @builtin @inline export const Infinity: f64 = 1 / 0; +// @ts-ignore: decorator @builtin export declare function isInteger(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isFloat(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isSigned(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isReference(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isString(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isArray(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isArrayLike(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isFunction(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isNullable(value?: T): bool; +// @ts-ignore: decorator @builtin export declare function isDefined(expression: void): bool; +// @ts-ignore: decorator @builtin export declare function isConstant(expression: void): bool; +// @ts-ignore: decorator @builtin export declare function isManaged(value?: T): bool; +// @ts-ignore: decorator @inline export function isNaN(value: T): bool { return value != value; } +// @ts-ignore: decorator @inline export function isFinite(value: T): bool { return value - value == 0; } +// @ts-ignore: decorator @builtin export declare function clz(value: T): T; +// @ts-ignore: decorator @builtin export declare function ctz(value: T): T; +// @ts-ignore: decorator @builtin export declare function popcnt(value: T): T; +// @ts-ignore: decorator @builtin export declare function rotl(value: T, shift: T): T; +// @ts-ignore: decorator @builtin export declare function rotr(value: T, shift: T): T; +// @ts-ignore: decorator @builtin export declare function abs(value: T): T; +// @ts-ignore: decorator @builtin export declare function max(left: T, right: T): T; +// @ts-ignore: decorator @builtin export declare function min(left: T, right: T): T; +// @ts-ignore: decorator @builtin export declare function ceil(value: T): T; +// @ts-ignore: decorator @builtin export declare function floor(value: T): T; +// @ts-ignore: decorator @builtin export declare function copysign(left: T, right: T): T; +// @ts-ignore: decorator @builtin export declare function nearest(value: T): T; -@builtin export declare function reinterpret(value: void): T; +// @ts-ignore: decorator +@builtin export declare function reinterpret(value: number): T; +// @ts-ignore: decorator @builtin export declare function sqrt(value: T): T; +// @ts-ignore: decorator @builtin export declare function trunc(value: T): T; +// @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize, immAlign?: usize): T; +// @ts-ignore: decorator @builtin export declare function store(offset: usize, value: void, immOffset?: usize, immAlign?: usize): void; +// @ts-ignore: decorator @builtin export declare function sizeof(): usize; // | u32 / u64 +// @ts-ignore: decorator @builtin export declare function alignof(): usize; // | u32 / u64 +// @ts-ignore: decorator @builtin export declare function offsetof(fieldName?: string): usize; // | u32 / u64 +// @ts-ignore: decorator @builtin export declare function select(ifTrue: T, ifFalse: T, condition: bool): T; +// @ts-ignore: decorator @builtin export declare function unreachable(): void; +// @ts-ignore: decorator @builtin export declare function changetype(value: void): T; +// @ts-ignore: decorator @builtin export declare function assert(isTrueish: T, message?: string): T; +// @ts-ignore: decorator @builtin export declare function unchecked(expr: T): T; +// @ts-ignore: decorator @builtin export declare function call_indirect(target: void, ...args: void[]): T; +// @ts-ignore: decorator @builtin export declare function instantiate(...args: void[]): T; export namespace atomic { + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: T, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function add(ptr: usize, value: T, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function sub(ptr: usize, value: T, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function and(ptr: usize, value: T, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function or(ptr: usize, value: T, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function xor(ptr: usize, value: T, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function xchg(ptr: usize, value: T, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function cmpxchg(ptr: usize, expected: T, replacement: T, immOffset?: usize): T; + // @ts-ignore: decorator @builtin export declare function wait(ptr: usize, expected: T, timeout: i64): AtomicWaitResult; + // @ts-ignore: decorator @builtin export declare function notify(ptr: usize, count: i32): i32; + // @ts-ignore: decorators } +// @ts-ignore: decorator @lazy export const enum AtomicWaitResult { OK = 0, NOT_EQUAL = 1, TIMED_OUT = 2 } +// @ts-ignore: decorator @builtin export declare function i8(value: void): i8; export namespace i8 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: i8 = -128; + // @ts-ignore: decorator @lazy export const MAX_VALUE: i8 = 127; } +// @ts-ignore: decorator @builtin export declare function i16(value: void): i16; export namespace i16 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: i16 = -32768; + // @ts-ignore: decorator @lazy export const MAX_VALUE: i16 = 32767; } +// @ts-ignore: decorator @builtin export declare function i32(value: void): i32; export namespace i32 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: i32 = -2147483648; + // @ts-ignore: decorator @lazy export const MAX_VALUE: i32 = 2147483647; + // @ts-ignore: decorator @builtin export declare function clz(value: i32): i32; + // @ts-ignore: decorator @builtin export declare function ctz(value: i32): i32; + // @ts-ignore: decorator @builtin export declare function popcnt(value: i32): i32; + // @ts-ignore: decorator @builtin export declare function rotl(value: i32, shift: i32): i32; + // @ts-ignore: decorator @builtin export declare function rotr(value: i32, shift: i32): i32; + // @ts-ignore: decorator @builtin export declare function reinterpret_f32(value: f32): i32; + // @ts-ignore: decorator @builtin export declare function load8_s(offset: usize, immOffset?: usize, immAlign?: usize): i32; + // @ts-ignore: decorator @builtin export declare function load8_u(offset: usize, immOffset?: usize, immAlign?: usize): i32; + // @ts-ignore: decorator @builtin export declare function load16_s(offset: usize, immOffset?: usize, immAlign?: usize): i32; + // @ts-ignore: decorator @builtin export declare function load16_u(offset: usize, immOffset?: usize, immAlign?: usize): i32; + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize, immAlign?: usize): i32; + // @ts-ignore: decorator @builtin export declare function store8(offset: usize, value: i32, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function store16(offset: usize, value: i32, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: i32, immOffset?: usize, immAlign?: usize): void; export namespace atomic { + // @ts-ignore: decorator @builtin export declare function load8_u(offset: usize, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function load16_u(offset: usize, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function store8(offset: usize, value: i32, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function store16(offset: usize, value: i32, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: i32, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function wait(ptr: usize, expected: i32, timeout: i64): AtomicWaitResult; + // @ts-ignore: decorator @builtin export declare function notify(ptr: usize, count: i32): i32; export namespace rmw8 { + // @ts-ignore: decorator @builtin export declare function add_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function sub_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function and_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function or_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function xor_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function xchg_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function cmpxchg_u(offset: usize, expected: i32, replacement: i32, immOffset?: usize): i32; } export namespace rmw16 { + // @ts-ignore: decorator @builtin export declare function add_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function sub_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function and_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function or_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function xor_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function xchg_u(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function cmpxchg_u(offset: usize, expected: i32, replacement: i32, immOffset?: usize): i32; } export namespace rmw { + // @ts-ignore: decorator @builtin export declare function add(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function sub(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function and(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function or(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function xor(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function xchg(offset: usize, value: i32, immOffset?: usize): i32; + // @ts-ignore: decorator @builtin export declare function cmpxchg(offset: usize, expected: i32, replacement: i32, immOffset?: usize): i32; } } } +// @ts-ignore: decorator @builtin export declare function i64(value: void): i64; export namespace i64 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: i64 = -9223372036854775808; + // @ts-ignore: decorator @lazy export const MAX_VALUE: i64 = 9223372036854775807; + // @ts-ignore: decorator @builtin export declare function clz(value: i64): i64; + // @ts-ignore: decorator @builtin export declare function ctz(value: i64): i64; + // @ts-ignore: decorator @builtin export declare function load8_s(offset: usize, immOffset?: usize, immAlign?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load8_u(offset: usize, immOffset?: usize, immAlign?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load16_s(offset: usize, immOffset?: usize, immAlign?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load16_u(offset: usize, immOffset?: usize, immAlign?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load32_s(offset: usize, immOffset?: usize, immAlign?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load32_u(offset: usize, immOffset?: usize, immAlign?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function popcnt(value: i64): i64; + // @ts-ignore: decorator @builtin export declare function rotl(value: i64, shift: i64): i64; + // @ts-ignore: decorator @builtin export declare function rotr(value: i64, shift: i64): i64; + // @ts-ignore: decorator @builtin export declare function reinterpret_f64(value: f64): i64; + // @ts-ignore: decorator @builtin export declare function store8(offset: usize, value: i64, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function store16(offset: usize, value: i64, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function store32(offset: usize, value: i64, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: i64, immOffset?: usize, immAlign?: usize): void; export namespace atomic { + // @ts-ignore: decorator @builtin export declare function load8_u(offset: usize, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load16_u(offset: usize, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load32_u(offset: usize, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function store8(offset: usize, value: i64, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function store16(offset: usize, value: i64, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function store32(offset: usize, value: i64, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: i64, immOffset?: usize): void; + // @ts-ignore: decorator @builtin export declare function wait(ptr: usize, expected: i64, timeout: i64): AtomicWaitResult; + // @ts-ignore: decorator @builtin export declare function notify(ptr: usize, count: i32): i32; export namespace rmw8 { + // @ts-ignore: decorator @builtin export declare function add_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function sub_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function and_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function or_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xor_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xchg_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function cmpxchg_u(offset: usize, expected: i64, replacement: i64, immOffset?: usize): i64; } export namespace rmw16 { + // @ts-ignore: decorator @builtin export declare function add_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function sub_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function and_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function or_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xor_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xchg_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function cmpxchg_u(offset: usize, expected: i64, replacement: i64, immOffset?: usize): i64; } export namespace rmw32 { + // @ts-ignore: decorator @builtin export declare function add_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function sub_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function and_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function or_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xor_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xchg_u(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function cmpxchg_u(offset: usize, expected: i64, replacement: i64, immOffset?: usize): i64; } export namespace rmw { + // @ts-ignore: decorator @builtin export declare function add(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function sub(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function and(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function or(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xor(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function xchg(offset: usize, value: i64, immOffset?: usize): i64; + // @ts-ignore: decorator @builtin export declare function cmpxchg(offset: usize, expected: i64, replacement: i64, immOffset?: usize): i64; } } } +// @ts-ignore: decorator @builtin export declare function isize(value: void): isize; export namespace isize { + // @ts-ignore: decorator @lazy export const MIN_VALUE: isize = sizeof() == sizeof() ? -2147483648 : -9223372036854775808; + // @ts-ignore: decorator @lazy export const MAX_VALUE: isize = sizeof() == sizeof() ? 2147483647 : 9223372036854775807; } +// @ts-ignore: decorator @builtin export declare function u8(value: void): u8; export namespace u8 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: u8 = 0; + // @ts-ignore: decorator @lazy export const MAX_VALUE: u8 = 255; } +// @ts-ignore: decorator @builtin export declare function u16(value: void): u16; export namespace u16 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: u16 = 0; + // @ts-ignore: decorator @lazy export const MAX_VALUE: u16 = 65535; } +// @ts-ignore: decorator @builtin export declare function u32(value: void): u32; export namespace u32 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: u32 = 0; + // @ts-ignore: decorator @lazy export const MAX_VALUE: u32 = 4294967295; } +// @ts-ignore: decorator @builtin export declare function u64(value: void): u64; export namespace u64 { + // @ts-ignore: decorator @lazy export const MIN_VALUE: u64 = 0; + // @ts-ignore: decorator @lazy export const MAX_VALUE: u64 = 18446744073709551615; } +// @ts-ignore: decorator @builtin export declare function usize(value: void): usize; export namespace usize { + // @ts-ignore: decorator @lazy export const MIN_VALUE: usize = 0; + // @ts-ignore: decorator @lazy export const MAX_VALUE: usize = sizeof() == sizeof() ? 4294967295 : 18446744073709551615; } +// @ts-ignore: decorator @builtin export declare function bool(value: void): bool; export namespace bool { + // @ts-ignore: decorator @lazy export const MIN_VALUE: bool = false; + // @ts-ignore: decorator @lazy export const MAX_VALUE: bool = true; } +// @ts-ignore: decorator @builtin export declare function f32(value: void): f32; export namespace f32 { + // @ts-ignore: decorator @lazy export const EPSILON = reinterpret(0x34000000); // 0x1p-23f + // @ts-ignore: decorator @lazy export const MIN_VALUE = reinterpret(0x00000001); // 0x0.000001p+0f + // @ts-ignore: decorator @lazy export const MAX_VALUE = reinterpret(0x7F7FFFFF); // 0x1.fffffep+127f + // @ts-ignore: decorator @lazy export const MIN_NORMAL_VALUE = reinterpret(0x00800000); // 0x1p-126f + // @ts-ignore: decorator @lazy export const MIN_SAFE_INTEGER: f32 = -16777215; + // @ts-ignore: decorator @lazy export const MAX_SAFE_INTEGER: f32 = 16777215; + // @ts-ignore: decorator @builtin export declare function abs(value: f32): f32; + // @ts-ignore: decorator @builtin export declare function ceil(value: f32): f32; + // @ts-ignore: decorator @builtin export declare function copysign(x: f32, y: f32): f32; + // @ts-ignore: decorator @builtin export declare function floor(value: f32): f32; + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize, immAlign?: usize): f32; + // @ts-ignore: decorator @builtin export declare function max(left: f32, right: f32): f32; + // @ts-ignore: decorator @builtin export declare function min(left: f32, right: f32): f32; + // @ts-ignore: decorator @builtin export declare function nearest(value: f32): f32; + // @ts-ignore: decorator @builtin export declare function reinterpret_i32(value: i32): f32; + // @ts-ignore: decorator @builtin export declare function sqrt(value: f32): f32; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: f32, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function trunc(value: f32): f32; } +// @ts-ignore: decorator @builtin export declare function f64(value: void): f64; export namespace f64 { + // @ts-ignore: decorator @lazy export const EPSILON = reinterpret(0x3CB0000000000000); // 0x1p-52 + // @ts-ignore: decorator @lazy export const MIN_VALUE = reinterpret(0x0000000000000001); // 0x0.0000000000001p+0 + // @ts-ignore: decorator @lazy export const MAX_VALUE = reinterpret(0x7FEFFFFFFFFFFFFF); // 0x1.fffffffffffffp+1023 + // @ts-ignore: decorator @lazy export const MIN_NORMAL_VALUE = reinterpret(0x0010000000000000); // 0x1p-1022 + // @ts-ignore: decorator @lazy export const MIN_SAFE_INTEGER: f64 = -9007199254740991; + // @ts-ignore: decorator @lazy export const MAX_SAFE_INTEGER: f64 = 9007199254740991; + // @ts-ignore: decorator @builtin export declare function abs(value: f64): f64; + // @ts-ignore: decorator @builtin export declare function ceil(value: f64): f64; + // @ts-ignore: decorator @builtin export declare function copysign(x: f64, y: f64): f64; + // @ts-ignore: decorator @builtin export declare function floor(value: f64): f64; + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize, immAlign?: usize): f64; + // @ts-ignore: decorator @builtin export declare function max(left: f64, right: f64): f64; + // @ts-ignore: decorator @builtin export declare function min(left: f64, right: f64): f64; + // @ts-ignore: decorator @builtin export declare function nearest(value: f64): f64; + // @ts-ignore: decorator @builtin export declare function reinterpret_i64(value: i64): f64; + // @ts-ignore: decorator @builtin export declare function sqrt(value: f64): f64; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: f64, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function trunc(value: f64): f64; } +// @ts-ignore: decorator @builtin export declare function v128(a: i8, b: i8, c: i8, d: i8, e: i8, f: i8, g: i8, h: i8, i: i8, j: i8, k: i8, l: i8, m: i8, n: i8, o: i8, p: i8): v128; export namespace v128 { + // @ts-ignore: decorator @builtin export declare function splat(x: T): v128; + // @ts-ignore: decorator @builtin export declare function extract_lane(x: v128, idx: u8): T; + // @ts-ignore: decorator @builtin export declare function replace_lane(x: v128, idx: u8, value: T): v128; + // @ts-ignore: decorator @builtin export declare function shuffle(a: v128, b: v128, ...lanes: u8[]): v128; + // @ts-ignore: decorator @builtin export declare function load(offset: usize, immOffset?: usize, immAlign?: usize): v128; + // @ts-ignore: decorator @builtin export declare function store(offset: usize, value: v128, immOffset?: usize, immAlign?: usize): void; + // @ts-ignore: decorator @builtin export declare function add(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function mul(a: v128, b: v128): v128; // except i64 + // @ts-ignore: decorator @builtin export declare function div(a: v128, b: v128): v128; // f32, f64 only + // @ts-ignore: decorator @builtin export declare function neg(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function add_saturate(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub_saturate(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function shl(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function and(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function or(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function xor(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function not(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function bitselect(v1: v128, v2: v128, c: v128): v128; + // @ts-ignore: decorator @builtin export declare function any_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function all_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function min(a: v128, b: v128): v128; // f32, f64 only + // @ts-ignore: decorator @builtin export declare function max(a: v128, b: v128): v128; // f32, f64 only + // @ts-ignore: decorator @builtin export declare function abs(a: v128): v128; // f32, f64 only + // @ts-ignore: decorator @builtin export declare function sqrt(a: v128): v128; // f32, f64 only + // @ts-ignore: decorator @builtin export declare function eq(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ne(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function convert(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function trunc(a: v128): v128; } +// @ts-ignore: decorator @builtin export declare function i8x16(a: i8, b: i8, c: i8, d: i8, e: i8, f: i8, g: i8, h: i8, i: i8, j: i8, k: i8, l: i8, m: i8, n: i8, o: i8, p: i8): v128; export namespace i8x16 { + // @ts-ignore: decorator @builtin export declare function splat(x: i8): v128; + // @ts-ignore: decorator @builtin export declare function extract_lane_s(x: v128, idx: u8): i8; + // @ts-ignore: decorator @builtin export declare function extract_lane_u(x: v128, idx: u8): u8; + // @ts-ignore: decorator @builtin export declare function replace_lane(x: v128, idx: u8, value: i8): v128; + // @ts-ignore: decorator @builtin export declare function add(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function mul(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function neg(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function add_saturate_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function add_saturate_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub_saturate_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub_saturate_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function shl(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_s(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_u(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function any_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function all_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function eq(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ne(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge_u(a: v128, b: v128): v128; } +// @ts-ignore: decorator @builtin export declare function i16x8(a: i16, b: i16, c: i16, d: i16, e: i16, f: i16, g: i16, h: i16): v128; export namespace i16x8 { + // @ts-ignore: decorator @builtin export declare function splat(x: i16): v128; + // @ts-ignore: decorator @builtin export declare function extract_lane_s(x: v128, idx: u8): i16; + // @ts-ignore: decorator @builtin export declare function extract_lane_u(x: v128, idx: u8): u16; + // @ts-ignore: decorator @builtin export declare function replace_lane(x: v128, idx: u8, value: i16): v128; + // @ts-ignore: decorator @builtin export declare function add(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function mul(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function neg(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function add_saturate_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function add_saturate_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub_saturate_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub_saturate_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function shl(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_s(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_u(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function any_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function all_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function eq(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ne(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge_u(a: v128, b: v128): v128; } +// @ts-ignore: decorator @builtin export declare function i32x4(a: i32, b: i32, c: i32, d: i32): v128; export namespace i32x4 { + // @ts-ignore: decorator @builtin export declare function splat(x: i32): v128; + // @ts-ignore: decorator @builtin export declare function extract_lane(x: v128, idx: u8): i32; + // @ts-ignore: decorator @builtin export declare function replace_lane(x: v128, idx: u8, value: i32): v128; + // @ts-ignore: decorator @builtin export declare function add(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function mul(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function neg(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function shl(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_s(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_u(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function any_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function all_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function eq(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ne(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge_s(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge_u(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function trunc_s_f32x4_sat(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function trunc_u_f32x4_sat(a: v128): v128; } +// @ts-ignore: decorator @builtin export declare function i64x2(a: i64, b: i64): v128; export namespace i64x2 { + // @ts-ignore: decorator @builtin export declare function splat(x: i64): v128; + // @ts-ignore: decorator @builtin export declare function extract_lane(x: v128, idx: u8): i64; + // @ts-ignore: decorator @builtin export declare function replace_lane(x: v128, idx: u8, value: i64): v128; + // @ts-ignore: decorator @builtin export declare function add(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function mul(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function neg(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function shl(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_s(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function shr_u(a: v128, b: i32): v128; + // @ts-ignore: decorator @builtin export declare function any_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function all_true(a: v128): bool; + // @ts-ignore: decorator @builtin export declare function trunc_s_f64x2_sat(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function trunc_u_f64x2_sat(a: v128): v128; } +// @ts-ignore: decorator @builtin export declare function f32x4(a: f32, b: f32, c: f32, d: f32): v128; export namespace f32x4 { + // @ts-ignore: decorator @builtin export declare function splat(x: f32): v128; + // @ts-ignore: decorator @builtin export declare function extract_lane(x: v128, idx: u8): f32; + // @ts-ignore: decorator @builtin export declare function replace_lane(x: v128, idx: u8, value: f32): v128; + // @ts-ignore: decorator @builtin export declare function add(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function mul(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function div(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function neg(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function min(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function max(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function abs(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function sqrt(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function eq(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ne(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function convert_s_i32x4(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function convert_u_i32x4(a: v128): v128; } +// @ts-ignore: decorator @builtin export declare function f64x2(a: f64, b: f64): v128; export namespace f64x2 { + // @ts-ignore: decorator @builtin export declare function splat(x: f64): v128; + // @ts-ignore: decorator @builtin export declare function extract_lane(x: v128, idx: u8): f64; + // @ts-ignore: decorator @builtin export declare function replace_lane(x: v128, idx: u8, value: f64): v128; + // @ts-ignore: decorator @builtin export declare function add(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function sub(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function mul(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function div(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function neg(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function min(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function max(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function abs(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function sqrt(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function eq(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ne(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function lt(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function le(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function gt(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function ge(a: v128, b: v128): v128; + // @ts-ignore: decorator @builtin export declare function convert_s_i64x2(a: v128): v128; + // @ts-ignore: decorator @builtin export declare function convert_u_i64x2(a: v128): v128; } export namespace v8x16 { + // @ts-ignore: decorator @builtin export declare function shuffle(a: v128, b: v128, l0: u8, l1: u8, l2: u8, l3: u8, l4: u8, l5: u8, l6: u8, l7: u8, l8: u8, l9: u8, l10: u8, l11: u8, l12: u8, l13: u8, l14: u8, l15: u8): v128; } diff --git a/std/assembly/collector/itcm.ts b/std/assembly/collector/itcm.ts index 27fb146f..e63b7fdb 100644 --- a/std/assembly/collector/itcm.ts +++ b/std/assembly/collector/itcm.ts @@ -1,8 +1,10 @@ // Largely based on Bach Le's μgc, see: https://github.com/bullno1/ugc +// @ts-ignore: decorator @inline const TRACE = false; /** Size of a managed object header. */ +// @ts-ignore: decorator @inline export const HEADER_SIZE: usize = (offsetof() + AL_MASK) & ~AL_MASK; import { AL_MASK, MAX_SIZE_32 } from "../util/allocator"; @@ -146,6 +148,7 @@ function step(): void { if (obj !== toSpace) { if (TRACE) trace("gc~step/MARK iterate", 1, objToRef(obj)); iter = obj; + // @ts-ignore: cast obj.color = !white; // if (TRACE) { // trace(" next/prev/hook", 3, @@ -163,6 +166,7 @@ function step(): void { let from = fromSpace; fromSpace = toSpace; toSpace = from; + // @ts-ignore: cast white = !white; iter = from.next; state = State.SWEEP; @@ -188,14 +192,17 @@ function step(): void { } } +// @ts-ignore: decorator @inline function refToObj(ref: usize): ManagedObject { return changetype(ref - HEADER_SIZE); } +// @ts-ignore: decorator @inline function objToRef(obj: ManagedObject): usize { return changetype(obj) + HEADER_SIZE; } +// @ts-ignore: decorator @global @unsafe export function __gc_allocate( // TODO: make this register only / reuse header size: usize, markFn: (ref: usize) => void @@ -210,12 +217,15 @@ function step(): void { return objToRef(obj); } +// @ts-ignore: decorator @global @unsafe export function __gc_link(parentRef: usize, childRef: usize): void { if (TRACE) trace("gc.link", 2, parentRef, childRef); var parent = refToObj(parentRef); + // @ts-ignore: cast if (parent.color == !white && refToObj(childRef).color == white) parent.makeGray(); } +// @ts-ignore: decorator @global @unsafe export function __gc_mark(ref: usize): void { if (TRACE) trace("gc.mark", 1, ref); if (ref) { @@ -224,6 +234,7 @@ function step(): void { } } +// @ts-ignore: decorator @global @unsafe export function __gc_collect(): void { if (TRACE) trace("gc.collect"); // begin collecting if not yet collecting diff --git a/std/assembly/diagnostics.ts b/std/assembly/diagnostics.ts index 291f45b4..5254ac15 100644 --- a/std/assembly/diagnostics.ts +++ b/std/assembly/diagnostics.ts @@ -1,3 +1,8 @@ +// @ts-ignore: decorator @builtin export declare function ERROR(message?: string): void; + +// @ts-ignore: decorator @builtin export declare function WARNING(message?: string): void; + +// @ts-ignore: decorator @builtin export declare function INFO(message?: string): void; diff --git a/std/assembly/gc.ts b/std/assembly/gc.ts index b226d39d..26ae9df2 100644 --- a/std/assembly/gc.ts +++ b/std/assembly/gc.ts @@ -2,6 +2,7 @@ export namespace gc { /** Whether the garbage collector interface is implemented. */ + // @ts-ignore: decorator, undefined @lazy export const implemented: bool = isDefined(__gc_register); /** Gets the computed unique class id of a class type. */ @@ -12,24 +13,28 @@ export namespace gc { /** Registers a managed object to be tracked by the garbage collector. */ @unsafe export function register(ref: usize): void { + // @ts-ignore: undefined if (isDefined(__gc_register)) __gc_register(ref); else ERROR("missing implementation: gc.register"); } /** Links a registered object with the registered object now referencing it. */ @unsafe export function link(ref: usize, parentRef: usize): void { + // @ts-ignore: undefined if (isDefined(__gc_link)) __gc_link(ref, parentRef); else ERROR("missing implementation: gc.link"); } /** Marks an object as being reachable. */ @unsafe export function mark(ref: usize): void { + // @ts-ignore: undefined if (isDefined(__gc_mark)) __gc_mark(ref); else ERROR("missing implementation: gc.mark"); } /** Performs a full garbage collection cycle. */ export function collect(): void { + // @ts-ignore: undefined if (isDefined(__gc_collect)) __gc_collect(); else WARNING("missing implementation: gc.collect"); } diff --git a/std/assembly/map.ts b/std/assembly/map.ts index 2152b929..2a8a1925 100644 --- a/std/assembly/map.ts +++ b/std/assembly/map.ts @@ -3,8 +3,13 @@ import { HASH } from "./util/hash"; // A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht +// @ts-ignore: decorator @inline const INITIAL_CAPACITY = 4; + +// @ts-ignore: decorator @inline const FILL_FACTOR: f64 = 8 / 3; + +// @ts-ignore: decorator @inline const FREE_FACTOR: f64 = 3 / 4; /** Structure of a map entry. */ @@ -15,12 +20,15 @@ import { HASH } from "./util/hash"; } /** Empty bit. */ +// @ts-ignore: decorator @inline const EMPTY: usize = 1 << 0; /** Size of a bucket. */ +// @ts-ignore: decorator @inline const BUCKET_SIZE = sizeof(); /** Computes the alignment of an entry. */ +// @ts-ignore: decorator @inline function ENTRY_ALIGN(): usize { // can align to 4 instead of 8 if 32-bit and K/V is <= 32-bits const maxkv = sizeof() > sizeof() ? sizeof() : sizeof(); @@ -29,6 +37,7 @@ import { HASH } from "./util/hash"; } /** Computes the aligned size of an entry. */ +// @ts-ignore: decorator @inline function ENTRY_SIZE(): usize { const align = ENTRY_ALIGN(); const size = (offsetof>() + align) & ~align; diff --git a/std/assembly/math.ts b/std/assembly/math.ts index f899f1b0..22efc2a3 100644 --- a/std/assembly/math.ts +++ b/std/assembly/math.ts @@ -42,6 +42,7 @@ function R(z: f64): f64 { // Rational approximation of (asin(x)-x)/x^3 return p / q; } +// @ts-ignore: decorator @inline function expo2(x: f64): f64 { // exp(x)/2 for x >= log(DBL_MAX) const // see: musl/src/math/__expo2.c k = 2043, @@ -50,11 +51,15 @@ function R(z: f64): f64 { // Rational approximation of (asin(x)-x)/x^3 return NativeMath.exp(x - kln2) * scale * scale; } -/** @internal */ +// @ts-ignore: decorator @lazy var random_seeded = false; +// @ts-ignore: decorator @lazy var random_state0_64: u64; +// @ts-ignore: decorator @lazy var random_state1_64: u64; +// @ts-ignore: decorator @lazy var random_state0_32: u32; +// @ts-ignore: decorator @lazy var random_state1_32: u32; function murmurHash3(h: u64): u64 { // Force all bits of a hash block to avalanche @@ -75,17 +80,25 @@ function splitMix32(h: u32): u32 { export namespace NativeMath { + // @ts-ignore: decorator @lazy export const E = reinterpret(0x4005BF0A8B145769); // 2.7182818284590452354 + // @ts-ignore: decorator @lazy export const LN2 = reinterpret(0x3FE62E42FEFA39EF); // 0.69314718055994530942 + // @ts-ignore: decorator @lazy export const LN10 = reinterpret(0x40026BB1BBB55516); // 2.30258509299404568402 + // @ts-ignore: decorator @lazy export const LOG2E = reinterpret(0x3FF71547652B82FE); // 1.4426950408889634074 + // @ts-ignore: decorator @lazy export const LOG10E = reinterpret(0x3FDBCB7B1526E50E); // 0.43429448190325182765 + // @ts-ignore: decorator @lazy export const PI = reinterpret(0x400921FB54442D18); // 3.14159265358979323846 + // @ts-ignore: decorator @lazy export const SQRT1_2 = reinterpret(0x3FE6A09E667F3BCD); // 0.70710678118654752440 + // @ts-ignore: decorator @lazy export const SQRT2 = reinterpret(0x3FF6A09E667F3BCD); // 1.41421356237309504880 - @inline - export function abs(x: f64): f64 { + // @ts-ignore: decorator + @inline export function abs(x: f64): f64 { return builtin_abs(x); } @@ -346,8 +359,8 @@ export namespace NativeMath { return t; } - @inline - export function ceil(x: f64): f64 { + // @ts-ignore: decorator + @inline export function ceil(x: f64): f64 { return builtin_ceil(x); } @@ -497,13 +510,13 @@ export namespace NativeMath { return (x + y) * twopk; } - @inline - export function floor(x: f64): f64 { + // @ts-ignore: decorator + @inline export function floor(x: f64): f64 { return builtin_floor(x); } - @inline - export function fround(x: f64): f32 { + // @ts-ignore: decorator + @inline export function fround(x: f64): f32 { return x; } @@ -763,13 +776,13 @@ export namespace NativeMath { return val_lo + val_hi; } - @inline - export function max(value1: f64, value2: f64): f64 { + // @ts-ignore: decorator + @inline export function max(value1: f64, value2: f64): f64 { return builtin_max(value1, value2); } - @inline - export function min(value1: f64, value2: f64): f64 { + // @ts-ignore: decorator + @inline export function min(value1: f64, value2: f64): f64 { return builtin_min(value1, value2); } @@ -997,13 +1010,13 @@ export namespace NativeMath { return reinterpret(r) - 1; } - @inline - export function round(x: f64): f64 { + // @ts-ignore: decorator + @inline export function round(x: f64): f64 { return builtin_copysign(builtin_floor(x + 0.5), x); } - @inline - export function sign(x: f64): f64 { + // @ts-ignore: decorator + @inline export function sign(x: f64): f64 { if (ASC_SHRINK_LEVEL > 0) { return builtin_abs(x) > 0 ? builtin_copysign(1, x) : x; } else { @@ -1011,10 +1024,11 @@ export namespace NativeMath { } } - @inline - export function signbit(x: f64): bool { + // @ts-ignore: decorator + @inline export function signbit(x: f64): bool { // In ECMAScript all NaN values are indistinguishable from each other // so we need handle NaN and negative NaN in similar way + // @ts-ignore: type return ((reinterpret(x) >>> 63) & (x == x)); } @@ -1041,8 +1055,8 @@ export namespace NativeMath { return t; } - @inline - export function sqrt(x: f64): f64 { + // @ts-ignore: decorator + @inline export function sqrt(x: f64): f64 { return builtin_sqrt(x); } @@ -1074,8 +1088,8 @@ export namespace NativeMath { return builtin_copysign(t, x); } - @inline - export function trunc(x: f64): f64 { + // @ts-ignore: decorator + @inline export function trunc(x: f64): f64 { return builtin_trunc(x); } @@ -1232,8 +1246,9 @@ export namespace NativeMath { } } -/** @internal */ +// @ts-ignore: decorator @lazy var rempio2f_y: f64; +// @ts-ignore: decorator @lazy const PIO2_TABLE: u64[] = [ 0xA2F9836E4E441529, 0xFC2757D1F534DDC0, @@ -1241,7 +1256,6 @@ export namespace NativeMath { 0xFE5163ABDEBBC561 ]; -/** @internal */ function Rf(z: f32): f32 { // Rational approximation of (asin(x)-x)/x^3 const // see: musl/src/math/asinf.c and SUN COPYRIGHT NOTICE above pS0 = reinterpret(0x3E2AAA75), // 1.6666586697e-01f @@ -1253,6 +1267,7 @@ function Rf(z: f32): f32 { // Rational approximation of (asin(x)-x)/x^3 return p / q; } +// @ts-ignore: decorator @inline function expo2f(x: f32): f32 { // exp(x)/2 for x >= log(DBL_MAX) const // see: musl/src/math/__expo2f.c k = 235, @@ -1261,8 +1276,8 @@ function Rf(z: f32): f32 { // Rational approximation of (asin(x)-x)/x^3 return NativeMathf.exp(x - kln2) * scale * scale; } -@inline /** @internal */ -function pio2_large_quot(x: f32, u: i32): i32 { // see: jdh8/metallic/blob/master/src/math/float/rem_pio2f.c +// @ts-ignore: decorator +@inline function pio2_large_quot(x: f32, u: i32): i32 { // see: jdh8/metallic/blob/master/src/math/float/rem_pio2f.c const coeff = reinterpret(0x3BF921FB54442D18); // π * 0x1p-65 = 8.51530395021638647334e-20 const bits = PIO2_TABLE; @@ -1291,8 +1306,8 @@ function pio2_large_quot(x: f32, u: i32): i32 { // see: jdh8/metallic/blob return q; } -@inline /** @internal */ -function rempio2f(x: f32, u: u32, sign: i32): i32 { // see: jdh8/metallic/blob/master/src/math/float/rem_pio2f.c +// @ts-ignore: decorator +@inline function rempio2f(x: f32, u: u32, sign: i32): i32 { // see: jdh8/metallic/blob/master/src/math/float/rem_pio2f.c const pi2hi = reinterpret(0x3FF921FB50000000); // 1.57079631090164184570 const pi2lo = reinterpret(0x3E5110B4611A6263); // 1.58932547735281966916e-8 const _2_pi = reinterpret(0x3FE45F306DC9C883); // 0.63661977236758134308 @@ -1308,8 +1323,8 @@ function rempio2f(x: f32, u: u32, sign: i32): i32 { // see: jdh8/metallic/blob } /* |sin(x)/x - s(x)| < 2**-37.5 (~[-4.89e-12, 4.824e-12]). */ -@inline /** @internal */ -function sin_kernf(x: f64): f32 { // see: musl/tree/src/math/__sindf.c +// @ts-ignore: decorator +@inline function sin_kernf(x: f64): f32 { // see: musl/tree/src/math/__sindf.c const S1 = reinterpret(0xBFC5555554CBAC77); // -0x15555554cbac77.0p-55 const S2 = reinterpret(0x3F811110896EFBB2); // 0x111110896efbb2.0p-59 const S3 = reinterpret(0xBF2A00F9E2CAE774); // -0x1a00f9e2cae774.0p-65 @@ -1323,8 +1338,8 @@ function sin_kernf(x: f64): f32 { // see: musl/tree/src/math/__sindf.c } /* |cos(x) - c(x)| < 2**-34.1 (~[-5.37e-11, 5.295e-11]). */ -@inline /** @internal */ -function cos_kernf(x: f64): f32 { // see: musl/tree/src/math/__cosdf.c +// @ts-ignore: decorator +@inline function cos_kernf(x: f64): f32 { // see: musl/tree/src/math/__cosdf.c const C0 = reinterpret(0xBFDFFFFFFD0C5E81); // -0x1ffffffd0c5e81.0p-54 const C1 = reinterpret(0x3FA55553E1053A42); // 0x155553e1053a42.0p-57 const C2 = reinterpret(0xBF56C087E80F1E27); // -0x16c087e80f1e27.0p-62 @@ -1337,8 +1352,8 @@ function cos_kernf(x: f64): f32 { // see: musl/tree/src/math/__cosdf.c } /* |tan(x)/x - t(x)| < 2**-25.5 (~[-2e-08, 2e-08]). */ -@inline /** @internal */ -function tan_kernf(x: f64, odd: i32): f32 { // see: musl/tree/src/math/__tandf.c +// @ts-ignore: decorator +@inline function tan_kernf(x: f64, odd: i32): f32 { // see: musl/tree/src/math/__tandf.c const T0 = reinterpret(0x3FD5554D3418C99F); /* 0x15554d3418c99f.0p-54 */ const T1 = reinterpret(0x3FC112FD38999F72); /* 0x1112fd38999f72.0p-55 */ @@ -1360,21 +1375,31 @@ function tan_kernf(x: f64, odd: i32): f32 { // see: musl/tree/src/math/__tandf.c export namespace NativeMathf { + // @ts-ignore: decorator @lazy export const E = NativeMath.E; + // @ts-ignore: decorator @lazy export const LN2 = NativeMath.LN2; + // @ts-ignore: decorator @lazy export const LN10 = NativeMath.LN10; + // @ts-ignore: decorator @lazy export const LOG2E = NativeMath.LOG2E; + // @ts-ignore: decorator @lazy export const LOG10E = NativeMath.LOG10E; + // @ts-ignore: decorator @lazy export const PI = NativeMath.PI; + // @ts-ignore: decorator @lazy export const SQRT1_2 = NativeMath.SQRT1_2; + // @ts-ignore: decorator @lazy export const SQRT2 = NativeMath.SQRT2; /** Used as return values from Mathf.sincos */ + // @ts-ignore: decorator @lazy export var sincos_sin: f32 = 0; + // @ts-ignore: decorator @lazy export var sincos_cos: f32 = 0; - @inline - export function abs(x: f32): f32 { + // @ts-ignore: decorator + @inline export function abs(x: f32): f32 { return builtin_abs(x); } @@ -1609,8 +1634,8 @@ export namespace NativeMathf { return t; } - @inline - export function ceil(x: f32): f32 { + // @ts-ignore: decorator + @inline export function ceil(x: f32): f32 { return builtin_ceil(x); } @@ -1685,8 +1710,8 @@ export namespace NativeMathf { return expo2f(x); } - @inline - export function floor(x: f32): f32 { + // @ts-ignore: decorator + @inline export function floor(x: f32): f32 { return builtin_floor(x); } @@ -1796,8 +1821,8 @@ export namespace NativeMathf { return (x + y) * twopk; } - @inline - export function fround(x: f32): f32 { + // @ts-ignore: decorator + @inline export function fround(x: f32): f32 { return x; } @@ -1831,8 +1856,8 @@ export namespace NativeMathf { return z * builtin_sqrt((x * x + y * y)); } - @inline - export function imul(x: f32, y: f32): f32 { + // @ts-ignore: decorator + @inline export function imul(x: f32, y: f32): f32 { /* * Wasm (MVP) and JS have different approaches for double->int conversions. * @@ -2010,13 +2035,13 @@ export namespace NativeMathf { return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + dk; } - @inline - export function max(value1: f32, value2: f32): f32 { + // @ts-ignore: decorator + @inline export function max(value1: f32, value2: f32): f32 { return builtin_max(value1, value2); } - @inline - export function min(value1: f32, value2: f32): f32 { + // @ts-ignore: decorator + @inline export function min(value1: f32, value2: f32): f32 { return builtin_min(value1, value2); } @@ -2209,8 +2234,8 @@ export namespace NativeMathf { return sn * z; } - @inline - export function seedRandom(value: i64): void { + // @ts-ignore: decorator + @inline export function seedRandom(value: i64): void { NativeMath.seedRandom(value); } @@ -2229,13 +2254,13 @@ export namespace NativeMathf { return reinterpret((r >> 9) | (127 << 23)) - 1.0; } - @inline - export function round(x: f32): f32 { + // @ts-ignore: decorator + @inline export function round(x: f32): f32 { return builtin_copysign(builtin_floor(x + 0.5), x); } - @inline - export function sign(x: f32): f32 { + // @ts-ignore: decorator + @inline export function sign(x: f32): f32 { if (ASC_SHRINK_LEVEL > 0) { return builtin_abs(x) > 0 ? builtin_copysign(1, x) : x; } else { @@ -2243,8 +2268,9 @@ export namespace NativeMathf { } } - @inline - export function signbit(x: f32): bool { + // @ts-ignore: decorator + @inline export function signbit(x: f32): bool { + // @ts-ignore: type return ((reinterpret(x) >>> 31) & (x == x)); } @@ -2308,8 +2334,8 @@ export namespace NativeMathf { return t; } - @inline - export function sqrt(x: f32): f32 { + // @ts-ignore: decorator + @inline export function sqrt(x: f32): f32 { return builtin_sqrt(x); } @@ -2377,8 +2403,8 @@ export namespace NativeMathf { return builtin_copysign(t, x); } - @inline - export function trunc(x: f32): f32 { + // @ts-ignore: decorator + @inline export function trunc(x: f32): f32 { return builtin_trunc(x); } @@ -2541,12 +2567,12 @@ export namespace NativeMathf { if (ix <= 0x3f490fda) { /* |x| ~<= π/4 */ if (ix < 0x39800000) { /* |x| < 2**-12 */ - sincos_s32 = x; - sincos_c32 = 1; + sincos_sin = x; + sincos_cos = 1; return; } - sincos_s32 = sin_kernf(x); - sincos_c32 = cos_kernf(x); + sincos_sin = sin_kernf(x); + sincos_cos = cos_kernf(x); return; } @@ -2554,33 +2580,33 @@ export namespace NativeMathf { if (ix <= 0x407b53d1) { /* |x| ~<= 5π/4 */ if (ix <= 0x4016cbe3) { /* |x| ~<= 3π/4 */ if (sign) { - sincos_s32 = -cos_kernf(x + s1pio2); - sincos_c32 = sin_kernf(x + s1pio2); + sincos_sin = -cos_kernf(x + s1pio2); + sincos_cos = sin_kernf(x + s1pio2); } else { - sincos_s32 = cos_kernf(s1pio2 - x); - sincos_c32 = sin_kernf(s1pio2 - x); + sincos_sin = cos_kernf(s1pio2 - x); + sincos_cos = sin_kernf(s1pio2 - x); } return; } /* -sin(x + c) is not correct if x+c could be 0: -0 vs +0 */ - sincos_s32 = -sin_kernf(sign ? x + s2pio2 : x - s2pio2); - sincos_c32 = -cos_kernf(sign ? x + s2pio2 : x - s2pio2); + sincos_sin = -sin_kernf(sign ? x + s2pio2 : x - s2pio2); + sincos_cos = -cos_kernf(sign ? x + s2pio2 : x - s2pio2); return; } if (ix <= 0x40e231d5) { /* |x| ~<= 9π/4 */ if (ix <= 0x40afeddf) { /* |x| ~<= 7π/4 */ if (sign) { - sincos_s32 = cos_kernf(x + s3pio2); - sincos_c32 = -sin_kernf(x + s3pio2); + sincos_sin = cos_kernf(x + s3pio2); + sincos_cos = -sin_kernf(x + s3pio2); } else { - sincos_s32 = -cos_kernf(x - s3pio2); - sincos_c32 = sin_kernf(x - s3pio2); + sincos_sin = -cos_kernf(x - s3pio2); + sincos_cos = sin_kernf(x - s3pio2); } return; } - sincos_s32 = sin_kernf(sign ? x + s4pio2 : x - s4pio2); - sincos_c32 = cos_kernf(sign ? x + s4pio2 : x - s4pio2); + sincos_sin = sin_kernf(sign ? x + s4pio2 : x - s4pio2); + sincos_cos = cos_kernf(sign ? x + s4pio2 : x - s4pio2); return; } } @@ -2588,8 +2614,8 @@ export namespace NativeMathf { /* sin(Inf or NaN) is NaN */ if (ix >= 0x7f800000) { let xx = x - x; - sincos_s32 = xx; - sincos_c32 = xx; + sincos_sin = xx; + sincos_cos = xx; return; } @@ -2601,24 +2627,24 @@ export namespace NativeMathf { switch (n & 3) { case 0: { - sincos_s32 = s; - sincos_c32 = c; + sincos_sin = s; + sincos_cos = c; break; } case 1: { - sincos_s32 = c; - sincos_c32 = -s; + sincos_sin = c; + sincos_cos = -s; break; } case 2: { - sincos_s32 = -s; - sincos_c32 = -c; + sincos_sin = -s; + sincos_cos = -c; break; } case 3: default: { - sincos_s32 = -c; - sincos_c32 = s; + sincos_sin = -c; + sincos_cos = s; break; } } diff --git a/std/assembly/memory.ts b/std/assembly/memory.ts index 260bc5e3..bd276f03 100644 --- a/std/assembly/memory.ts +++ b/std/assembly/memory.ts @@ -1,56 +1,70 @@ import { memcmp, memmove, memset } from "./util/memory"; +// @ts-ignore: decorator @builtin export declare const HEAP_BASE: usize; /** Memory manager interface. */ export namespace memory { /** Gets the size of the memory in pages. */ + // @ts-ignore: decorator @builtin export declare function size(): i32; /** Grows the memory by the given size in pages and returns the previous size in pages. */ + // @ts-ignore: decorator @unsafe @builtin export declare function grow(pages: i32): i32; /** Fills a section in memory with the specified byte value. */ + // @ts-ignore: decorator @unsafe @builtin export function fill(dst: usize, c: u8, n: usize): void { memset(dst, c, n); // fallback if "bulk-memory" isn't enabled } /** Copies a section of memory to another. Has move semantics. */ + // @ts-ignore: decorator @unsafe @builtin export function copy(dst: usize, src: usize, n: usize): void { memmove(dst, src, n); // fallback if "bulk-memory" isn't enabled } /** Initializes a memory segment. */ + // @ts-ignore: decorator @unsafe export function init(segmentIndex: u32, srcOffset: usize, dstOffset: usize, n: usize): void { ERROR("not implemented"); } /** Drops a memory segment. */ + // @ts-ignore: decorator @unsafe export function drop(segmentIndex: u32): void { ERROR("not implemented"); } /** Dynamically allocates a section of memory and returns its address. */ + // @ts-ignore: decorator @unsafe export function allocate(size: usize): usize { + // @ts-ignore: undefined if (isDefined(__memory_allocate)) return __memory_allocate(size); else ERROR("missing implementation: memory.allocate"); return unreachable(); } /** Dynamically frees a section of memory by the previously allocated address. */ + // @ts-ignore: decorator @unsafe export function free(ptr: usize): void { + // @ts-ignore: undefined if (isDefined(__memory_free)) __memory_free(ptr); else ERROR("missing implementation: memory.free"); } /** Resets the memory to its initial state. Arena allocator only. */ + // @ts-ignore: decorator @unsafe export function reset(): void { + // @ts-ignore: undefined if (isDefined(__memory_reset)) __memory_reset(); else ERROR("missing implementation: memory.reset"); } /** Repeats a section of memory at a specific address. */ + // @ts-ignore: decorator @unsafe export function repeat(dst: usize, src: usize, srcLength: usize, count: usize): void { var index: usize = 0; var total = srcLength * count; @@ -61,6 +75,7 @@ export namespace memory { } /** Compares a section of memory to another. */ + // @ts-ignore: decorator @inline export function compare(vl: usize, vr: usize, n: usize): i32 { return memcmp(vl, vr, n); } diff --git a/std/assembly/number.ts b/std/assembly/number.ts index e562b58a..b44b0654 100644 --- a/std/assembly/number.ts +++ b/std/assembly/number.ts @@ -3,7 +3,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class I8 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: i8 = i8.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: i8 = i8.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): i8 { @@ -18,7 +20,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class I16 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: i16 = i16.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: i16 = i16.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): i16 { @@ -33,7 +37,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class I32 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: i32 = i32.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: i32 = i32.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): i32 { @@ -48,7 +54,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class I64 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: i64 = i64.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: i64 = i64.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): i64 { @@ -63,7 +71,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class Isize { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: isize = isize.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: isize = isize.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): isize { @@ -78,7 +88,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class U8 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: u8 = u8.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: u8 = u8.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): u8 { @@ -93,7 +105,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class U16 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: u16 = u16.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: u16 = u16.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): u16 { @@ -108,7 +122,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class U32 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: u32 = u32.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: u32 = u32.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): u32 { @@ -123,7 +139,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class U64 { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: u64 = u64.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: u64 = u64.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): u64 { @@ -138,7 +156,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class Usize { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: usize = usize.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: usize = usize.MAX_VALUE; static parseInt(value: string, radix: i32 = 0): usize { @@ -153,7 +173,9 @@ import { isNaN as builtin_isNaN, isFinite as builtin_isFinite } from "./builtins @sealed export abstract class Bool { + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: bool = bool.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: bool = bool.MAX_VALUE; toString(this: bool): String { @@ -166,13 +188,21 @@ export { Bool as Boolean }; @sealed export abstract class F32 { + // @ts-ignore: decorator @lazy static readonly EPSILON: f32 = f32.EPSILON; + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: f32 = f32.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: f32 = f32.MAX_VALUE; + // @ts-ignore: decorator @lazy static readonly MIN_SAFE_INTEGER: f32 = f32.MIN_SAFE_INTEGER; + // @ts-ignore: decorator @lazy static readonly MAX_SAFE_INTEGER: f32 = f32.MAX_SAFE_INTEGER; + // @ts-ignore: decorator @lazy static readonly POSITIVE_INFINITY: f32 = Infinity; + // @ts-ignore: decorator @lazy static readonly NEGATIVE_INFINITY: f32 = -Infinity; + // @ts-ignore: decorator @lazy static readonly NaN: f32 = NaN; static isNaN(value: f32): bool { @@ -207,13 +237,21 @@ export { Bool as Boolean }; @sealed export abstract class F64 { + // @ts-ignore: decorator @lazy static readonly EPSILON: f64 = f64.EPSILON; + // @ts-ignore: decorator @lazy static readonly MIN_VALUE: f64 = f64.MIN_VALUE; + // @ts-ignore: decorator @lazy static readonly MAX_VALUE: f64 = f64.MAX_VALUE; + // @ts-ignore: decorator @lazy static readonly MIN_SAFE_INTEGER: f64 = f64.MIN_SAFE_INTEGER; + // @ts-ignore: decorator @lazy static readonly MAX_SAFE_INTEGER: f64 = f64.MAX_SAFE_INTEGER; + // @ts-ignore: decorator @lazy static readonly POSITIVE_INFINITY: f64 = Infinity; + // @ts-ignore: decorator @lazy static readonly NEGATIVE_INFINITY: f64 = -Infinity; + // @ts-ignore: decorator @lazy static readonly NaN: f64 = NaN; static isNaN(value: f64): bool { diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts index 00274f94..87b7bc4d 100644 --- a/std/assembly/polyfills.ts +++ b/std/assembly/polyfills.ts @@ -25,7 +25,7 @@ export function bswap(value: T): T { return value; } -@inline export function bswap16(value: T): T { +export function bswap16(value: T): T { if (isInteger() && sizeof() <= 4) { if (sizeof() == 2) { return ((value << 8) | ((value >> 8) & 0x00FF)); diff --git a/std/assembly/runtime.ts b/std/assembly/runtime.ts index d6b5727f..721b4198 100644 --- a/std/assembly/runtime.ts +++ b/std/assembly/runtime.ts @@ -9,11 +9,13 @@ export namespace runtime { @unmanaged export class Header { /** Size of a runtime header. */ + // @ts-ignore: decorator @lazy @inline static readonly SIZE: usize = gc.implemented ? (offsetof
( ) + AL_MASK) & ~AL_MASK // full header if GC is present : (offsetof
("gc1") + AL_MASK) & ~AL_MASK; // half header if GC is absent /** Magic value used to validate runtime headers. */ + // @ts-ignore: decorator @lazy @inline static readonly MAGIC: u32 = 0xA55E4B17; /** Unique id of the respective class or a magic value if not yet registered.*/ @@ -42,6 +44,7 @@ export namespace runtime { } /** Allocates a new object and returns a pointer to its payload. Does not fill. */ + // @ts-ignore: decorator @unsafe export function allocRaw(payloadSize: u32): usize { var header = changetype
(memory.allocate(adjust(payloadSize))); header.classId = Header.MAGIC; @@ -54,6 +57,7 @@ export namespace runtime { } /** Allocates a new object and returns a pointer to its payload. Fills with zeroes.*/ + // @ts-ignore: decorator @unsafe export function alloc(payloadSize: u32): usize { var ref = allocRaw(payloadSize); memory.fill(ref, 0, payloadSize); @@ -61,6 +65,7 @@ export namespace runtime { } /** Reallocates an object if necessary. Returns a pointer to its (moved) payload. */ + // @ts-ignore: decorator @unsafe export function realloc(ref: usize, newPayloadSize: u32): usize { // Background: When managed objects are allocated these aren't immediately registered with GC // but can be used as scratch objects while unregistered. This is useful in situations where @@ -111,11 +116,13 @@ export namespace runtime { } /** Frees an object. Must not have been registered with GC yet. */ + // @ts-ignore: decorator @unsafe @inline export function free(ref: T): void { memory.free(changetype(unref(changetype(ref)))); } /** Registers a managed object. Cannot be free'd anymore afterwards. */ + // @ts-ignore: decorator @unsafe @inline export function register(ref: usize): T { if (!isReference()) ERROR("reference expected"); // see comment in REALLOC why this is useful. also inline this because @@ -126,6 +133,7 @@ export namespace runtime { } /** Links a managed object with its managed parent. */ + // @ts-ignore: decorator @unsafe @inline export function link(ref: T, parentRef: TParent): void { assert(changetype(ref) >= HEAP_BASE + Header.SIZE); // must be a heap object var header = changetype
(changetype(ref) - Header.SIZE); @@ -141,8 +149,11 @@ export abstract class ArrayBufferView { [key: number]: number; + // @ts-ignore: decorator @unsafe data: ArrayBuffer; + // @ts-ignore: decorator @unsafe dataStart: usize; + // @ts-ignore: decorator @unsafe dataEnd: usize; constructor(length: i32, alignLog2: i32) { diff --git a/std/assembly/set.ts b/std/assembly/set.ts index bac34da2..ff8624a9 100644 --- a/std/assembly/set.ts +++ b/std/assembly/set.ts @@ -3,8 +3,11 @@ import { HASH } from "./util/hash"; // A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht +// @ts-ignore: decorator @inline const INITIAL_CAPACITY = 4; +// @ts-ignore: decorator @inline const FILL_FACTOR: f64 = 8 / 3; +// @ts-ignore: decorator @inline const FREE_FACTOR: f64 = 3 / 4; /** Structure of a set entry. */ @@ -14,12 +17,15 @@ import { HASH } from "./util/hash"; } /** Empty bit. */ +// @ts-ignore: decorator @inline const EMPTY: usize = 1 << 0; /** Size of a bucket. */ +// @ts-ignore: decorator @inline const BUCKET_SIZE = sizeof(); /** Computes the alignment of an entry. */ +// @ts-ignore: decorator @inline function ENTRY_ALIGN(): usize { // can align to 4 instead of 8 if 32-bit and K is <= 32-bits const align = (sizeof() > sizeof() ? sizeof() : sizeof()) - 1; @@ -27,6 +33,7 @@ import { HASH } from "./util/hash"; } /** Computes the aligned size of an entry. */ +// @ts-ignore: decorator @inline function ENTRY_SIZE(): usize { const align = ENTRY_ALIGN(); const size = (offsetof>() + align) & ~align; diff --git a/std/assembly/string.ts b/std/assembly/string.ts index 2765333b..77a1d24a 100644 --- a/std/assembly/string.ts +++ b/std/assembly/string.ts @@ -3,6 +3,7 @@ import { MAX_SIZE_32 } from "./util/allocator"; import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./util/string"; @sealed export abstract class String { + // @ts-ignore: decorator @lazy static readonly MAX_LENGTH: i32 = (MAX_SIZE_32 - runtime.Header.SIZE) >> alignof(); get length(): i32 { @@ -19,6 +20,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut static fromCodePoint(code: i32): String { assert(code <= 0x10FFFF); var sur = code > 0xFFFF; + // @ts-ignore: type var out = runtime.allocRaw((sur + 1) << 1); if (!sur) { store(out, code); @@ -80,6 +82,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut var searchLength: isize = searchString.length; var start: isize = end - searchLength; if (start < 0) return false; + // @ts-ignore: string/String return !compareImpl(this, start, searchString, 0, searchLength); } @@ -88,6 +91,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut if (left === null || right === null) return false; var leftLength = left.length; if (leftLength != right.length) return false; + // @ts-ignore: string/String return !compareImpl(left, 0, right, 0, leftLength); } @@ -101,6 +105,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut var rightLength = right.length; if (!leftLength) return false; if (!rightLength) return true; + // @ts-ignore: string/String return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) > 0; } @@ -114,6 +119,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut var rightLength = right.length; if (!rightLength) return false; if (!leftLength) return true; + // @ts-ignore: string/String return compareImpl(left, 0, right, 0, min(leftLength, rightLength)) < 0; } @@ -135,6 +141,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut var start = min(max(fromIndex, 0), len); len -= searchLen; for (let k: isize = start; k <= len; ++k) { + // @ts-ignore: string/String if (!compareImpl(this, k, searchString, 0, searchLen)) return k; } return -1; @@ -149,6 +156,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut if (!len) return -1; var start = min(max(fromIndex, 0), len - searchLen); for (let k = start; k >= 0; --k) { + // @ts-ignore: string/String if (!compareImpl(this, k, searchString, 0, searchLen)) return k; } return -1; @@ -162,6 +170,7 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut var start = min(max(pos, 0), len); var searchLength: isize = searchString.length; if (searchLength + start > len) return false; + // @ts-ignore: string/String return !compareImpl(this, start, searchString, 0, searchLength); } @@ -510,17 +519,21 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut } } +// @ts-ignore: nolib export type string = String; export function parseInt(str: String, radix: i32 = 0): f64 { + // @ts-ignore: string/String return parse(str, radix); } export function parseI32(str: String, radix: i32 = 0): i32 { + // @ts-ignore: string/String return parse(str, radix); } export function parseI64(str: String, radix: i32 = 0): i64 { + // @ts-ignore: string/String return parse(str, radix); } diff --git a/std/assembly/symbol.ts b/std/assembly/symbol.ts index 625b2c0b..e50325f6 100644 --- a/std/assembly/symbol.ts +++ b/std/assembly/symbol.ts @@ -4,6 +4,7 @@ import { Map } from "./map"; @lazy var idToString: Map; @lazy var nextId: usize = 12; // Symbol.unscopables + 1 +// @ts-ignore: nolib @unmanaged export class symbol { toString(): string { var id = changetype(this); @@ -52,6 +53,7 @@ export namespace Symbol { @lazy export const unscopables = changetype(11); /* tslint:disable */// not valid TS + // @ts-ignore: identifier export function for(key: string): symbol { if (!stringToId) { stringToId = new Map(); idToString = new Map(); } else if (stringToId.has(key)) return changetype(stringToId.get(key)); diff --git a/std/assembly/typedarray.ts b/std/assembly/typedarray.ts index 2f1b89b9..f23c2c9e 100644 --- a/std/assembly/typedarray.ts +++ b/std/assembly/typedarray.ts @@ -6,6 +6,7 @@ function clampToByte(value: i32): i32 { } export class Int8Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -68,6 +69,7 @@ export class Int8Array extends ArrayBufferView { } export class Uint8Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -124,6 +126,7 @@ export class Uint8Array extends ArrayBufferView { } export class Uint8ClampedArray extends Uint8Array { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8ClampedArray { @@ -174,6 +177,7 @@ export class Uint8ClampedArray extends Uint8Array { } export class Int16Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -236,6 +240,7 @@ export class Int16Array extends ArrayBufferView { } export class Uint16Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -298,6 +303,7 @@ export class Uint16Array extends ArrayBufferView { } export class Int32Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -360,6 +366,7 @@ export class Int32Array extends ArrayBufferView { } export class Uint32Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -422,6 +429,7 @@ export class Uint32Array extends ArrayBufferView { } export class Int64Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -484,6 +492,7 @@ export class Int64Array extends ArrayBufferView { } export class Uint64Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -546,6 +555,7 @@ export class Uint64Array extends ArrayBufferView { } export class Float32Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -608,6 +618,7 @@ export class Float32Array extends ArrayBufferView { } export class Float64Array extends ArrayBufferView { + // @ts-ignore: decorator @lazy static readonly BYTES_PER_ELEMENT: usize = sizeof(); constructor(length: i32) { @@ -669,6 +680,7 @@ export class Float64Array extends ArrayBufferView { } } +// @ts-ignore: decorator @inline function FILL( array: TArray, value: native, @@ -689,6 +701,7 @@ export class Float64Array extends ArrayBufferView { return array; } +// @ts-ignore: decorator @inline function SORT( array: TArray, comparator: (a: T, b: T) => i32 @@ -709,6 +722,7 @@ export class Float64Array extends ArrayBufferView { return array; } +// @ts-ignore: decorator @inline function SUBARRAY( array: TArray, begin: i32, @@ -728,6 +742,7 @@ export class Float64Array extends ArrayBufferView { return changetype(out); } +// @ts-ignore: decorator @inline function REDUCE( array: TArray, callbackfn: (accumulator: TRet, value: T, index: i32, array: TArray) => TRet, @@ -740,6 +755,7 @@ export class Float64Array extends ArrayBufferView { return initialValue; } +// @ts-ignore: decorator @inline function REDUCE_RIGHT( array: TArray, callbackfn: (accumulator: TRet, value: T, index: i32, array: TArray) => TRet, @@ -752,6 +768,7 @@ export class Float64Array extends ArrayBufferView { return initialValue; } +// @ts-ignore: decorator @inline function MAP( array: TArray, callbackfn: (value: T, index: i32, self: TArray) => T, @@ -769,6 +786,7 @@ export class Float64Array extends ArrayBufferView { return out; } +// @ts-ignore: decorator @inline function FIND_INDEX( array: TArray, callbackfn: (value: T, index: i32, array: TArray) => bool, @@ -780,6 +798,7 @@ export class Float64Array extends ArrayBufferView { return -1; } +// @ts-ignore: decorator @inline function SOME( array: TArray, callbackfn: (value: T, index: i32, array: TArray) => bool, @@ -791,6 +810,7 @@ export class Float64Array extends ArrayBufferView { return false; } +// @ts-ignore: decorator @inline function EVERY( array: TArray, callbackfn: (value: T, index: i32, array: TArray) => bool, @@ -803,6 +823,7 @@ export class Float64Array extends ArrayBufferView { return true; } +// @ts-ignore: decorator @inline function FOREACH( array: TArray, callbackfn: (value: T, index: i32, array: TArray) => void, diff --git a/std/assembly/util/allocator.ts b/std/assembly/util/allocator.ts index 508710c6..af722401 100644 --- a/std/assembly/util/allocator.ts +++ b/std/assembly/util/allocator.ts @@ -1,8 +1,15 @@ /** Number of alignment bits. */ +// @ts-ignore: decorator @inline export const AL_BITS: u32 = 3; + /** Number of possible alignment values. */ +// @ts-ignore: decorator @inline export const AL_SIZE: usize = 1 << AL_BITS; + /** Mask to obtain just the alignment bits. */ +// @ts-ignore: decorator @inline export const AL_MASK: usize = AL_SIZE - 1; + /** Maximum 32-bit allocation size. */ +// @ts-ignore: decorator @inline export const MAX_SIZE_32: usize = 1 << 30; // 1GB diff --git a/std/assembly/util/hash.ts b/std/assembly/util/hash.ts index f4dcea3e..86a3d92a 100644 --- a/std/assembly/util/hash.ts +++ b/std/assembly/util/hash.ts @@ -1,16 +1,24 @@ +// @ts-ignore: decorator @inline export function HASH(key: T): u32 { if (isString(key)) { + // @ts-ignore: type return hashStr(key); } else if (isReference()) { if (sizeof() == 4) return hash32(changetype(key)); if (sizeof() == 8) return hash64(changetype(key)); } else if (isFloat()) { + // @ts-ignore: type if (sizeof() == 4) return hash32(reinterpret(key)); + // @ts-ignore: type if (sizeof() == 8) return hash64(reinterpret(key)); } else { + // @ts-ignore: type if (sizeof() == 1) return hash8 (key); + // @ts-ignore: type if (sizeof() == 2) return hash16(key); + // @ts-ignore: type if (sizeof() == 4) return hash32(key); + // @ts-ignore: type if (sizeof() == 8) return hash64(key); } unreachable(); @@ -18,7 +26,9 @@ // FNV-1a 32-bit as a starting point, see: http://isthe.com/chongo/tech/comp/fnv/ +// @ts-ignore: decorator @inline const FNV_OFFSET: u32 = 2166136261; +// @ts-ignore: decorator @inline const FNV_PRIME: u32 = 16777619; function hash8(key: u32): u32 { diff --git a/std/assembly/util/number.ts b/std/assembly/util/number.ts index 45dbefe1..48bb41b4 100644 --- a/std/assembly/util/number.ts +++ b/std/assembly/util/number.ts @@ -1,8 +1,10 @@ import { runtime } from "../runtime"; import { CharCode } from "./string"; +// @ts-ignore: decorator @inline export const MAX_DOUBLE_LENGTH = 28; +// @ts-ignore: decorator @lazy @inline const POWERS10: u32[] = [ 1, 10, @@ -30,6 +32,7 @@ import { CharCode } from "./string"; "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99" */ +// @ts-ignore: decorator @lazy @inline const DIGITS: u32[] = [ 0x00300030, 0x00310030, 0x00320030, 0x00330030, 0x00340030, 0x00350030, 0x00360030, 0x00370030, 0x00380030, 0x00390030, @@ -53,6 +56,7 @@ import { CharCode } from "./string"; 0x00350039, 0x00360039, 0x00370039, 0x00380039, 0x00390039 ]; +// @ts-ignore: decorator @lazy @inline const EXP_POWERS: i16[] = [ -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, @@ -66,6 +70,7 @@ import { CharCode } from "./string"; ]; // 1e-348, 1e-340, ..., 1e340 +// @ts-ignore: decorator @lazy @inline const FRC_POWERS: u64[] = [ 0xFA8FD5A0081C0288, 0xBAAEE17FA23EBF76, 0x8B16FB203055AC76, 0xCF42894A5DCE35EA, 0x9A6BB0AA55653B2D, 0xE61ACF033D1A45DF, 0xAB70FE17C79AC6CA, 0xFF77B1FCBEBCDC4F, diff --git a/std/assembly/util/sort.ts b/std/assembly/util/sort.ts index 40690d20..6604972d 100644 --- a/std/assembly/util/sort.ts +++ b/std/assembly/util/sort.ts @@ -1,45 +1,59 @@ import { compareImpl } from "./string"; +// @ts-ignore: decorator @inline export function COMPARATOR(): (a: T, b: T) => i32 { if (isInteger()) { if (isSigned() && sizeof() <= 4) { + // @ts-ignore: type return (a: T, b: T): i32 => ((a - b)); } else { + // @ts-ignore: type return (a: T, b: T): i32 => ((a > b) - (a < b)); } } else if (isFloat()) { if (sizeof() == 4) { return (a: T, b: T): i32 => { + // @ts-ignore: type var ia = reinterpret(a); + // @ts-ignore: type var ib = reinterpret(b); ia ^= (ia >> 31) >>> 1; ib ^= (ib >> 31) >>> 1; + // @ts-ignore: type return (ia > ib) - (ia < ib); }; } else { return (a: T, b: T): i32 => { + // @ts-ignore: type var ia = reinterpret(a); + // @ts-ignore: type var ib = reinterpret(b); ia ^= (ia >> 63) >>> 1; ib ^= (ib >> 63) >>> 1; + // @ts-ignore: type return (ia > ib) - (ia < ib); }; } } else if (isString()) { return (a: T, b: T): i32 => { if (a === b || a === null || b === null) return 0; + // @ts-ignore: type var alen = (a).length; + // @ts-ignore: type var blen = (b).length; if (!alen && !blen) return 0; if (!alen) return -1; if (!blen) return 1; + // @ts-ignore: type return compareImpl(a, 0, b, 0, min(alen, blen)); }; } else { + // @ts-ignore: type return (a: T, b: T): i32 => ((a > b) - (a < b)); } } +// @ts-ignore: decorator @inline export function SORT( dataStart: usize, length: i32, diff --git a/std/assembly/util/string.ts b/std/assembly/util/string.ts index a3374134..49579ef6 100644 --- a/std/assembly/util/string.ts +++ b/std/assembly/util/string.ts @@ -8,6 +8,7 @@ export function compareImpl(str1: string, index1: usize, str2: string, index2: u return result; } +// @ts-ignore: decorator @inline export const enum CharCode { PLUS = 0x2B, MINUS = 0x2D, @@ -57,29 +58,35 @@ export function isWhiteSpaceOrLineTerminator(c: u16): bool { /** Parses a string to an integer (usually), using the specified radix. */ export function parse(str: string, radix: i32 = 0): T { var len: i32 = str.length; + // @ts-ignore: type if (!len) return NaN; var ptr = changetype(str) /* + HEAD -> offset */; - var code = load(ptr, HEADER_SIZE); + var code = load(ptr); // determine sign var sign: T; if (code == CharCode.MINUS) { + // @ts-ignore: type if (!--len) return NaN; - code = load(ptr += 2, HEADER_SIZE); + code = load(ptr += 2); + // @ts-ignore: type sign = -1; } else if (code == CharCode.PLUS) { + // @ts-ignore: type if (!--len) return NaN; - code = load(ptr += 2, HEADER_SIZE); + code = load(ptr += 2); + // @ts-ignore: type sign = 1; } else { + // @ts-ignore: type sign = 1; } // determine radix if (!radix) { if (code == CharCode._0 && len > 2) { - switch (load(ptr + 2, HEADER_SIZE)) { + switch (load(ptr + 2)) { case CharCode.B: case CharCode.b: { ptr += 4; len -= 2; @@ -102,13 +109,15 @@ export function parse(str: string, radix: i32 = 0): T { } } else radix = 10; } else if (radix < 2 || radix > 36) { + // @ts-ignore: cast return NaN; } // calculate value + // @ts-ignore: type var num: T = 0; while (len--) { - code = load(ptr, HEADER_SIZE); + code = load(ptr); if (code >= CharCode._0 && code <= CharCode._9) { code -= CharCode._0; } else if (code >= CharCode.A && code <= CharCode.Z) { @@ -117,8 +126,10 @@ export function parse(str: string, radix: i32 = 0): T { code -= CharCode.a - 10; } else break; if (code >= radix) break; + // @ts-ignore: type num = (num * radix) + code; ptr += 2; } + // @ts-ignore: type return sign * num; }