Skip to content

Commit 165f796

Browse files
committed
Arrays work
1 parent c74f88e commit 165f796

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

lib/asbind-instance/bind-function.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,56 @@ function putArrayBuffer(asbindInstance, value, typeName) {
4949
);
5050
}
5151

52+
function arrayInnerType(typeName) {
53+
if (!typeName.startsWith("Array<")) {
54+
throw Error(`${JSON.stringify(typeName)} is not an array type`);
55+
}
56+
return typeName.slice("Array<".length, -1);
57+
}
58+
59+
function getArray(asbindInstance, value, typeName) {
60+
const innerTypeName = arrayInnerType(typeName);
61+
const innerTypeConverter = getConverterForType(innerTypeName);
62+
const rawArray = asbindInstance.exports.__getArray(value);
63+
return rawArray.map(v =>
64+
innerTypeConverter.ascToJs(asbindInstance, v, innerTypeName)
65+
);
66+
}
67+
68+
function putArray(asbindInstance, value, typeName) {
69+
const innerTypeName = arrayInnerType(typeName);
70+
const innerTypeConverter = getConverterForType(innerTypeName);
71+
const convertedValues = value.map(v =>
72+
innerTypeConverter.jsToAsc(asbindInstance, v, innerTypeName)
73+
);
74+
return asbindInstance.exports.__newArray(
75+
getTypeId(asbindInstance, typeName),
76+
convertedValues
77+
);
78+
}
79+
5280
const converters = new Map([
53-
[/void/, { ascToJs: nop, jsToAsc: nop }],
54-
[/(i|u)(8|16|32)/, { ascToJs: nop, jsToAsc: nop }],
55-
[/f(32|64)/, { ascToJs: nop, jsToAsc: nop }],
56-
[/[sS]tring/, { ascToJs: getString, jsToAsc: putString }],
81+
[/^void$/, { ascToJs: nop, jsToAsc: nop }],
82+
[/^(i|u)(8|16|32)$/, { ascToJs: nop, jsToAsc: nop }],
83+
[/^f(32|64)$/, { ascToJs: nop, jsToAsc: nop }],
84+
[/^[sS]tring$/, { ascToJs: getString, jsToAsc: putString }],
5785
[
58-
/(Ui|I)nt(8|16|32)Array/,
86+
/^(Ui|I)nt(8|16|32)Array$/,
5987
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
6088
],
6189
[
62-
/Big(Ui|I)nt64Array/,
90+
/^Big(Ui|I)nt64Array$/,
6391
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
6492
],
6593
[
66-
/Uint8ClampedArray/,
94+
/^Uint8ClampedArray$/,
6795
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
6896
],
6997
[
70-
/Float(32|64)Array/,
98+
/^Float(32|64)Array$/,
7199
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBuffer }
72-
]
100+
],
101+
[/^Array<.+>$/, { ascToJs: getArray, jsToAsc: putArray }]
73102
]);
74103

75104
const warned = new Set();

test/tests/array/asc.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function swapAndPad(a: Array<f64>, b: f64[]): Array<f64> {
2+
const result = swappedConcat(a, b);
3+
result.unshift(255);
4+
result.push(255);
5+
return result;
6+
}
7+
8+
declare function swappedConcat(a: f64[], b: Array<f64>): f64[];

test/tests/array/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
describe("as-bind", function() {
2+
it("should handle array", async function() {
3+
const instance = await AsBind.instantiate(this.rawModule, {
4+
asc: {
5+
swappedConcat(a, b) {
6+
return [].concat(b, a);
7+
}
8+
}
9+
});
10+
assert(
11+
instance.exports.swapAndPad([1, 2, 3], [10, 11, 12]).join(",") ===
12+
[255, 10, 11, 12, 1, 2, 3, 255].join(",")
13+
);
14+
});
15+
});

transform.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ function elementHasFlag(el, flag) {
1111
}
1212

1313
function typeName(type) {
14-
return type.name.text ?? type.name.identifier.text;
14+
let name = type.name.text ?? type.name.identifier.text;
15+
if (type.typeArguments.length > 0) {
16+
name = `${name}<${type.typeArguments.map(typeName).join(",")}>`;
17+
}
18+
return name;
1519
}
1620

1721
function containingModule(func) {
@@ -24,8 +28,6 @@ function containingModule(func) {
2428
}
2529

2630
function getFunctionTypeDescriptor(func) {
27-
// TODO: Generics?
28-
func = func.instances.get("");
2931
return {
3032
returnType: typeName(func.declaration.signature.returnType),
3133
parameters: func.declaration.signature.parameters.map(parameter =>
@@ -76,7 +78,6 @@ class AsBindTransform extends Transform {
7678

7779
const typeIds = {};
7880
const importedFunctions = {};
79-
debugger;
8081
for (const importedFunction of flatImportedFunctions) {
8182
// To know under what module name an imported function will be expected,
8283
// we have to find the containing module of the given function, take the

0 commit comments

Comments
 (0)