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

@ -61,7 +61,7 @@ Instances are automatically populated with useful utility:
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 pointer. Requires `allocate_memory` to be exported from your module's entry file, i.e.:
Allocates a new string in the module's memory and returns its pointer. Requires `memory.allocate` to be exported from your module's entry file, i.e.:
```js
import "allocator/tlsf";
@ -71,6 +71,18 @@ Instances are automatically populated with useful utility:
* **getString**(ptr: `number`): `string`<br />
Gets a string from the module's memory by its pointer.
* **newArray**(view: `TypedArray`, length?: `number`, unsafe?: `boolean`): `number`<br />
Copies a typed array into the module's memory and returns its pointer.
* **newArray**(ctor: `TypedArrayConstructor`, length: `number`): `number`<br />
Creates a typed array in the module's memory and returns its pointer.
* **getArray**(ctor: `TypedArrayConstructor`, ptr: `number`): `TypedArray`<br />
Gets a view on a typed array in the module's memory by its pointer.
* **freeArray**(ptr: `number`): `void`<br />
Frees a typed array in the module's memory. Must not be accessed anymore afterwards.
<sup>1</sup> This feature has not yet landed in any VM as of this writing.
Examples
@ -125,7 +137,7 @@ myModule.F64[ptrToFloat64 >>> 3] = newValue;
var str = "Hello world!";
var ptr = module.newString(str);
// Disposing a string that is no longer needed (requires free_memory to be exported)
// Disposing a string that is no longer needed (requires memory.free to be exported)
module.memory.free(ptr);
// Obtaining a string, i.e. as returned by an export