mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-13 15:01:28 +00:00
More stdlib setup
This commit is contained in:
88
std/assembly.d.ts
vendored
88
std/assembly.d.ts
vendored
@ -157,8 +157,8 @@ declare function unreachable(): any; // sic
|
||||
declare const NaN: f32 | f64;
|
||||
/** Positive infinity as a 32-bit or 64-bit float depending on context. */
|
||||
declare const Infinity: f32 | f64;
|
||||
/** Heap start offset. */
|
||||
declare const HEAP_START: usize;
|
||||
/** Heap base offset. */
|
||||
declare const HEAP_BASE: usize;
|
||||
/** Determines the byte size of the specified core or class type. Compiles to a constant. */
|
||||
declare function sizeof<T>(): usize;
|
||||
/** Changes the type of a value to another one. Useful for casting class instances to their pointer values and vice-versa. */
|
||||
@ -174,18 +174,72 @@ declare function parseInt(str: string, radix?: i32): f64;
|
||||
/** Parses a string to a 64-bit float. */
|
||||
declare function parseFloat(str: string): f64;
|
||||
|
||||
// Internal decorators (not yet implemented)
|
||||
|
||||
/** Annotates an element being part of the global namespace. */
|
||||
declare function global(): any;
|
||||
/** Annotates a function being always inlined. */
|
||||
declare function inline(): any;
|
||||
/** Annotates a class using a C-style memory layout. */
|
||||
declare function struct(): any;
|
||||
|
||||
// Standard library (not yet implemented)
|
||||
|
||||
interface Array<T> {}
|
||||
/** Class representing a sequence of values of type `T`. */
|
||||
declare class Array<T> {
|
||||
|
||||
/** Current maximum capacity of the array. */
|
||||
readonly capacity: i32;
|
||||
|
||||
/** Current length of the array. */
|
||||
length: i32;
|
||||
|
||||
/** Constructs a new array. */
|
||||
constructor(capacity?: i32);
|
||||
}
|
||||
|
||||
/** Class representing a sequence of characters. */
|
||||
declare class String {
|
||||
static fromCharCode(ls: i32, hs?: i32): string;
|
||||
static fromCharCodes(arr: u16[]): string;
|
||||
static fromCodePoint(cp: i32): string;
|
||||
static fromCodePoints(arr: i32[]): string;
|
||||
}
|
||||
|
||||
/** Class for representing a runtime error. Base class of all errors. */
|
||||
declare class Error {
|
||||
|
||||
/** Error name. */
|
||||
name: string;
|
||||
|
||||
/** Message provided on construction. */
|
||||
message: string;
|
||||
|
||||
/** Stack trace. */
|
||||
stack: string;
|
||||
|
||||
/** Constructs a new error, optionally with a message. */
|
||||
constructor(message?: string);
|
||||
}
|
||||
|
||||
/** Class for indicating an error when a value is not in the set or range of allowed values. */
|
||||
declare class RangeError extends Error { }
|
||||
|
||||
/** A static class representing the heap. */
|
||||
declare class Heap {
|
||||
|
||||
/** Gets the amount of used heap space, in bytes. */
|
||||
static readonly used: usize;
|
||||
|
||||
/** Gets the amount of free heap space, in bytes. */
|
||||
static readonly free: usize;
|
||||
|
||||
/** Gets the size of the heap, in bytes. */
|
||||
static readonly size: usize;
|
||||
|
||||
/** Allocates a chunk of memory and returns a pointer to it. */
|
||||
static allocate(size: usize): usize;
|
||||
|
||||
/** Disposes a chunk of memory by its pointer. */
|
||||
static dispose(ptr: usize): void;
|
||||
|
||||
/** Copies a chunk of memory from one location to another. */
|
||||
static copy(dest: usize, src: usize, n: usize): usize;
|
||||
|
||||
private constructor();
|
||||
}
|
||||
|
||||
interface Boolean {}
|
||||
interface Function {}
|
||||
interface IArguments {}
|
||||
@ -193,9 +247,7 @@ interface Number {}
|
||||
interface Object {}
|
||||
interface RegExp {}
|
||||
|
||||
declare class String {
|
||||
static fromCharCode(ls: i32, hs?: i32): string;
|
||||
static fromCharCodes(arr: u16[]): string;
|
||||
static fromCodePoint(cp: i32): string;
|
||||
static fromCodePoints(arr: i32[]): string;
|
||||
}
|
||||
// Internal decorators (not yet implemented)
|
||||
|
||||
/** Annotates an element being part of the global namespace. */
|
||||
declare function global(): any;
|
||||
|
@ -1,25 +1,13 @@
|
||||
// Multiple options:
|
||||
// 1. C-like with no 'length' or 'push'
|
||||
// 2. Descriptors that can be constructed from lower level arrays
|
||||
|
||||
@global()
|
||||
class Array<T> {
|
||||
export class Array<T> {
|
||||
|
||||
readonly capacity: i32;
|
||||
length: i32;
|
||||
ptr: usize;
|
||||
|
||||
static fromPtr<T>(ptr: usize, capacity: i32): Array<T> {
|
||||
assert(capacity >= 0);
|
||||
const arr: Array<T> = new Array(0);
|
||||
store<i32>(changetype<Array<T>, usize>(arr), capacity);
|
||||
arr.length = ptr;
|
||||
arr.ptr = ptr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
constructor(capacity: i32 = 0) {
|
||||
assert(capacity >= 0);
|
||||
if (capacity < 0)
|
||||
throw new RangeError("invalid array length");
|
||||
this.capacity = this.length = capacity;
|
||||
if (capacity > 0) {
|
||||
this.ptr = Heap.allocate(<usize>capacity);
|
||||
@ -35,5 +23,5 @@ class Array<T> {
|
||||
Heap.dispose(changetype<this,usize>(this));
|
||||
}
|
||||
|
||||
static test(): void {}
|
||||
// TODO
|
||||
}
|
||||
|
16
std/assembly/error.ts
Normal file
16
std/assembly/error.ts
Normal file
@ -0,0 +1,16 @@
|
||||
@global()
|
||||
export class Error {
|
||||
|
||||
name: string = "Error";
|
||||
message: string;
|
||||
stack: string = ""; // TODO
|
||||
|
||||
constructor(message: string = "") {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
@global()
|
||||
export class RangeError extends Error {
|
||||
name: string = "RangeError";
|
||||
}
|
@ -2,10 +2,16 @@ const ALIGN_LOG2: usize = 3;
|
||||
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;
|
||||
const ALIGN_MASK: usize = ALIGN_SIZE - 1;
|
||||
|
||||
let HEAP_OFFSET: usize = HEAP_START; // HEAP_START is a constant generated by the compiler
|
||||
let HEAP_OFFSET: usize = HEAP_BASE; // HEAP_BASE is a constant generated by the compiler
|
||||
|
||||
// TODO: maybe tlsf
|
||||
|
||||
@global()
|
||||
class Heap {
|
||||
export class Heap {
|
||||
|
||||
static get used(): usize { return HEAP_OFFSET - HEAP_BASE; }
|
||||
static get free(): usize { return (<usize>current_memory() << 16) - HEAP_OFFSET; }
|
||||
static get size(): usize { return (<usize>current_memory() << 16) - HEAP_BASE; }
|
||||
|
||||
static allocate(size: usize): usize {
|
||||
if (!size) return 0;
|
||||
@ -23,20 +29,8 @@ class Heap {
|
||||
// just a big chunk of non-disposable memory for now
|
||||
}
|
||||
|
||||
static get used(): usize {
|
||||
return HEAP_OFFSET - HEAP_START;
|
||||
}
|
||||
|
||||
static get free(): usize {
|
||||
return (<usize>current_memory() << 16) - HEAP_OFFSET;
|
||||
}
|
||||
|
||||
static get size(): usize {
|
||||
return (<usize>current_memory() << 16) - HEAP_START;
|
||||
}
|
||||
|
||||
static copy(dest: usize, src: usize, n: usize): usize {
|
||||
assert(dest >= HEAP_START);
|
||||
assert(dest >= HEAP_BASE);
|
||||
|
||||
// the following is based on musl's implementation of memcpy
|
||||
let dst: usize = dest;
|
||||
@ -179,4 +173,6 @@ class Heap {
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
private constructor() {}
|
||||
}
|
||||
|
4
std/assembly/map.ts
Normal file
4
std/assembly/map.ts
Normal file
@ -0,0 +1,4 @@
|
||||
@global()
|
||||
export class Map<K,V> {
|
||||
// TODO
|
||||
}
|
4
std/assembly/set.ts
Normal file
4
std/assembly/set.ts
Normal file
@ -0,0 +1,4 @@
|
||||
@global()
|
||||
export class Set<T> {
|
||||
// TODO
|
||||
}
|
4
std/assembly/string.ts
Normal file
4
std/assembly/string.ts
Normal file
@ -0,0 +1,4 @@
|
||||
@global()
|
||||
export class String {
|
||||
// TODO
|
||||
}
|
@ -37,7 +37,7 @@ Object.defineProperties(
|
||||
"MAX_VALUE": { value: 4294967295, writable: false }
|
||||
});
|
||||
Object.defineProperties(
|
||||
globalScope["bool"] = function bool(value) { return Boolean(value); }
|
||||
globalScope["bool"] = function bool(value) { return !!value; }
|
||||
, {
|
||||
"MIN_VALUE": { value: 0, writable: false },
|
||||
"MAX_VALUE": { value: 1, writable: false }
|
||||
|
3
std/portable/heap.d.ts
vendored
3
std/portable/heap.d.ts
vendored
@ -15,4 +15,7 @@ declare class Heap {
|
||||
|
||||
/** Gets the size of the heap, in bytes. */
|
||||
static readonly size: usize;
|
||||
|
||||
/** Copies a chunk of memory from one location to another. */
|
||||
static copy(dest: usize, src: usize, n: usize): usize;
|
||||
}
|
||||
|
Reference in New Issue
Block a user