support normal arrays

This commit is contained in:
dcode
2019-05-25 01:14:26 +02:00
parent 9620f18249
commit c34ed66fd9
42 changed files with 831 additions and 468 deletions

View File

@ -28,7 +28,7 @@ API
**Note** that `T` above can either be omitted if the structure of the module is unknown, or can reference a `.d.ts` (i.e. `typeof MyModule`) as produced by the compiler with the `-d` option.
Instances are automatically populated with useful utility:
Besides demangling classes exported from your entry file to a handy object structure one can use like JS objects, instances are automatically populated with useful utility:
* **I8**: `Int8Array`<br />
An 8-bit signed integer view on the memory.
@ -162,7 +162,7 @@ Instances are automatically populated with useful utility:
**Note** that the allocation and ownership features above require the `full` (this is the default) or the `stub` runtime to be present in your module. Other runtime variations do not export this functionality without further ado (so the compiler can eliminate what's dead code).
**Note** that references returned from exported functions have already been retained for you and the runtime expects that you release them once not needed anymore.
**Note** that references returned from exported functions have already been retained for you and the runtime expects that you release them once not needed anymore. This is also true for constructors and getters.
Examples
--------

View File

@ -10,25 +10,28 @@ const STRING_ID = 1;
const ARRAYBUFFERVIEW_ID = 2;
// Runtime type information
const ARRAY = 1 << 0;
const SET = 1 << 1;
const MAP = 1 << 2;
const VAL_ALIGN = 1 << 4;
const VAL_SIGNED = 1 << 9;
const VAL_FLOAT = 1 << 10;
const VAL_NULLABLE = 1 << 11;
const VAL_MANAGED = 1 << 12;
const KEY_ALIGN = 1 << 13;
const KEY_SIGNED = 1 << 18;
const KEY_FLOAT = 1 << 19;
const KEY_NULLABLE = 1 << 20;
const KEY_MANAGED = 1 << 21;
const ARRAYBUFFERVIEW = 1 << 0;
const ARRAY = 1 << 1;
const SET = 1 << 2;
const MAP = 1 << 3;
const VAL_ALIGN = 1 << 5;
const VAL_SIGNED = 1 << 10;
const VAL_FLOAT = 1 << 11;
const VAL_NULLABLE = 1 << 12;
const VAL_MANAGED = 1 << 13;
const KEY_ALIGN = 1 << 14;
const KEY_SIGNED = 1 << 19;
const KEY_FLOAT = 1 << 20;
const KEY_NULLABLE = 1 << 21;
const KEY_MANAGED = 1 << 22;
// ArrayBufferView layout
const ABV_BUFFER_OFFSET = 0;
const ABV_DATASTART_OFFSET = 4;
const ABV_DATALENGTH_OFFSET = 8;
const ABV_SIZE = 12;
// Array(BufferView) layout
const ARRAYBUFFERVIEW_BUFFER_OFFSET = 0;
const ARRAYBUFFERVIEW_DATASTART_OFFSET = 4;
const ARRAYBUFFERVIEW_DATALENGTH_OFFSET = 8;
const ARRAYBUFFERVIEW_SIZE = 12;
const ARRAY_LENGTH_OFFSET = 12;
const ARRAY_SIZE = 16;
const BIGINT = typeof BigUint64Array !== "undefined";
const THIS = Symbol();
@ -169,15 +172,16 @@ function postInstantiate(baseModule, instance) {
/** Allocates a new array in the module's memory and returns its retained pointer. */
function __allocArray(id, values) {
const info = getInfo(id);
if (!(info & ARRAY)) throw Error("not an array: " + id + " @ " + info);
if (!(info & (ARRAYBUFFERVIEW | ARRAY))) throw Error("not an array: " + id + " @ " + info);
const align = getAlign(VAL_ALIGN, info);
const length = values.length;
const buf = alloc(length << align, ARRAYBUFFER_ID);
const arr = alloc(ABV_SIZE, id);
const arr = alloc(ARRAYBUFFERVIEW_SIZE, id);
checkMem();
U32[arr + ABV_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ABV_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ABV_DATALENGTH_OFFSET >>> 2] = length << align;
U32[arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2] = length << align;
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
for (let i = 0; i < length; ++i) view[(buf >> align) + i] = values[i];
if (info & VAL_MANAGED) for (let i = 0; i < length; ++i) retain(values[i]);
@ -191,10 +195,12 @@ function postInstantiate(baseModule, instance) {
checkMem();
const id = U32[arr + ID_OFFSET >>> 2];
const info = getInfo(id);
if (!(info & ARRAY)) throw Error("not an array: " + id);
if (!(info & (ARRAYBUFFERVIEW | ARRAY))) throw Error("not an array: " + id);
const align = getAlign(VAL_ALIGN, info);
var buf = U32[arr + ABV_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2] >>> align;
var buf = U32[arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = info & ARRAY
? U32[arr + ARRAY_LENGTH_OFFSET >>> 2]
: U32[buf + SIZE_OFFSET >>> 2] >>> align;
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
return Array.from(view.slice(buf >>>= align, buf + length));
}

View File

@ -45,6 +45,10 @@ export function sum(arr: Int32Array): i32 {
return v;
}
export function changeLength(arr: Array<i32>, length: i32): void {
arr.length = length;
}
export function varadd(a: i32 = 1, b: i32 = 2): i32 {
return a + b;
}
@ -62,3 +66,4 @@ export function dotrace(num: f64): void {
export const INT32ARRAY_ID = idof<Int32Array>();
export const UINT32ARRAY_ID = idof<Uint32Array>();
export const FLOAT32ARRAY_ID = idof<Float32Array>();
export const ARRAYI32_ID = idof<Array<i32>>();

View File

@ -72,6 +72,17 @@ assert.strictEqual(module.__getString(module.COLOR), "red");
try { module.__release(ref); assert(false); } catch (e) {};
}
// should be able to work with normal arrays
{
let arr = [1, 2, 3, 4, 5];
let ref = module.__retain(module.__allocArray(module.ARRAYI32_ID, arr));
assert(module.__instanceof(ref, module.ARRAYI32_ID));
module.changeLength(ref, 3);
assert.deepEqual(module.__getArray(ref), [1, 2, 3]);
module.__release(ref);
try { module.__release(ref); assert(false); } catch (e) {};
}
// should be able to correctly call a function with variable arguments
assert.strictEqual(module.varadd(), 3);
assert.strictEqual(module.varadd(2, 3), 5);