Skip to content

Commit b6d2771

Browse files
MaxGraeydcodeIO
authored andcommitted
Split get typed array methods in loader to safe and unsafe (view) (#1047)
1 parent f700e2a commit b6d2771

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

lib/loader/index.d.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,62 @@ interface ASUtil {
1919
_start(): void;
2020
/** Allocates a new string in the module's memory and returns a reference (pointer) to it. */
2121
__allocString(str: string): number;
22-
/** Reads (copies) the value of a string from the module's memory. */
23-
__getString(ptr: number): string;
2422
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
2523
__allocArray(id: number, values: ArrayLike<number>): number;
24+
25+
/** Reads (copies) the value of a string from the module's memory. */
26+
__getString(ptr: number): string;
2627
/** Reads (copies) the values of an array from the module's memory. */
2728
__getArray(ptr: number): number[];
2829
/** Gets a view on the values of an array in the module's memory. */
2930
__getArrayView(ptr: number): ArrayBufferView;
30-
/** Reads the values of Int8Array from the module's memory. */
31+
32+
/** Reads (copies) the values of Int8Array from the module's memory. */
3133
__getInt8Array(ptr: number): Int8Array;
32-
/** Reads the values of Uint8Array from the module's memory. */
34+
/** Reads (copies) the values of Uint8Array from the module's memory. */
3335
__getUint8Array(ptr: number): Uint8Array;
34-
/** Reads the values of Uint8Array from the module's memory. */
36+
/** Reads (copies) the values of Uint8Array from the module's memory. */
3537
__getUint8ClampedArray(ptr: number): Uint8ClampedArray;
36-
/** Reads the values of Int16Array from the module's memory. */
38+
/** Reads (copies) the values of Int16Array from the module's memory. */
3739
__getInt16Array(ptr: number): Int16Array;
38-
/** Reads the values of Uint16Array from the module's memory. */
40+
/** Reads (copies) the values of Uint16Array from the module's memory. */
3941
__getUint16Array(ptr: number): Uint16Array;
40-
/** Reads the values of Int32Array from the module's memory. */
42+
/** Reads (copies) the values of Int32Array from the module's memory. */
4143
__getInt32Array(ptr: number): Int32Array;
42-
/** Reads the values of Uint32Array from the module's memory. */
44+
/** Reads (copies) the values of Uint32Array from the module's memory. */
4345
__getUint32Array(ptr: number): Uint32Array;
44-
/** Reads the values of Int32Array from the module's memory. */
46+
/** Reads (copies) the values of Int32Array from the module's memory. */
4547
__getInt64Array?(ptr: number): BigInt64Array;
46-
/** Reads the values of Uint32Array from the module's memory. */
48+
/** Reads (copies) the values of Uint32Array from the module's memory. */
4749
__getUint64Array?(ptr: number): BigUint64Array;
48-
/** Reads the values of Float32Array from the module's memory. */
50+
/** Reads (copies) the values of Float32Array from the module's memory. */
4951
__getFloat32Array(ptr: number): Float32Array;
50-
/** Reads the values of Float64Array from the module's memory. */
52+
/** Reads (copies) the values of Float64Array from the module's memory. */
5153
__getFloat64Array(ptr: number): Float64Array;
54+
55+
/** Reads the values of Int8Array from the module's memory. */
56+
__getInt8ArrayView(ptr: number): Int8Array;
57+
/** Reads the values of Uint8Array from the module's memory. */
58+
__getUint8ArrayView(ptr: number): Uint8Array;
59+
/** Reads the values of Uint8Array from the module's memory. */
60+
__getUint8ClampedArrayView(ptr: number): Uint8ClampedArray;
61+
/** Reads the values of Int16Array from the module's memory. */
62+
__getInt16ArrayView(ptr: number): Int16Array;
63+
/** Reads the values of Uint16Array from the module's memory. */
64+
__getUint16ArrayView(ptr: number): Uint16Array;
65+
/** Reads the values of Int32Array from the module's memory. */
66+
__getInt32ArrayView(ptr: number): Int32Array;
67+
/** Reads the values of Uint32Array from the module's memory. */
68+
__getUint32ArrayView(ptr: number): Uint32Array;
69+
/** Reads the values of Int32Array from the module's memory. */
70+
__getInt64ArrayView?(ptr: number): BigInt64Array;
71+
/** Reads the values of Uint32Array from the module's memory. */
72+
__getUint64ArrayView?(ptr: number): BigUint64Array;
73+
/** Reads the values of Float32Array from the module's memory. */
74+
__getFloat32ArrayView(ptr: number): Float32Array;
75+
/** Reads the values of Float64Array from the module's memory. */
76+
__getFloat64ArrayView(ptr: number): Float64Array;
77+
5278
/** Reads (copies) the data of an ArrayBuffer from the module's memory. */
5379
__getArrayBuffer(ptr: number): ArrayBuffer;
5480
/** Retains a reference to a managed object externally, making sure that it doesn't become collected prematurely. Returns the pointer. */

lib/loader/index.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,37 +217,52 @@ function postInstantiate(baseModule, instance) {
217217

218218
baseModule.__getArrayBuffer = __getArrayBuffer;
219219

220-
function getTypedArrayImpl(Type, alignLog2, ptr) {
220+
function getTypedArrayView(Type, alignLog2, ptr) {
221221
const buffer = memory.buffer;
222222
const U32 = new Uint32Array(buffer);
223223
const bufPtr = U32[ptr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2];
224224
return new Type(buffer, bufPtr, U32[bufPtr + SIZE_OFFSET >>> 2] >>> alignLog2);
225225
}
226226

227+
function getTypedArray(Type, alignLog2, ptr) {
228+
return new Type(getTypedArrayView(Type, alignLog2, ptr));
229+
}
230+
227231
/** Gets a view on the values of a known-to-be Int8Array in the module's memory. */
228-
baseModule.__getInt8Array = getTypedArrayImpl.bind(null, Int8Array, 0);
232+
baseModule.__getInt8Array = getTypedArray.bind(null, Int8Array, 0);
233+
baseModule.__getInt8ArrayView = getTypedArrayView.bind(null, Int8Array, 0);
229234
/** Gets a view on the values of a known-to-be Uint8Array in the module's memory. */
230-
baseModule.__getUint8Array = getTypedArrayImpl.bind(null, Uint8Array, 0);
235+
baseModule.__getUint8Array = getTypedArray.bind(null, Uint8Array, 0);
236+
baseModule.__getUint8ArrayView = getTypedArrayView.bind(null, Uint8Array, 0);
231237
/** Gets a view on the values of a known-to-be Uint8ClampedArray in the module's memory. */
232-
baseModule.__getUint8ClampedArray = getTypedArrayImpl.bind(null, Uint8ClampedArray, 0);
238+
baseModule.__getUint8ClampedArray = getTypedArray.bind(null, Uint8ClampedArray, 0);
239+
baseModule.__getUint8ClampedArrayView = getTypedArrayView.bind(null, Uint8ClampedArray, 0);
233240
/** Gets a view on the values of a known-to-be Int16Array in the module's memory. */
234-
baseModule.__getInt16Array = getTypedArrayImpl.bind(null, Int16Array, 1);
241+
baseModule.__getInt16Array = getTypedArray.bind(null, Int16Array, 1);
242+
baseModule.__getInt16ArrayView = getTypedArrayView.bind(null, Int16Array, 1);
235243
/** Gets a view on the values of a known-to-be Uint16Array in the module's memory. */
236-
baseModule.__getUint16Array = getTypedArrayImpl.bind(null, Uint16Array, 1);
244+
baseModule.__getUint16Array = getTypedArray.bind(null, Uint16Array, 1);
245+
baseModule.__getUint16ArrayView = getTypedArrayView.bind(null, Uint16Array, 1);
237246
/** Gets a view on the values of a known-to-be Int32Array in the module's memory. */
238-
baseModule.__getInt32Array = getTypedArrayImpl.bind(null, Int32Array, 2);
247+
baseModule.__getInt32Array = getTypedArray.bind(null, Int32Array, 2);
248+
baseModule.__getInt32ArrayView = getTypedArrayView.bind(null, Int32Array, 2);
239249
/** Gets a view on the values of a known-to-be Uint32Array in the module's memory. */
240-
baseModule.__getUint32Array = getTypedArrayImpl.bind(null, Uint32Array, 2);
250+
baseModule.__getUint32Array = getTypedArray.bind(null, Uint32Array, 2);
251+
baseModule.__getUint32ArrayView = getTypedArrayView.bind(null, Uint32Array, 2);
241252
if (BIGINT) {
242253
/** Gets a view on the values of a known-to-be-Int64Array in the module's memory. */
243-
baseModule.__getInt64Array = getTypedArrayImpl.bind(null, BigInt64Array, 3);
254+
baseModule.__getInt64Array = getTypedArray.bind(null, BigInt64Array, 3);
255+
baseModule.__getInt64ArrayView = getTypedArrayView.bind(null, BigInt64Array, 3);
244256
/** Gets a view on the values of a known-to-be-Uint64Array in the module's memory. */
245-
baseModule.__getUint64Array = getTypedArrayImpl.bind(null, BigUint64Array, 3);
257+
baseModule.__getUint64Array = getTypedArray.bind(null, BigUint64Array, 3);
258+
baseModule.__getUint64ArrayView = getTypedArrayView.bind(null, BigUint64Array, 3);
246259
}
247260
/** Gets a view on the values of a known-to-be Float32Array in the module's memory. */
248-
baseModule.__getFloat32Array = getTypedArrayImpl.bind(null, Float32Array, 2);
261+
baseModule.__getFloat32Array = getTypedArray.bind(null, Float32Array, 2);
262+
baseModule.__getFloat32ArrayView = getTypedArrayView.bind(null, Float32Array, 2);
249263
/** Gets a view on the values of a known-to-be Float64Array in the module's memory. */
250-
baseModule.__getFloat64Array = getTypedArrayImpl.bind(null, Float64Array, 3);
264+
baseModule.__getFloat64Array = getTypedArray.bind(null, Float64Array, 3);
265+
baseModule.__getFloat64ArrayView = getTypedArrayView.bind(null, Float64Array, 3);
251266

252267
/** Tests whether an object is an instance of the class represented by the specified base id. */
253268
function __instanceof(ptr, baseId) {

lib/loader/tests/build/untouched.wasm

104 Bytes
Binary file not shown.

lib/loader/tests/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ assert.strictEqual(module.__getString(module.COLOR), "red");
9595
let ref = module.__retain(module.__allocArray(module.UINT32ARRAY_ID, arr));
9696
assert(module.__instanceof(ref, module.UINT32ARRAY_ID));
9797
assert.deepEqual(module.__getUint32Array(ref), new Uint32Array(arr));
98+
assert.deepEqual(module.__getUint32ArrayView(ref), new Uint32Array(arr));
9899
assert.deepEqual(module.__getArray(ref), arr);
99100
module.__release(ref);
100101
try { module.__release(ref); assert(false); } catch (e) {};
@@ -106,6 +107,7 @@ assert.strictEqual(module.__getString(module.COLOR), "red");
106107
let ref = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, arr));
107108
assert(module.__instanceof(ref, module.FLOAT32ARRAY_ID));
108109
assert.deepEqual(module.__getFloat32Array(ref), new Float32Array(arr));
110+
assert.deepEqual(module.__getFloat32ArrayView(ref), new Float32Array(arr));
109111
assert.deepEqual(module.__getArray(ref), arr);
110112
module.__release(ref);
111113
try { module.__release(ref); assert(false); } catch (e) {};
@@ -154,7 +156,7 @@ module.dotrace(42);
154156

155157
// should be able to mutate an array in place using getArrayView
156158
{
157-
let ptr = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, [ 1, 2, 3]));
159+
let ptr = module.__retain(module.__allocArray(module.FLOAT32ARRAY_ID, [1, 2, 3]));
158160
let view = module.__getArrayView(ptr);
159161
assert.deepEqual(view, new Float32Array([1, 2, 3]));
160162
module.modifyFloat32Array(ptr, 0, 4);
@@ -165,12 +167,15 @@ module.dotrace(42);
165167
// should be able to mutate an array in place using getFloat32Array
166168
{
167169
let ptr = module.newFloat32Array(3); // returns are pre-retained
168-
let view = module.__getFloat32Array(ptr);
170+
let view = module.__getFloat32ArrayView(ptr);
171+
let arr = module.__getFloat32Array(ptr);
169172
assert.deepEqual(view, new Float32Array([0, 0, 0]));
173+
assert.deepEqual(arr, new Float32Array([0, 0, 0]));
170174
module.modifyFloat32Array(ptr, 0, 3);
171175
module.modifyFloat32Array(ptr, 1, 2);
172176
module.modifyFloat32Array(ptr, 2, 1);
173177
assert.deepEqual(view, new Float32Array([3, 2, 1]));
178+
assert.deepEqual(arr, new Float32Array([0, 0, 0]));
174179
module.__release(ptr);
175180
}
176181

0 commit comments

Comments
 (0)