Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/loader/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface ASUtil {
/** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */
__newArrayBuffer(buf: ArrayBuffer): number;
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
__newArray(id: number, values: ArrayLike<number>): number;
__newArray(id: number, valuesOrCapacity?: Array<number> | ArrayBufferView | number): number;

/** Allocates an instance of the class represented by the specified id. */
__new(size: number, id: number): number;
Expand Down
24 changes: 14 additions & 10 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ function postInstantiate(extendedExports, instance) {
}

/** Allocates a new array in the module's memory and returns its pointer. */
function __newArray(id, values) {
function __newArray(id, valuesOrCapacity = 0) {
const input = valuesOrCapacity;
const info = getArrayInfo(id);
const align = getValueAlign(info);
const length = values.length;
const isArrayLike = typeof input !== "number";
const length = isArrayLike ? input.length : input;
const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID);
let result;
if (info & STATICARRAY) {
Expand All @@ -212,14 +214,16 @@ function postInstantiate(extendedExports, instance) {
if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length;
result = arr;
}
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
if (info & VAL_MANAGED) {
for (let i = 0; i < length; ++i) {
const value = values[i];
view[(buf >>> align) + i] = value;
if (isArrayLike) {
const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT);
const start = buf >>> align;
if (info & VAL_MANAGED) {
for (let i = 0; i < length; ++i) {
view[start + i] = input[i];
}
} else {
view.set(input, start);
}
} else {
view.set(values, buf >>> align);
}
return result;
}
Expand Down Expand Up @@ -416,7 +420,7 @@ export function demangle(exports, extendedExports = {}) {
});
}
} else {
if (name === 'constructor') {
if (name === "constructor") {
(curr[name] = (...args) => {
setArgumentsLength(args.length);
return elem(...args);
Expand Down
14 changes: 14 additions & 0 deletions lib/loader/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ function test(file) {
assert.deepStrictEqual(exports.__getArray(ref), arr);
}

// should be able to create empty arrays
{
let ref = exports.__newArray(exports.ARRAYI32_ID);
assert(exports.__instanceof(ref, exports.ARRAYI32_ID));
assert.deepStrictEqual(exports.__getArray(ref), []);
}

// should be able to create arrays with capacity
{
let ref = exports.__newArray(exports.ARRAYI32_ID, 32);
assert(exports.__instanceof(ref, exports.ARRAYI32_ID));
assert.strictEqual(exports.__getArray(ref).length, 32);
}

// should be able to work with normal arrays
{
let arr = [1, 2, 3, 4, 5];
Expand Down