Skip to content

Commit 6acadb1

Browse files
committed
Have the implementation just writing tests
1 parent 9ab3ece commit 6acadb1

File tree

4 files changed

+122
-13
lines changed

4 files changed

+122
-13
lines changed

lib/asbind-instance/asbind-instance.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ export default class AsbindInstance {
122122
});
123123
}
124124

125+
enableExportFunctionUnsafeReturnValue() {
126+
Object.keys(this.exports).forEach(exportKey => {
127+
this.exports[exportKey].unsafeReturnValue = true;
128+
});
129+
}
130+
131+
disableExportFunctionUnsafeReturnValue() {
132+
Object.keys(this.exports).forEach(exportKey => {
133+
this.exports[exportKey].unsafeReturnValue = false;
134+
});
135+
}
136+
125137
enableImportFunctionTypeCaching() {
126138
// Need to traverse the importObject and bind all import functions
127139
traverseObjectAndRunCallbackForFunctions(

lib/asbind-instance/bind-function.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,17 @@ export function bindExportFunction(asbindInstance, exportFunctionKey) {
226226
}
227227

228228
if (supportedType) {
229-
response = supportedType.getValueFromRef(
230-
exports,
231-
exportFunctionResponse
232-
);
229+
if (functionThis.unsafeReturnValue) {
230+
response = supportedType.getUnsafeValueFromRef(
231+
exports,
232+
exportFunctionResponse
233+
);
234+
} else {
235+
response = supportedType.getValueFromRef(
236+
exports,
237+
exportFunctionResponse
238+
);
239+
}
233240
} else if (typeof exportFunctionResponse === "number") {
234241
response = exportFunctionResponse;
235242
if (functionThis.shouldCacheTypes) {

lib/asbind-instance/supported-ref-types.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const SUPPORTED_REF_TYPES = {
2626
);
2727
},
2828
getValueFromRef: (wasmExports, responseRef) => {
29-
return wasmExports.__getInt8Array(responseRef).slice();
29+
return wasmExports.__getInt8Array(responseRef);
30+
},
31+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
32+
return wasmExports.__getInt8ArrayView(responseRef);
3033
}
3134
},
3235
UINT8ARRAY: {
@@ -42,7 +45,10 @@ const SUPPORTED_REF_TYPES = {
4245
);
4346
},
4447
getValueFromRef: (wasmExports, responseRef) => {
45-
return wasmExports.__getUint8Array(responseRef).slice();
48+
return wasmExports.__getUint8Array(responseRef);
49+
},
50+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
51+
return wasmExports.__getUint8ArrayView(responseRef);
4652
}
4753
},
4854
INT16ARRAY: {
@@ -58,7 +64,10 @@ const SUPPORTED_REF_TYPES = {
5864
);
5965
},
6066
getValueFromRef: (wasmExports, responseRef) => {
61-
return wasmExports.__getInt16Array(responseRef).slice();
67+
return wasmExports.__getInt16Array(responseRef);
68+
},
69+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
70+
return wasmExports.__getInt16ArrayView(responseRef);
6271
}
6372
},
6473
UINT16ARRAY: {
@@ -74,7 +83,10 @@ const SUPPORTED_REF_TYPES = {
7483
);
7584
},
7685
getValueFromRef: (wasmExports, responseRef) => {
77-
return wasmExports.__getUint16Array(responseRef).slice();
86+
return wasmExports.__getUint16Array(responseRef);
87+
},
88+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
89+
return wasmExports.__getUint16ArrayView(responseRef);
7890
}
7991
},
8092
INT32ARRAY: {
@@ -90,7 +102,10 @@ const SUPPORTED_REF_TYPES = {
90102
);
91103
},
92104
getValueFromRef: (wasmExports, responseRef) => {
93-
return wasmExports.__getInt32Array(responseRef).slice();
105+
return wasmExports.__getInt32Array(responseRef);
106+
},
107+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
108+
return wasmExports.__getInt32ArrayView(responseRef);
94109
}
95110
},
96111
UINT32ARRAY: {
@@ -106,7 +121,10 @@ const SUPPORTED_REF_TYPES = {
106121
);
107122
},
108123
getValueFromRef: (wasmExports, responseRef) => {
109-
return wasmExports.__getUint32Array(responseRef).slice();
124+
return wasmExports.__getUint32Array(responseRef);
125+
},
126+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
127+
return wasmExports.__getUint32ArrayView(responseRef);
110128
}
111129
},
112130
FLOAT32ARRAY: {
@@ -125,7 +143,10 @@ const SUPPORTED_REF_TYPES = {
125143
);
126144
},
127145
getValueFromRef: (wasmExports, responseRef) => {
128-
return wasmExports.__getFloat32Array(responseRef).slice();
146+
return wasmExports.__getFloat32Array(responseRef);
147+
},
148+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
149+
return wasmExports.__getFloat32ArrayView(responseRef);
129150
}
130151
},
131152
FLOAT64ARRAY: {
@@ -144,7 +165,10 @@ const SUPPORTED_REF_TYPES = {
144165
);
145166
},
146167
getValueFromRef: (wasmExports, responseRef) => {
147-
return wasmExports.__getFloat64Array(responseRef).slice();
168+
return wasmExports.__getFloat64Array(responseRef);
169+
},
170+
getUnsafeValueFromRef: (wasmExports, responseRef) => {
171+
return wasmExports.__getFloat64ArrayView(responseRef);
148172
}
149173
}
150174
};

