Skip to content

Commit 812f2eb

Browse files
authored
Fix typed array views obtained using the loader (AssemblyScript#857)
1 parent f00c481 commit 812f2eb

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

lib/loader/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function postInstantiate(baseModule, instance) {
192192
? U32[arr + ARRAY_LENGTH_OFFSET >>> 2]
193193
: U32[buf + SIZE_OFFSET >>> 2] >>> align;
194194
return getView(align, info & VAL_SIGNED, info & VAL_FLOAT)
195-
.slice(buf >>>= align, buf + length);
195+
.subarray(buf >>>= align, buf + length);
196196
}
197197

198198
baseModule.__getArrayView = __getArrayView;
@@ -221,8 +221,7 @@ function postInstantiate(baseModule, instance) {
221221
const buffer = memory.buffer;
222222
const U32 = new Uint32Array(buffer);
223223
const bufPtr = U32[ptr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
224-
const length = U32[bufPtr + SIZE_OFFSET >>> 2];
225-
return new Type(buffer).slice(bufPtr >>> alignLog2, bufPtr + length >>> alignLog2);
224+
return new Type(buffer, bufPtr, U32[bufPtr + SIZE_OFFSET >>> 2] >>> alignLog2);
226225
}
227226

228227
/** Gets a view on the values of a known-to-be Int8Array in the module's memory. */

lib/loader/tests/assembly/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,11 @@ export const INT32ARRAY_ID = idof<Int32Array>();
7070
export const UINT32ARRAY_ID = idof<Uint32Array>();
7171
export const FLOAT32ARRAY_ID = idof<Float32Array>();
7272
export const ARRAYI32_ID = idof<Array<i32>>();
73+
74+
export function newFloat32Array(size: i32): Float32Array {
75+
return new Float32Array(size);
76+
}
77+
78+
export function modifyFloat32Array(array: Float32Array, index: i32, value: f32): void {
79+
array[index] = value;
80+
}

lib/loader/tests/build/untouched.wasm

454 Bytes
Binary file not shown.

lib/loader/tests/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,25 @@ module.__release(car); // uses Car.prototype.valueOf to obtain `thisPtr`
151151

152152
// should be able to use trace
153153
module.dotrace(42);
154+
155+
// should be able to mutate an array in place using getArrayView
156+
{
157+
let ptr = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, [ 1, 2, 3]));
158+
let view = module.__getArrayView(ptr);
159+
assert.deepEqual(view, new Float32Array([1, 2, 3]));
160+
module.modifyFloat32Array(ptr, 0, 4);
161+
assert.deepEqual(view, new Float32Array([4, 2, 3]));
162+
module.__release(ptr);
163+
}
164+
165+
// should be able to mutate an array in place using getFloat32Array
166+
{
167+
let ptr = module.newFloat32Array(3); // returns are pre-retained
168+
let view = module.__getFloat32Array(ptr);
169+
assert.deepEqual(view, new Float32Array([0, 0, 0]));
170+
module.modifyFloat32Array(ptr, 0, 3);
171+
module.modifyFloat32Array(ptr, 1, 2);
172+
module.modifyFloat32Array(ptr, 2, 1);
173+
assert.deepEqual(view, new Float32Array([3, 2, 1]));
174+
module.__release(ptr);
175+
}

0 commit comments

Comments
 (0)