forked from bytecodealliance/jco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
93 lines (82 loc) · 3.46 KB
/
api.js
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
import { deepStrictEqual, ok, strictEqual } from 'node:assert';
import { readFile } from 'node:fs/promises';
import { transpile, opt, print, parse, componentWit, componentNew } from 'js-component-tools';
export async function apiTest (fixtures) {
suite('API', () => {
test('Transpile', async () => {
const name = 'exports_only';
const component = await readFile(`test/fixtures/${name}.component.wasm`);
const { files, imports, exports } = await transpile(component, { name });
strictEqual(imports.length, 0);
strictEqual(exports.length, 1);
deepStrictEqual(exports[0], ['thunk', 'function']);
ok(files[name + '.js']);
});
test('Transpile & Optimize & Minify', async () => {
const name = 'exports_only';
const component = await readFile(`test/fixtures/${name}.component.wasm`);
const { files, imports, exports } = await transpile(component, {
name,
minify: true,
validLiftingOptimization: true,
tlaCompat: true,
optimize: true,
base64Cutoff: 0,
});
strictEqual(imports.length, 0);
strictEqual(exports.length, 1);
deepStrictEqual(exports[0], ['thunk', 'function']);
ok(files[name + '.js'].length < 8000);
});
test('Transpile to JS', async () => {
const name = 'flavorful';
const component = await readFile(`test/fixtures/${name}.component.wasm`);
const { files, imports, exports } = await transpile(component, {
map: {
'testwasi': './wasi.js'
},
name,
validLiftingOptimization: true,
tlaCompat: true,
base64Cutoff: 0,
js: true,
});
strictEqual(imports.length, 2);
strictEqual(exports.length, 2);
deepStrictEqual(exports[0], ['exports', 'instance']);
deepStrictEqual(exports[1], ['testImports', 'function']);
const source = Buffer.from(files[name + '.js']).toString();
ok(source.includes('./wasi.js'));
ok(source.includes('testwasi'));
ok(source.includes('FUNCTION_TABLE'));
for (let i = 0; i < 2; i++)
ok(source.includes(exports[i][0]));
});
test('Optimize', async () => {
const component = await readFile(`test/fixtures/exports_only.component.wasm`);
const { component: optimizedComponent } = await opt(component);
ok(optimizedComponent.byteLength < component.byteLength);
});
test('Print & Parse', async () => {
const component = await readFile(`test/fixtures/exports_only.component.wasm`);
const output = await print(component);
strictEqual(output.slice(0, 10), '(component');
const componentParsed = await parse(output);
ok(componentParsed);
});
test('Wit & New', async () => {
const component = await readFile(`test/fixtures/exports_only.component.wasm`);
const wit = await componentWit(component);
strictEqual(wit.slice(0, 25), 'default world component {');
// TODO: reenable when dummy is supported
// const generatedComponent = await componentNew(null, { wit });
// const output = await print(generatedComponent);
// strictEqual(output.slice(0, 10), '(component');
});
test('Component new adapt', async () => {
const component = await readFile(`test/fixtures/exitcode.wasm`);
const generatedComponent = await componentNew(component, [['wasi_snapshot_preview1', await readFile('test/fixtures/wasi_snapshot_preview1.wasm')]]);
await print(generatedComponent);
});
});
}