Skip to content

Commit 370bd3d

Browse files
committed
Added support for unsafe return types
1 parent 6acadb1 commit 370bd3d

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,12 @@ An AsBindInstance is vaugley similar to a [WebAssembly instance](https://develop
235235
236236
Similar to to [WebAssembly.Instance.prototype.exports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance/exports), this is an object containing all of the exported fields from the WebAssembly module. However, **exported functions** are bound / wrapped in which they will handle passing the supported high-level data types to the exported AssemblyScript function.
237237
238-
Each **exported function** has the property: `shouldCacheTypes`. If you would like to disable type caching (speculative execution) for a particular function, you can do: `asBindInstance.exports.myFunction.shouldCacheTypes = false;`. Or set to true, to re-enable type caching.
238+
Each **exported function** has the properties:
239+
240+
- `shouldCacheTypes`
241+
- If you would like to disable type caching (speculative execution) for a particular function, you can do: `asBindInstance.exports.myFunction.shouldCacheTypes = false;`. Or set to true, to re-enable type caching.
242+
- `unsafeReturnValue`
243+
- By default, all values (in particular [TypedArrays](https://www.assemblyscript.org/stdlib/typedarray.html#typedarray)) will be copied out of Wasm Memory, instead of giving direct read/write access. If you would like to use a view of the returned memory, you can do: `asBindInstance.exports.myFunction.unsafeReturnValue = true;`. For More context, please see the [AssemblyScript loader documentation](https://www.assemblyscript.org/loader.html#module-instance-utility) on array views.
239244
240245
##### unboundExports
241246
@@ -255,6 +260,14 @@ Calling this method will (re-)enable type caching (speculative execution) for AL
255260
256261
Calling this method will disable type caching (speculative execution) for ALL exported functions on the AsBindInstance.
257262
263+
##### enableExportFunctionUnsafeReturnValue
264+
265+
Calling this method will (re-)enable unsafe return types for ALL exported functions on the AsBindInstance.
266+
267+
##### disableExportFunctionUnsafeReturnValue
268+
269+
Calling this method will disable unsafe return types for ALL exported functions on the AsBindInstance.
270+
258271
##### enableImportFunctionTypeCaching
259272
260273
Calling this method will (re-)enable type caching (speculative execution) for ALL importObject functions on the AsBindInstance.

lib/asbind-instance/bind-function.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export function bindExportFunction(asbindInstance, exportFunctionKey) {
253253

254254
// Initialize the state of our function
255255
boundExport.shouldCacheTypes = true;
256+
boundExport.unsafeReturnValue = false;
256257
boundExport.cachedArgTypes = [];
257258
boundExport.cachedReturnTypes = [];
258259

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
const getUnsafeResponse = (value, ptr) => {
2+
return {
3+
ptr: ptr,
4+
value: value
5+
};
6+
};
7+
18
const SUPPORTED_REF_TYPES = {
29
STRING: {
310
isTypeFromArgument: arg => {
@@ -29,7 +36,10 @@ const SUPPORTED_REF_TYPES = {
2936
return wasmExports.__getInt8Array(responseRef);
3037
},
3138
getUnsafeValueFromRef: (wasmExports, responseRef) => {
32-
return wasmExports.__getInt8ArrayView(responseRef);
39+
return getUnsafeResponse(
40+
wasmExports.__getInt8ArrayView(responseRef),
41+
responseRef
42+
);
3343
}
3444
},
3545
UINT8ARRAY: {
@@ -48,7 +58,10 @@ const SUPPORTED_REF_TYPES = {
4858
return wasmExports.__getUint8Array(responseRef);
4959
},
5060
getUnsafeValueFromRef: (wasmExports, responseRef) => {
51-
return wasmExports.__getUint8ArrayView(responseRef);
61+
return getUnsafeResponse(
62+
wasmExports.__getUint8ArrayView(responseRef),
63+
responseRef
64+
);
5265
}
5366
},
5467
INT16ARRAY: {
@@ -67,7 +80,10 @@ const SUPPORTED_REF_TYPES = {
6780
return wasmExports.__getInt16Array(responseRef);
6881
},
6982
getUnsafeValueFromRef: (wasmExports, responseRef) => {
70-
return wasmExports.__getInt16ArrayView(responseRef);
83+
return getUnsafeResponse(
84+
wasmExports.__getInt16ArrayView(responseRef),
85+
responseRef
86+
);
7187
}
7288
},
7389
UINT16ARRAY: {
@@ -86,7 +102,10 @@ const SUPPORTED_REF_TYPES = {
86102
return wasmExports.__getUint16Array(responseRef);
87103
},
88104
getUnsafeValueFromRef: (wasmExports, responseRef) => {
89-
return wasmExports.__getUint16ArrayView(responseRef);
105+
return getUnsafeResponse(
106+
wasmExports.__getUint16ArrayView(responseRef),
107+
responseRef
108+
);
90109
}
91110
},
92111
INT32ARRAY: {
@@ -105,7 +124,10 @@ const SUPPORTED_REF_TYPES = {
105124
return wasmExports.__getInt32Array(responseRef);
106125
},
107126
getUnsafeValueFromRef: (wasmExports, responseRef) => {
108-
return wasmExports.__getInt32ArrayView(responseRef);
127+
return getUnsafeResponse(
128+
wasmExports.__getInt32ArrayView(responseRef),
129+
responseRef
130+
);
109131
}
110132
},
111133
UINT32ARRAY: {
@@ -124,7 +146,10 @@ const SUPPORTED_REF_TYPES = {
124146
return wasmExports.__getUint32Array(responseRef);
125147
},
126148
getUnsafeValueFromRef: (wasmExports, responseRef) => {
127-
return wasmExports.__getUint32ArrayView(responseRef);
149+
return getUnsafeResponse(
150+
wasmExports.__getUint32ArrayView(responseRef),
151+
responseRef
152+
);
128153
}
129154
},
130155
FLOAT32ARRAY: {
@@ -146,7 +171,10 @@ const SUPPORTED_REF_TYPES = {
146171
return wasmExports.__getFloat32Array(responseRef);
147172
},
148173
getUnsafeValueFromRef: (wasmExports, responseRef) => {
149-
return wasmExports.__getFloat32ArrayView(responseRef);
174+
return getUnsafeResponse(
175+
wasmExports.__getFloat32ArrayView(responseRef),
176+
responseRef
177+
);
150178
}
151179
},
152180
FLOAT64ARRAY: {
@@ -168,7 +196,10 @@ const SUPPORTED_REF_TYPES = {
168196
return wasmExports.__getFloat64Array(responseRef);
169197
},
170198
getUnsafeValueFromRef: (wasmExports, responseRef) => {
171-
return wasmExports.__getFloat64ArrayView(responseRef);
199+
return getUnsafeResponse(
200+
wasmExports.__getFloat64ArrayView(responseRef),
201+
responseRef
202+
);
172203
}
173204
}
174205
};

test/test.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -674,19 +674,29 @@ describe("asbind", () => {
674674

675675
assert.equal(
676676
asbindInstance.exports[exportName].unsafeReturnValue,
677-
undefined
677+
false
678678
);
679679

680-
asbindInstance.exports[exportName].unsafeReturnValue = true;
680+
let randomValue;
681+
let array;
682+
let arrayMapResponse;
681683

682-
const randomValue = Math.floor(Math.random() * 10) + 1;
683-
const array = global[typedArrayKey].from([randomValue]);
684-
const arrayMapResponse = asbindInstance.exports[exportName](array);
684+
randomValue = Math.floor(Math.random() * 10) + 1;
685+
array = global[typedArrayKey].from([randomValue]);
686+
arrayMapResponse = asbindInstance.exports[exportName](array);
685687

686-
console.log(arrayMapResponse);
688+
// Check to make sure it returns an arrary
689+
assert(arrayMapResponse.length > 0);
687690

688-
// Ensure it has the correct values
689-
assert.equal(testImportCalledWith[0][0], randomValue);
691+
asbindInstance.exports[exportName].unsafeReturnValue = true;
692+
693+
randomValue = Math.floor(Math.random() * 10) + 1;
694+
array = global[typedArrayKey].from([randomValue]);
695+
arrayMapResponse = asbindInstance.exports[exportName](array);
696+
697+
// Assert it now returns a pointer and a value
698+
assert(arrayMapResponse.ptr !== undefined);
699+
assert(arrayMapResponse.value !== undefined);
690700
});
691701
});
692702
});

0 commit comments

Comments
 (0)