typenative 0.6.0 → 0.6.2

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.
@@ -10,6 +10,9 @@
10
10
  // the compiler's own fixtures: `// @compilationMode:"infer" @enableJsxOutlining`.
11
11
 
12
12
  import * as fs from 'node:fs';
13
+ import * as nodePath from 'node:path';
14
+ import * as nodeOs from 'node:os';
15
+ import { spawn } from 'node:child_process';
13
16
  import { parse, type ParserPlugin } from './vendor/babel/parser/src/index.ts';
14
17
  import traverse, { type NodePath } from './vendor/babel/traverse/src/index.ts';
15
18
  import generate from './vendor/babel/generator/src/index.ts';
@@ -88,6 +91,11 @@ function describe(event: LoggerEvent): string {
88
91
  }
89
92
 
90
93
  function compile(source: string, filename: string): string {
94
+ const { code, events } = compileParts(source, filename);
95
+ return [code, ...events].join('\n');
96
+ }
97
+
98
+ function compileParts(source: string, filename: string): { code: string; events: string[] } {
91
99
  const firstLine = source.substring(0, source.indexOf('\n'));
92
100
  const flow = firstLine.includes('@flow');
93
101
  const plugins: ParserPlugin[] = flow
@@ -132,17 +140,153 @@ function compile(source: string, filename: string): string {
132
140
  },
133
141
  });
134
142
 
