array is an abv, views

This commit is contained in:
dcode
2019-05-25 14:40:13 +02:00
parent 6d6ed710e5
commit 968192f99b
18 changed files with 90 additions and 53 deletions

View File

@ -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;
}