Skip to content

Fix typed array views obtained using the loader #857

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 1 commit into from
Sep 21, 2019
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
5 changes: 2 additions & 3 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function postInstantiate(baseModule, instance) {
? U32[arr + ARRAY_LENGTH_OFFSET >>> 2]
: U32[buf + SIZE_OFFSET >>> 2] >>> align;
return getView(align, info & VAL_SIGNED, info & VAL_FLOAT)
.slice(buf >>>= align, buf + length);
.subarray(buf >>>= align, buf + length);
}

baseModule.__getArrayView = __getArrayView;
Expand Down Expand Up @@ -221,8 +221,7 @@ function postInstantiate(baseModule, instance) {
const buffer = memory.buffer;
const U32 = new Uint32Array(buffer);
const bufPtr = U32[ptr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
const length = U32[bufPtr + SIZE_OFFSET >>> 2];
return new Type(buffer).slice(bufPtr >>> alignLog2, bufPtr + length >>> alignLog2);
return new Type(buffer, bufPtr, U32[bufPtr + SIZE_OFFSET >>> 2] >>> alignLog2);
}

/** Gets a view on the values of a known-to-be Int8Array in the module's memory. */
Expand Down
8 changes: 8 additions & 0 deletions lib/loader/tests/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ export const INT32ARRAY_ID = idof<Int32Array>();
export const UINT32ARRAY_ID = idof<Uint32Array>();
export const FLOAT32ARRAY_ID = idof<Float32Array>();
export const ARRAYI32_ID = idof<Array<i32>>();

export function newFloat32Array(size: i32): Float32Array {
return new Float32Array(size);
}

export function modifyFloat32Array(array: Float32Array, index: i32, value: f32): void {
array[index] = value;
}
Binary file modified lib/loader/tests/build/untouched.wasm
Binary file not shown.
22 changes: 22 additions & 0 deletions lib/loader/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,25 @@ module.__release(car); // uses Car.prototype.valueOf to obtain `thisPtr`

// should be able to use trace
module.dotrace(42);

// should be able to mutate an array in place using getArrayView
{
let ptr = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, [ 1, 2, 3]));
let view = module.__getArrayView(ptr);
assert.deepEqual(view, new Float32Array([1, 2, 3]));
module.modifyFloat32Array(ptr, 0, 4);
assert.deepEqual(view, new Float32Array([4, 2, 3]));
module.__release(ptr);
}

// should be able to mutate an array in place using getFloat32Array
{
let ptr = module.newFloat32Array(3); // returns are pre-retained
let view = module.__getFloat32Array(ptr);
assert.deepEqual(view, new Float32Array([0, 0, 0]));
module.modifyFloat32Array(ptr, 0, 3);
module.modifyFloat32Array(ptr, 1, 2);
module.modifyFloat32Array(ptr, 2, 1);
assert.deepEqual(view, new Float32Array([3, 2, 1]));
module.__release(ptr);
}