Skip to content

Commit

Permalink
Add a test for custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Mar 15, 2021
1 parent 464614c commit 41dae7a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
14 changes: 14 additions & 0 deletions lib/asbind-instance/asbind-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ export default class AsbindInstance {
this.importObject = {};
}

getTypeId(typeName) {
if (typeName in this.typeDescriptor.typeIds) {
return this.typeDescriptor.typeIds[typeName].id;
}
throw Error(`Unknown type ${JSON.stringify(typeName)}`);
}

getTypeSize(typeName) {
if (typeName in this.typeDescriptor.typeIds) {
return this.typeDescriptor.typeIds[typeName].byteSize;
}
throw Error(`Unknown type ${JSON.stringify(typeName)}`);
}

_validate() {
if (
!WebAssembly.Module.exports(this.module).find(exp => exp.name === "__new")
Expand Down
15 changes: 4 additions & 11 deletions lib/asbind-instance/bind-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ function normalizeArrayBufferViewTypeName(typeName) {
return typeName;
}

function getTypeId(asbindInstance, typeName) {
if (typeName in asbindInstance.typeDescriptor.typeIds) {
return asbindInstance.typeDescriptor.typeIds[typeName];
}
throw Error(`Unknown type ${JSON.stringify(typeName)}`);
}

function nop(asbindInstance, value, typeName) {
return value;
}
Expand All @@ -49,7 +42,7 @@ function getArrayBuffer(asbindInstance, value, typeName) {
function putArrayBuffer(asbindInstance, value, typeName) {
const ptr = asbindInstance.exports.__new(
value.byteLength,
getTypeId(asbindInstance, typeName)
asbindInstance.getTypeId(typeName)
);
new Uint8Array(
asbindInstance.exports.memory.buffer,
Expand All @@ -66,7 +59,7 @@ function getArrayBufferView(asbindInstance, value, typeName) {
}
function putArrayBufferView(asbindInstance, value, typeName) {
return asbindInstance.exports.__newArray(
getTypeId(asbindInstance, typeName),
asbindInstance.getTypeId(typeName),
value
);
}
Expand Down Expand Up @@ -96,12 +89,12 @@ function putArray(asbindInstance, value, typeName) {
innerTypeConverter.jsToAsc(asbindInstance, v, innerTypeName)
);
return asbindInstance.exports.__newArray(
getTypeId(asbindInstance, typeName),
asbindInstance.getTypeId(typeName),
convertedValues
);
}

const converters = new Map([
export const converters = new Map([
[/^void$/, { ascToJs: nop, jsToAsc: nop }],
[/^(i|u)(8|16|32)$/, { ascToJs: nop, jsToAsc: nop }],
[/^f(32|64)$/, { ascToJs: nop, jsToAsc: nop }],
Expand Down
1 change: 1 addition & 0 deletions lib/lib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { version } from "../package.json";
import AsbindInstance from "./asbind-instance/asbind-instance";
export { converters } from "./asbind-instance/bind-function";

export async function instantiate(source, importObject) {
let asbindInstance = new AsbindInstance();
Expand Down
13 changes: 13 additions & 0 deletions test/tests/custom-type/asc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class X {
x: string;
constructor(x: string) {
this.x = x;
}
}
export function makeAThing(v: string): X {
return new X(v);
}

export function readAThing(v: X): u32 {
return v.x.length;
}
24 changes: 24 additions & 0 deletions test/tests/custom-type/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe("as-bind", function() {
it("should be extensible with custom type handlers", async function() {
AsBind.converters.set(/^tests\/custom-type\/asc\/X$/, {
ascToJs(asbindInstance, value, typeName) {
const dv = new DataView(asbindInstance.exports.memory.buffer);
const strPtr = dv.getUint32(value, true);
return asbindInstance.exports.__getString(strPtr);
},
jsToAsc(asbindInstance, value, typeName) {
const ptr = asbindInstance.exports.__new(
asbindInstance.getTypeId(typeName),
asbindInstance.getTypeSize(typeName)
);
const strPtr = asbindInstance.exports.__newString(value);
const dv = new DataView(asbindInstance.exports.memory.buffer);
dv.setUint32(ptr, strPtr, true);
return ptr;
}
});
const instance = await AsBind.instantiate(this.rawModule);
assert(instance.exports.makeAThing("hello") === "hello");
assert(instance.exports.readAThing("hello") === 5);
});
});
5 changes: 4 additions & 1 deletion transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ function extractTypeIds(type) {
if (!clazz) {
return result;
}
result[clazz.internalName] = clazz.id;
result[clazz.internalName] = {
id: clazz.id,
byteSize: clazz.nextMemoryOffset
};
if (clazz.typeArguments) {
for (const subType of clazz.typeArguments) {
Object.assign(result, extractTypeIds(subType));
Expand Down

0 comments on commit 41dae7a

Please sign in to comment.