@bytecodealliance/jco 1.10.2 → 1.11.1

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.
@@ -1,121 +1,156 @@
1
- import { writeFile } from "node:fs/promises";
1
+ import { writeFile } from 'node:fs/promises';
2
2
  import { readFile, isWindows } from '../common.js';
3
- import { $init, tools } from "../../obj/wasm-tools.js";
4
- const { print: printFn, parse: parseFn, componentWit: componentWitFn, componentNew: componentNewFn, componentEmbed: componentEmbedFn, metadataAdd: metadataAddFn, metadataShow: metadataShowFn } = tools;
3
+ import { $init, tools } from '../../obj/wasm-tools.js';
4
+ const {
5
+ print: printFn,
6
+ parse: parseFn,
7
+ componentWit: componentWitFn,
8
+ componentNew: componentNewFn,
9
+ componentEmbed: componentEmbedFn,
10
+ metadataAdd: metadataAddFn,
11
+ metadataShow: metadataShowFn,
12
+ } = tools;
5
13
  import { resolve, basename, extname } from 'node:path';
6
14
  import c from 'chalk-template';
7
15
 
8
16
  export async function parse(file, opts) {
9
- await $init;
10
- const source = (await readFile(file)).toString();
11
- const output = parseFn(source);
12
- await writeFile(opts.output, output);
17
+ await $init;
18
+ const source = (await readFile(file)).toString();
19
+ const output = parseFn(source);
20
+ await writeFile(opts.output, output);
13
21
  }
14
22
 
15
23
  export async function print(file, opts) {
16
- await $init;
17
- const source = await readFile(file);
18
- const output = printFn(source);
19
- if (opts.output) {
20
- await writeFile(opts.output, output);
21
- } else {
22
- console.log(output);
23
- }
24
+ await $init;
25
+ const source = await readFile(file);
26
+ const output = printFn(source);
27
+ if (opts.output) {
28
+ await writeFile(opts.output, output);
29
+ } else {
30
+ console.log(output);
31
+ }
24
32
  }
25
33
 
26
34
  export async function componentWit(file, opts) {
27
- await $init;
28
- const source = await readFile(file);
29
- const output = componentWitFn(source, opts.document);
30
- if (opts.output) {
31
- await writeFile(opts.output, output);
32
- } else {
33
- console.log(output);
34
- }
35
+ await $init;
36
+ const source = await readFile(file);
37
+ const output = componentWitFn(source, opts.document);
38
+ if (opts.output) {
39
+ await writeFile(opts.output, output);
40
+ } else {
41
+ console.log(output);
42
+ }
35
43
  }
36
44
 
