mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-20 10:16:37 +00:00
changetype builtin; some namespace parsing; more stdlib ideas; compiler options for asc
This commit is contained in:
@ -1,29 +1,25 @@
|
||||
/// <reference path="../../assembly.d.ts" />
|
||||
|
||||
/** A C-compatible Array class. */
|
||||
@global()
|
||||
@struct()
|
||||
class CArray<T> {
|
||||
|
||||
/** Constructs a new C-Array of the specified capacity. */
|
||||
constructor(capacity: usize) {
|
||||
return unsafe_cast<usize,this>(Memory.allocate(capacity * sizeof<T>()));
|
||||
return changetype<usize, this>(Heap.allocate(capacity * sizeof<T>()));
|
||||
}
|
||||
|
||||
/** Gets the element at the specified index using bracket notation. */
|
||||
@inline()
|
||||
"[]"(index: usize): T {
|
||||
return load<T>(unsafe_cast<this,usize>(this) + index * sizeof<T>());
|
||||
return load<T>(changetype<this, usize>(this) + index * sizeof<T>());
|
||||
}
|
||||
|
||||
/** Sets the element at the specified index using bracket notation. */
|
||||
@inline()
|
||||
"[]="(index: usize, value: T): T {
|
||||
store<T>(unsafe_cast<this,usize>(this) + index * sizeof<T>(), value);
|
||||
store<T>(changetype<this, usize>(this) + index * sizeof<T>(), value);
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Disposes this instance and the memory associated with it. */
|
||||
dispose(): void {
|
||||
Memory.dispose(unsafe_cast<this,usize>(this));
|
||||
Heap.dispose(changetype<this, usize>(this));
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,16 @@
|
||||
/// <reference path="../../assembly.d.ts" />
|
||||
|
||||
/** A C-compatible string class. */
|
||||
@global()
|
||||
class CString extends CArray<u8> {
|
||||
@struct()
|
||||
class CString {
|
||||
|
||||
/** Constructs a new C-String from a String. */
|
||||
constructor(text: string) {
|
||||
super(text.length * 2 + 1);
|
||||
let idx: usize = unsafe_cast<this,usize>(this);
|
||||
for (let i: usize = 0, k: usize = (<string>str).length; i < k; ++i) {
|
||||
let u: i32 = text.charCodeAt(i);
|
||||
constructor(string: string) {
|
||||
const ptr: usize = Heap.allocate(<usize>string.length * 2 + 1);
|
||||
let idx: usize = ptr;
|
||||
for (let i: usize = 0, k: usize = <usize>(<string>str).length; i < k; ++i) {
|
||||
let u: i32 = string.charCodeAt(i);
|
||||
if (u >= 0xD800 && u <= 0xDFFF && i + 1 < k)
|
||||
u = 0x10000 + ((u & 0x3FF) << 10) | (text.charCodeAt(++i) & 0x3FF);
|
||||
u = 0x10000 + ((u & 0x3FF) << 10) | (string.charCodeAt(++i) & 0x3FF);
|
||||
if (u <= 0x7F)
|
||||
store<u8>(idx++, u as u8);
|
||||
else if (u <= 0x7FF) {
|
||||
@ -43,5 +42,17 @@ class CString extends CArray<u8> {
|
||||
}
|
||||
}
|
||||
store<u8>(idx, 0);
|
||||
return changetype<usize, this>(ptr);
|
||||
}
|
||||
|
||||
@inline()
|
||||
"[]"(index: usize): u8 {
|
||||
return load<u8>(changetype<this, usize>(this) + index /* * sizeof<u8>() */);
|
||||
}
|
||||
|
||||
// read-only
|
||||
|
||||
dispose(): void {
|
||||
Heap.dispose(changetype<this, usize>(this));
|
||||
}
|
||||
}
|
||||
|
38
std/impl/heap.ts
Normal file
38
std/impl/heap.ts
Normal file
@ -0,0 +1,38 @@
|
||||
/// <reference path="../../assembly.d.ts" />
|
||||
|
||||
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
|
||||
|
||||
@global()
|
||||
@struct()
|
||||
class Heap {
|
||||
|
||||
static allocate(size: usize): usize {
|
||||
const ptr: usize = HEAP_OFFSET;
|
||||
assert(ptr + size <= (<usize>current_memory() << 16));
|
||||
if (((HEAP_OFFSET += size) & ALIGN_MASK) != 0) // align next offset
|
||||
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static dispose(ptr: usize): void {
|
||||
// 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;
|
||||
}
|
||||
|
||||
private constructor() {}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
/// <reference path="../../assembly.d.ts" />
|
||||
|
||||
const MEMORY_ALIGN_LOG2: usize = 3;
|
||||
const MEMORY_ALIGN_SIZE: usize = 1 << MEMORY_ALIGN_LOG2;
|
||||
const MEMORY_ALIGN_MASK: usize = MEMORY_ALIGN_SIZE - 1;
|
||||
|
||||
@global()
|
||||
class Memory {
|
||||
|
||||
static allocate(size: usize): usize {
|
||||
const ptr: usize = HEAP_OFFSET;
|
||||
HEAP_OFFSET += size;
|
||||
if ((HEAP_OFFSET & MEMORY_ALIGN_MASK) != 0)
|
||||
HEAP_OFFSET = (HEAP_OFFSET | MEMORY_ALIGN_MASK) + 1;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static dispose(ptr: usize): void {
|
||||
// just a big chunk of non-disposable memory for now
|
||||
}
|
||||
}
|
@ -6,6 +6,6 @@
|
||||
"files": [
|
||||
"carray.ts",
|
||||
"cstring.ts",
|
||||
"memory.ts"
|
||||
"heap.ts"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user