@bytecodealliance/jco 1.16.1 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/obj/js-component-bindgen-component.core.wasm +0 -0
- package/obj/js-component-bindgen-component.js +1983 -1678
- package/obj/wasm-tools.core.wasm +0 -0
- package/obj/wasm-tools.js +2071 -1686
- package/package.json +94 -95
- package/src/api.js +5 -14
- package/src/browser.js +1 -1
- package/src/cmd/componentize.d.ts +1 -1
- package/src/cmd/componentize.js +14 -18
- package/src/cmd/opt.d.ts +10 -7
- package/src/cmd/opt.js +44 -96
- package/src/cmd/run.d.ts +1 -1
- package/src/cmd/run.js +31 -48
- package/src/cmd/transpile.d.ts +44 -39
- package/src/cmd/transpile.js +253 -316
- package/src/cmd/types.d.ts +15 -12
- package/src/cmd/types.js +27 -36
- package/src/cmd/wasm-tools.d.ts +1 -1
- package/src/cmd/wasm-tools.js +27 -44
- package/src/common.js +43 -57
- package/src/jco.js +200 -347
- package/types/api.d.ts.map +1 -1
- package/types/common.d.ts.map +1 -1
package/package.json
CHANGED
|
@@ -1,100 +1,99 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
"name": "@bytecodealliance/jco",
|
|
3
|
+
"version": "1.17.0",
|
|
4
|
+
"description": "JavaScript tooling for working with WebAssembly Components",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Component",
|
|
7
|
+
"Wasm",
|
|
8
|
+
"WebAssembly"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/bytecodealliance/jco#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/bytecodealliance/jco/issues"
|
|
10
13
|
},
|
|
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
|
-
|
|
14
|
+
"license": "(Apache-2.0 WITH LLVM-exception)",
|
|
15
|
+
"author": "Guy Bedford",
|
|
16
|
+
"contributors": [
|
|
17
|
+
{
|
|
18
|
+
"name": "Guy Bedford"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"name": "Victor Adossi",
|
|
22
|
+
"email": "vadossi@cosmonic.com"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/bytecodealliance/jco.git"
|
|
28
|
+
},
|
|
29
|
+
"bin": {
|
|
30
|
+
"jco": "src/jco.js"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"lib",
|
|
34
|
+
"src",
|
|
35
|
+
"types",
|
|
36
|
+
"obj/*.core*.wasm",
|
|
37
|
+
"obj/*.js",
|
|
38
|
+
"obj/*.ts",
|
|
39
|
+
"obj/interfaces"
|
|
40
|
+
],
|
|
41
|
+
"type": "module",
|
|
42
|
+
"main": "src/jco.js",
|
|
43
|
+
"types": "types/jco.d.ts",
|
|
44
|
+
"imports": {
|
|
45
|
+
"#ora": {
|
|
46
|
+
"types": "./src/ora-shim.d.ts",
|
|
47
|
+
"browser": "./src/ora-shim.js",
|
|
48
|
+
"default": "ora"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"exports": {
|
|
52
|
+
".": {
|
|
53
|
+
"types": "./types/api.d.ts",
|
|
54
|
+
"browser": "./src/browser.js",
|
|
55
|
+
"default": "./src/api.js"
|
|
56
|
+
},
|
|
57
|
+
"./component": {
|
|
58
|
+
"types": "./obj/js-component-bindgen-component.d.ts",
|
|
59
|
+
"default": "./src/browser.js"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "cargo xtask build debug",
|
|
64
|
+
"build:release": "cargo xtask build release",
|
|
65
|
+
"build:types:preview2-shim": "npm run build:types:preview2-shim --include-workspace-root",
|
|
66
|
+
"build:test:components": "cargo xtask build-test-components",
|
|
67
|
+
"fmt": "oxfmt",
|
|
68
|
+
"fmt:check": "oxfmt --check",
|
|
69
|
+
"lint": "oxlint",
|
|
70
|
+
"lint:fix": "npm run lint -- --fix",
|
|
71
|
+
"test": "vitest run -c test/vitest.ts",
|
|
72
|
+
"test:lts": "vitest run -c test/vitest.lts.ts",
|
|
73
|
+
"prepack": "cargo xtask build release"
|
|
74
|
+
},
|
|
75
|
+
"dependencies": {
|
|
76
|
+
"@bytecodealliance/componentize-js": "^0.19.3",
|
|
77
|
+
"@bytecodealliance/preview2-shim": "^0.17.3",
|
|
78
|
+
"binaryen": "^123.0.0",
|
|
79
|
+
"commander": "^14",
|
|
80
|
+
"mkdirp": "^3",
|
|
81
|
+
"ora": "^8",
|
|
82
|
+
"terser": "^5"
|
|
44
83
|
},
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
84
|
+
"devDependencies": {
|
|
85
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
86
|
+
"@types/node": "^24.3.0",
|
|
87
|
+
"commitlint": "^19.8.1",
|
|
88
|
+
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
89
|
+
"mime": "^4.0.7",
|
|
90
|
+
"oxfmt": "^0.28.0",
|
|
91
|
+
"puppeteer": "^24.16.2",
|
|
92
|
+
"semver": "^7.7.1",
|
|
93
|
+
"smol-toml": "^1.4.2",
|
|
94
|
+
"typescript": "^5.9.2",
|
|
95
|
+
"vite": "^7.1.5",
|
|
96
|
+
"vitest": "^3.2.4",
|
|
97
|
+
"which": "^2.0.2"
|
|
48
98
|
}
|
|
49
|
-
},
|
|
50
|
-
"bin": {
|
|
51
|
-
"jco": "src/jco.js"
|
|
52
|
-
},
|
|
53
|
-
"files": [
|
|
54
|
-
"lib",
|
|
55
|
-
"src",
|
|
56
|
-
"types",
|
|
57
|
-
"obj/*.core*.wasm",
|
|
58
|
-
"obj/*.js",
|
|
59
|
-
"obj/*.ts",
|
|
60
|
-
"obj/interfaces"
|
|
61
|
-
],
|
|
62
|
-
"scripts": {
|
|
63
|
-
"build": "cargo xtask build debug",
|
|
64
|
-
"build:release": "cargo xtask build release",
|
|
65
|
-
"build:types:preview2-shim": "npm run build:types:preview2-shim --include-workspace-root",
|
|
66
|
-
"build:test:components": "cargo xtask build-test-components",
|
|
67
|
-
"fmt": "npm run lint:fix",
|
|
68
|
-
"lint": "eslint -c ../../eslint.config.mjs --ext .js src test",
|
|
69
|
-
"lint:fix": "npm run lint -- --fix",
|
|
70
|
-
"test": "vitest run -c test/vitest.ts",
|
|
71
|
-
"test:lts": "vitest run -c test/vitest.lts.ts",
|
|
72
|
-
"prepack": "cargo xtask build release"
|
|
73
|
-
},
|
|
74
|
-
"dependencies": {
|
|
75
|
-
"@bytecodealliance/componentize-js": "^0.19.3",
|
|
76
|
-
"@bytecodealliance/preview2-shim": "^0.17.3",
|
|
77
|
-
"binaryen": "^123.0.0",
|
|
78
|
-
"commander": "^14",
|
|
79
|
-
"mkdirp": "^3",
|
|
80
|
-
"ora": "^8",
|
|
81
|
-
"terser": "^5"
|
|
82
|
-
},
|
|
83
|
-
"devDependencies": {
|
|
84
|
-
"@commitlint/config-conventional": "^19.8.1",
|
|
85
|
-
"@types/node": "^24.3.0",
|
|
86
|
-
"@typescript-eslint/eslint-plugin": "^8.39.1",
|
|
87
|
-
"@typescript-eslint/parser": "^8.39.1",
|
|
88
|
-
"commitlint": "^19.8.1",
|
|
89
|
-
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
90
|
-
"eslint": "^9.39.1",
|
|
91
|
-
"mime": "^4.0.7",
|
|
92
|
-
"puppeteer": "^24.16.2",
|
|
93
|
-
"semver": "^7.7.1",
|
|
94
|
-
"smol-toml": "^1.4.2",
|
|
95
|
-
"typescript": "^5.9.2",
|
|
96
|
-
"vite": "^7.1.5",
|
|
97
|
-
"vitest": "^3.2.4",
|
|
98
|
-
"which": "^2.0.2"
|
|
99
|
-
}
|
|
100
99
|
}
|
package/src/api.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
export { optimizeComponent as opt } from
|
|
2
|
-
export {
|
|
3
|
-
|
|
4
|
-
typesComponent as types,
|
|
5
|
-
} from './cmd/transpile.js';
|
|
6
|
-
import { $init, tools } from '../obj/wasm-tools.js';
|
|
1
|
+
export { optimizeComponent as opt } from "./cmd/opt.js";
|
|
2
|
+
export { transpileComponent as transpile, typesComponent as types } from "./cmd/transpile.js";
|
|
3
|
+
import { $init, tools } from "../obj/wasm-tools.js";
|
|
7
4
|
const {
|
|
8
5
|
print: printFn,
|
|
9
6
|
parse: parseFn,
|
|
@@ -73,14 +70,8 @@ export async function metadataShow(binary) {
|
|
|
73
70
|
return metadataShowFn(binary);
|
|
74
71
|
}
|
|
75
72
|
export function preview1AdapterCommandPath() {
|
|
76
|
-
return new URL(
|
|
77
|
-
'../lib/wasi_snapshot_preview1.command.wasm',
|
|
78
|
-
import.meta.url
|
|
79
|
-
);
|
|
73
|
+
return new URL("../lib/wasi_snapshot_preview1.command.wasm", import.meta.url);
|
|
80
74
|
}
|
|
81
75
|
export function preview1AdapterReactorPath() {
|
|
82
|
-
return new URL(
|
|
83
|
-
'../lib/wasi_snapshot_preview1.reactor.wasm',
|
|
84
|
-
import.meta.url
|
|
85
|
-
);
|
|
76
|
+
return new URL("../lib/wasi_snapshot_preview1.reactor.wasm", import.meta.url);
|
|
86
77
|
}
|
package/src/browser.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export function componentize(jsSource: any, opts: any): Promise<void>;
|
|
2
|
-
//# sourceMappingURL=componentize.d.ts.map
|
|
2
|
+
//# sourceMappingURL=componentize.d.ts.map
|
package/src/cmd/componentize.js
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import { stat, readFile, writeFile } from
|
|
2
|
-
import { resolve, basename } from
|
|
3
|
-
import { styleText } from
|
|
1
|
+
import { stat, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { resolve, basename } from "node:path";
|
|
3
|
+
import { styleText } from "../common.js";
|
|
4
4
|
|
|
5
5
|
/** All features that can be enabled/disabled */
|
|
6
|
-
const ALL_FEATURES = [
|
|
6
|
+
const ALL_FEATURES = ["clocks", "http", "random", "stdio", "fetch-event"];
|
|
7
7
|
|
|
8
8
|
/** Features that should be used for --debug mode */
|
|
9
|
-
const DEBUG_FEATURES = [
|
|
9
|
+
const DEBUG_FEATURES = ["stdio"];
|
|
10
10
|
|
|
11
11
|
export async function componentize(jsSource, opts) {
|
|
12
|
-
const { componentize: componentizeFn } = await eval(
|
|
13
|
-
'import("@bytecodealliance/componentize-js")'
|
|
14
|
-
);
|
|
12
|
+
const { componentize: componentizeFn } = await eval('import("@bytecodealliance/componentize-js")');
|
|
15
13
|
|
|
16
14
|
const { disableFeatures, enableFeatures } = calculateFeatureSet(opts);
|
|
17
15
|
|
|
18
|
-
const source = await readFile(jsSource,
|
|
16
|
+
const source = await readFile(jsSource, "utf8");
|
|
19
17
|
|
|
20
18
|
const witPath = resolve(opts.wit);
|
|
21
19
|
const sourceName = basename(jsSource);
|
|
@@ -43,15 +41,13 @@ export async function componentize(jsSource, opts) {
|
|
|
43
41
|
},
|
|
44
42
|
});
|
|
45
43
|
if (result.debug) {
|
|
46
|
-
console.error(
|
|
47
|
-
`${styleText('cyan', 'DEBUG')} Debug output\n${JSON.stringify(debug, null, 2)}\n`
|
|
48
|
-
);
|
|
44
|
+
console.error(`${styleText("cyan", "DEBUG")} Debug output\n${JSON.stringify(debug, null, 2)}\n`);
|
|
49
45
|
}
|
|
50
46
|
|
|
51
47
|
component = result.component;
|
|
52
48
|
} catch (err) {
|
|
53
49
|
// Detect package resolution issues that usually mean a misconfigured "witPath"
|
|
54
|
-
if (err.toString().includes(
|
|
50
|
+
if (err.toString().includes("no known packages")) {
|
|
55
51
|
const isFile = await stat(witPath).then((s) => s.isFile());
|
|
56
52
|
if (isFile) {
|
|
57
53
|
const hint = await printWITPathHint(witPath);
|
|
@@ -65,7 +61,7 @@ export async function componentize(jsSource, opts) {
|
|
|
65
61
|
|
|
66
62
|
await writeFile(opts.out, component);
|
|
67
63
|
|
|
68
|
-
console.log(`${styleText(
|
|
64
|
+
console.log(`${styleText("green", "OK")} Successfully written ${styleText("bold", opts.out)}.`);
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
/**
|
|
@@ -75,9 +71,9 @@ export async function componentize(jsSource, opts) {
|
|
|
75
71
|
* @returns {string} user-visible, highlighted output that can be printed
|
|
76
72
|
*/
|
|
77
73
|
async function printWITPathHint(witPath) {
|
|
78
|
-
const warningPrefix = styleText([
|
|
74
|
+
const warningPrefix = styleText(["yellow", "bold"], "warning");
|
|
79
75
|
const pathMeta = await stat(witPath);
|
|
80
|
-
let output =
|
|
76
|
+
let output = "\n";
|
|
81
77
|
if (!pathMeta.isFile() && !pathMeta.isDirectory()) {
|
|
82
78
|
output += `${warningPrefix} The supplited WIT path [${witPath}] is neither a file or directory.\n`;
|
|
83
79
|
return output;
|
|
@@ -103,14 +99,14 @@ function calculateFeatureSet(opts) {
|
|
|
103
99
|
const enable = opts?.enable ?? [];
|
|
104
100
|
|
|
105
101
|
// Process disabled features
|
|
106
|
-
if (disable.includes(
|
|
102
|
+
if (disable.includes("all")) {
|
|
107
103
|
ALL_FEATURES.forEach((v) => disableFeatures.add(v));
|
|
108
104
|
} else {
|
|
109
105
|
disable.forEach((v) => disableFeatures.add(v));
|
|
110
106
|
}
|
|
111
107
|
|
|
112
108
|
// Process enabled features
|
|
113
|
-
if (enable.includes(
|
|
109
|
+
if (enable.includes("all")) {
|
|
114
110
|
ALL_FEATURES.forEach((v) => disableFeatures.delete(v));
|
|
115
111
|
} else {
|
|
116
112
|
enable.forEach((v) => disableFeatures.delete(v));
|
package/src/cmd/opt.d.ts
CHANGED
|
@@ -5,16 +5,19 @@ export function opt(componentPath: any, opts: any, program: any): Promise<void>;
|
|
|
5
5
|
* @param {{ quiet: boolean, asyncify?: boolean, optArgs?: string[], noVerify?: boolean }} opts?
|
|
6
6
|
* @returns {Promise<{ component: Uint8Array, compressionInfo: { beforeBytes: number, afterBytes: number }[] >}
|
|
7
7
|
*/
|
|
8
|
-
export function optimizeComponent(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
export function optimizeComponent(
|
|
9
|
+
componentBytes: Uint8Array,
|
|
10
|
+
opts: {
|
|
11
|
+
quiet: boolean;
|
|
12
|
+
asyncify?: boolean;
|
|
13
|
+
optArgs?: string[];
|
|
14
|
+
noVerify?: boolean;
|
|
15
|
+
},
|
|
16
|
+
): Promise<{
|
|
14
17
|
component: Uint8Array;
|
|
15
18
|
compressionInfo: {
|
|
16
19
|
beforeBytes: number;
|
|
17
20
|
afterBytes: number;
|
|
18
21
|
}[];
|
|
19
22
|
}>;
|
|
20
|
-
//# sourceMappingURL=opt.d.ts.map
|
|
23
|
+
//# sourceMappingURL=opt.d.ts.map
|
package/src/cmd/opt.js
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
import { writeFile } from
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
2
|
|
|
3
|
-
import { $init, tools } from
|
|
3
|
+
import { $init, tools } from "../../obj/wasm-tools.js";
|
|
4
4
|
const { metadataShow, print } = tools;
|
|
5
|
-
import { fileURLToPath } from
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
sizeStr,
|
|
9
|
-
fixedDigitDisplay,
|
|
10
|
-
table,
|
|
11
|
-
spawnIOTmp,
|
|
12
|
-
setShowSpinner,
|
|
13
|
-
getShowSpinner,
|
|
14
|
-
} from '../common.js';
|
|
15
|
-
import ora from '#ora';
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { readFile, sizeStr, fixedDigitDisplay, table, spawnIOTmp, setShowSpinner, getShowSpinner } from "../common.js";
|
|
7
|
+
import ora from "#ora";
|
|
16
8
|
|
|
17
|
-
import { styleText } from
|
|
9
|
+
import { styleText } from "../common.js";
|
|
18
10
|
|
|
19
11
|
export async function opt(componentPath, opts, program) {
|
|
20
12
|
await $init;
|
|
21
|
-
const varIdx = program.parent.rawArgs.indexOf(
|
|
13
|
+
const varIdx = program.parent.rawArgs.indexOf("--");
|
|
22
14
|
if (varIdx !== -1) {
|
|
23
15
|
opts.optArgs = program.parent.rawArgs.slice(varIdx + 1);
|
|
24
16
|
}
|
|
@@ -36,7 +28,7 @@ export async function opt(componentPath, opts, program) {
|
|
|
36
28
|
totalAfterBytes = 0;
|
|
37
29
|
|
|
38
30
|
if (!opts.quiet) {
|
|
39
|
-
const tableContent =
|
|
31
|
+
const tableContent = table(
|
|
40
32
|
[
|
|
41
33
|
...compressionInfo.map(({ beforeBytes, afterBytes }, i) => {
|
|
42
34
|
totalBeforeBytes += beforeBytes;
|
|
@@ -44,25 +36,25 @@ export async function opt(componentPath, opts, program) {
|
|
|
44
36
|
return [
|
|
45
37
|
` - Core Module ${i + 1}: `,
|
|
46
38
|
sizeStr(beforeBytes),
|
|
47
|
-
|
|
48
|
-
`${styleText(
|
|
39
|
+
" -> ",
|
|
40
|
+
`${styleText("cyan", sizeStr(afterBytes))} `,
|
|
49
41
|
`(${fixedDigitDisplay((afterBytes / beforeBytes) * 100, 2)}%)`,
|
|
50
42
|
];
|
|
51
43
|
}),
|
|
52
|
-
[
|
|
44
|
+
["", "", "", "", ""],
|
|
53
45
|
[
|
|
54
46
|
` = Total: `,
|
|
55
47
|
`${sizeStr(totalBeforeBytes)}`,
|
|
56
48
|
` => `,
|
|
57
|
-
`${styleText(
|
|
49
|
+
`${styleText("cyan", sizeStr(totalAfterBytes))} `,
|
|
58
50
|
`(${fixedDigitDisplay((totalAfterBytes / totalBeforeBytes) * 100, 2)}%)`,
|
|
59
51
|
],
|
|
60
52
|
],
|
|
61
|
-
[, , , ,
|
|
53
|
+
[, , , , "right"],
|
|
62
54
|
);
|
|
63
55
|
|
|
64
56
|
console.log(`
|
|
65
|
-
${styleText(
|
|
57
|
+
${styleText("bold", "Optimized WebAssembly Component Internal Core Modules:")}
|
|
66
58
|
|
|
67
59
|
${tableContent}`);
|
|
68
60
|
}
|
|
@@ -101,18 +93,16 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
101
93
|
// compute previous LEB128 encoding length
|
|
102
94
|
metadata.prevLEBLen = byteLengthLEB128(size);
|
|
103
95
|
});
|
|
104
|
-
const coreModules = componentMetadata.filter(
|
|
105
|
-
({ metaType }) => metaType.tag === 'module'
|
|
106
|
-
);
|
|
96
|
+
const coreModules = componentMetadata.filter(({ metaType }) => metaType.tag === "module");
|
|
107
97
|
|
|
108
98
|
// log number of core Wasm modules to be run with wasm-opt
|
|
109
99
|
let completed = 0;
|
|
110
100
|
const spinnerText = () =>
|
|
111
|
-
`${styleText(
|
|
101
|
+
`${styleText("cyan", `${completed} / ${coreModules.length}`)} Running Binaryen on WebAssembly Component Internal Core Modules \n`;
|
|
112
102
|
if (showSpinner) {
|
|
113
103
|
spinner = ora({
|
|
114
|
-
color:
|
|
115
|
-
spinner:
|
|
104
|
+
color: "cyan",
|
|
105
|
+
spinner: "bouncingBar",
|
|
116
106
|
}).start();
|
|
117
107
|
spinner.text = spinnerText();
|
|
118
108
|
}
|
|
@@ -120,34 +110,25 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
120
110
|
// gather the options for wasm-opt. optionally, adding the asyncify flag
|
|
121
111
|
const args = opts?.optArgs
|
|
122
112
|
? [...opts.optArgs]
|
|
123
|
-
: [
|
|
124
|
-
'-Oz',
|
|
125
|
-
'--low-memory-unused',
|
|
126
|
-
'--enable-bulk-memory',
|
|
127
|
-
'--strip-debug',
|
|
128
|
-
];
|
|
113
|
+
: ["-Oz", "--low-memory-unused", "--enable-bulk-memory", "--strip-debug"];
|
|
129
114
|
if (opts?.asyncify) {
|
|
130
|
-
args.push(
|
|
115
|
+
args.push("--asyncify");
|
|
131
116
|
}
|
|
132
117
|
|
|
133
118
|
// process core Wasm modules with wasm-opt
|
|
134
119
|
await Promise.all(
|
|
135
120
|
coreModules.map(async (metadata) => {
|
|
136
|
-
if (metadata.metaType.tag ===
|
|
121
|
+
if (metadata.metaType.tag === "module") {
|
|
137
122
|
// store the wasm-opt processed module in the metadata
|
|
138
123
|
metadata.optimized = await wasmOpt(
|
|
139
|
-
componentBytes.subarray(
|
|
140
|
-
metadata.range[0],
|
|
141
|
-
metadata.range[1]
|
|
142
|
-
),
|
|
124
|
+
componentBytes.subarray(metadata.range[0], metadata.range[1]),
|
|
143
125
|
args,
|
|
144
|
-
opts
|
|
126
|
+
opts,
|
|
145
127
|
);
|
|
146
128
|
|
|
147
129
|
// compute the size change, including the change to
|
|
148
130
|
// the LEB128 encoding of the size change
|
|
149
|
-
const prevModuleSize =
|
|
150
|
-
metadata.range[1] - metadata.range[0];
|
|
131
|
+
const prevModuleSize = metadata.range[1] - metadata.range[0];
|
|
151
132
|
const newModuleSize = metadata.optimized.byteLength;
|
|
152
133
|
metadata.newLEBLen = byteLengthLEB128(newModuleSize);
|
|
153
134
|
metadata.sizeChange = newModuleSize - prevModuleSize;
|
|
@@ -157,7 +138,7 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
157
138
|
spinner.text = spinnerText();
|
|
158
139
|
}
|
|
159
140
|
}
|
|
160
|
-
})
|
|
141
|
+
}),
|
|
161
142
|
);
|
|
162
143
|
|
|
163
144
|
// organize components in modules into tree parent and children
|
|
@@ -170,18 +151,11 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
170
151
|
nodes.splice(i, 1); // remove from nodes
|
|
171
152
|
i--;
|
|
172
153
|
metadata.children = getChildren(metadata.index);
|
|
173
|
-
metadata.sizeChange = metadata.children.reduce(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
? total + sizeChange + newLEBLen - prevLEBLen
|
|
177
|
-
: total;
|
|
178
|
-
},
|
|
179
|
-
metadata.sizeChange || 0
|
|
180
|
-
);
|
|
154
|
+
metadata.sizeChange = metadata.children.reduce((total, { prevLEBLen, newLEBLen, sizeChange }) => {
|
|
155
|
+
return sizeChange ? total + sizeChange + newLEBLen - prevLEBLen : total;
|
|
156
|
+
}, metadata.sizeChange || 0);
|
|
181
157
|
const prevSize = metadata.range[1] - metadata.range[0];
|
|
182
|
-
metadata.newLEBLen = byteLengthLEB128(
|
|
183
|
-
prevSize + metadata.sizeChange
|
|
184
|
-
);
|
|
158
|
+
metadata.newLEBLen = byteLengthLEB128(prevSize + metadata.sizeChange);
|
|
185
159
|
children.push(metadata);
|
|
186
160
|
}
|
|
187
161
|
}
|
|
@@ -190,31 +164,17 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
190
164
|
const componentTree = getChildren(0);
|
|
191
165
|
|
|
192
166
|
// compute the total size change in the component binary
|
|
193
|
-
const sizeChange = componentTree.reduce(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
},
|
|
197
|
-
0
|
|
198
|
-
);
|
|
167
|
+
const sizeChange = componentTree.reduce((total, { prevLEBLen, newLEBLen, sizeChange }) => {
|
|
168
|
+
return total + (sizeChange || 0) + newLEBLen - prevLEBLen;
|
|
169
|
+
}, 0);
|
|
199
170
|
|
|
200
|
-
let outComponentBytes = new Uint8Array(
|
|
201
|
-
componentBytes.byteLength + sizeChange
|
|
202
|
-
);
|
|
171
|
+
let outComponentBytes = new Uint8Array(componentBytes.byteLength + sizeChange);
|
|
203
172
|
let nextReadPos = 0,
|
|
204
173
|
nextWritePos = 0;
|
|
205
174
|
|
|
206
|
-
const write = ({
|
|
207
|
-
prevLEBLen,
|
|
208
|
-
range,
|
|
209
|
-
optimized,
|
|
210
|
-
children,
|
|
211
|
-
sizeChange,
|
|
212
|
-
}) => {
|
|
175
|
+
const write = ({ prevLEBLen, range, optimized, children, sizeChange }) => {
|
|
213
176
|
// write from the last read to the LEB byte start
|
|
214
|
-
outComponentBytes.set(
|
|
215
|
-
componentBytes.subarray(nextReadPos, range[0] - prevLEBLen),
|
|
216
|
-
nextWritePos
|
|
217
|
-
);
|
|
177
|
+
outComponentBytes.set(componentBytes.subarray(nextReadPos, range[0] - prevLEBLen), nextWritePos);
|
|
218
178
|
nextWritePos += range[0] - prevLEBLen - nextReadPos;
|
|
219
179
|
|
|
220
180
|
// write the new LEB bytes
|
|
@@ -222,8 +182,7 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
222
182
|
do {
|
|
223
183
|
const byte = val & 0x7f;
|
|
224
184
|
val >>>= 7;
|
|
225
|
-
outComponentBytes[nextWritePos++] =
|
|
226
|
-
val === 0 ? byte : byte | 0x80;
|
|
185
|
+
outComponentBytes[nextWritePos++] = val === 0 ? byte : byte | 0x80;
|
|
227
186
|
} while (val !== 0);
|
|
228
187
|
|
|
229
188
|
if (optimized) {
|
|
@@ -237,10 +196,7 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
237
196
|
children.forEach(write);
|
|
238
197
|
} else {
|
|
239
198
|
// write component
|
|
240
|
-
outComponentBytes.set(
|
|
241
|
-
componentBytes.subarray(range[0], range[1]),
|
|
242
|
-
nextWritePos
|
|
243
|
-
);
|
|
199
|
+
outComponentBytes.set(componentBytes.subarray(range[0], range[1]), nextWritePos);
|
|
244
200
|
nextReadPos = range[1];
|
|
245
201
|
nextWritePos += range[1] - range[0];
|
|
246
202
|
}
|
|
@@ -250,19 +206,14 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
250
206
|
componentTree.forEach(write);
|
|
251
207
|
|
|
252
208
|
// write remaining
|
|
253
|
-
outComponentBytes.set(
|
|
254
|
-
componentBytes.subarray(nextReadPos),
|
|
255
|
-
nextWritePos
|
|
256
|
-
);
|
|
209
|
+
outComponentBytes.set(componentBytes.subarray(nextReadPos), nextWritePos);
|
|
257
210
|
|
|
258
211
|
// verify it still parses ok
|
|
259
212
|
if (!opts?.noVerify) {
|
|
260
213
|
try {
|
|
261
214
|
print(outComponentBytes);
|
|
262
215
|
} catch (e) {
|
|
263
|
-
throw new Error(
|
|
264
|
-
`Internal error performing optimization.\n${e.message}`
|
|
265
|
-
);
|
|
216
|
+
throw new Error(`Internal error performing optimization.\n${e.message}`);
|
|
266
217
|
}
|
|
267
218
|
}
|
|
268
219
|
|
|
@@ -286,14 +237,12 @@ export async function optimizeComponent(componentBytes, opts) {
|
|
|
286
237
|
* @returns {Promise<Uint8Array>}
|
|
287
238
|
*/
|
|
288
239
|
async function wasmOpt(source, args, transpileOpts) {
|
|
289
|
-
const wasmOptBin =
|
|
290
|
-
transpileOpts?.wasmOptBin ??
|
|
291
|
-
fileURLToPath(import.meta.resolve('binaryen/bin/wasm-opt'));
|
|
240
|
+
const wasmOptBin = transpileOpts?.wasmOptBin ?? fileURLToPath(import.meta.resolve("binaryen/bin/wasm-opt"));
|
|
292
241
|
|
|
293
242
|
try {
|
|
294
|
-
return await spawnIOTmp(wasmOptBin, source, [...args,
|
|
243
|
+
return await spawnIOTmp(wasmOptBin, source, [...args, "-o"]);
|
|
295
244
|
} catch (e) {
|
|
296
|
-
if (e.toString().includes(
|
|
245
|
+
if (e.toString().includes("BasicBlock requested")) {
|
|
297
246
|
return wasmOpt(source, args);
|
|
298
247
|
}
|
|
299
248
|
throw e;
|
|
@@ -301,7 +250,6 @@ async function wasmOpt(source, args, transpileOpts) {
|
|
|
301
250
|
}
|
|
302
251
|
|
|
303
252
|
// see: https://github.com/vitest-dev/vitest/issues/6953#issuecomment-2505310022
|
|
304
|
-
if (typeof __vite_ssr_import_meta__ !==
|
|
305
|
-
__vite_ssr_import_meta__.resolve = (path) =>
|
|
306
|
-
'file://' + globalCreateRequire(import.meta.url).resolve(path);
|
|
253
|
+
if (typeof __vite_ssr_import_meta__ !== "undefined") {
|
|
254
|
+
__vite_ssr_import_meta__.resolve = (path) => "file://" + globalCreateRequire(import.meta.url).resolve(path);
|
|
307
255
|
}
|
package/src/cmd/run.d.ts
CHANGED