Skip to content

Commit e3e0fe8

Browse files
MaxGraeydcodeIO
authored andcommitted
Improve portables (AssemblyScript#386)
* Hardened load/save * Added memory.reset and memory.fill * Added isDefined and isConstant * Use true/false as bool's min/max values
1 parent 9ec226d commit e3e0fe8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

std/portable/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ declare function isReference(value: any): value is object | string;
9494
declare function isString(value: any): value is string | String;
9595
/** Tests if the specified value can be used as an array. */
9696
declare function isArray(value: any): value is Array<any>;
97+
/** Tests if the specified expression resolves to a defined element. */
98+
declare function isDefined(expression: any): bool;
99+
/** Tests if the specified expression evaluates to a constant value. */
100+
declare function isConstant(expression: any): bool;
97101
/** Traps if the specified value is not true-ish, otherwise returns the value. */
98102
declare function assert<T>(isTrueish: T | null, message?: string): T;
99103
/** Parses an integer string to a 64-bit float. */
@@ -285,6 +289,8 @@ declare namespace memory {
285289
function free(ptr: usize): void;
286290
/** Copies n bytes from the specified source to the specified destination in memory. These regions may overlap. */
287291
function copy(dst: usize, src: usize, n: usize): void;
292+
/** Fills size bytes from from the specified destination by same value in memory. */
293+
function fill(dst: usize, value: u8, size: usize): void;
288294
/** Resets the allocator to its initial state, if supported. */
289295
function reset(): void;
290296
}

std/portable/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ Object.defineProperties(
5252
Object.defineProperties(
5353
globalScope["bool"] = function bool(value) { return !!value; }
5454
, {
55-
"MIN_VALUE": { value: 0, writable: false },
56-
"MAX_VALUE": { value: 1, writable: false }
55+
"MIN_VALUE": { value: false, writable: false },
56+
"MAX_VALUE": { value: true, writable: false }
5757
});
5858

5959
Object.defineProperties(
@@ -207,6 +207,14 @@ globalScope["isString"] = function isString(arg) {
207207

208208
globalScope["isArray"] = Array.isArray;
209209

210+
globalScope["isDefined"] = function isDefined(expr) {
211+
return typeof expr !== "undefined";
212+
}
213+
214+
globalScope["isConstant"] = function isConstant(expr) {
215+
return false;
216+
};
217+
210218
globalScope["unchecked"] = function unchecked(expr) {
211219
return expr;
212220
};
@@ -239,17 +247,24 @@ globalScope["memory"] = (() => {
239247
if ((HEAP_OFFSET += size) & 7) HEAP_OFFSET = (HEAP_OFFSET | 7) + 1;
240248
return ptr;
241249
},
250+
fill: globalScope["__memory_fill"] || function fill(dest, value, size) {
251+
HEAP.fill(value, dest, dest + size);
252+
},
242253
free: globalScope["__memory_free"] || function free(ptr) { },
243254
copy: globalScope["__memory_copy"] || function copy(dest, src, size) {
244255
HEAP.copyWithin(dest, src, src + size);
256+
},
257+
reset: globalScope["__memory_reset"] || function reset() {
258+
HEAP = new Uint8Array(0);
259+
HEAP_OFFSET = 0;
245260
}
246261
};
247262
})();
248263

249264
globalScope["store"] = globalScope["__store"] || function store(ptr, value, offset) {
250-
HEAP[ptr + (offset | 0)] = value;
265+
HEAP[(ptr | 0) + (offset | 0)] = value;
251266
};
252267

253268
globalScope["load"] = globalScope["__load"] || function load(ptr, offset) {
254-
return HEAP[ptr + (offset | 0)];
269+
return HEAP[(ptr | 0) + (offset | 0)];
255270
};

0 commit comments

Comments
 (0)