-
Notifications
You must be signed in to change notification settings - Fork 3
/
linker.ts
119 lines (101 loc) · 4.24 KB
/
linker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import "mocha";
import { assert } from "chai";
import * as wat from "./wat";
import { Linker } from "../src/linker";
describe("linker tests", async () => {
it("correctly define a single method and instantiate", async () => {
let linker = new Linker();
linker.define("calculator", "add", (a: number, b: number) => a + b);
let instance = await linker.instantiate(wat.moduleFromText(wat.usingAdd));
assert.equal(await (instance.exports["add"] as Function)(1, 2), 3);
});
it("correctly define an entire module and instantiate", async () => {
let linker = new Linker();
await linker.module("calculator", wat.moduleFromText(wat.add));
let instance = await linker.instantiate(wat.moduleFromText(wat.usingAdd));
assert.equal(await (instance.exports["add"] as Function)(1, 2), 3);
});
it("ensure the last import is chosen between identical imports", async () => {
let linker = new Linker();
linker.define("calculator", "add", (a: number, b: number) => 42);
await linker.module("calculator", wat.moduleFromText(wat.add));
let instance = await linker.instantiate(wat.moduleFromText(wat.usingAdd));
assert.equal(await (instance.exports["add"] as Function)(1, 2), 3);
});
it("ensure the last import is chosen between identical imports with changed order", async () => {
let linker = new Linker();
await linker.module("calculator", wat.moduleFromText(wat.add));
linker.define("calculator", "add", (a: number, b: number) => 42);
let instance = await linker.instantiate(wat.moduleFromText(wat.usingAdd));
assert.equal(await (instance.exports["add"] as Function)(1, 2), 42);
});
it("use last item when multiple identical items are defined", async () => {
let linker = new Linker();
linker.define("calculator", "add", (a: number, b: number) => 41);
linker.define("calculator", "add", (a: number, b: number) => 42);
await linker.module("calculator", wat.moduleFromText(wat.add));
linker.define("calculator", "add", (a: number, b: number) => 43);
let instance = await linker.instantiate(wat.moduleFromText(wat.usingAdd));
assert.equal(await (instance.exports["add"] as Function)(1, 2), 43);
});
it("define an async import", async () => {
let useAsyncify = true;
let linker = new Linker(useAsyncify);
linker.define("calculator", "add", async () => {
await sleep(1500);
return 42;
});
let instance = await linker.instantiate(
wat.asyncModuleFromText(wat.usingAdd)
);
assert.equal(await (instance.exports["add"] as Function)(1, 2), 42);
});
it("add an existing instance to the linker", async () => {
let linker = new Linker();
let calc = await WebAssembly.instantiate(wat.moduleFromText(wat.add), {});
linker.instance("calculator", calc);
let instance = await linker.instantiate(wat.moduleFromText(wat.usingAdd));
assert.equal(await (instance.exports["add"] as Function)(1, 2), 3);
});
it("alias modules", async () => {
let linker = new Linker();
await linker.module("calculator1", wat.moduleFromText(wat.add));
linker.alias("calculator1", "calculator2");
let instance = await linker.instantiate(
wat.asyncModuleFromText(wat.usingAddWithAlias)
);
assert.equal(await (instance.exports["add"] as Function)(1, 2), 3);
});
it("add import object to the linker", async () => {
let linker = new Linker();
var importObject = {
calculator: {
add: (a: number, b: number) => a + b,
},
};
linker.imports(importObject);
let instance = await linker.instantiate(wat.moduleFromText(wat.usingAdd));
assert.equal(await (instance.exports["add"] as Function)(1, 2), 3);
});
it("add import object with multiple imports to the linker", async () => {
let linker = new Linker();
var importObject = {
calculator1: {
add: (a: number, b: number) => a + b,
},
calculator2: {
add: (a: number, b: number) => a + b,
},
};
linker.imports(importObject);
let instance = await linker.instantiate(
wat.moduleFromText(wat.usingAddWithAlias)
);
assert.equal(await (instance.exports["add"] as Function)(1, 2), 3);
});
});
async function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}