test/test.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ describe("asbind", () => {
313313
describe("type caching", () => {
314314
let asbindInstance;
315315
let testImportCalledWith = [];
316-
let weappedBaseImportObject = {};
317316

318317
beforeEach(async () => {
319318
const importObjectFunction = value => {
@@ -624,4 +623,71 @@ describe("asbind", () => {
624623
);
625624
});
626625
});
626+
627+
describe("Unsafe Return Value", () => {
628+
let asbindInstance;
629+
let testImportCalledWith = [];
630+
631+
beforeEach(async () => {
632+
const importObjectFunction = value => {
633+
testImportCalledWith = [value];
634+
};
635+
636+
wrappedBaseImportObject = {
637+
...baseImportObject,
638+
test: {
639+
testImportString: importObjectFunction,
640+
testImportTwoStrings: (value1, value2) => {
641+
testImportCalledWith = [value1, value2];
642+
},
643+
testImportReturnNumber: () => -1,
644+
testImportInt8Array: importObjectFunction,
645+
testImportUint8Array: importObjectFunction,
646+
testImportInt16Array: importObjectFunction,
647+
testImportUint16Array: importObjectFunction,
648+
testImportInt32Array: importObjectFunction,
649+
testImportUint32Array: importObjectFunction,
650+
testImportFloat32Array: importObjectFunction,
651+
testImportFloat64Array: importObjectFunction
652+
}
653+
};
654+
655+
asbindInstance = await AsBind.instantiate(
656+
wasmBytes,
657+
wrappedBaseImportObject
658+
);
659+
});
660+
661+
// TypedArrays
662+
[
663+
"Int8Array",
664+
"Uint8Array",
665+
"Int16Array",
666+
"Uint16Array",
667+
"Int32Array",
668+
"Uint32Array",
669+
"Float32Array",
670+
"Float64Array"
671+
].forEach(typedArrayKey => {
672+
it(`should handle ${typedArrayKey} being returned unsafe`, () => {
673+
const exportName = `map${typedArrayKey}`;
674+
675+
assert.equal(
676+
asbindInstance.exports[exportName].unsafeReturnValue,
677+
undefined
678+
);
679+
680+
asbindInstance.exports[exportName].unsafeReturnValue = true;
681+
682+
const randomValue = Math.floor(Math.random() * 10) + 1;
683+
const array = global[typedArrayKey].from([randomValue]);
684+
const arrayMapResponse = asbindInstance.exports[exportName](array);
685+
686+
console.log(arrayMapResponse);
687+
688+
// Ensure it has the correct values
689+
assert.equal(testImportCalledWith[0][0], randomValue);
690+
});
691+
});
692+
});
627693
});

0 commit comments

Comments
 (0)