Add initial newArray and getArray helpers to loader

Essentially creates an unmanaged typed array in memory that one can work with and free again respectively obtain from the AS side. No support for GC or generic arrays yet, and is likely to change substentially once WASM GC becomes a thing.
This commit is contained in:
dcodeIO
2018-09-18 15:17:44 +02:00
parent 16d1a833dd
commit 9c770d801e
11 changed files with 132 additions and 9 deletions

View File

@ -33,3 +33,16 @@ var str = "Hello world!𤭢";
var ptr = module.newString(str);
assert.strictEqual(module.getString(ptr), str);
assert.strictEqual(module.strlen(ptr), str.length);
// should be able to allocate a typed array and sum up its values
var arr = [1, 2, 3, 4, 5, 0x7fffffff];
ptr = module.newArray(new Int32Array(arr));
assert.strictEqual(module.sum(ptr), arr.reduce((a, b) => a + b, 0) | 0);
// should be able to get a view on an internal typed array
assert.deepEqual(module.getArray(Int32Array, ptr), arr);
// should be able to free and reuse the space of an internal typed array
module.freeArray(ptr);
var ptr2 = module.newArray(new Int32Array(arr));
assert.strictEqual(ptr, ptr2);