Skip to content

Commit 41dae7a

Browse files
committed
Add a test for custom types
1 parent 464614c commit 41dae7a

File tree

6 files changed

+60
-12
lines changed

6 files changed

+60
-12
lines changed

lib/asbind-instance/asbind-instance.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ export default class AsbindInstance {
5454
this.importObject = {};
5555
}
5656

57+
getTypeId(typeName) {
58+
if (typeName in this.typeDescriptor.typeIds) {
59+
return this.typeDescriptor.typeIds[typeName].id;
60+
}
61+
throw Error(`Unknown type ${JSON.stringify(typeName)}`);
62+
}
63+
64+
getTypeSize(typeName) {
65+
if (typeName in this.typeDescriptor.typeIds) {
66+
return this.typeDescriptor.typeIds[typeName].byteSize;
67+
}
68+
throw Error(`Unknown type ${JSON.stringify(typeName)}`);
69+
}
70+
5771
_validate() {
5872
if (
5973
!WebAssembly.Module.exports(this.module).find(exp => exp.name === "__new")

lib/asbind-instance/bind-function.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ function normalizeArrayBufferViewTypeName(typeName) {
2323
return typeName;
2424
}
2525

26-
function getTypeId(asbindInstance, typeName) {
27-
if (typeName in asbindInstance.typeDescriptor.typeIds) {
28-
return asbindInstance.typeDescriptor.typeIds[typeName];
29-
}
30-
throw Error(`Unknown type ${JSON.stringify(typeName)}`);
31-
}
32-
3326
function nop(asbindInstance, value, typeName) {
3427
return value;
3528
}
@@ -49,7 +42,7 @@ function getArrayBuffer(asbindInstance, value, typeName) {
4942
function putArrayBuffer(asbindInstance, value, typeName) {
5043
const ptr = asbindInstance.exports.__new(
5144
value.byteLength,
52-
getTypeId(asbindInstance, typeName)
45+
asbindInstance.getTypeId(typeName)
5346
);
5447
new Uint8Array(
5548
asbindInstance.exports.memory.buffer,
@@ -66,7 +59,7 @@ function getArrayBufferView(asbindInstance, value, typeName) {
6659
}
6760
function putArrayBufferView(asbindInstance, value, typeName) {
6861
return asbindInstance.exports.__newArray(
69-
getTypeId(asbindInstance, typeName),
62+
asbindInstance.getTypeId(typeName),
7063
value
7164
);
7265
}
@@ -96,12 +89,12 @@ function putArray(asbindInstance, value, typeName) {
9689
innerTypeConverter.jsToAsc(asbindInstance, v, innerTypeName)
9790
);
9891
return asbindInstance.exports.__newArray(
99-
getTypeId(asbindInstance, typeName),
92+
asbindInstance.getTypeId(typeName),
10093
convertedValues
10194
);
10295
}
10396

104-
const converters = new Map([
97+
export const converters = new Map([
10598
[/^void$/, { ascToJs: nop, jsToAsc: nop }],
10699
[/^(i|u)(8|16|32)$/, { ascToJs: nop, jsToAsc: nop }],
107100
[/^f(32|64)$/, { ascToJs: nop, jsToAsc: nop }],

lib/lib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { version } from "../package.json";
22
import AsbindInstance from "./asbind-instance/asbind-instance";
3+
export { converters } from "./asbind-instance/bind-function";
34

45
export async function instantiate(source, importObject) {
56
let asbindInstance = new AsbindInstance();

test/tests/custom-type/asc.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class X {
2+
x: string;
3+
constructor(x: string) {
4+
this.x = x;
5+
}
6+
}
7+
export function makeAThing(v: string): X {
8+
return new X(v);
9+
}
10+
11+
export function readAThing(v: X): u32 {
12+
return v.x.length;
13+
}

test/tests/custom-type/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
describe("as-bind", function() {
2+
it("should be extensible with custom type handlers", async function() {
3+
AsBind.converters.set(/^tests\/custom-type\/asc\/X$/, {
4+
ascToJs(asbindInstance, value, typeName) {
5+
const dv = new DataView(asbindInstance.exports.memory.buffer);
6+
const strPtr = dv.getUint32(value, true);
7+
return asbindInstance.exports.__getString(strPtr);
8+
},
9+
jsToAsc(asbindInstance, value, typeName) {
10+
const ptr = asbindInstance.exports.__new(
11+
asbindInstance.getTypeId(typeName),
12+
asbindInstance.getTypeSize(typeName)
13+
);
14+
const strPtr = asbindInstance.exports.__newString(value);
15+
const dv = new DataView(asbindInstance.exports.memory.buffer);
16+
dv.setUint32(ptr, strPtr, true);
17+
return ptr;
18+
}
19+
});
20+
const instance = await AsBind.instantiate(this.rawModule);
21+
assert(instance.exports.makeAThing("hello") === "hello");
22+
assert(instance.exports.readAThing("hello") === 5);
23+
});
24+
});

transform.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ function extractTypeIds(type) {
3838
if (!clazz) {
3939
return result;
4040
}
41-
result[clazz.internalName] = clazz.id;
41+
result[clazz.internalName] = {
42+
id: clazz.id,
43+
byteSize: clazz.nextMemoryOffset
44+
};
4245
if (clazz.typeArguments) {
4346
for (const subType of clazz.typeArguments) {
4447
Object.assign(result, extractTypeIds(subType));

0 commit comments

Comments
 (0)