Skip to content

Commit

Permalink
Have the implementation just writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
torch2424 committed Oct 9, 2020
1 parent 9ab3ece commit 6acadb1
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 13 deletions.
12 changes: 12 additions & 0 deletions lib/asbind-instance/asbind-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ export default class AsbindInstance {
});
}

enableExportFunctionUnsafeReturnValue() {
Object.keys(this.exports).forEach(exportKey => {
this.exports[exportKey].unsafeReturnValue = true;
});
}

disableExportFunctionUnsafeReturnValue() {
Object.keys(this.exports).forEach(exportKey => {
this.exports[exportKey].unsafeReturnValue = false;
});
}

enableImportFunctionTypeCaching() {
// Need to traverse the importObject and bind all import functions
traverseObjectAndRunCallbackForFunctions(
Expand Down
15 changes: 11 additions & 4 deletions lib/asbind-instance/bind-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,17 @@ export function bindExportFunction(asbindInstance, exportFunctionKey) {
}

if (supportedType) {
response = supportedType.getValueFromRef(
exports,
exportFunctionResponse
);
if (functionThis.unsafeReturnValue) {
response = supportedType.getUnsafeValueFromRef(
exports,
exportFunctionResponse
);
} else {
response = supportedType.getValueFromRef(
exports,
exportFunctionResponse
);
}
} else if (typeof exportFunctionResponse === "number") {
response = exportFunctionResponse;
if (functionThis.shouldCacheTypes) {
Expand Down
40 changes: 32 additions & 8 deletions lib/asbind-instance/supported-ref-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt8Array(responseRef).slice();
return wasmExports.__getInt8Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt8ArrayView(responseRef);
}
},
UINT8ARRAY: {
Expand All @@ -42,7 +45,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint8Array(responseRef).slice();
return wasmExports.__getUint8Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint8ArrayView(responseRef);
}
},
INT16ARRAY: {
Expand All @@ -58,7 +64,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt16Array(responseRef).slice();
return wasmExports.__getInt16Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt16ArrayView(responseRef);
}
},
UINT16ARRAY: {
Expand All @@ -74,7 +83,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint16Array(responseRef).slice();
return wasmExports.__getUint16Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint16ArrayView(responseRef);
}
},
INT32ARRAY: {
Expand All @@ -90,7 +102,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt32Array(responseRef).slice();
return wasmExports.__getInt32Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getInt32ArrayView(responseRef);
}
},
UINT32ARRAY: {
Expand All @@ -106,7 +121,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint32Array(responseRef).slice();
return wasmExports.__getUint32Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getUint32ArrayView(responseRef);
}
},
FLOAT32ARRAY: {
Expand All @@ -125,7 +143,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getFloat32Array(responseRef).slice();
return wasmExports.__getFloat32Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getFloat32ArrayView(responseRef);
}
},
FLOAT64ARRAY: {
Expand All @@ -144,7 +165,10 @@ const SUPPORTED_REF_TYPES = {
);
},
getValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getFloat64Array(responseRef).slice();
return wasmExports.__getFloat64Array(responseRef);
},
getUnsafeValueFromRef: (wasmExports, responseRef) => {
return wasmExports.__getFloat64ArrayView(responseRef);
}
}
};
Expand Down
68 changes: 67 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ describe("asbind", () => {
describe("type caching", () => {
let asbindInstance;
let testImportCalledWith = [];
let weappedBaseImportObject = {};

beforeEach(async () => {
const importObjectFunction = value => {
Expand Down Expand Up @@ -624,4 +623,71 @@ describe("asbind", () => {
);
});
});

describe("Unsafe Return Value", () => {
let asbindInstance;
let testImportCalledWith = [];

beforeEach(async () => {
const importObjectFunction = value => {
testImportCalledWith = [value];
};

wrappedBaseImportObject = {
...baseImportObject,
test: {
testImportString: importObjectFunction,
testImportTwoStrings: (value1, value2) => {
testImportCalledWith = [value1, value2];
},
testImportReturnNumber: () => -1,
testImportInt8Array: importObjectFunction,
testImportUint8Array: importObjectFunction,
testImportInt16Array: importObjectFunction,
testImportUint16Array: importObjectFunction,
testImportInt32Array: importObjectFunction,
testImportUint32Array: importObjectFunction,
testImportFloat32Array: importObjectFunction,
testImportFloat64Array: importObjectFunction
}
};

asbindInstance = await AsBind.instantiate(
wasmBytes,
wrappedBaseImportObject
);
});

// TypedArrays
[
"Int8Array",
"Uint8Array",
"Int16Array",
"Uint16Array",
"Int32Array",
"Uint32Array",
"Float32Array",
"Float64Array"
].forEach(typedArrayKey => {
it(`should handle ${typedArrayKey} being returned unsafe`, () => {
const exportName = `map${typedArrayKey}`;

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

asbindInstance.exports[exportName].unsafeReturnValue = true;

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

console.log(arrayMapResponse);

// Ensure it has the correct values
assert.equal(testImportCalledWith[0][0], randomValue);
});
});
});
});

0 comments on commit 6acadb1

Please sign in to comment.