mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-20 02:11:31 +00:00
array is an abv, views
This commit is contained in:
@ -110,7 +110,7 @@ Besides demangling classes exported from your entry file to a handy object struc
|
||||
```
|
||||
|
||||
* **__getString**(ref: `number`): `string`<br />
|
||||
Gets the value of a string from the module's memory.
|
||||
Reads (copies) the value of a string from the module's memory.
|
||||
|
||||
```ts
|
||||
var str = module.__getString(ref);
|
||||
@ -128,13 +128,16 @@ Besides demangling classes exported from your entry file to a handy object struc
|
||||
```
|
||||
|
||||
* **__getArray**(ref: `number`): `number[]`<br />
|
||||
Gets the values of an array from the module's memory.
|
||||
Reads (copies) the values of an array from the module's memory.
|
||||
|
||||
```ts
|
||||
var arr = module.__getArray(ref);
|
||||
...
|
||||
```
|
||||
|
||||
* **__getArrayView**(ref: `number`): `TypedArray`<br />
|
||||
Gets a view on the values of an array in the module's memory. This differs from `__getArray` in that the data isn't copied but remains *live* in both directions. That's faster but also unsafe because if the array grows or becomes released, the view will no longer represent the correct memory region and modifying its values in this state will most likely corrupt memory. Use, but use with care.
|
||||
|
||||
* **__retain**(ref: `number`): `number`<br />
|
||||
Retains a reference externally, making sure that it doesn't become collected prematurely. Returns the reference.
|
||||
|
||||
@ -142,7 +145,7 @@ Besides demangling classes exported from your entry file to a handy object struc
|
||||
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.
|
||||
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 interior references once the class becomes collected.
|
||||
|
||||
```ts
|
||||
var ref = module.__retain(module.__alloc(module.MYCLASS_SIZE, module.MYCLASS_ID));
|
||||
|
@ -190,23 +190,31 @@ function postInstantiate(baseModule, instance) {
|
||||
|
||||
baseModule.__allocArray = __allocArray;
|
||||
|
||||
/** Gets the values of an array in the module's memory by its pointer. */
|
||||
function __getArray(arr) {
|
||||
/** Gets a view on the values of an array in the module's memory. */
|
||||
function __getArrayView(arr) {
|
||||
checkMem();
|
||||
const id = U32[arr + ID_OFFSET >>> 2];
|
||||
const info = getInfo(id);
|
||||
if (!(info & (ARRAYBUFFERVIEW | ARRAY))) throw Error("not an array: " + id);
|
||||
if (!(info & ARRAYBUFFERVIEW)) throw Error("not an array: " + id);
|
||||
const align = getAlign(VAL_ALIGN, info);
|
||||
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));
|
||||
return getView(align, info & VAL_SIGNED, info & VAL_FLOAT)
|
||||
.slice(buf >>>= align, buf + length);
|
||||
}
|
||||
|
||||
baseModule.__getArrayView = __getArrayView;
|
||||
|
||||
/** Reads (copies) the values of an array from the module's memory. */
|
||||
function __getArray(arr) {
|
||||
return Array.from(__getArrayView(arr));
|
||||
}
|
||||
|
||||
baseModule.__getArray = __getArray;
|
||||
|
||||
/** Tests whether an object is an instance of the class represented by the specified base id. */
|
||||
function __instanceof(ref, baseId) {
|
||||
var id = U32[(ref + ID_OFFSET) >>> 2];
|
||||
if (id <= U32[rtti >>> 2]) {
|
||||
@ -243,8 +251,6 @@ function wrapFunction(fn, setargc) {
|
||||
setargc(args.length);
|
||||
return fn(...args);
|
||||
}
|
||||
// adding a function to the table with `newFunction` is limited to actual WebAssembly functions,
|
||||
// hence we can't use the wrapper and instead need to provide a reference to the original
|
||||
wrap.original = fn;
|
||||
return wrap;
|
||||
}
|
||||
|
Binary file not shown.
@ -41,9 +41,12 @@ assert.strictEqual(module.__getString(module.COLOR), "red");
|
||||
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
|
||||
// should be able to get the values of an array
|
||||
assert.deepEqual(module.__getArray(ref), arr);
|
||||
|
||||
// should be able to get a view on an array
|
||||
assert.deepEqual(module.__getArrayView(ref), new Int32Array(arr));
|
||||
|
||||
// should be able to sum up its values
|
||||
assert.strictEqual(module.sum(ref), arr.reduce((a, b) => (a + b) | 0, 0) | 0);
|
||||
|
||||
|
Reference in New Issue
Block a user