changetype builtin; some namespace parsing; more stdlib ideas; compiler options for asc

This commit is contained in:
dcodeIO
2017-12-08 04:03:44 +01:00
parent 59dafc8d22
commit bbb57baecb
62 changed files with 636 additions and 469 deletions

View File

@ -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));
}
}

View File

@ -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
View 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() {}
}

View File

@ -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
}
}

View File

@ -6,6 +6,6 @@
"files": [
"carray.ts",
"cstring.ts",
"memory.ts"
"heap.ts"
]
}