37
45
  export async function componentNew(file, opts) {
38
- await $init;
39
- const source = file ? await readFile(file) : null;
40
- let adapters = [];
41
- if (opts.wasiReactor && opts.wasiCommand)
42
- throw new Error('Must select one of --wasi-command or --wasi-reactor');
43
- if (opts.wasiReactor)
44
- adapters = [['wasi_snapshot_preview1', (await readFile(new URL('../../lib/wasi_snapshot_preview1.reactor.wasm', import.meta.url))).buffer]];
45
- else if (opts.wasiCommand)
46
- adapters = [['wasi_snapshot_preview1', (await readFile(new URL('../../lib/wasi_snapshot_preview1.command.wasm', import.meta.url))).buffer]];
47
- if (opts.adapt)
48
- adapters = adapters.concat(await Promise.all(opts.adapt.map(async adapt => {
49
- let adapter;
50
- if (adapt.includes('='))
51
- adapter = adapt.split('=');
52
- else
53
- adapter = [basename(adapt).slice(0, -extname(adapt).length), adapt];
54
- adapter[1] = await readFile(adapter[1]);
55
- return adapter;
56
- })));
57
- const output = componentNewFn(source, adapters);
58
- await writeFile(opts.output, output);
46
+ await $init;
47
+ const source = file ? await readFile(file) : null;
48
+ let adapters = [];
49
+ if (opts.wasiReactor && opts.wasiCommand)
50
+ throw new Error('Must select one of --wasi-command or --wasi-reactor');
51
+ if (opts.wasiReactor)
52
+ adapters = [
53
+ [
54
+ 'wasi_snapshot_preview1',
55
+ (
56
+ await readFile(
57
+ new URL(
58
+ '../../lib/wasi_snapshot_preview1.reactor.wasm',
59
+ import.meta.url
60
+ )
61
+ )
62
+ ).buffer,
63
+ ],
64
+ ];
65
+ else if (opts.wasiCommand)
66
+ adapters = [
67
+ [
68
+ 'wasi_snapshot_preview1',
69
+ (
70
+ await readFile(
71
+ new URL(
72
+ '../../lib/wasi_snapshot_preview1.command.wasm',
73
+ import.meta.url
74
+ )
75
+ )
76
+ ).buffer,
77
+ ],
78
+ ];
79
+ if (opts.adapt)
80
+ adapters = adapters.concat(
81
+ await Promise.all(
82
+ opts.adapt.map(async (adapt) => {
83
+ let adapter;
84
+ if (adapt.includes('=')) adapter = adapt.split('=');
85
+ else
86
+ adapter = [
87
+ basename(adapt).slice(0, -extname(adapt).length),
88
+ adapt,
89
+ ];
90
+ adapter[1] = await readFile(adapter[1]);
91
+ return adapter;
92
+ })
93
+ )
94
+ );
95
+ const output = componentNewFn(source, adapters);
96
+ await writeFile(opts.output, output);
59
97
  }
60
98
 
61
99
  export async function componentEmbed(file, opts) {
62
- await $init;
63
- if (opts.metadata)
64
- opts.metadata = opts.metadata.map(meta => {
65
- const [field, data = ''] = meta.split('=');
66
- const [name, version = ''] = data.split('@');
67
- return [field, [[name, version]]];
68
- });
69
- const source = file ? await readFile(file) : null;
70
- opts.binary = source;
71
- opts.witPath = (isWindows ? '//?/' : '') + resolve(opts.wit);
72
- const output = componentEmbedFn(opts);
73
- await writeFile(opts.output, output);
100
+ await $init;
101
+ if (opts.metadata)
102
+ opts.metadata = opts.metadata.map((meta) => {
103
+ const [field, data = ''] = meta.split('=');
104
+ const [name, version = ''] = data.split('@');
105
+ return [field, [[name, version]]];
106
+ });
107
+ const source = file ? await readFile(file) : null;
108
+ opts.binary = source;
109
+ opts.witPath = (isWindows ? '//?/' : '') + resolve(opts.wit);
110
+ const output = componentEmbedFn(opts);
111
+ await writeFile(opts.output, output);
74
112
  }
75
113
 
76
114
  export async function metadataAdd(file, opts) {
77
- await $init;
78
- const metadata = opts.metadata.map(meta => {
79
- const [field, data = ''] = meta.split('=');
80
- const [name, version = ''] = data.split('@');
81
- return [field, [[name, version]]];
82
- });
83
- const source = await readFile(file);
84
- const output = metadataAddFn(source, metadata);
85
- await writeFile(opts.output, output);
115
+ await $init;
116
+ const metadata = opts.metadata.map((meta) => {
117
+ const [field, data = ''] = meta.split('=');
118
+ const [name, version = ''] = data.split('@');
119
+ return [field, [[name, version]]];
120
+ });
121
+ const source = await readFile(file);
122
+ const output = metadataAddFn(source, metadata);
123
+ await writeFile(opts.output, output);
86
124
  }
87
125
 
