Stdlib preparations

This commit is contained in:
dcodeIO
2017-12-16 02:27:39 +01:00
parent 4b3cc981a6
commit 85a9fb1eb5
50 changed files with 834 additions and 137 deletions

View File

@ -2,6 +2,7 @@
// 1. C-like with no 'length' or 'push'
// 2. Descriptors that can be constructed from lower level arrays
@global()
class Array<T> {
readonly capacity: i32;
@ -33,4 +34,6 @@ class Array<T> {
this.ptr = 0;
Heap.dispose(changetype<this,usize>(this));
}
static test(): void {}
}

View File

@ -5,7 +5,6 @@ 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 {
@ -180,6 +179,4 @@ class Heap {
}
return dest;
}
private constructor() {}
}

View File

@ -66,9 +66,12 @@ globalScope["sqrt"] = Math.sqrt;
globalScope["trunc"] = Math.trunc;
function UnreachableError() {
this.stack = new Error().stack;
if (Error.captureStackTrace)
Error.captureStackTrace(this, UnreachableError);
else
this.stack = this.name + ": " + this.message + "\n" + new Error().stack;
}
UnreachableError.prototype = new Error;
UnreachableError.prototype = Object.create(Error.prototype);
UnreachableError.prototype.name = "UnreachableError";
UnreachableError.prototype.message = "unreachable";
@ -76,9 +79,12 @@ globalScope["unreachable"] = function unreachable() { throw new UnreachableError
function AssertionError(message) {
this.message = message || "assertion failed";
this.stack = new Error().stack;
if (Error.captureStackTrace)
Error.captureStackTrace(this, AssertionError);
else
this.stack = this.name + ": " + this.message + "\n" + new Error().stack;
}
AssertionError.prototype = new Error;
AssertionError.prototype = Object.create(Error.prototype);
AssertionError.prototype.name = "AssertionError";
globalScope["assert"] = function assert(isTrue, message) { if (!isTrue) throw new AssertionError(message); };

View File

@ -5,7 +5,7 @@ var HEAP_OFFSET = 0;
Object.defineProperties(globalScope["Heap"] = {
allocate: function allocate(size) {
if (!size) return 0;
if (!(size >>>= 0)) return 0;
if (HEAP_OFFSET + size > HEAP.length) {
var oldHeap = HEAP;
HEAP = new Uint8Array(Math.max(65536, HEAP.length + size, HEAP.length * 2));
@ -18,7 +18,7 @@ Object.defineProperties(globalScope["Heap"] = {
},
dispose: function dispose() { },
copy: function copy(dest, src, n) {
HEAP.set(HEAP.subarray(src, src + n), dest);
HEAP.set(HEAP.subarray(src >>> 0, (src + n) >>> 0), dest >>> 0);
return dest;
}
}, {
@ -26,9 +26,6 @@ Object.defineProperties(globalScope["Heap"] = {
free: { get: function get_free() { return HEAP.length - HEAP_OFFSET; } },
size: { get: function get_size() { return HEAP.length; } }
});
globalScope["store"] = function store(ptr, val) {
binaryen.HEAPU8[ptr] = val;
};
globalScope["load"] = function load(ptr) {
return binaryen.HEAPU8[ptr];
};
globalScope["store"] = function store(ptr, val) { binaryen.HEAPU8[ptr] = val; };
globalScope["load"] = function load(ptr) { return binaryen.HEAPU8[ptr]; };