Skip to content

Commit 0e3a585

Browse files
authored
refactor(typescript): refactor to ts (fixes #56) (#100)
* ⚙️refactor (typescript): refactor to ts (fixes #56) * lint stuff * change lint settings * fix * lint again * finish transform.ts
1 parent cd49913 commit 0e3a585

File tree

36 files changed

+445
-292
lines changed

36 files changed

+445
-292
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/tests/namespace-import/asc.ts

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"trailingComma": "none",
3+
"semi": true,
4+
"arrowParens": "avoid",
5+
"printWidth": 80,
6+
"bracketSpacing": true
7+
}

examples/quickstart/browser-puppeteer.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const http = require("http");
1313
// Host a static server of the local directory
1414
// https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/
1515
http
16-
.createServer(function(req, res) {
17-
fs.readFile(__dirname + req.url, function(err, data) {
16+
.createServer(function (req, res) {
17+
fs.readFile(__dirname + req.url, function (err, data) {
1818
if (err) {
1919
res.writeHead(404);
2020
res.end(JSON.stringify(err));
@@ -53,10 +53,7 @@ http
5353
// Listen to JS Console messages, log them, and resolve our promise on an expected message
5454
page.on("console", message => {
5555
console.log(
56-
`${message
57-
.type()
58-
.substr(0, 3)
59-
.toUpperCase()} ${message.text()}`
56+
`${message.type().substr(0, 3).toUpperCase()} ${message.text()}`
6057
);
6158

6259
if (message.text() === "AsBind: Hello World!") {

examples/quickstart/browser.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ const asyncTask = async () => {
1717
const asBindInstance = await AsBind.instantiate(wasmBuffer);
1818

1919
// You can now use your wasm / as-bind instance!
20-
const response = asBindInstance.exports.myExportedFunctionThatTakesAString(
21-
"Hello World!"
22-
);
20+
const response =
21+
asBindInstance.exports.myExportedFunctionThatTakesAString("Hello World!");
2322
console.log(response); // AsBind: Hello World!
2423
};
2524
asyncTask();

examples/quickstart/nodejs.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ const asyncTask = async () => {
1111
const asBindInstance = await AsBind.instantiate(wasm);
1212

1313
// You can now use your wasm / as-bind instance!
14-
const response = asBindInstance.exports.myExportedFunctionThatTakesAString(
15-
"Hello World!"
16-
);
14+
const response =
15+
asBindInstance.exports.myExportedFunctionThatTakesAString("Hello World!");
1716
console.log(response); // AsBind: Hello World!
1817
};
1918
asyncTask();

examples/quickstart/package-lock.json

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const fs = require("fs");
2-
const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/optimized.wasm"));
2+
const compiled = new WebAssembly.Module(
3+
fs.readFileSync(__dirname + "/build/optimized.wasm")
4+
);
35
const imports = {
46
env: {
57
abort: () => {}
@@ -9,8 +11,7 @@ const wasmInstance = new WebAssembly.Instance(compiled, imports);
911
wasmInstance.exports._asbind_add = wasmInstance.exports.add;
1012
wasmInstance.exports.add = (x, y) => {
1113
console.log(wasmInstance.exports._asbind_add(x, y));
12-
}
14+
};
1315

1416
console.log(wasmInstance.exports);
1517
console.log(wasmInstance.exports.add(3, 2));
16-

lib/as-bind.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/asbind-instance/asbind-instance.js renamed to lib/asbind-instance/asbind-instance.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Class for asbind instances
2-
32
import { asbindInstantiate, asbindInstantiateSync } from "./instantiate";
43
import { bindImportFunction, bindExportFunction } from "./bind-function";
5-
import { isReservedExportKey } from "./reserved-export-keys";
4+
import {
5+
TypeDef,
6+
WebAssemblyModuleStreaming,
7+
WebAssemblyModuleSync,
8+
WebAssemblyLoaderResult
9+
} from "../types";
10+
11+
import { ASUtil } from "@assemblyscript/loader";
612

713
const SECTION_NAME = "as-bind_bindings";
814

915
// Basically a deep-copy, but can be limited in levels.
10-
function copyObject(obj, { depth = Number.POSITIVE_INFINITY } = {}) {
16+
function copyObject<T>(obj: T, { depth = Number.POSITIVE_INFINITY } = {}): T {
1117
if (depth <= 0 || !obj || typeof obj !== "object") {
1218
return obj;
1319
}
@@ -16,21 +22,22 @@ function copyObject(obj, { depth = Number.POSITIVE_INFINITY } = {}) {
1622
key,
1723
copyObject(val, { depth: depth - 1 })
1824
])
19-
);
25+
) as T;
2026
}
2127

22-
async function compileStreaming(source) {
28+
async function compileStreaming(source: WebAssemblyModuleStreaming) {
2329
source = await Promise.resolve(source);
2430
if (typeof Response !== "undefined" && source instanceof Response) {
2531
if (WebAssembly.compileStreaming) {
2632
return WebAssembly.compileStreaming(source);
2733
}
2834
source = await source.arrayBuffer();
2935
}
30-
return WebAssembly.compile(source);
36+
37+
return WebAssembly.compile(source as BufferSource);
3138
}
3239

33-
function extractTypeDescriptor(module) {
40+
function extractTypeDescriptor(module: WebAssembly.Module): TypeDef {
3441
const sections = WebAssembly.Module.customSections(module, SECTION_NAME);
3542
const str = new TextDecoder("utf8").decode(new Uint8Array(sections[0]));
3643
try {
@@ -41,20 +48,20 @@ function extractTypeDescriptor(module) {
4148
}
4249

4350
export default class AsbindInstance {
44-
constructor() {
45-
this.unboundExports = {};
46-
this.exports = {};
47-
this.importObject = {};
48-
}
51+
exports: Record<string, never> | ASUtil = {};
52+
importObject: WebAssembly.Imports = {};
53+
typeDescriptor: TypeDef;
54+
module: WebAssembly.Module;
55+
loadedModule: WebAssemblyLoaderResult;
4956

50-
getTypeId(typeName) {
57+
getTypeId(typeName: string) {
5158
if (typeName in this.typeDescriptor.typeIds) {
5259
return this.typeDescriptor.typeIds[typeName].id;
5360
}
5461
throw Error(`Unknown type ${JSON.stringify(typeName)}`);
5562
}
5663

57-
getTypeSize(typeName) {
64+
getTypeSize(typeName: string) {
5865
if (typeName in this.typeDescriptor.typeIds) {
5966
return this.typeDescriptor.typeIds[typeName].byteSize;
6067
}
@@ -78,7 +85,10 @@ export default class AsbindInstance {
7885
}
7986
}
8087

81-
async _instantiate(source, importObject) {
88+
async _instantiate(
89+
source: WebAssemblyModuleStreaming,
90+
importObject: WebAssembly.Imports
91+
) {
8292
this.module = await compileStreaming(source);
8393

8494
this._validate();
@@ -89,7 +99,10 @@ export default class AsbindInstance {
8999
this._instantiateBindUnboundExports();
90100
}
91101

92-
_instantiateSync(source, importObject) {
102+
_instantiateSync(
103+
source: WebAssemblyModuleSync,
104+
importObject: WebAssembly.Imports
105+
) {
93106
this.module = new WebAssembly.Module(source);
94107

95108
this._validate();
@@ -99,7 +112,7 @@ export default class AsbindInstance {
99112
this._instantiateBindUnboundExports();
100113
}
101114

102-
_instantiateBindImportFunctions(importObject) {
115+
_instantiateBindImportFunctions(importObject: WebAssembly.Imports) {
103116
this.importObject = copyObject(importObject, { depth: 2 });
104117

105118
for (const [moduleName, moduleDescriptor] of Object.entries(
@@ -111,13 +124,12 @@ export default class AsbindInstance {
111124
this.importObject[moduleName][
112125
`__asbind_unbound_${importedFunctionName}`
113126
] = importObject[moduleName][importedFunctionName];
114-
this.importObject[moduleName][
115-
importedFunctionName
116-
] = bindImportFunction(
117-
this,
118-
importObject[moduleName][importedFunctionName],
119-
descriptor
120-
);
127+
this.importObject[moduleName][importedFunctionName] =
128+
bindImportFunction(
129+
this,
130+
importObject[moduleName][importedFunctionName] as Function,
131+
descriptor
132+
);
121133
}
122134
}
123135
}

lib/asbind-instance/bind-function.js renamed to lib/asbind-instance/bind-function.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { TypeDefFn } from "../types";
2+
import AsbindInstance from "./asbind-instance";
3+
14
import {
25
getAscToJsConverterForType,
36
getJsToAscConverterForType
@@ -12,9 +15,9 @@ function getFunctionFromKeyPath(baseObject, keys) {
1215
}
1316

1417
export function bindImportFunction(
15-
asbindInstance,
16-
importedFunction,
17-
importedFunctionDescriptor
18+
asbindInstance: AsbindInstance,
19+
importedFunction: Function,
20+
importedFunctionDescriptor: TypeDefFn
1821
) {
1922
// Grab type converter functions according to the type descriptor
2023
const argumentConverterFunctions = importedFunctionDescriptor.parameters.map(
@@ -26,7 +29,7 @@ export function bindImportFunction(
2629

2730
// Create a wrapper function that applies the correct converter function to arguments and
2831
// return value respectively.
29-
return function(...args) {
32+
return function (...args) {
3033
if (args.length != argumentConverterFunctions.length) {
3134
throw Error(
3235
`Expected ${argumentConverterFunctions.length} arguments, got ${args.length}`
@@ -51,9 +54,9 @@ export function bindImportFunction(
5154
// Function that takes in an asbind instance, and the key to the export function on the
5255
// abindInstance.exports object, to be wrapped and then re-assigned to the asbindInstance.exports.
5356
export function bindExportFunction(
54-
asbindInstance,
55-
exportedFunction,
56-
exportedFunctionDescriptor
57+
asbindInstance: AsbindInstance,
58+
exportedFunction: Function,
59+
exportedFunctionDescriptor: TypeDefFn
5760
) {
5861
// Grab type converter functions according to the type descriptor
5962
const argumentConverterFunctions = exportedFunctionDescriptor.parameters.map(

0 commit comments

Comments
 (0)