@bytecodealliance/jco 1.16.0 → 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 +1969 -1664
- package/obj/wasm-tools.core.wasm +0 -0
- package/obj/wasm-tools.js +2072 -1687
- 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/src/cmd/types.d.ts
CHANGED
|
@@ -16,18 +16,21 @@ export function guestTypes(witPath: any, opts: any): Promise<void>;
|
|
|
16
16
|
* }} opts
|
|
17
17
|
* @returns {Promise<{ [filename: string]: Uint8Array }>}
|
|
18
18
|
*/
|
|
19
|
-
export function typesComponent(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
19
|
+
export function typesComponent(
|
|
20
|
+
witPath: string,
|
|
21
|
+
opts: {
|
|
22
|
+
name?: string;
|
|
23
|
+
worldName?: string;
|
|
24
|
+
instantiation?: "async" | "sync";
|
|
25
|
+
tlaCompat?: boolean;
|
|
26
|
+
asyncMode?: string;
|
|
27
|
+
asyncImports?: string[];
|
|
28
|
+
asyncExports?: string[];
|
|
29
|
+
outDir?: string;
|
|
30
|
+
features?: string[] | "all";
|
|
31
|
+
guest?: boolean;
|
|
32
|
+
},
|
|
33
|
+
): Promise<{
|
|
31
34
|
[filename: string]: Uint8Array;
|
|
32
35
|
}>;
|
|
33
36
|
//# sourceMappingURL=types.d.ts.map
|
package/src/cmd/types.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { stat, mkdir } from
|
|
2
|
-
import { extname, basename, resolve } from
|
|
1
|
+
import { stat, mkdir } from "node:fs/promises";
|
|
2
|
+
import { extname, basename, resolve } from "node:path";
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
$init,
|
|
6
|
-
generateTypes,
|
|
7
|
-
} from '../../obj/js-component-bindgen-component.js';
|
|
4
|
+
import { $init, generateTypes } from "../../obj/js-component-bindgen-component.js";
|
|
8
5
|
|
|
9
6
|
import {
|
|
10
7
|
isWindows,
|
|
@@ -14,13 +11,13 @@ import {
|
|
|
14
11
|
ASYNC_WASI_IMPORTS,
|
|
15
12
|
ASYNC_WASI_EXPORTS,
|
|
16
13
|
DEFAULT_ASYNC_MODE,
|
|
17
|
-
} from
|
|
14
|
+
} from "../common.js";
|
|
18
15
|
|
|
19
16
|
/** Default relative path for guest type declaration generation */
|
|
20
|
-
const DEFAULT_GUEST_TYPES_OUTPUT_DIR_PATH =
|
|
17
|
+
const DEFAULT_GUEST_TYPES_OUTPUT_DIR_PATH = "./types/generated/wit/guest";
|
|
21
18
|
|
|
22
19
|
/** Default relative path for host type declaration generation */
|
|
23
|
-
const DEFAULT_HOST_TYPES_OUTPUT_DIR_PATH =
|
|
20
|
+
const DEFAULT_HOST_TYPES_OUTPUT_DIR_PATH = "./types/generated/wit/host";
|
|
24
21
|
|
|
25
22
|
export async function types(witPath, opts) {
|
|
26
23
|
witPath = await resolveDefaultWITPath(witPath);
|
|
@@ -30,13 +27,13 @@ export async function types(witPath, opts) {
|
|
|
30
27
|
await mkdir(DEFAULT_HOST_TYPES_OUTPUT_DIR_PATH, { recursive: true });
|
|
31
28
|
opts.outDir = resolve(DEFAULT_HOST_TYPES_OUTPUT_DIR_PATH);
|
|
32
29
|
console.error(
|
|
33
|
-
`no output directory specified for host type declarations, using [${DEFAULT_HOST_TYPES_OUTPUT_DIR_PATH}]
|
|
30
|
+
`no output directory specified for host type declarations, using [${DEFAULT_HOST_TYPES_OUTPUT_DIR_PATH}]`,
|
|
34
31
|
);
|
|
35
32
|
}
|
|
36
33
|
|
|
37
34
|
const files = await typesComponent(witPath, opts);
|
|
38
35
|
|
|
39
|
-
await writeFiles(files, opts.quiet ? false :
|
|
36
|
+
await writeFiles(files, opts.quiet ? false : "Generated Type Files");
|
|
40
37
|
}
|
|
41
38
|
|
|
42
39
|
export async function guestTypes(witPath, opts) {
|
|
@@ -47,17 +44,12 @@ export async function guestTypes(witPath, opts) {
|
|
|
47
44
|
await mkdir(DEFAULT_GUEST_TYPES_OUTPUT_DIR_PATH, { recursive: true });
|
|
48
45
|
opts.outDir = resolve(DEFAULT_GUEST_TYPES_OUTPUT_DIR_PATH);
|
|
49
46
|
console.error(
|
|
50
|
-
`no output directory specified for guest type declarations, using [${DEFAULT_GUEST_TYPES_OUTPUT_DIR_PATH}]
|
|
47
|
+
`no output directory specified for guest type declarations, using [${DEFAULT_GUEST_TYPES_OUTPUT_DIR_PATH}]`,
|
|
51
48
|
);
|
|
52
49
|
}
|
|
53
50
|
|
|
54
51
|
const files = await typesComponent(witPath, { ...opts, guest: true });
|
|
55
|
-
await writeFiles(
|
|
56
|
-
files,
|
|
57
|
-
opts.quiet
|
|
58
|
-
? false
|
|
59
|
-
: 'Generated Guest Typescript Definition Files (.d.ts)'
|
|
60
|
-
);
|
|
52
|
+
await writeFiles(files, opts.quiet ? false : "Generated Guest Typescript Definition Files (.d.ts)");
|
|
61
53
|
}
|
|
62
54
|
|
|
63
55
|
/**
|
|
@@ -87,25 +79,25 @@ export async function typesComponent(witPath, opts) {
|
|
|
87
79
|
const name =
|
|
88
80
|
opts.name ||
|
|
89
81
|
(opts.worldName
|
|
90
|
-
? opts.worldName.split(
|
|
82
|
+
? opts.worldName.split(":").pop().split("/").pop()
|
|
91
83
|
: basename(witPath.slice(0, -extname(witPath).length || Infinity)));
|
|
92
84
|
let instantiation;
|
|
93
85
|
if (opts.instantiation) {
|
|
94
86
|
instantiation = { tag: opts.instantiation };
|
|
95
87
|
}
|
|
96
|
-
let outDir = (opts.outDir ??
|
|
97
|
-
if (!outDir.endsWith(
|
|
98
|
-
outDir +=
|
|
88
|
+
let outDir = (opts.outDir ?? "").replace(/\\/g, "/");
|
|
89
|
+
if (!outDir.endsWith("/") && outDir !== "") {
|
|
90
|
+
outDir += "/";
|
|
99
91
|
}
|
|
100
92
|
|
|
101
93
|
// Bulid list of enabled features
|
|
102
94
|
let features = null;
|
|
103
95
|
if (opts.allFeatures) {
|
|
104
|
-
features = { tag:
|
|
96
|
+
features = { tag: "all" };
|
|
105
97
|
} else if (Array.isArray(opts.feature)) {
|
|
106
|
-
features = { tag:
|
|
98
|
+
features = { tag: "list", val: opts.feature };
|
|
107
99
|
} else if (Array.isArray(opts.features)) {
|
|
108
|
-
features = { tag:
|
|
100
|
+
features = { tag: "list", val: opts.features };
|
|
109
101
|
}
|
|
110
102
|
|
|
111
103
|
// Build list of async imports/exports
|
|
@@ -123,15 +115,15 @@ export async function typesComponent(witPath, opts) {
|
|
|
123
115
|
// be used to generate a guest that is never transpiled).
|
|
124
116
|
let asyncMode = opts.asyncMode ?? DEFAULT_ASYNC_MODE;
|
|
125
117
|
let asyncModeObj;
|
|
126
|
-
if (asyncMode ===
|
|
118
|
+
if (asyncMode === "jspi" || asyncExports.size > 0) {
|
|
127
119
|
asyncModeObj = {
|
|
128
|
-
tag:
|
|
120
|
+
tag: "jspi",
|
|
129
121
|
val: {
|
|
130
122
|
imports: [...asyncImports],
|
|
131
123
|
exports: [...asyncExports],
|
|
132
124
|
},
|
|
133
125
|
};
|
|
134
|
-
} else if (asyncMode ===
|
|
126
|
+
} else if (asyncMode === "sync") {
|
|
135
127
|
asyncModeObj = null;
|
|
136
128
|
} else {
|
|
137
129
|
throw new Error(`invalid/unrecognized async mode [${asyncMode}]`);
|
|
@@ -142,7 +134,7 @@ export async function typesComponent(witPath, opts) {
|
|
|
142
134
|
const absWitPath = resolve(witPath);
|
|
143
135
|
try {
|
|
144
136
|
types = generateTypes(name, {
|
|
145
|
-
wit: { tag:
|
|
137
|
+
wit: { tag: "path", val: (isWindows ? "//?/" : "") + absWitPath },
|
|
146
138
|
instantiation,
|
|
147
139
|
tlaCompat: opts.tlaCompat ?? false,
|
|
148
140
|
world: opts.worldName,
|
|
@@ -151,7 +143,7 @@ export async function typesComponent(witPath, opts) {
|
|
|
151
143
|
asyncMode: asyncModeObj,
|
|
152
144
|
}).map(([name, file]) => [`${outDir}${name}`, file]);
|
|
153
145
|
} catch (err) {
|
|
154
|
-
if (err.toString().includes(
|
|
146
|
+
if (err.toString().includes("does not match previous package name")) {
|
|
155
147
|
const hint = await printWITLayoutHint(absWitPath);
|
|
156
148
|
if (err.message) {
|
|
157
149
|
err.message += `\n${hint}`;
|
|
@@ -170,14 +162,14 @@ export async function typesComponent(witPath, opts) {
|
|
|
170
162
|
* @param {(string, any) => void} consoleFn
|
|
171
163
|
*/
|
|
172
164
|
async function printWITLayoutHint(witPath) {
|
|
173
|
-
const warningPrefix = styleText([
|
|
165
|
+
const warningPrefix = styleText(["yellow", "bold"], "warning");
|
|
174
166
|
const pathMeta = await stat(witPath);
|
|
175
|
-
let output =
|
|
167
|
+
let output = "\n";
|
|
176
168
|
if (!pathMeta.isFile() && !pathMeta.isDirectory()) {
|
|
177
169
|
output += `${warningPrefix} The supplited WIT path [${witPath}] is neither a file or directory.\n`;
|
|
178
170
|
return output;
|
|
179
171
|
}
|
|
180
|
-
const ftype = pathMeta.isDirectory() ?
|
|
172
|
+
const ftype = pathMeta.isDirectory() ? "directory" : "file";
|
|
181
173
|
output += `${warningPrefix} Your WIT ${ftype} [${witPath}] may be laid out incorrectly\n`;
|
|
182
174
|
output += `${warningPrefix} Keep in mind the following rules:\n`;
|
|
183
175
|
output += `${warningPrefix} - Top level WIT files are in the same package (i.e. "ns:pkg" in "wit/*.wit")\n`;
|
|
@@ -186,7 +178,6 @@ async function printWITLayoutHint(witPath) {
|
|
|
186
178
|
}
|
|
187
179
|
|
|
188
180
|
// see: https://github.com/vitest-dev/vitest/issues/6953#issuecomment-2505310022
|
|
189
|
-
if (typeof __vite_ssr_import_meta__ !==
|
|
190
|
-
__vite_ssr_import_meta__.resolve = (path) =>
|
|
191
|
-
'file://' + globalCreateRequire(import.meta.url).resolve(path);
|
|
181
|
+
if (typeof __vite_ssr_import_meta__ !== "undefined") {
|
|
182
|
+
__vite_ssr_import_meta__.resolve = (path) => "file://" + globalCreateRequire(import.meta.url).resolve(path);
|
|
192
183
|
}
|
package/src/cmd/wasm-tools.d.ts
CHANGED
|
@@ -5,4 +5,4 @@ export function componentNew(file: any, opts: any): Promise<void>;
|
|
|
5
5
|
export function componentEmbed(file: any, opts: any): Promise<void>;
|
|
6
6
|
export function metadataAdd(file: any, opts: any): Promise<void>;
|
|
7
7
|
export function metadataShow(file: any, opts: any): Promise<void>;
|
|
8
|
-
//# sourceMappingURL=wasm-tools.d.ts.map
|
|
8
|
+
//# sourceMappingURL=wasm-tools.d.ts.map
|
package/src/cmd/wasm-tools.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { resolve, basename, extname } from
|
|
2
|
-
import { writeFile } from
|
|
1
|
+
import { resolve, basename, extname } from "node:path";
|
|
2
|
+
import { writeFile } from "node:fs/promises";
|
|
3
3
|
|
|
4
|
-
import { readFile, isWindows, styleText } from
|
|
5
|
-
import { $init, tools } from
|
|
4
|
+
import { readFile, isWindows, styleText } from "../common.js";
|
|
5
|
+
import { $init, tools } from "../../obj/wasm-tools.js";
|
|
6
6
|
const {
|
|
7
7
|
print: printFn,
|
|
8
8
|
parse: parseFn,
|
|
@@ -47,34 +47,20 @@ export async function componentNew(file, opts) {
|
|
|
47
47
|
const source = file ? await readFile(file) : null;
|
|
48
48
|
let adapters = [];
|
|
49
49
|
if (opts.wasiReactor && opts.wasiCommand) {
|
|
50
|
-
throw new Error(
|
|
50
|
+
throw new Error("Must select one of --wasi-command or --wasi-reactor");
|
|
51
51
|
}
|
|
52
52
|
if (opts.wasiReactor) {
|
|
53
53
|
adapters = [
|
|
54
54
|
[
|
|
55
|
-
|
|
56
|
-
(
|
|
57
|
-
await readFile(
|
|
58
|
-
new URL(
|
|
59
|
-
'../../lib/wasi_snapshot_preview1.reactor.wasm',
|
|
60
|
-
import.meta.url
|
|
61
|
-
)
|
|
62
|
-
)
|
|
63
|
-
).buffer,
|
|
55
|
+
"wasi_snapshot_preview1",
|
|
56
|
+
(await readFile(new URL("../../lib/wasi_snapshot_preview1.reactor.wasm", import.meta.url))).buffer,
|
|
64
57
|
],
|
|
65
58
|
];
|
|
66
59
|
} else if (opts.wasiCommand) {
|
|
67
60
|
adapters = [
|
|
68
61
|
[
|
|
69
|
-
|
|
70
|
-
(
|
|
71
|
-
await readFile(
|
|
72
|
-
new URL(
|
|
73
|
-
'../../lib/wasi_snapshot_preview1.command.wasm',
|
|
74
|
-
import.meta.url
|
|
75
|
-
)
|
|
76
|
-
)
|
|
77
|
-
).buffer,
|
|
62
|
+
"wasi_snapshot_preview1",
|
|
63
|
+
(await readFile(new URL("../../lib/wasi_snapshot_preview1.command.wasm", import.meta.url))).buffer,
|
|
78
64
|
],
|
|
79
65
|
];
|
|
80
66
|
}
|
|
@@ -83,18 +69,15 @@ export async function componentNew(file, opts) {
|
|
|
83
69
|
await Promise.all(
|
|
84
70
|
opts.adapt.map(async (adapt) => {
|
|
85
71
|
let adapter;
|
|
86
|
-
if (adapt.includes(
|
|
87
|
-
adapter = adapt.split(
|
|
72
|
+
if (adapt.includes("=")) {
|
|
73
|
+
adapter = adapt.split("=");
|
|
88
74
|
} else {
|
|
89
|
-
adapter = [
|
|
90
|
-
basename(adapt).slice(0, -extname(adapt).length),
|
|
91
|
-
adapt,
|
|
92
|
-
];
|
|
75
|
+
adapter = [basename(adapt).slice(0, -extname(adapt).length), adapt];
|
|
93
76
|
}
|
|
94
77
|
adapter[1] = await readFile(adapter[1]);
|
|
95
78
|
return adapter;
|
|
96
|
-
})
|
|
97
|
-
)
|
|
79
|
+
}),
|
|
80
|
+
),
|
|
98
81
|
);
|
|
99
82
|
}
|
|
100
83
|
const output = componentNewFn(source, adapters);
|
|
@@ -105,14 +88,14 @@ export async function componentEmbed(file, opts) {
|
|
|
105
88
|
await $init;
|
|
106
89
|
if (opts.metadata) {
|
|
107
90
|
opts.metadata = opts.metadata.map((meta) => {
|
|
108
|
-
const [field, data =
|
|
109
|
-
const [name, version =
|
|
91
|
+
const [field, data = ""] = meta.split("=");
|
|
92
|
+
const [name, version = ""] = data.split("@");
|
|
110
93
|
return [field, [[name, version]]];
|
|
111
94
|
});
|
|
112
95
|
}
|
|
113
96
|
const source = file ? await readFile(file) : null;
|
|
114
97
|
opts.binary = source;
|
|
115
|
-
opts.witPath = (isWindows ?
|
|
98
|
+
opts.witPath = (isWindows ? "//?/" : "") + resolve(opts.wit);
|
|
116
99
|
const output = componentEmbedFn(opts);
|
|
117
100
|
await writeFile(opts.output, output);
|
|
118
101
|
}
|
|
@@ -120,8 +103,8 @@ export async function componentEmbed(file, opts) {
|
|
|
120
103
|
export async function metadataAdd(file, opts) {
|
|
121
104
|
await $init;
|
|
122
105
|
const metadata = opts.metadata.map((meta) => {
|
|
123
|
-
const [field, data =
|
|
124
|
-
const [name, version =
|
|
106
|
+
const [field, data = ""] = meta.split("=");
|
|
107
|
+
const [name, version = ""] = data.split("@");
|
|
125
108
|
return [field, [[name, version]]];
|
|
126
109
|
});
|
|
127
110
|
const source = await readFile(file);
|
|
@@ -132,32 +115,32 @@ export async function metadataAdd(file, opts) {
|
|
|
132
115
|
export async function metadataShow(file, opts) {
|
|
133
116
|
await $init;
|
|
134
117
|
const source = await readFile(file);
|
|
135
|
-
let output =
|
|
118
|
+
let output = "",
|
|
136
119
|
stack = [1];
|
|
137
120
|
const meta = metadataShowFn(source);
|
|
138
121
|
if (opts.json) {
|
|
139
122
|
console.log(JSON.stringify(meta, null, 2));
|
|
140
123
|
} else {
|
|
141
124
|
for (const { name, metaType, producers } of meta) {
|
|
142
|
-
output +=
|
|
143
|
-
const indent =
|
|
144
|
-
if (metaType.tag ===
|
|
145
|
-
output += `${styleText(
|
|
125
|
+
output += " ".repeat(stack.length - 1);
|
|
126
|
+
const indent = " ".repeat(stack.length);
|
|
127
|
+
if (metaType.tag === "component") {
|
|
128
|
+
output += `${styleText("bold", `[component${name ? " " + name : ""}]`)}\n`;
|
|
146
129
|
if (metaType.val > 0) {
|
|
147
130
|
stack.push(metaType.val);
|
|
148
131
|
}
|
|
149
132
|
} else {
|
|
150
|
-
output += `${styleText(
|
|
133
|
+
output += `${styleText("bold", `[module${name ? " " + name : ""}]`)}\n`;
|
|
151
134
|
}
|
|
152
135
|
if (producers.length === 0) {
|
|
153
136
|
output += `${indent}(no metadata)\n`;
|
|
154
137
|
}
|
|
155
138
|
for (const [field, items] of producers) {
|
|
156
139
|
for (const [name, version] of items) {
|
|
157
|
-
output += `${indent}${(field +
|
|
140
|
+
output += `${indent}${(field + ":").padEnd(13, " ")} ${name}${version ? `${styleText("cyan", version)}` : ""}\n`;
|
|
158
141
|
}
|
|
159
142
|
}
|
|
160
|
-
output +=
|
|
143
|
+
output += "\n";
|
|
161
144
|
if (stack[stack.length - 1] === 0) {
|
|
162
145
|
stack.pop();
|
|
163
146
|
}
|
package/src/common.js
CHANGED
|
@@ -1,40 +1,30 @@
|
|
|
1
|
-
import { normalize, resolve, sep, dirname } from
|
|
2
|
-
import { tmpdir } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from 'node:fs/promises';
|
|
11
|
-
import { spawn } from 'node:child_process';
|
|
12
|
-
import { argv0 } from 'node:process';
|
|
13
|
-
import { platform } from 'node:process';
|
|
14
|
-
import * as nodeUtils from 'node:util';
|
|
15
|
-
|
|
16
|
-
export const isWindows = platform === 'win32';
|
|
1
|
+
import { normalize, resolve, sep, dirname } from "node:path";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { readFile, writeFile, rm, mkdtemp, mkdir, stat } from "node:fs/promises";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { argv0 } from "node:process";
|
|
6
|
+
import { platform } from "node:process";
|
|
7
|
+
import * as nodeUtils from "node:util";
|
|
8
|
+
|
|
9
|
+
export const isWindows = platform === "win32";
|
|
17
10
|
|
|
18
11
|
export const ASYNC_WASI_IMPORTS = [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
"wasi:io/poll#poll",
|
|
13
|
+
"wasi:io/poll#[method]pollable.block",
|
|
14
|
+
"wasi:io/streams#[method]input-stream.blocking-read",
|
|
15
|
+
"wasi:io/streams#[method]input-stream.blocking-skip",
|
|
16
|
+
"wasi:io/streams#[method]output-stream.blocking-flush",
|
|
17
|
+
"wasi:io/streams#[method]output-stream.blocking-write-and-flush",
|
|
18
|
+
"wasi:io/streams#[method]output-stream.blocking-write-zeroes-and-flush",
|
|
19
|
+
"wasi:io/streams#[method]output-stream.blocking-splice",
|
|
27
20
|
];
|
|
28
21
|
|
|
29
|
-
export const ASYNC_WASI_EXPORTS = [
|
|
30
|
-
'wasi:cli/run#run',
|
|
31
|
-
'wasi:http/incoming-handler#handle',
|
|
32
|
-
];
|
|
22
|
+
export const ASYNC_WASI_EXPORTS = ["wasi:cli/run#run", "wasi:http/incoming-handler#handle"];
|
|
33
23
|
|
|
34
|
-
export const DEFAULT_ASYNC_MODE =
|
|
24
|
+
export const DEFAULT_ASYNC_MODE = "sync";
|
|
35
25
|
|
|
36
26
|
/** Path of WIT files by default when one is not specified */
|
|
37
|
-
export const DEFAULT_WIT_PATH =
|
|
27
|
+
export const DEFAULT_WIT_PATH = "./wit";
|
|
38
28
|
|
|
39
29
|
let _showSpinner = false;
|
|
40
30
|
export function setShowSpinner(val) {
|
|
@@ -58,7 +48,7 @@ export function sizeStr(num) {
|
|
|
58
48
|
}
|
|
59
49
|
|
|
60
50
|
export function fixedDigitDisplay(num, maxChars) {
|
|
61
|
-
const significantDigits = String(num).split(
|
|
51
|
+
const significantDigits = String(num).split(".")[0].length;
|
|
62
52
|
let str;
|
|
63
53
|
if (significantDigits >= maxChars - 1) {
|
|
64
54
|
str = String(Math.round(num));
|
|
@@ -70,7 +60,7 @@ export function fixedDigitDisplay(num, maxChars) {
|
|
|
70
60
|
if (maxChars - str.length < 0) {
|
|
71
61
|
return str;
|
|
72
62
|
}
|
|
73
|
-
return
|
|
63
|
+
return " ".repeat(maxChars - str.length) + str;
|
|
74
64
|
}
|
|
75
65
|
|
|
76
66
|
/**
|
|
@@ -82,22 +72,22 @@ export function fixedDigitDisplay(num, maxChars) {
|
|
|
82
72
|
*/
|
|
83
73
|
export function table(rows, align = []) {
|
|
84
74
|
if (rows.length === 0) {
|
|
85
|
-
return
|
|
75
|
+
return "";
|
|
86
76
|
}
|
|
87
77
|
const colLens = rows.reduce(
|
|
88
78
|
(maxLens, cur) => maxLens.map((len, i) => Math.max(len, cur[i].length)),
|
|
89
|
-
rows[0].map((cell) => cell.length)
|
|
79
|
+
rows[0].map((cell) => cell.length),
|
|
90
80
|
);
|
|
91
|
-
let outTable =
|
|
81
|
+
let outTable = "";
|
|
92
82
|
for (const row of rows) {
|
|
93
83
|
for (const [i, cell] of row.entries()) {
|
|
94
|
-
if (align[i] ===
|
|
95
|
-
outTable +=
|
|
84
|
+
if (align[i] === "right") {
|
|
85
|
+
outTable += " ".repeat(colLens[i] - cell.length) + cell;
|
|
96
86
|
} else {
|
|
97
|
-
outTable += cell +
|
|
87
|
+
outTable += cell + " ".repeat(colLens[i] - cell.length);
|
|
98
88
|
}
|
|
99
89
|
}
|
|
100
|
-
outTable +=
|
|
90
|
+
outTable += "\n";
|
|
101
91
|
}
|
|
102
92
|
return outTable;
|
|
103
93
|
}
|
|
@@ -124,7 +114,7 @@ async function readFileCli(filePath, encoding) {
|
|
|
124
114
|
try {
|
|
125
115
|
return await readFile(filePath, encoding);
|
|
126
116
|
} catch {
|
|
127
|
-
throw `Unable to read file ${styleText(
|
|
117
|
+
throw `Unable to read file ${styleText("bold", filePath)}`;
|
|
128
118
|
}
|
|
129
119
|
}
|
|
130
120
|
export { readFileCli as readFile };
|
|
@@ -148,22 +138,22 @@ export { readFileCli as readFile };
|
|
|
148
138
|
export async function spawnIOTmp(cmd, inputWasmBytes, args) {
|
|
149
139
|
const tmpDir = await getTmpDir();
|
|
150
140
|
try {
|
|
151
|
-
const inFile = resolve(tmpDir,
|
|
152
|
-
let outFile = resolve(tmpDir,
|
|
141
|
+
const inFile = resolve(tmpDir, "in.wasm");
|
|
142
|
+
let outFile = resolve(tmpDir, "out.wasm");
|
|
153
143
|
|
|
154
144
|
await writeFile(inFile, inputWasmBytes);
|
|
155
145
|
|
|
156
146
|
const cp = spawn(argv0, [cmd, inFile, ...args, outFile], {
|
|
157
|
-
stdio:
|
|
147
|
+
stdio: "pipe",
|
|
158
148
|
});
|
|
159
149
|
|
|
160
|
-
let stderr =
|
|
150
|
+
let stderr = "";
|
|
161
151
|
const p = new Promise((resolve, reject) => {
|
|
162
|
-
cp.stderr.on(
|
|
163
|
-
cp.on(
|
|
152
|
+
cp.stderr.on("data", (data) => (stderr += data.toString()));
|
|
153
|
+
cp.on("error", (e) => {
|
|
164
154
|
reject(e);
|
|
165
155
|
});
|
|
166
|
-
cp.on(
|
|
156
|
+
cp.on("exit", (code) => {
|
|
167
157
|
if (code === 0) {
|
|
168
158
|
resolve();
|
|
169
159
|
} else {
|
|
@@ -196,18 +186,18 @@ export async function writeFiles(files, summaryTitle) {
|
|
|
196
186
|
Object.entries(files).map(async ([filePath, contents]) => {
|
|
197
187
|
await mkdir(dirname(filePath), { recursive: true });
|
|
198
188
|
await writeFile(filePath, contents);
|
|
199
|
-
})
|
|
189
|
+
}),
|
|
200
190
|
);
|
|
201
191
|
if (!summaryTitle) {
|
|
202
192
|
return;
|
|
203
193
|
}
|
|
204
194
|
|
|
205
195
|
let rows = Object.entries(files).map(([name, source]) => [
|
|
206
|
-
` - ${styleText(
|
|
207
|
-
`${styleText([
|
|
196
|
+
` - ${styleText("italic", name)} `,
|
|
197
|
+
`${styleText(["black", "italic"], sizeStr(source.length))}`,
|
|
208
198
|
]);
|
|
209
199
|
console.log(`
|
|
210
|
-
${styleText(
|
|
200
|
+
${styleText("bold", summaryTitle + ":")}
|
|
211
201
|
|
|
212
202
|
${table(rows)}`);
|
|
213
203
|
}
|
|
@@ -228,14 +218,10 @@ export async function resolveDefaultWITPath(witPath) {
|
|
|
228
218
|
.then((p) => p.isDirectory())
|
|
229
219
|
.catch(() => false);
|
|
230
220
|
if (!witDirExists) {
|
|
231
|
-
throw new Error(
|
|
232
|
-
'Failed to determine WIT directory, please specify WIT directory argument'
|
|
233
|
-
);
|
|
221
|
+
throw new Error("Failed to determine WIT directory, please specify WIT directory argument");
|
|
234
222
|
}
|
|
235
223
|
witPath = resolve(DEFAULT_WIT_PATH);
|
|
236
|
-
console.error(
|
|
237
|
-
`no WIT directory specified, using detected WIT directory @ [${DEFAULT_WIT_PATH}]`
|
|
238
|
-
);
|
|
224
|
+
console.error(`no WIT directory specified, using detected WIT directory @ [${DEFAULT_WIT_PATH}]`);
|
|
239
225
|
return witPath;
|
|
240
226
|
}
|
|
241
227
|
|