Skip to content

feat: add ability to create arrays with capacity for __newArray in loader #2024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -92,7 +92,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
22 changes: 13 additions & 9 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,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 @@ -214,14 +216,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
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