Skip to content

Commit

Permalink
feat: add namespace exported declare function support #91 and fix #94 (
Browse files Browse the repository at this point in the history
…#92)

* feat: add namespace support #91

* fix: #94 add FLoat32Array test case
  • Loading branch information
deepkolos authored Aug 16, 2021
1 parent 158f9d0 commit a1a36ba
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/asbind-instance/type-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ export const converters = new Map([
],
[
"~lib/typedarray/Float32Array",
{ ascToJs: getArrayBuffer, jsToAsc: putArrayBufferView }
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView }
],
[
"~lib/typedarray/Float64Array",
{ ascToJs: getArrayBuffer, jsToAsc: putArrayBufferView }
{ ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView }
],
[
"~lib/arraybuffer/ArrayBuffer",
Expand Down
7 changes: 7 additions & 0 deletions test/tests/arraybufferview/asc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ export function swapAndPad(a: Uint8Array, b: Uint8Array): Uint8Array {
return result;
}

export function testFloat32Array(): void {
const data = new Float32Array(1);
data[0] = <f32>0.5;
testF32Arr(data);
}

declare function swappedConcat(a: Uint8Array, b: Uint8Array): Uint8Array;
declare function testF32Arr(data: Float32Array): void;
6 changes: 5 additions & 1 deletion test/tests/arraybufferview/test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
describe("as-bind", function() {
it("should handle Uint8Arrays", async function() {
it("should handle Uint8Arrays and Float32Array", async function() {
const instance = await AsBind.instantiate(this.rawModule, {
asc: {
swappedConcat(a, b) {
const result = new Uint8Array(a.length + b.length);
result.set(b, 0);
result.set(a, b.length);
return result;
},
testF32Arr(data) {
assert(data instanceof Float32Array);
assert(data[0] == 0.5);
}
}
});
Expand Down
7 changes: 7 additions & 0 deletions test/tests/namespace/asc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace console {
export declare function log(str: string): void;
}

export function fn(): void {
console.log("ok");
}
12 changes: 12 additions & 0 deletions test/tests/namespace/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe("as-bind", function() {
it("should support exported declare function in namespace", async function() {
const instance = await AsBind.instantiate(this.rawModule, {
asc: {
"console.log"(str) {
assert(str === "ok");
}
}
});
instance.exports.fn();
});
});
22 changes: 14 additions & 8 deletions transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as asc from "visitor-as/as";
const { CommonFlags, NodeKind } = asc;
const { CommonFlags, NodeKind, ElementKind } = asc;

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

export default class AsBindTransform {
afterCompile(module) {
const flatExportedFunctions = [
...this.program.elementsByDeclaration.values()
]
/** @type {asc.Program} */
const program = this.program;
const flatExportedFunctions = [...program.elementsByDeclaration.values()]
.filter(el => elementHasFlag(el, CommonFlags.MODULE_EXPORT))
.filter(el => !isInternalElement(el))
.filter(el => el.declaration.kind === NodeKind.FUNCTIONDECLARATION);
const flatImportedFunctions = [
...this.program.elementsByDeclaration.values()
]
const flatImportedFunctions = [...program.elementsByDeclaration.values()]
.filter(el => elementHasFlag(el, CommonFlags.DECLARE))
.filter(el => !isInternalElement(el))
.filter(v => v.declaration.kind === NodeKind.FUNCTIONDECLARATION);
Expand Down Expand Up @@ -101,8 +99,16 @@ export default class AsBindTransform {
if (!importedFunctions.hasOwnProperty(moduleName)) {
importedFunctions[moduleName] = {};
}
let importedFunctionName = importedFunction.name;
if (
importedFunction.parent &&
importedFunction.parent.kind === ElementKind.NAMESPACE
) {
importedFunctionName =
importedFunction.parent.name + "." + importedFunction.name;
}
importedFunctions[moduleName][
importedFunction.name
importedFunctionName
] = getFunctionTypeDescriptor(importedFunction);
Object.assign(typeIds, extractTypeIdsFromFunction(importedFunction));
}
Expand Down

0 comments on commit a1a36ba

Please sign in to comment.