135
- const out = [generate(ast, {}, source).code];
136
- for (const event of events) out.push(describe(event));
137
- return out.join('\n');
143
+ const code = generate(ast, {}, source).code;
144
+ return { code, events: events.map(describe) };
145
+ }
146
+
147
+ // Large files of independent top-level functions: a worker process compiles
148
+ // each contiguous run of functions, and the parent joins their output. The
149
+ // per-function output is independent, and each chunk is padded so source
150
+ // positions (the // CompileSuccess lines) stay the ones of the whole file.
151
+ // Under Node each worker re-imports the compiler (expensive); TypeNative's
152
+ // workers start from the compiled binary. TN_PARALLEL=0 disables it.
153
+ const MAX_WORKERS = 6;
154
+ const MIN_FUNCTIONS = 8;
155
+
156
+ type Chunk = { first: string; text: string };
157
+
158
+ function splitSource(source: string): Chunk[] | undefined {
159
+ // (compiler temps are renamed per chunk; a source using such a name keeps
160
+ // the single-process path)
161
+ if (/\b_temp\d*\b/.test(source)) return undefined;
162
+ const lines = source.split('\n');
163
+ const starts: { name: string; line: number }[] = [];
164
+ for (let i = 0; i < lines.length; i++) {
165
+ const match = /^(?:export\s+)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)\s*\(/.exec(lines[i]);
166
+ if (match) starts.push({ name: match[1], line: i });
167
+ }
168
+ if (starts.length < MIN_FUNCTIONS) return undefined;
169
+ // (the header must be imports / comments only: the chunks share it)
170
+ const header = lines.slice(0, starts[0].line);
171
+ if (header.some((l) => l.trim() !== '' && !/^(import\b|\/\/|\/\*|\*|'use |"use )/.test(l.trim()))) return undefined;
172
+ // (each function runs to the line before the next one; brace-counted, so
173
+ // templates and strings with braces could fool it — then no parallelism)
174
+ const ranges: { name: string; start: number; end: number }[] = [];
175
+ for (let i = 0; i < starts.length; i++) {
176
+ const start = starts[i].line;
177
+ const limit = i + 1 < starts.length ? starts[i + 1].line : lines.length;
178
+ let depth = 0;
179
+ let end = -1;
180
+ for (let j = start; j < limit; j++) {
181
+ for (const ch of lines[j]) {
182
+ if (ch === '{') depth++;
183
+ else if (ch === '}') depth--;
184
+ }
185
+ if (depth === 0 && j > start) { end = j; break; }
186
+ }
187
+ if (end < 0 || !/^\}/.test(lines[end])) return undefined;
188
+ // (only functions are split: anything else between them — variables,
189
+ // statements — would be dropped, so such a file compiles in one process)
190
+ if (i + 1 < starts.length) {
191
+ for (let j = end + 1; j < starts[i + 1].line; j++) {
192
+ const text = lines[j].trim();
193
+ if (text !== '' && !/^(\/\/|\/\*|\*)/.test(text)) return undefined;
194
+ }
195
+ }
196
+ ranges.push({ name: starts[i].name, start, end });
197
+ }
198
+ const workers = Math.min(MAX_WORKERS, Math.ceil(ranges.length / 10));
199
+ // (one worker would only add a process start: compile in this one)
200
+ if (workers < 2) return undefined;
201
+ const chunks: Chunk[] = [];
202
+ const per = Math.ceil(ranges.length / workers);
203
+ for (let i = 0; i < ranges.length; i += per) {
204
+ const group = ranges.slice(i, i + per);
205
+ const firstStart = group[0].start;
206
+ const padding = Math.max(0, firstStart - header.length);
207
+ const pad: string[] = [];
208
+ for (let k = 0; k < padding; k++) pad.push('');
209
+ const body = lines.slice(firstStart, group[group.length - 1].end + 1);
210
+ // the last chunk carries the rest of the file (export default …)
211
+ const tail = i + per >= ranges.length ? lines.slice(group[group.length - 1].end + 1) : [];
212
+ chunks.push({
213
+ first: group[0].name,
214
+ text: [...header, ...pad, ...body, ...tail].join('\n'),
215
+ });
216
+ }
217
+ return chunks;
218
+ }
219
+
220
+ function runWorkerProcess(chunkFile: string): Promise<string> {
221
+ return new Promise((resolve, reject) => {
222
+ const nodeMain = process.env.TN_NODE_MAIN;
223
+ const args = nodeMain ? [nodeMain, chunkFile, '--worker'] : [chunkFile, '--worker'];
224
+ const child = spawn(process.execPath, args, { stdio: ['ignore', 'pipe', 'pipe'] });
225
+ let out = '';
226
+ let err = '';
227
+ child.stdout?.on('data', (data: unknown) => (out += String(data)));
228
+ child.stderr?.on('data', (data: unknown) => (err += String(data)));
229
+ child.on('error', (e: unknown) => {
230
+ reject(e);
231
+ });
232
+ child.on('close', (code: number | null) => {
233
+ if (code === 0) {
234
+ resolve(out);
235
+ } else {
236
+ reject(new Error(err || `worker exited with ${code}`));
237
+ }
238
+ });
239
+ });
138
240
  }
139
241
 
140
- const file = process.argv[2];
242
+ async function parallelCompile(source: string, filename: string, chunks: Chunk[]): Promise<string> {
243
+ const dir = fs.mkdtempSync(nodePath.join(nodeOs.tmpdir(), 'tn-react-compile-'));
244
+ try {
245
+ const files: string[] = [];
246
+ for (let i = 0; i < chunks.length; i++) {
247
+ const file = nodePath.join(dir, `chunk${i}.jsx`);
248
+ fs.writeFileSync(file, chunks[i].text);
249
+ files.push(file);
250
+ }
251
+ const outputs: string[] = await Promise.all(files.map(runWorkerProcess));
252
+ const codes: string[] = [];
253
+ const events: string[] = [];
254
+ for (const text of outputs) {
255
+ const part = JSON.parse(text) as { code: string; events: string[] };
256
+ codes.push(part.code);
257
+ for (const event of part.events) events.push(event);
258
+ }
259
+ let prefix = '';
260
+ const sections: string[] = [];
261
+ let needsMemoCache = false;
262
+ for (let i = 0; i < codes.length; i++) {
263
+ const marker = `function ${chunks[i].first}(`;
264
+ const at = codes[i].indexOf(marker);
265
+ if (at < 0) throw new Error(`chunk ${i}: ${marker} not found`);
266
+ if (i === 0) prefix = codes[i].slice(0, at);
267
+ if (codes[i].includes('react/compiler-runtime')) needsMemoCache = true;
268
+ // (each chunk numbers its generated _temp functions from 0: keep them
269
+ // apart across the merged program)
270
+ const section = codes[i].slice(at).replace(/\b_temp(\d*)\b/g, (_m, n: string) => `_temp${n}__tn${i}`).trimEnd();
271
+ sections.push(section);
272
+ }
273
+ // (chunk 0 may not need the memo cache import when a later one does)
274
+ if (needsMemoCache && !prefix.includes('react/compiler-runtime')) {
275
+ prefix = `import { c as _c } from "react/compiler-runtime";\n${prefix}`;
276
+ }
277
+ return [prefix + sections.join('\n'), ...events].join('\n');
278
+ } finally {
279
+ fs.rmSync(dir, { recursive: true, force: true });
280
+ }
281
+ }
282
+
283
+ const workerMode = process.argv.includes('--worker');
284
+ const fileArgs = process.argv.slice(1).filter((a) => !a.startsWith('--'));
285
+ const file = workerMode ? fileArgs[fileArgs.length - 1] : process.argv[2];
141
286
  const source = file ? fs.readFileSync(file, 'utf-8') : DEMO;
142
287
  const filename = file ?? 'demo.jsx';
143
- try {
144
- console.log(compile(source, filename));
145
- } catch (e) {
288
+
289
+ function printError(e: unknown): void {
146
290
  // (an error the compiler or the parser throws, as a comment: the same
147
291
  // under both runtimes)
148
292
  const message = e instanceof CompilerError ? e.withPrintedMessage(source, { eslint: false }).message : (e as Error).message;
@@ -156,3 +300,25 @@ try {
156
300
  }
157
301
  process.exitCode = 1;
158
302
  }
303
+
304
+ if (workerMode) {
305
+ try {
306
+ console.log(JSON.stringify(compileParts(source, filename)));
307
+ } catch (e) {
308
+ printError(e);
309
+ }
310
+ } else {
311
+ try {
312
+ // (a worker re-runs this module; under Node it needs the module's path)
313
+ const nodeRuntime = !process.env.TN_NODE_MAIN && /(^|[\\/])node(\.exe)?$/i.test(process.execPath);
314
+ const chunks = process.env.TN_PARALLEL === '0' || nodeRuntime ? undefined : splitSource(source);
315
+ console.log(chunks ? await parallelCompile(source, filename, chunks) : compile(source, filename));
316
+ } catch {
317
+ // (a worker failed: the whole file, compiled in this process)
318
+ try {
319
+ console.log(compile(source, filename));
320
+ } catch (e) {
321
+ printError(e);
322
+ }
323
+ }
324
+ }
@@ -83,6 +83,8 @@ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.me
83
83
  buildNode();
84
84
  if (process.argv[2] !== '--build') {
85
85
  process.argv = [process.argv[0], nodeMain, ...process.argv.slice(2)];
86
+ // (a worker process the compiler spawns runs this same module)
87
+ process.env.TN_NODE_MAIN = nodeMain;
86
88
  await import(pathToFileURL(nodeMain).href);
87
89
  }
88
90
  }
@@ -5,7 +5,11 @@ compiled from its **unmodified** upstream TypeScript source: about 11,000
5
5
  lines across 78 files, vendored as is in [vendor/yaml/](vendor/yaml/) (see
6
6
  [VENDOR.md](vendor/yaml/VENDOR.md): no patches). [main.ts](main.ts) ports
7
7
  upstream's `bin.js` to TypeScript. It hands stdin and the arguments to the
8
- library's `cli()` and turns the error it reports into an exit code.
8
+ library's `cli()` and turns the error it reports into an exit code. For
9
+ `yaml --json` on a large, plain stream it splits the input before the `---`
10
+ lines that start its documents and composes runs of them in parallel workers,
11
+ then joins their JSON arrays; any directive, error, warning, CR line ending or
12
+ non-scalar map key sends the whole input back through `cli()`.
9
13
 
10
14
  This is a test of TypeNative on real code nobody wrote for it: a lexer and
11
15
  parser driven by sticky regexes, class hierarchies with overrides (an
@@ -83,49 +87,39 @@ and checks that the two JSON outputs are identical:
83
87
  generator, so every machine gets the same input).
84
88
 
85
89
  **Environment:** Windows 11 Pro, AMD Ryzen 5 7600 (6 cores, 12 threads),
86
- Node v26.13.0, Go 1.27.4, TypeNative 0.5.0 (unreleased changes), default
87
- (non-`--release`) build. The binary is 7.6 MB.
90
+ Node v26.13.0, Go 1.27.4, TypeNative 0.6.1, default
91
+ (non-`--release`) build.
88
92
 
89
93
  (a) `yaml --json < docker-compose.yml`, 30 runs each:
90
94
 
91
95
  | Runtime | min | median | mean | max |
92
96
  | ---------- | --------- | --------- | --------- | --------- |
93
- | Node.js | 113.39 ms | 117.24 ms | 120.06 ms | 134.47 ms |
94
- | TypeNative | 8.71 ms | 9.28 ms | 16.79 ms | 234.75 ms |
97
+ | Node.js | 102.38 ms | 104.87 ms | 106.76 ms | 117.83 ms |
98
+ | TypeNative | 8.23 ms | 8.78 ms | 9.89 ms | 41.91 ms |
95
99
 
96
100
  (b) `yaml --json < manifests.yaml` (10.0 MB), 5 runs each:
97
101
 
98
102
  | Runtime | min | median | mean | max | MB/s (median) |
99
103
  | ---------- | -------- | -------- | -------- | -------- | ------------- |
100
- | Node.js | 1,316 ms | 1,320 ms | 1,340 ms | 1,375 ms | 7.58 |
101
- | TypeNative | 5,064 ms | 5,169 ms | 5,270 ms | 5,634 ms | 1.93 |
104
+ | Node.js | 807.92 ms | 851.20 ms | 844.81 ms | 869.64 ms | 11.75 |
105
+ | TypeNative | 741.86 ms | 753.61 ms | 751.74 ms | 760.93 ms | 13.27 |
102
106
 
103
- **Per call, TypeNative is 13x faster** (9 ms vs 117 ms, median). Node's
107
+ **Per call, TypeNative is 11.9x faster** (9 ms vs 105 ms, median). Node's
104
108
  cost here is mostly startup. It loads and type-strips the 78 TypeScript
105
109
  files on every run before it reads a byte of input. That's what you feel
106
110
  when a tool calls the converter once per file.
107
111
 
108
- **On bulk throughput, TypeNative is 3.9x slower.** Once V8's JIT has warmed
109
- up on a long input, it runs this code faster than the compiled Go does, in
110
- every phase rather than in one bad spot: lexing, parsing and composing each
111
- take several times as long as under Node. The library passes its values around as
112
- structural object types and unions (`Scalar | YAMLMap | YAMLSeq | Pair`, CST
113
- tokens), which TypeNative holds as `interface{}`, so the cost is spread across
114
- property reads, conversions between object types and allocation, which alone
115
- takes about a fifth of the main thread. Garbage collection runs on other cores
116
- and hardly affects the wall clock. These costs grow linearly with input size,
117
- while Node's head start is fixed, so the native binary finishes first up to
118
- about 350 KB of YAML per process:
119
-
120
- | Input | Node.js | TypeNative |
121
- | ------ | ------- | ---------- |
122
- | 128 KB | 136 ms | 67 ms |
123
- | 256 KB | 160 ms | 125 ms |
124
- | 512 KB | 206 ms | 245 ms |
125
- | 1 MB | 257 ms | 490 ms |
126
- | 2 MB | 388 ms | 988 ms |
127
-
128
- (Medians of 5 full process runs each, same generator.)
112
+ **On bulk throughput the two are close** (754 ms vs 851 ms, median; 13.3 vs
113
+ 11.8 MB/s). Both runtimes take the split-and-workers path for this input, so
114
+ Node pays six worker startups while the native binary pays almost none; that
115
+ largely cancels the native single-process deficit in this benchmark. Without
116
+ the workers, the native process takes about 3.0 s for the same 10 MB (lex
117
+ 0.31 s, parse 0.91 s, compose 1.42 s, JSON 0.40 s) against Node's 1.3 s.
118
+ Once V8's JIT has warmed up, it runs this dynamic object-heavy code faster:
119
+ the library passes structural object types and unions (`Scalar | YAMLMap |
120
+ YAMLSeq | Pair`, CST tokens) that TypeNative holds as `interface{}`, so the
121
+ cost is spread across property reads, conversions and allocation. Garbage
122
+ collection runs on other cores and hardly affects the wall clock.
129
123
 
130
124
  Getting here fixed performance bugs in TypeNative's runtime, all general:
131
125
 
@@ -137,7 +131,19 @@ Getting here fixed performance bugs in TypeNative's runtime, all general:
137
131
  that's consumed on the spot, such as a comparison, doesn't box the value.
138
132
  - `obj.list.push(x)` on an object held as any appends in place. It used to
139
133
  convert the whole array on every push, which made building a list quadratic.
140
- - Conversions between object types are compiled once per pair of types.
134
+ - Single-value pushes grow the backing slice by 2x, not Go's 1.25x: building
135
+ the lexer's 3.2 million tokens copies the array out about five times over
136
+ otherwise. This alone cuts roughly 40 ms from lexing and 50 ms from compose.
137
+ - Conversions between object types are compiled once per pair of types; a
138
+ fresh local object converted once copies without an identity entry, and a
139
+ conversion between two statically known struct types is a struct literal.
140
+ Conversion identity holds one weak pointer per entry and sweeps dead ones
141
+ in batches.
142
+ - `Object.assign({ defaults }, typedSource)` sets fields directly instead of
143
+ reflecting over them, `obj[key] = v` on a plain object skips reflection,
144
+ and `s += chunk` for an untyped chunk appends in place.
145
+ - An optional call or chain whose value is only tested returns its primitive
146
+ directly instead of boxing it into a pointer first.
141
147
  - `switch` on a value held as any compiles to a string switch when its cases
142
148
  are string literals.
143
149
  - Regexes run on a VM that works on byte offsets, so a sticky match on a 10 MB
@@ -10,20 +10,242 @@
10
10
  // yaml cst < in.yaml print the concrete syntax tree
11
11
  //
12
12
  // Runs as-is under Node.js (`node main.ts`) and compiles with TypeNative.
13
+ //
14
+ // One addition to bin.js: `yaml --json` on a large stream of documents
15
+ // converts runs of documents in parallel worker processes (see below). The
16
+ // output is the same JSON cli() prints, byte for byte.
17
+ import { promises } from 'node:fs';
18
+ import { availableParallelism, tmpdir } from 'node:os';
19
+ import { PassThrough } from 'node:stream';
20
+ import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads';
13
21
  import { UserError, cli, help } from './vendor/yaml/src/cli.ts';
22
+ import { Composer } from './vendor/yaml/src/compose/composer.ts';
23
+ import type { Document } from './vendor/yaml/src/doc/Document.ts';
24
+ import { Pair } from './vendor/yaml/src/nodes/Pair.ts';
25
+ import { Scalar } from './vendor/yaml/src/nodes/Scalar.ts';
26
+ import { YAMLMap } from './vendor/yaml/src/nodes/YAMLMap.ts';
27
+ import { YAMLSeq } from './vendor/yaml/src/nodes/YAMLSeq.ts';
28
+ import { YAMLSet } from './vendor/yaml/src/nodes/YAMLSet.ts';
29
+ import { Parser } from './vendor/yaml/src/parse/parser.ts';
30
+
31
+ const { readFile, unlink, writeFile } = promises;
32
+
33
+ function done(error?: Error) {
34
+ if (error instanceof UserError) {
35
+ if (error.code === UserError.ARGS) console.error(`${help}\n`);
36
+ console.error(error.message);
37
+ process.exitCode = error.code;
38
+ } else if (error) {
39
+ // bin.js rethrows here; Node then prints the error and exits with 1
40
+ console.error(error);
41
+ process.exitCode = 1;
42
+ }
43
+ }
44
+
45
+ // `yaml --json` on a large stream is split before the `---` lines that start
46
+ // its documents, and up to six worker processes each compose one contiguous
47
+ // run of documents and print it as JSON; the main process joins the arrays.
48
+ // A `---` at the start of a line always ends the document before it (it ends
49
+ // any scalar or collection still open, or that one is an error), so each
50
+ // run's documents are the ones the whole stream has. Only plain streams take
51
+ // this path: no directives (% lines, which carry over to later documents) and
52
+ // no CR line endings. Any error, warning, collection-valued key (its warning
53
+ // is printed while the JSON is made) or exception in a worker sends the whole
54
+ // input through cli() instead, which reports it as it always would. A run
55
+ // travels in workerData; its JSON comes back through a temporary file (the
56
+ // worker IPC carries one compact message, not megabytes).
57
+ const PARALLEL_MIN_BYTES = 512 * 1024;
58
+
59
+ interface ConvertJob {
60
+ text: string;
61
+ output: string;
62
+ indent?: string;
63
+ }
14
64
 
15
- cli(
16
- process.stdin,
17
- (error) => {
18
- if (error instanceof UserError) {
19
- if (error.code === UserError.ARGS) console.error(`${help}\n`);
20
- console.error(error.message);
21
- process.exitCode = error.code;
22
- } else if (error) {
23
- // bin.js rethrows here; Node then prints the error and exits with 1
24
- console.error(error);
25
- process.exitCode = 1;
65
+ // The --indent value when the arguments are just --json (and --indent):
66
+ // undefined for none, null when the arguments ask for anything else
67
+ function jsonOnlyArgs(argv: string[]): { indent?: string } | null {
68
+ let json = false;
69
+ let indent: string | undefined;
70
+ for (let i = 0; i < argv.length; i++) {
71
+ const arg = argv[i];
72
+ if (arg === '--json' || arg === '-j') json = true;
73
+ else if ((arg === '--indent' || arg === '-i') && i + 1 < argv.length && !argv[i + 1].startsWith('-')) indent = argv[++i];
74
+ else if (arg.startsWith('--indent=')) indent = arg.slice('--indent='.length);
75
+ else return null;
76
+ }
77
+ return json ? { indent } : null;
78
+ }
79
+
80
+ function splitDocuments(data: string): string[] | undefined {
81
+ if (data.length < PARALLEL_MIN_BYTES || data.includes('\r')) return undefined;
82
+ if (data.startsWith('%') || data.includes('\n%')) return undefined;
83
+ const cuts: number[] = [0];
84
+ let at = data.indexOf('\n---');
85
+ while (at !== -1) {
86
+ const after = data.charCodeAt(at + 4);
87
+ // (`---` then a space, a tab, a newline or the end: a document start)
88
+ if (Number.isNaN(after) || after === 32 || after === 9 || after === 10) cuts.push(at + 1);
89
+ at = data.indexOf('\n---', at + 4);
90
+ }
91
+ if (cuts.length < 3) return undefined;
92
+ cuts.push(data.length);
93
+ const requested = Number(process.env.YAML_WORKERS ?? '0') || availableParallelism();
94
+ const count = Math.min(6, Math.max(1, requested), cuts.length - 1);
95
+ if (count < 2) return undefined;
96
+ // one contiguous run of documents per process, balanced by size
97
+ const parts: string[] = [];
98
+ let next = 1;
99
+ let start = 0;
100
+ for (let m = 1; m <= count; m++) {
101
+ const target = (data.length * m) / count;
102
+ while (next < cuts.length - 1 && cuts[next] < target) next++;
103
+ const end = m === count ? data.length : cuts[next];
104
+ if (end > start) parts.push(data.slice(start, end));
105
+ start = end;
106
+ }
107
+ return parts.length > 1 ? parts : undefined;
108
+ }
109
+
110
+ // Whether toJS could warn on this node: a map key that is not a scalar (an
111
+ // alias, a collection) is stringified with a warning. (A set: assumed so.)
112
+ function warnsOnKeys(node: unknown): boolean {
113
+ if (node instanceof Pair) {
114
+ return (node.key !== null && !(node.key instanceof Scalar)) || warnsOnKeys(node.value);
115
+ }
116
+ if (node instanceof YAMLMap) {
117
+ for (const pair of node.values.values()) if (warnsOnKeys(pair)) return true;
118
+ return false;
119
+ }
120
+ if (node instanceof YAMLSeq) {
121
+ for (const item of node) if (warnsOnKeys(item)) return true;
122
+ return false;
123
+ }
124
+ return node instanceof YAMLSet;
125
+ }
126
+
127
+ async function workerMain() {
128
+ const job = workerData as ConvertJob;
129
+ const text = job.text as string;
130
+ const output = job.output as string;
131
+ const indent = job.indent as string | undefined;
132
+ try {
133
+ // as cli() composes a stream: new Composer({ version: opt.yaml, merge: opt.merge })
134
+ const composer = new Composer({ version: '1.2', merge: undefined });
135
+ const parser = new Parser();
136
+ for (const tok of parser.parse(text)) composer.next(tok);
137
+ const docs: Document[] = [];
138
+ let clean = true;
139
+ for (const doc of composer.end(false)) {
140
+ if (doc.errors.length > 0 || doc.warnings.length > 0 || warnsOnKeys(doc.value)) clean = false;
141
+ docs.push(doc);
142
+ }
143
+ // (a run with no document must be all blank lines and comments: an error
144
+ // with no document to report it is otherwise lost)
145
+ if (docs.length === 0 && /^[ \t]*[^ \t\n#]/m.test(job.text)) clean = false;
146
+ if (clean) {
147
+ await writeFile(output, JSON.stringify(docs, null, Number(indent)));
148
+ parentPort!.postMessage('ok');
149
+ } else {
150
+ parentPort!.postMessage('fallback');
26
151
  }
27
- },
28
- process.argv.slice(2)
29
- );
152
+ } catch {
153
+ parentPort!.postMessage('fallback');
154
+ }
155
+ parentPort!.close();
156
+ }
157
+
158
+ // The JSON of each run's array, as the JSON of one array of all their items
159
+ function joinArrays(arrays: string[]): string {
160
+ const items: string[] = [];
161
+ let separator = ',';
162
+ for (const json of arrays) {
163
+ if (json === '[]') continue;
164
+ if (json.startsWith('[\n')) {
165
+ separator = ',\n';
166
+ items.push(json.slice(2, -2));
167
+ } else {
168
+ items.push(json.slice(1, -1));
169
+ }
170
+ }
171
+ if (items.length === 0) return '[]';
172
+ return separator === ',' ? '[' + items.join(',') + ']' : '[\n' + items.join(',\n') + '\n]';
173
+ }
174
+
175
+ // The runs converted in workers: their JSON, or '' when a worker found
176
+ // something cli() has to report
177
+ async function convertParallel(parts: string[], indent: string | undefined): Promise<string> {
178
+ const base = tmpdir() + '/yaml-' + process.pid + '-' + Date.now();
179
+ const results: string[] = await Promise.all(parts.map((text, index) => new Promise<string>((resolve, reject) => {
180
+ const output = base + '-' + index + '.json';
181
+ const job: ConvertJob = { text, output, indent };
182
+ try {
183
+ const worker = new Worker(new URL(import.meta.url), { workerData: job });
184
+ worker.on('message', async (message: string) => {
185
+ try {
186
+ if (message === 'ok') {
187
+ const json: string = await readFile(output, 'utf8');
188
+ await unlink(output).catch(() => {});
189
+ resolve(json);
190
+ } else {
191
+ resolve('');
192
+ }
193
+ } catch (err) {
194
+ reject(err);
195
+ }
196
+ worker.terminate();
197
+ });
198
+ worker.on('error', (err: Error) => {
199
+ unlink(output).catch(() => {});
200
+ reject(err);
201
+ });
202
+ } catch (err) {
203
+ reject(err);
204
+ }
205
+ })));
206
+ if (results.some((json) => json === '')) return '';
207
+ return joinArrays(results);
208
+ }
209
+
210
+ function readStdin(): Promise<string> {
211
+ return new Promise((resolve, reject) => {
212
+ let source = '';
213
+ process.stdin.setEncoding('utf-8');
214
+ process.stdin.on('data', (chunk: string) => {
215
+ source += chunk;
216
+ });
217
+ process.stdin.on('error', (err: Error) => reject(err));
218
+ process.stdin.on('end', () => resolve(source));
219
+ });
220
+ }
221
+
222
+ async function main() {
223
+ const argv = process.argv.slice(2);
224
+ const json = jsonOnlyArgs(argv);
225
+ if (!json) {
226
+ cli(process.stdin, done, argv);
227
+ return;
228
+ }
229
+ const source = await readStdin();
230
+ const parts = splitDocuments(source);
231
+ if (parts) {
232
+ const output = await convertParallel(parts, json.indent);
233
+ if (output !== '') {
234
+ // (flushed before the process exits: a pipe gets all of it)
235
+ await new Promise<void>((resolve) => {
236
+ process.stdout.write(output + '\n', () => resolve());
237
+ });
238
+ return;
239
+ }
240
+ }
241
+ // (the input read here, replayed to cli() as its stdin)
242
+ const replay = new PassThrough();
243
+ replay.end(source);
244
+ cli(replay, done, argv);
245
+ }
246
+
247
+ if (isMainThread) {
248
+ main();
249
+ } else {
250
+ workerMain();
251
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typenative",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Build native applications using Typescript.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -157,6 +157,7 @@ the reference machine.
157
157
  | `events-emit` | `TnInvoke` called every listener through `reflect.Value.Call`. `emit` copied the listener list and built a `kept` list on every call. | Typed fast paths for `func()`, `func(any)`, `func(any, any)`, `func(...any)`, `func(float64)` and `func(string)`. `emit` uses the copy-on-write listener slice as its snapshot. | 1372 → 658 ms |
158
158
  | `regex-site` | Each evaluation of a regex literal looked its compiled program up again (key concatenation and a mutex). | One package-level `TnRegexSite` per literal shares the program and RE2 fallbacks. Each evaluation is still a new `RegExp` with its own `lastIndex`. | 177 → 168 ms |
159
159
  | `gen-for-of` (examples) | Generators called through dynamic dispatch (`unit.ops()` on an abstract `CompilationUnit`, 26 phases of angular-templates) still ran as goroutines, and `for…of` over them collected every value first; spread and `Array.from` of a generator received through reflect. | `TnCallSeq_<m>[T]` dispatches a generator method on a dynamic receiver to the class's push form (its channel in lockstep for a generator without one, any other value's elements otherwise), for `for…of`, `yield*` and `super.m()`. A spread or `Array.from` of a generator call collects its push form into a typed slice (`TnGeneratorItems`). | angular-templates at 64 KB: 721 → 601 ms |
160
+ | `gen-for-of` (examples) | `Array.from` / spread over a class iterator reached through a dynamic field (`op.handlerOps`, `op.trackByOps` in angular-templates) found `*[Symbol.iterator]()` with `reflect.MethodByName`, called it, and drained its channel on a goroutine: one reflective call and two goroutine hand-offs per operation list. | `TnToArray` asserts the class's push form directly (`Symbol_iterator__seq`, a `func(func(T) bool)`) and iterates it in place; an interface-yield channel method gets its own type assertion too, without reflection. | angular-templates at 64 KB: 601 → 305 ms |
160
161
  | `class-dispatch` | A call on an abstract base class or an interface went through `TnCall_<m>(recv, args ...interface{})`: arguments and the result boxed, then unboxed with `TnAs`. | When every class of the receiver's declared hierarchy declares the method with the same Go signature, the call goes through `TnCall_<C>_<m>(recv, a P0, …) R`: a type switch calling the method with its own types, the boxed dispatcher only for any other receiver. The call's inferred type is `R`, so the value isn't boxed again where it's used. | 709 → 124 ms |
161
162
  | `async-await` | Each async call started a goroutine and handed the JS baton over twice, and each settle allocated two wake-up channels even with nobody waiting. | An async function that awaits only calls of other such functions (whose promises are settled when they return), with no `for await` and no promise returned, runs on the caller's goroutine (`TnRunAsyncInline`). `TnAsyncSend` / `TnAsyncClose` allocate a wake-up channel only for a parked awaiter. | 1985 → 201 ms |
162
163
  | `json-roundtrip` | `JSON.parse` built a `TnMap` per object from `encoding/json` tokens, then `as Item[]` converted every map to a struct through `TnConvertDynamic`. | `JSON.parse(text)` into a typed slot is `TnJSONParseTyped[T]`: a scanner decodes the text straight into `T` (per-type plans over field offsets) when every object has `T`'s keys in its field order and every value its kind; anything else (other keys, null, any fields, invalid text) decodes and converts as before. | 347 → 97 ms |
package/tsparser/go.mod CHANGED
@@ -5,7 +5,7 @@ go 1.26.0
5
5
  require github.com/buke/typescript-go-internal v0.0.2
6
6
 
7
7
  require (
8
- github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e // indirect
8
+ github.com/go-json-experiment/json v1.27.2 // indirect
9
9
  github.com/klauspost/cpuid/v2 v2.2.10 // indirect
10
10
  github.com/zeebo/xxh3 v1.1.0 // indirect
11
11
  golang.org/x/sync v0.20.0 // indirect
package/tsparser/go.sum CHANGED
@@ -1,7 +1,7 @@
1
1
  github.com/buke/typescript-go-internal v0.0.2 h1:JTHAXQfj1HVN4eXh7XqUWZurxjowXoE+5Ud8QLS4uRg=
2
2
  github.com/buke/typescript-go-internal v0.0.2/go.mod h1:nZjFbdZyxx5D9s/GafAh6ESVUy3GMAjNMMMBjVEqDgc=
3
- github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e h1:Lf/gRkoycfOBPa42vU2bbgPurFong6zXeFtPoxholzU=
4
- github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e/go.mod h1:uNVvRXArCGbZ508SxYYTC5v1JWoz2voff5pm25jU1Ok=
3
+ github.com/go-json-experiment/json v1.27.2 h1:rZIyqYh+Qq+nEa+Y5iwohr8h7G/MfLJqu2CD4/ZONqM=
4
+ github.com/go-json-experiment/json v1.27.2/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
5
5
  github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
6
6
  github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
7
7
  github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=