Skip to content

Commit a1a36ba

Browse files
authored
feat: add namespace exported declare function support #91 and fix #94 (#92)
* feat: add namespace support #91 * fix: #94 add FLoat32Array test case
1 parent 158f9d0 commit a1a36ba

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

lib/asbind-instance/type-converters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ export const converters = new Map([
134134
],
135135
[
136136
"~lib/typedarray/Float32Array",
137-
{ ascToJs: getArrayBuffer, jsToAsc: putArrayBufferView }
137+
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView }
138138
],
139139
[
140140
"~lib/typedarray/Float64Array",
141-
{ ascToJs: getArrayBuffer, jsToAsc: putArrayBufferView }
141+
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView }
142142
],
143143
[
144144
"~lib/arraybuffer/ArrayBuffer",

test/tests/arraybufferview/asc.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ export function swapAndPad(a: Uint8Array, b: Uint8Array): Uint8Array {
77
return result;
88
}
99

10+
export function testFloat32Array(): void {
11+
const data = new Float32Array(1);
12+
data[0] = <f32>0.5;
13+
testF32Arr(data);
14+
}
15+
1016
declare function swappedConcat(a: Uint8Array, b: Uint8Array): Uint8Array;
17+
declare function testF32Arr(data: Float32Array): void;

test/tests/arraybufferview/test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
describe("as-bind", function() {
2-
it("should handle Uint8Arrays", async function() {
2+
it("should handle Uint8Arrays and Float32Array", async function() {
33
const instance = await AsBind.instantiate(this.rawModule, {
44
asc: {
55
swappedConcat(a, b) {
66
const result = new Uint8Array(a.length + b.length);
77
result.set(b, 0);
88
result.set(a, b.length);
99
return result;
10+
},
11+
testF32Arr(data) {
12+
assert(data instanceof Float32Array);
13+
assert(data[0] == 0.5);
1014
}
1115
}
1216
});

test/tests/namespace/asc.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace console {
2+
export declare function log(str: string): void;
3+
}
4+
5+
export function fn(): void {
6+
console.log("ok");
7+
}

test/tests/namespace/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
describe("as-bind", function() {
2+
it("should support exported declare function in namespace", async function() {
3+
const instance = await AsBind.instantiate(this.rawModule, {
4+
asc: {
5+
"console.log"(str) {
6+
assert(str === "ok");
7+
}
8+
}
9+
});
10+
instance.exports.fn();
11+
});
12+
});

transform.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as asc from "visitor-as/as";
2-
const { CommonFlags, NodeKind } = asc;
2+
const { CommonFlags, NodeKind, ElementKind } = asc;
33

44
function isInternalElement(element) {
55
return element.internalName.startsWith("~");
@@ -62,15 +62,13 @@ const SECTION_NAME = "as-bind_bindings";
6262

6363
export default class AsBindTransform {
6464
afterCompile(module) {
65-
const flatExportedFunctions = [
66-
...this.program.elementsByDeclaration.values()
67-
]
65+
/** @type {asc.Program} */
66+
const program = this.program;
67+
const flatExportedFunctions = [...program.elementsByDeclaration.values()]
6868
.filter(el => elementHasFlag(el, CommonFlags.MODULE_EXPORT))
6969
.filter(el => !isInternalElement(el))
7070
.filter(el => el.declaration.kind === NodeKind.FUNCTIONDECLARATION);
71-
const flatImportedFunctions = [
72-
...this.program.elementsByDeclaration.values()
73-
]
71+
const flatImportedFunctions = [...program.elementsByDeclaration.values()]
7472
.filter(el => elementHasFlag(el, CommonFlags.DECLARE))
7573
.filter(el => !isInternalElement(el))
7674
.filter(v => v.declaration.kind === NodeKind.FUNCTIONDECLARATION);
@@ -101,8 +99,16 @@ export default class AsBindTransform {
10199
if (!importedFunctions.hasOwnProperty(moduleName)) {
102100
importedFunctions[moduleName] = {};
103101
}
102+
let importedFunctionName = importedFunction.name;
103+
if (
104+
importedFunction.parent &&
105+
importedFunction.parent.kind === ElementKind.NAMESPACE
106+
) {
107+
importedFunctionName =
108+
importedFunction.parent.name + "." + importedFunction.name;
109+
}
104110
importedFunctions[moduleName][
105-
importedFunction.name
111+
importedFunctionName
106112
] = getFunctionTypeDescriptor(importedFunction);
107113
Object.assign(typeIds, extractTypeIdsFromFunction(importedFunction));
108114
}

0 commit comments

Comments
 (0)