more loader work

This commit is contained in:
dcode
2019-05-25 00:38:50 +02:00
parent a684bb1f65
commit 9620f18249
33 changed files with 5101 additions and 1528 deletions

View File

@ -7,7 +7,7 @@ Usage
-----
```js
const loader = require("@assemblyscript/loader");
const loader = require("assemblyscript/lib/loader");
...
```
@ -26,54 +26,143 @@ API
* **demangle**<`T`>(exports: `WasmExports`, baseModule?: `Object`): `T`<br />
Demangles an AssemblyScript module's exports to a friendly object structure. You usually don't have to call this manually as instantiation does this implicitly.
**Note:** `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.
**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:
* **I8**: `Int8Array`<br />
An 8-bit signed integer view on the memory.
```ts
var value = module.I8[ptr];
```
* **U8**: `Uint8Array`<br />
An 8-bit unsigned integer view on the memory.
```ts
var value = module.U8[ptr];
```
* **I16**: `Int16Array`<br />
A 16-bit signed integer view on the memory.
```ts
var value = module.I16[ptr >>> 1];
```
* **U16**: `Uint16Array`<br />
A 16-bit unsigned integer view on the memory.
```ts
var value = module.U16[ptr >>> 1];
```
* **I32**: `Int32Array`<br />
A 32-bit signed integer view on the memory.
```ts
var value = module.I32[ptr >>> 2];
```
* **U32**: `Uint32Array`<br />
A 32-bit unsigned integer view on the memory.
```ts
var value = module.U32[ptr >>> 2];
```
* **I64**: `BigInt64Array`<br />
A 64-bit signed integer view on the memory<sup>1</sup>.
A 64-bit signed integer view on the memory, if supported by the VM.
```ts
var value = module.I64[ptr >>> 3];
```
* **U64**: `BigUint64Array`<br />
A 64-bit unsigned integer view on the memory<sup>1</sup>.
A 64-bit unsigned integer view on the memory, if supported by the VM.
```ts
var value = module.U64[ptr >>> 3];
```
* **F32**: `Float32Array`<br />
A 32-bit float view on the memory.
```ts
var value = module.I32[ptr >>> 2];
```
* **F64**: `Float64Array`<br />
A 64-bit float view on the memory.
* **newString**(str: `string`): `number`<br />
Allocates a new string in the module's memory and returns its retained pointer. When done with the string, make sure to `Module#__release` it.
```ts
var value = module.F64[ptr >>> 3];
```
* **getString**(ptr: `number`): `string`<br />
Reads a string from the module's memory by its pointer.
* **__allocString**(str: `string`): `number`<br />
Allocates a new string in the module's memory and returns a reference (pointer) to it.
* **newArray**(id: `number`, values: `number[]`): `number`<br />
Allocates a new array in the module's memory and returns its retained pointer.
The `id` is the unique runtime id of the respective array class. If you are using `Int32Array` for example, the best way to know the relevant value is an `export const INT32ARRAY_ID = idof<Int32Array>()`. When done with the array, make sure to `Module#__release` it.
```ts
var ref = module.__retain(module.__allocString("hello world"));
...
module.__release(ref);
```
* **getArray**(ptr: `number`): `number[]`<br />
Gets the values of an array in the module's memory by its pointer.
* **__getString**(ref: `number`): `string`<br />
Gets the value of a string from the module's memory.
<sup>1</sup> This feature has not yet landed in any VM as of this writing.
```ts
var str = module.__getString(ref);
...
```
* **__allocArray**(id: `number`, values: `number[]`): `number`<br />
Allocates a new array in the module's memory and returns a reference (pointer) to it.
The `id` is the unique runtime id of the respective array class. If you are using `Int32Array` for example, the best way to know the id is an `export const INT32ARRAY_ID = idof<Int32Array>()`. When done with the array, make sure to release it.
```ts
var ref = module.__retain(module.__allocArray(module.INT32ARRAY, [1, 2, 3]));
...
module.__release(ref);
```
* **__getArray**(ref: `number`): `number[]`<br />
Gets the values of an array from the module's memory.
```ts
var arr = module.__getArray(ref);
...
```
* **__retain**(ref: `number`): `number`<br />
Retains a reference externally, making sure that it doesn't become collected prematurely. Returns the reference.
* **__release**(ref: `number`): `void`<br />
Releases a previously retained reference to an object, allowing the runtime to collect it once its reference count reaches zero.
* **__alloc**(size: `number`, id: `number`): `number`<br />
Allocates an instance of the class represented by the specified id. If you are using `MyClass` for example, the best way to know the id and the necessary size is an `export const MYCLASS_ID = idof<MyClass>()` and an `export const MYCLASS_SIZE = offsetof<MyClass>()`. Afterwards, use the respective views to assign values to the class's memory while making sure to retain interior references to other managed objects once. When done with the class, make sure to release it, which will automatically release any interiour references once the class becomes collected.
```ts
var ref = module.__retain(module.__alloc(module.MYCLASS_SIZE, module.MYCLASS_ID));
F32[ref + MYCLASS_BASICFIELD1_OFFSET >>> 2] = field1_value_f32;
U32[ref + MYCLASS_MANAGEDFIELD2_OFFSET >>> 2] = module.__retain(field2_value_ref);
...
module.__release(ref);
```
* **__instanceof**(ref: `number`, baseId: `number`): `boolean`<br />
Tests whether an object is an instance of the class represented by the specified base id.
* **__collect**(): `void`<br />
Forces a cycle collection. Only relevant if objects potentially forming reference cycles are used.
**Note** that the views like `I32` above will automatically be updated when the module's memory grows. Don't cache these if this can happen.
**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.
Examples
--------
@ -88,62 +177,6 @@ const myModule = loader.instantiateBuffer(fs.readFileSync("myModule.wasm"), myIm
const myModule = await loader.instantiateStreaming(fetch("myModule.wasm"), myImports);
```
### Reading/writing basic values to/from memory
```js
var ptrToInt8 = ...;
var value = myModule.I8[ptrToInt8]; // alignment of log2(1)=0
var ptrToInt16 = ...;
var value = myModule.I16[ptrToInt16 >>> 1]; // alignment of log2(2)=1
var ptrToInt32 = ...;
var value = myModule.I32[ptrToInt32 >>> 2]; // alignment of log2(4)=2
var ptrToInt64 = ...;
var value = myModule.I64[ptrToInt64 >>> 3]; // alignment of log2(8)=3
var ptrToFloat32 = ...;
var value = myModule.F32[ptrToFloat32 >>> 2]; // alignment of log2(4)=2
var ptrToFloat64 = ...;
var value = myModule.F64[ptrToFloat64 >>> 3]; // alignment of log2(8)=3
// Likewise, for writing
myModule.I8[ptrToInt8] = newValue;
myModule.I16[ptrToInt16 >>> 1] = newValue;
myModule.I32[ptrToInt32 >>> 2] = newValue;
myModule.I64[ptrToInt64 >>> 3] = newValue;
myModule.F32[ptrToFloat32 >>> 2] = newValue;
myModule.F64[ptrToFloat64 >>> 3] = newValue;
```
**Note:** Make sure to reference the views as shown above as these will automatically be updated when the module's memory grows.
### Working with strings and arrays
Strings and arrays cannot yet flow in and out of WebAssembly naturally, hence it is necessary to create them in the module's memory using the `newString` and `newArray` helpers. Afterwards, instead of passing the string or array directly, the resulting reference (pointer) is provided instead:
```js
var str = "Hello world!";
var ptr = module.newString(str);
// do something with ptr, i.e. call a WebAssembly export
...
// when done, allow the runtime to collect it
module.__release(ptr);
```
Similarly, when a string or array is returned from a WebAssembly function, a reference (pointer) is received on the JS side and the `getString` and `getArray` helpers can be used to obtain their values:
```js
var ptrToString = ...;
var str = module.getString(ptrToString);
// ... do something with str ...
```
### Usage with TypeScript definitions produced by the compiler
```ts

62
lib/loader/index.d.ts vendored
View File

@ -3,33 +3,14 @@ import "@types/webassembly-js-api";
/** WebAssembly imports with two levels of nesting. */
interface ImportsObject {
[key: string]: {},
env: {
env?: {
memory?: WebAssembly.Memory,
table?: WebAssembly.Table,
abort?: (msg: number, file: number, line: number, column: number) => void
abort?: (msg: number, file: number, line: number, column: number) => void,
trace?: (msg: number, numArgs?: number, ...args: any[]) => void
}
}
type TypedArray
= Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array;
type TypedArrayConstructor
= Int8ArrayConstructor
| Uint8ArrayConstructor
| Int16ArrayConstructor
| Uint16ArrayConstructor
| Int32ArrayConstructor
| Uint32ArrayConstructor
| Float32ArrayConstructor
| Float32ArrayConstructor;
/** Utility mixed in by the loader. */
interface ASUtil {
/** An 8-bit signed integer view on the memory. */
@ -52,25 +33,24 @@ interface ASUtil {
readonly F32: Float32Array;
/** A 64-bit float view on the memory. */
readonly F64: Float64Array;
/** Allocates a new string in the module's memory and returns its pointer. */
newString(str: string): number;
/** Gets a string from the module's memory by its pointer. */
getString(ptr: number): string;
/** Copies a typed array into the module's memory and returns its pointer. */
newArray(view: TypedArray, length?: number): number;
/** Creates a typed array in the module's memory and returns its pointer. */
newArray(ctor: TypedArrayConstructor, length: number, unsafe?: boolean): number;
/** Gets a view on a typed array in the module's memory by its pointer. */
getArray<T extends TypedArray = TypedArray>(ctor: TypedArrayConstructor, ptr: number): T;
/** Frees a typed array in the module's memory. Must not be accessed anymore afterwards. */
freeArray(ptr: number): void;
/** Gets a function by its pointer. */
getFunction<R = any>(ptr: number): (...args: any[]) => R;
/**
* Creates a new function in the module's table and returns its pointer. Note that only actual
* WebAssembly functions, i.e. as exported by the module, are supported.
*/
newFunction(fn: (...args: any[]) => any): number;
/** Allocates a new string in the module's memory and returns a reference (pointer) to it. */
__allocString(str: string): number;
/** Gets the value of a string from the module's memory. */
__getString(ref: number): string;
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
__allocArray(id: number, values: number[]): number;
/** Gets the values of an array from the module's memory. */
__getArray(ref: number): number[];
/** Retains a reference externally, making sure that it doesn't become collected prematurely. Returns the reference. */
__retain(ref: number): number;
/** Releases a previously retained reference to an object, allowing the runtime to collect it once its reference count reaches zero. */
__release(ref: number): void;
/** Allocates an instance of the class represented by the specified id. */
__alloc(size: number, id: number): number;
/** Tests whether an object is an instance of the class represented by the specified base id. */
__instanceof(ref: number, baseId: number): boolean;
/** Forces a cycle collection. Only relevant if objects potentially forming reference cycles are used. */
__collect(): void;
}
/** Instantiates an AssemblyScript module using the specified imports. */

View File

@ -14,11 +14,15 @@ const ARRAY = 1 << 0;
const SET = 1 << 1;
const MAP = 1 << 2;
const VAL_ALIGN = 1 << 4;
const VAL_NULLABLE = 1 << 9;
const VAL_MANAGED = 1 << 10;
const KEY_ALIGN = 1 << 11;
const KEY_NULLABLE = 1 << 16;
const KEY_MANAGED = 1 << 17;
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;
// ArrayBufferView layout
const ABV_BUFFER_OFFSET = 0;
@ -28,41 +32,41 @@ const ABV_SIZE = 12;
const BIGINT = typeof BigUint64Array !== "undefined";
const THIS = Symbol();
const CHUNKSIZE = 1024;
/** Gets a string from an U32 and an U16 view on a memory. */
function getStringImpl(U32, U16, ptr) {
var length = U32[(ptr + SIZE_OFFSET) >>> 2] >>> 1;
var offset = ptr >>> 1;
var parts = [];
const chunkSize = 1024;
while (length > chunkSize) {
let last = U16[offset + chunkSize - 1];
let size = last >= 0xD800 && last < 0xDC00 ? chunkSize - 1 : chunkSize;
let part = U16.subarray(offset, offset += size);
parts.push(String.fromCharCode.apply(String, part));
function getStringImpl(U32, U16, ref) {
var length = U32[(ref + SIZE_OFFSET) >>> 2] >>> 1;
var offset = ref >>> 1;
if (length <= CHUNKSIZE) return String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
const parts = [];
do {
const last = U16[offset + CHUNKSIZE - 1];
const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE;
parts.push(String.fromCharCode.apply(String, U16.subarray(offset, offset += size)));
length -= size;
}
} while (length > CHUNKSIZE);
return parts.join("") + String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
}
/** Prepares the base module prior to instantiation. */
function preInstantiate(imports) {
var baseModule = {};
const baseModule = {};
function getString(memory, ptr) {
function getString(memory, ref) {
if (!memory) return "<yet unknown>";
var buffer = memory.buffer;
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ptr);
const buffer = memory.buffer;
return getStringImpl(new Uint32Array(buffer), new Uint16Array(buffer), ref);
}
// add common imports used by stdlib for convenience
var env = (imports.env = imports.env || {});
const env = (imports.env = imports.env || {});
env.abort = env.abort || function abort(mesg, file, line, colm) {
var memory = baseModule.memory || env.memory; // prefer exported, otherwise try imported
const memory = baseModule.memory || env.memory; // prefer exported, otherwise try imported
throw Error("abort: " + getString(memory, mesg) + " at " + getString(memory, file) + ":" + line + ":" + colm);
}
env.trace = env.trace || function trace(mesg, n) {
var memory = baseModule.memory || env.memory;
const memory = baseModule.memory || env.memory;
console.log("trace: " + getString(memory, mesg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
}
imports.Math = imports.Math || Math;
@ -73,12 +77,12 @@ function preInstantiate(imports) {
/** Prepares the final module once instantiation is complete. */
function postInstantiate(baseModule, instance) {
var rawExports = instance.exports;
var memory = rawExports.memory;
var table = rawExports.table;
var alloc = rawExports["__alloc"];
var retain = rawExports["__retain"];
var rtti = rawExports["__rtti"] | 0;
const rawExports = instance.exports;
const memory = rawExports.memory;
const table = rawExports.table;
const alloc = rawExports["__alloc"];
const retain = rawExports["__retain"];
const rtti = rawExports["__rtti"] || ~0; // oob if not present
// Provide views for all sorts of basic values
var buffer, I8, U8, I16, U16, I32, U32, F32, F64, I64, U64;
@ -120,32 +124,50 @@ function postInstantiate(baseModule, instance) {
/** Gets the runtime alignment of a collection's values or keys. */
function getAlign(which, info) {
return 31 - Math.clz32((info / which) & 31);
return 31 - Math.clz32((info / which) & 31); // -1 if none
}
/** Allocates a new string in the module's memory and returns its retained pointer. */
function newString(str) {
function __allocString(str) {
const length = str.length;
const ptr = alloc(length << 1, STRING_ID);
const ref = alloc(length << 1, STRING_ID);
checkMem();
for (let i = 0, j = ptr >>> 1; i < length; ++i) U16[j + i] = str.charCodeAt(i);
return retain(ptr);
for (let i = 0, j = ref >>> 1; i < length; ++i) U16[j + i] = str.charCodeAt(i);
return ref;
}
baseModule.newString = newString;
baseModule.__allocString = __allocString;
/** Reads a string from the module's memory by its pointer. */
function getString(ptr) {
function __getString(ref) {
checkMem();
const id = U32[ptr + ID_OFFSET >>> 2];
if (id !== STRING_ID) throw Error("not a string: " + ptr);
return getStringImpl(U32, U16, ptr);
const id = U32[ref + ID_OFFSET >>> 2];
if (id !== STRING_ID) throw Error("not a string: " + ref);
return getStringImpl(U32, U16, ref);
}
baseModule.getString = getString;
baseModule.__getString = __getString;
/** Gets the view matching the specified alignment, signedness and floatness. */
function getView(align, signed, float) {
if (float) {
switch (align) {
case 2: return F32;
case 3: return F64;
}
} else {
switch (align) {
case 0: return signed ? I8 : U8;
case 1: return signed ? I16 : U16;
case 2: return signed ? I32 : U32;
case 3: return signed ? I64 : U64;
}
}
throw Error("unsupported align: " + align);
}
/** Allocates a new array in the module's memory and returns its retained pointer. */
function newArray(id, values) {
function __allocArray(id, values) {
const info = getInfo(id);
if (!(info & ARRAY)) throw Error("not an array: " + id + " @ " + info);
const align = getAlign(VAL_ALIGN, info);
@ -156,23 +178,16 @@ function postInstantiate(baseModule, instance) {
U32[arr + ABV_BUFFER_OFFSET >>> 2] = retain(buf);
U32[arr + ABV_DATASTART_OFFSET >>> 2] = buf;
U32[arr + ABV_DATALENGTH_OFFSET >>> 2] = length << align;
var view;
switch (align) {
case 0: view = U8; break;
case 1: view = U16; break;
case 2: view = U32; break;
case 3: view = U64; break;
default: throw Error("unsupported align: " + align);
}
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]);
return retain(arr);
return arr;
}
baseModule.newArray = newArray;
baseModule.__allocArray = __allocArray;
/** Gets the values of an array in the module's memory by its pointer. */
function getArray(arr) {
function __getArray(arr) {
checkMem();
const id = U32[arr + ID_OFFSET >>> 2];
const info = getInfo(id);
@ -180,19 +195,22 @@ function postInstantiate(baseModule, instance) {
const align = getAlign(VAL_ALIGN, info);
var buf = U32[arr + ABV_DATASTART_OFFSET >>> 2];
const length = U32[buf + SIZE_OFFSET >>> 2] >>> align;
var view;
switch (align) {
// TODO: signedness/floats
case 0: view = U8; break;
case 1: view = U16; break;
case 2: view = U32; break;
case 3: view = U64; break;
default: throw Error("unsupported align: " + align);
}
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
return Array.from(view.slice(buf >>>= align, buf + length));
}
baseModule.getArray = getArray;
baseModule.__getArray = __getArray;
function __instanceof(ref, baseId) {
var id = U32[(ref + ID_OFFSET) >>> 2];
if (id <= U32[rtti >>> 2]) {
do if (id == baseId) return true;
while (id = getBase(id));
}
return false;
}
baseModule.__instanceof = __instanceof;
// Pull basic exports to baseModule so code in preInstantiate can use them
baseModule.memory = baseModule.memory || memory;

View File

@ -49,7 +49,7 @@ export function varadd(a: i32 = 1, b: i32 = 2): i32 {
return a + b;
}
export const varadd_ptr = varadd;
export const varadd_ref = varadd;
export function calladd(fn: (a: i32, b: i32) => i32, a: i32, b: i32): i32 {
return fn(a, b);
@ -60,3 +60,5 @@ export function dotrace(num: f64): void {
}
export const INT32ARRAY_ID = idof<Int32Array>();
export const UINT32ARRAY_ID = idof<Uint32Array>();
export const FLOAT32ARRAY_ID = idof<Float32Array>();

View File

@ -24,43 +24,70 @@ assert(module.memory instanceof WebAssembly.Memory);
assert(typeof module.memory.copy === "function");
// should be able to get an exported string
assert.strictEqual(module.getString(module.COLOR), "red");
assert.strictEqual(module.__getString(module.COLOR), "red");
// should be able to allocate and work with a new string
var str = "Hello world!𤭢";
var ptr = module.newString(str);
assert.strictEqual(module.getString(ptr), str);
assert.strictEqual(module.strlen(ptr), str.length);
{
let str = "Hello world!𤭢";
let ref = module.__retain(module.__allocString(str));
assert.strictEqual(module.__getString(ref), str);
assert.strictEqual(module.strlen(ref), str.length);
module.__release(ref);
}
// should be able to allocate a typed array and sum up its values
var arr = [1, 2, 3, 4, 5, 0x7fffffff];
ptr = module.newArray(module.INT32ARRAY_ID, arr);
assert.strictEqual(module.sum(ptr), arr.reduce((a, b) => a + b, 0) | 0);
// should be able to allocate a typed array
{
var arr = [1, 2, 3, 4, 5, 0x80000000 | 0];
let ref = module.__retain(module.__allocArray(module.INT32ARRAY_ID, arr));
assert(module.__instanceof(ref, module.INT32ARRAY_ID));
// should be able to get a view on an internal typed array
assert.deepEqual(Array.from(module.getArray(ptr)), arr);
// should be able to get a view on an internal typed array
assert.deepEqual(module.__getArray(ref), arr);
// should be able to release no longer needed references
module.__release(ptr);
try {
module.__release(ptr);
assert(false);
} catch (e) {};
// should be able to sum up its values
assert.strictEqual(module.sum(ref), arr.reduce((a, b) => (a + b) | 0, 0) | 0);
// should be able to just call a function with variable arguments
// should be able to release no longer needed references
module.__release(ref);
try { module.__release(ref); assert(false); } catch (e) {};
}
// should be able to distinguish between signed and unsigned
{
let arr = [1, -1 >>> 0, 0x80000000];
let ref = module.__retain(module.__allocArray(module.UINT32ARRAY_ID, arr));
assert(module.__instanceof(ref, module.UINT32ARRAY_ID));
assert.deepEqual(module.__getArray(ref), arr);
module.__release(ref);
try { module.__release(ref); assert(false); } catch (e) {};
}
// should be able to distinguish between integer and float
{
let arr = [0.0, 1.5, 2.5];
let ref = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, arr));
assert(module.__instanceof(ref, module.FLOAT32ARRAY_ID));
assert.deepEqual(module.__getArray(ref), arr);
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);
assert.strictEqual(module.varadd(2), 4);
// TBD: table is no more exported by default to allow more optimizations
// should be able to get a function from the table and just call it with variable arguments
// var fn = module.getFunction(module.varadd_ptr);
// var fn = module.getFunction(module.varadd_ref);
// assert.strictEqual(fn(), 3);
// assert.strictEqual(fn(2, 3), 5);
// assert.strictEqual(fn(2), 4);
// should be able to create a new function and call it from WASM
// ptr = module.newFunction(module.varadd);
// assert.strictEqual(module.calladd(ptr, 2, 3), 5);
// ref = module.newFunction(module.varadd);
// assert.strictEqual(module.calladd(ref, 2, 3), 5);
// should be able to use a class
var car = new module.Car(5);