mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-17 17:01:37 +00:00
More stdlib setup
This commit is contained in:
@ -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
|
||||
}
|
Reference in New Issue
Block a user