Skip to content

Commit

Permalink
Added support for unsafe return types
Browse files Browse the repository at this point in the history
  • Loading branch information
torch2424 committed Oct 9, 2020
1 parent 6acadb1 commit 370bd3d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ An AsBindInstance is vaugley similar to a [WebAssembly instance](https://develop
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.
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.
Each **exported function** has the properties:
- `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.
- `unsafeReturnValue`
- 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.
##### unboundExports
Expand All @@ -255,6 +260,14 @@ Calling this method will (re-)enable type caching (speculative execution) for AL
Calling this method will disable type caching (speculative execution) for ALL exported functions on the AsBindInstance.
##### enableExportFunctionUnsafeReturnValue
Calling this method will (re-)enable unsafe return types for ALL exported functions on the AsBindInstance.
##### disableExportFunctionUnsafeReturnValue
Calling this method will disable unsafe return types for ALL exported functions on the AsBindInstance.
##### enableImportFunctionTypeCaching
Calling this method will (re-)enable type caching (speculative execution) for ALL importObject functions on the AsBindInstance.
Expand Down
1 change: 1 addition & 0 deletions lib/asbind-instance/bind-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export function bindExportFunction(asbindInstance, exportFunctionKey) {

// Initialize the state of our function
boundExport.shouldCacheTypes = true;
boundExport.unsafeReturnValue = false;
boundExport.cachedArgTypes = [];
boundExport.cachedReturnTypes = [];

Expand Down
47 changes: 39 additions & 8 deletions lib/asbind-instance/supported-ref-types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const getUnsafeResponse = (value, ptr) => {
return {
ptr: ptr,
value: value
};
};

const SUPPORTED_REF_TYPES = {
STRING: {
isTypeFromArgument: arg => {
Expand Down Expand Up @@ -29,7 +36,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getInt8Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt8ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getInt8ArrayView(responseRef),
responseRef
);
}
},
UINT8ARRAY: {
Expand All @@ -48,7 +58,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getUint8Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint8ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getUint8ArrayView(responseRef),
responseRef
);
}
},
INT16ARRAY: {
Expand All @@ -67,7 +80,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getInt16Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt16ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getInt16ArrayView(responseRef),
responseRef
);
}
},
UINT16ARRAY: {
Expand All @@ -86,7 +102,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getUint16Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint16ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getUint16ArrayView(responseRef),
responseRef
);
}
},
INT32ARRAY: {
Expand All @@ -105,7 +124,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getInt32Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt32ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getInt32ArrayView(responseRef),
responseRef
);
}
},
UINT32ARRAY: {
Expand All @@ -124,7 +146,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getUint32Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint32ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getUint32ArrayView(responseRef),
responseRef
);
}
},
FLOAT32ARRAY: {
Expand All @@ -146,7 +171,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getFloat32Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getFloat32ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getFloat32ArrayView(responseRef),
responseRef
);
}
},
FLOAT64ARRAY: {
Expand All @@ -168,7 +196,10 @@ const SUPPORTED_REF_TYPES = {
return wasmExports.__getFloat64Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getFloat64ArrayView(responseRef);
return getUnsafeResponse(
wasmExports.__getFloat64ArrayView(responseRef),
responseRef
);
}
}
};
Expand Down
26 changes: 18 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,19 +674,29 @@ describe("asbind", () => {

assert.equal(
asbindInstance.exports[exportName].unsafeReturnValue,
undefined
false
);

asbindInstance.exports[exportName].unsafeReturnValue = true;
let randomValue;
let array;
let arrayMapResponse;

const randomValue = Math.floor(Math.random() * 10) + 1;
const array = global[typedArrayKey].from([randomValue]);
const arrayMapResponse = asbindInstance.exports[exportName](array);
randomValue = Math.floor(Math.random() * 10) + 1;
array = global[typedArrayKey].from([randomValue]);
arrayMapResponse = asbindInstance.exports[exportName](array);

console.log(arrayMapResponse);
// Check to make sure it returns an arrary
assert(arrayMapResponse.length > 0);

// Ensure it has the correct values
assert.equal(testImportCalledWith[0][0], randomValue);
asbindInstance.exports[exportName].unsafeReturnValue = true;

randomValue = Math.floor(Math.random() * 10) + 1;
array = global[typedArrayKey].from([randomValue]);
arrayMapResponse = asbindInstance.exports[exportName](array);

// Assert it now returns a pointer and a value
assert(arrayMapResponse.ptr !== undefined);
assert(arrayMapResponse.value !== undefined);
});
});
});
Expand Down

0 comments on commit 370bd3d

Please sign in to comment.