88
126
  export async function metadataShow(file, opts) {
89
- await $init;
90
- const source = await readFile(file);
91
- let output = '', stack = [1];
92
- const meta = metadataShowFn(source);
93
- if (opts.json) {
94
- console.log(JSON.stringify(meta, null, 2));
95
- }
96
- else {
97
- for (const { name, metaType, producers } of meta) {
98
- output += ' '.repeat(stack.length - 1);
99
- const indent = ' '.repeat(stack.length);
100
- if (metaType.tag === 'component') {
101
- output += c`{bold [component${name ? ' ' + name : ''}]}\n`;
102
- if (metaType.val > 0)
103
- stack.push(metaType.val);
104
- } else {
105
- output += c`{bold [module${name ? ' ' + name : ''}]}\n`;
106
- }
107
- if (producers.length === 0)
108
- output += `${indent}(no metadata)\n`;
109
- for (const [field, items] of producers) {
110
- for (const [name, version] of items) {
111
- output += `${indent}${(field + ':').padEnd(13, ' ')} ${name}${version ? c`{cyan ${version}}` : ''}\n`;
127
+ await $init;
128
+ const source = await readFile(file);
129
+ let output = '',
130
+ stack = [1];
131
+ const meta = metadataShowFn(source);
132
+ if (opts.json) {
133
+ console.log(JSON.stringify(meta, null, 2));
134
+ } else {
135
+ for (const { name, metaType, producers } of meta) {
136
+ output += ' '.repeat(stack.length - 1);
137
+ const indent = ' '.repeat(stack.length);
138
+ if (metaType.tag === 'component') {
139
+ output += c`{bold [component${name ? ' ' + name : ''}]}\n`;
140
+ if (metaType.val > 0) stack.push(metaType.val);
141
+ } else {
142
+ output += c`{bold [module${name ? ' ' + name : ''}]}\n`;
143
+ }
144
+ if (producers.length === 0) output += `${indent}(no metadata)\n`;
145
+ for (const [field, items] of producers) {
146
+ for (const [name, version] of items) {
147
+ output += `${indent}${(field + ':').padEnd(13, ' ')} ${name}${version ? c`{cyan ${version}}` : ''}\n`;
148
+ }
149
+ }
150
+ output += '\n';
151
+ if (stack[stack.length - 1] === 0) stack.pop();
152
+ stack[stack.length - 1]--;
112
153
  }
113
- }
114
- output += '\n';
115
- if (stack[stack.length - 1] === 0)
116
- stack.pop();
117
- stack[stack.length - 1]--;
154
+ process.stdout.write(output);
118
155
  }
119
- process.stdout.write(output);
120
- }
121
156
  }
package/src/jco.js CHANGED
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/env node
2
+
3
+ import c from 'chalk-template';
2
4
  import { program, Option } from 'commander';
5
+
3
6
  import { opt } from './cmd/opt.js';
4
7
  import { transpile, types, guestTypes } from './cmd/transpile.js';
5
8
  import { run as runCmd, serve as serveCmd } from './cmd/run.js';
6
9
  import { parse, print, componentNew, componentEmbed, metadataAdd, metadataShow, componentWit } from './cmd/wasm-tools.js';
7
10
  import { componentize } from './cmd/componentize.js';
8
- import c from 'chalk-template';
9
11
 
10
12
  program
11
13
  .name('jco')
@@ -34,10 +36,12 @@ program.command('componentize')
34
36
  .requiredOption('-w, --wit <path>', 'WIT path to build with')
35
37
  .option('-n, --world-name <name>', 'WIT world to build')
36
38
  .option('--aot', 'Enable Weval AOT compilation of JS')
39
+ .option('--aot-min-stack-size-bytes <number>', 'Set the min stack size to be used during AOT')
37
40
  .option('--weval-bin <path>', 'Specify a custom weval binary to use')
38
41
  .addOption(new Option('-d, --disable <feature...>', 'disable WASI features').choices(['clocks', 'http', 'random', 'stdio', 'all']))
39
42
  // .addOption(new Option('-e, --enable <feature...>', 'enable WASI features').choices(['http']))
40
43
  .option('--preview2-adapter <adapter>', 'provide a custom preview2 adapter path')
44
+ .option('--debug-starlingmonkey-build', 'use a debug build of StarlingMonkey')
41
45
  .requiredOption('-o, --out <out>', 'output component file')
42
46
  .action(asyncAction(componentize));
43
47