Improve portables (#386)

* Hardened load/save
* Added memory.reset and memory.fill
* Added isDefined and isConstant
* Use true/false as bool's min/max values
This commit is contained in:
Max Graey 2019-01-09 14:17:18 +02:00 committed by Daniel Wirtz
parent 9ec226de1b
commit e3e0fe8045
2 changed files with 25 additions and 4 deletions

View File

@ -94,6 +94,10 @@ declare function isReference(value: any): value is object | string;
declare function isString(value: any): value is string | String;
/** Tests if the specified value can be used as an array. */
declare function isArray(value: any): value is Array<any>;
/** Tests if the specified expression resolves to a defined element. */
declare function isDefined(expression: any): bool;
/** Tests if the specified expression evaluates to a constant value. */
declare function isConstant(expression: any): bool;
/** Traps if the specified value is not true-ish, otherwise returns the value. */
declare function assert<T>(isTrueish: T | null, message?: string): T;
/** Parses an integer string to a 64-bit float. */
@ -285,6 +289,8 @@ declare namespace memory {
function free(ptr: usize): void;
/** Copies n bytes from the specified source to the specified destination in memory. These regions may overlap. */
function copy(dst: usize, src: usize, n: usize): void;
/** Fills size bytes from from the specified destination by same value in memory. */
function fill(dst: usize, value: u8, size: usize): void;
/** Resets the allocator to its initial state, if supported. */
function reset(): void;
}

View File

@ -52,8 +52,8 @@ Object.defineProperties(
Object.defineProperties(
globalScope["bool"] = function bool(value) { return !!value; }
, {
"MIN_VALUE": { value: 0, writable: false },
"MAX_VALUE": { value: 1, writable: false }
"MIN_VALUE": { value: false, writable: false },
"MAX_VALUE": { value: true, writable: false }
});
Object.defineProperties(
@ -207,6 +207,14 @@ globalScope["isString"] = function isString(arg) {
globalScope["isArray"] = Array.isArray;
globalScope["isDefined"] = function isDefined(expr) {
return typeof expr !== "undefined";
}
globalScope["isConstant"] = function isConstant(expr) {
return false;
};
globalScope["unchecked"] = function unchecked(expr) {
return expr;
};
@ -239,17 +247,24 @@ globalScope["memory"] = (() => {
if ((HEAP_OFFSET += size) & 7) HEAP_OFFSET = (HEAP_OFFSET | 7) + 1;
return ptr;
},
fill: globalScope["__memory_fill"] || function fill(dest, value, size) {
HEAP.fill(value, dest, dest + size);
},
free: globalScope["__memory_free"] || function free(ptr) { },
copy: globalScope["__memory_copy"] || function copy(dest, src, size) {
HEAP.copyWithin(dest, src, src + size);
},
reset: globalScope["__memory_reset"] || function reset() {
HEAP = new Uint8Array(0);
HEAP_OFFSET = 0;
}
};
})();
globalScope["store"] = globalScope["__store"] || function store(ptr, value, offset) {
HEAP[ptr + (offset | 0)] = value;
HEAP[(ptr | 0) + (offset | 0)] = value;
};
globalScope["load"] = globalScope["__load"] || function load(ptr, offset) {
return HEAP[ptr + (offset | 0)];
return HEAP[(ptr | 0) + (offset | 0)];
};