From e3e0fe80459fcec5cbde5f625569f62ac002d293 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Wed, 9 Jan 2019 14:17:18 +0200 Subject: [PATCH] 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 --- std/portable/index.d.ts | 6 ++++++ std/portable/index.js | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index 7929becc..5ea4bdf6 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -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; +/** 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(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; } diff --git a/std/portable/index.js b/std/portable/index.js index d8a7a7e0..ab0fad62 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -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)]; };