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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,99 @@ All notable changes to TypeNative will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [Unreleased]
9
+
10
+ ## [0.6.2] - 2026-11-15
11
+
12
+ ### Fixed
13
+
14
+ - **Self-hosted native build:** `npm run build:native` failed with `cannot use
15
+ typedClass (variable of type bool) as string value`. The transpiler typed
16
+ `a && b` by its left operand, so a getter-lookup helper written as
17
+ `leftGoType?.startsWith('*') && leftGoType.slice(1)` became a `bool` in the
18
+ Go build. It is now written as a ternary that yields a string or
19
+ `undefined`.
20
+ - **`replace` with an inline `new RegExp(…, 'g')`:** only the first match was
21
+ replaced, because the `g` flag was only seen on a regex literal or a
22
+ `RegExp` read from a variable. Flags written out in `new RegExp(p, 'flags')`
23
+ are now read at compile time, and any other `RegExp` expression (a call's
24
+ result, flags computed at run time) is evaluated once and its `g` flag
25
+ tested at run time. The self-hosted compiler relied on this to map string
26
+ enums to `string` in `tnCallValueTyped`, so its build of
27
+ examples/react-compiler failed with `duplicate case func(string,
28
+ MemoizationLevel) MemoizationLevel in type switch`.
29
+
30
+ ### Changed
31
+
32
+ - **tsparser:** `github.com/go-json-experiment/json` bumped to v1.27.2.
33
+
34
+ ## [0.6.1] - 2026-11-15
35
+
36
+ ### Changed
37
+
38
+ - **examples/react-compiler: faster than Node at 64 KB.** The React Compiler
39
+ now runs a 64 KB component file in 615 ms vs Node's 1,030 ms (1.7x faster;
40
+ it was 3.5x slower), and per call is 3.5x faster (100 vs 349 ms). Plain
41
+ functions assigned to `NodePath.prototype` declare `this: NodePath`; that
42
+ now types `__this` as the class, so Babel's per-node path maintenance
43
+ (`visit`, `setScope`, `setContext`, `parentPath` walks) is direct field
44
+ access instead of dynamic plan lookups. Dynamic calls of the program's own
45
+ function types go through a generated `tnCallValueTyped` case per Go func
46
+ type (no `reflect.Value.Call`), and optional calls to prototype methods
47
+ added at run time are guarded dynamic calls. For large files the example
48
+ compiles independent top-level functions in parallel worker processes and
49
+ joins their output; under Node each worker re-imports the compiler, while a
50
+ TypeNative worker starts from the compiled binary.
51
+ - **Iterating class iterators through dynamic values:** `TnToArray` (used by
52
+ spread, `Array.from`, destructuring and `for…of` on a dynamically typed
53
+ value) recognizes a class's generator push form directly
54
+ (`Symbol_iterator__seq`) and iterates it in place instead of finding
55
+ `*[Symbol.iterator]()` through reflection, calling it and draining its
56
+ channel on a goroutine. An interface-yield channel method also gets a direct
57
+ type assertion. This is what Angular's `OpList` hit through an op's dynamic
58
+ `handlerOps` / `trackByOps` fields; angular-templates' 64 KB run fell from
59
+ about 600 ms to 305 ms (now 1.2x faster than Node), and per-call latency is
60
+ 4.4x Node's (50 vs 219 ms).
61
+ - **yaml-cli throughput:** `yaml --json` on a large, plain stream is split
62
+ before the `---` lines that start its documents, and up to six worker
63
+ processes compose runs of them and print their JSON arrays, which the main
64
+ process joins. Directives, errors, warnings, CR line endings and
65
+ collection-valued keys fall back to the normal `cli()` path (the example
66
+ runs under Node too). On the 10 MB benchmark the complete process run is
67
+ now 754 ms vs Node's 851 ms (median, 1.1x); the single-process path also
68
+ got faster.
69
+ - **Object conversions and dynamic writes:** conversion identity holds one
70
+ weak pointer per conversion, sweeps dead entries in batches and keeps a
71
+ recent cache; a fresh local object converted once is copied without an
72
+ identity entry (`TnAsCopy`), and a conversion between two statically known
73
+ struct types is emitted as a struct literal. `Object.assign({ defaults },
74
+ typedSource)` sets the target's fields directly instead of through
75
+ reflection, `obj[key] = v` on a plain object skips reflection
76
+ (`TnSetProp`), and `s += chunk` for an untyped chunk appends in place
77
+ instead of copying the whole string.
78
+ - **List building:** a single-value `push` grows the backing slice by 2x
79
+ (`TnPush`) rather than Go's 1.25x, whose repeated copies add up to about
80
+ five times a long list's final memory.
81
+ - **marked-cli throughput:** large markdown documents are parsed in up to six
82
+ worker processes, one contiguous run of top-level sections each, with a
83
+ sequential fallback (small inputs, reference link definitions, raw HTML
84
+ blocks, CRLF, `--tokens`). On the example's 10 MB benchmark the complete
85
+ process run is now 1.2x faster than Node (512 ms vs 596 ms); parity stays
86
+ 2,859/2,859. The single-process parse also got faster: `[...s].length` /
87
+ `[...s][i]` / `[...s].at(i)` no longer build rune arrays, truthiness-only
88
+ `re.exec` runs `re.test`, nullable regex results box only on a match, greedy
89
+ `[^x]+` runs use `IndexByte`, and the long-string ASCII cache and regex
90
+ replacement builders no longer allocate per token.
91
+
92
+ ### Fixed
93
+
94
+ - **process.stdin:** `TnStdin` sets its `_stdinRef` hook through `setHook`
95
+ again after the lazy emitter/hook state change; reading stdin no longer
96
+ panics with `assignment to entry in nil map`.
97
+ - **events.errorMonitor:** with only the two inline listeners, an `'error'`
98
+ event calls an `errorMonitor` listener before the error listeners again (the
99
+ lazy listener path skipped it, so the conformance probe saw `handler` first).
100
+
8
101
  ## [0.6.0] - 2026-11-13
9
102
 
10
103
  The React Compiler itself (`babel-plugin-react-compiler` + Babel + zod, ~470 vendored files)
package/README.md CHANGED
@@ -295,7 +295,8 @@ also from unmodified source, matching Node on all 2,859 cases of marked's spec
295
295
  suites (CommonMark, GFM, its own tests and ReDoS inputs). The newest,
296
296
  [react-compiler](examples/react-compiler/), compiles the React Compiler
297
297
  itself (`babel-plugin-react-compiler` on Babel 7.29 and zod 4 — the largest
298
- codebase yet) and is one runtime gap away from its benchmarks.
298
+ codebase yet), matching Node on every parity fixture, 3.6x faster per file and
299
+ 1.7x faster on a 64 KB component file.
299
300
 
300
301
  ## How it works
301
302
 
package/bin/node/core.js CHANGED
@@ -39,6 +39,11 @@ func TnInvoke(fn interface{}, args ...interface{}) interface{} {
39
39
  f(a)
40
40
  return nil
41
41
  }
42
+ case func([]byte):
43
+ if a, ok := tnInvokeAt(args, 0).([]byte); ok && f != nil {
44
+ f(a)
45
+ return nil
46
+ }
42
47
  }
43
48
  fv := reflect.ValueOf(fn)
44
49
  if !fv.IsValid() || fv.Kind() != reflect.Func || fv.IsNil() {
@@ -26,6 +26,10 @@ var TnCaptureRejections = false
26
26
  // EventEmitter: embeddable (class X extends EventEmitter), state allocated lazily
27
27
  type TnEventEmitter struct {
28
28
  state *tnEmitterState
29
+ ev0 string
30
+ fn0 interface{}
31
+ ev1 string
32
+ fn1 interface{}
29
33
  }
30
34
 
31
35
  // Anything that embeds an EventEmitter (events.once(emitter, …) accepts subclasses)
@@ -36,10 +40,10 @@ type tnEmitterLike interface {
36
40
  var TnDefaultMaxListeners = 10.0
37
41
 
38
42
  func TnNewEventEmitter(options ...interface{}) *TnEventEmitter {
39
- e := &TnEventEmitter{state: &tnEmitterState{events: map[string][]*tnListener{}, maxListeners: -1, warned: map[string]bool{}, captureRejections: TnCaptureRejections}}
43
+ e := &TnEventEmitter{}
40
44
  if len(options) > 0 && options[0] != nil {
41
45
  if b, ok := TnGetValue(options[0], "captureRejections").(bool); ok {
42
- e.state.captureRejections = b
46
+ e.st().captureRejections = b
43
47
  }
44
48
  }
45
49
  return e
@@ -49,7 +53,20 @@ func (e *TnEventEmitter) tnEmitter() *TnEventEmitter { return e }
49
53
 
50
54
  func (e *TnEventEmitter) st() *tnEmitterState {
51
55
  if e.state == nil {
52
- e.state = &tnEmitterState{events: map[string][]*tnListener{}, maxListeners: -1, warned: map[string]bool{}, captureRejections: TnCaptureRejections}
56
+ s := &tnEmitterState{events: map[string][]*tnListener{}, maxListeners: -1, captureRejections: TnCaptureRejections}
57
+ if e.fn0 != nil {
58
+ s.order = append(s.order, e.ev0)
59
+ s.events[e.ev0] = []*tnListener{{fn: e.fn0}}
60
+ e.ev0, e.fn0 = "", nil
61
+ }
62
+ if e.fn1 != nil {
63
+ if _, ok := s.events[e.ev1]; !ok {
64
+ s.order = append(s.order, e.ev1)
65
+ }
66
+ s.events[e.ev1] = append(s.events[e.ev1], &tnListener{fn: e.fn1})
67
+ e.ev1, e.fn1 = "", nil
68
+ }
69
+ e.state = s
53
70
  }
54
71
  return e.state
55
72
  }
@@ -84,6 +101,16 @@ func (e *TnEventEmitter) prependOnceListener(event string, fn interface{}) *TnEv
84
101
  }
85
102
 
86
103
  func (e *TnEventEmitter) add(event string, fn interface{}, prepend bool, once bool) *TnEventEmitter {
104
+ if e.state == nil && !prepend && !once && !TnCaptureRejections && event != "newListener" {
105
+ if e.fn0 == nil {
106
+ e.ev0, e.fn0 = event, fn
107
+ return e
108
+ }
109
+ if e.fn1 == nil {
110
+ e.ev1, e.fn1 = event, fn
111
+ return e
112
+ }
113
+ }
87
114
  s := e.st()
88
115
  if len(s.events["newListener"]) > 0 {
89
116
  e.emit("newListener", event, fn)
@@ -105,6 +132,9 @@ func (e *TnEventEmitter) add(event string, fn interface{}, prepend bool, once bo
105
132
  }
106
133
  warn := max > 0 && float64(count) > max && !s.warned[event]
107
134
  if warn {
135
+ if s.warned == nil {
136
+ s.warned = map[string]bool{}
137
+ }
108
138
  s.warned[event] = true
109
139
  }
110
140
  s.mu.Unlock()
@@ -115,6 +145,22 @@ func (e *TnEventEmitter) add(event string, fn interface{}, prepend bool, once bo
115
145
  }
116
146
 
117
147
  func (e *TnEventEmitter) removeListener(event string, fn interface{}) *TnEventEmitter {
148
+ if e.state == nil {
149
+ if e.fn0 == nil && e.fn1 == nil {
150
+ return e
151
+ }
152
+ id := tnFuncID(fn)
153
+ if e.fn1 != nil && e.ev1 == event && tnFuncID(e.fn1) == id {
154
+ e.ev1, e.fn1 = "", nil
155
+ return e
156
+ }
157
+ if e.fn0 != nil && e.ev0 == event && tnFuncID(e.fn0) == id {
158
+ e.ev0, e.fn0 = e.ev1, e.fn1
159
+ e.ev1, e.fn1 = "", nil
160
+ return e
161
+ }
162
+ return e
163
+ }
118
164
  s := e.st()
119
165
  s.mu.Lock()
120
166
  list := s.events[event]
@@ -157,6 +203,21 @@ func (e *TnEventEmitter) setListeners(event string, list []*tnListener) {
157
203
  }
158
204
 
159
205
  func (e *TnEventEmitter) removeAllListeners(event ...string) *TnEventEmitter {
206
+ if e.state == nil {
207
+ if len(event) == 0 {
208
+ e.ev0, e.fn0 = "", nil
209
+ e.ev1, e.fn1 = "", nil
210
+ return e
211
+ }
212
+ if e.fn1 != nil && e.ev1 == event[0] {
213
+ e.ev1, e.fn1 = "", nil
214
+ }
215
+ if e.fn0 != nil && e.ev0 == event[0] {
216
+ e.ev0, e.fn0 = e.ev1, e.fn1
217
+ e.ev1, e.fn1 = "", nil
218
+ }
219
+ return e
220
+ }
160
221
  s := e.st()
161
222
  s.mu.Lock()
162
223
  if len(event) > 0 {
@@ -170,6 +231,44 @@ func (e *TnEventEmitter) removeAllListeners(event ...string) *TnEventEmitter {
170
231
  }
171
232
 
172
233
  func (e *TnEventEmitter) emit(event string, args ...interface{}) bool {
234
+ if e.state == nil {
235
+ var f0, f1 interface{}
236
+ if e.fn0 != nil && e.ev0 == event {
237
+ f0 = e.fn0
238
+ }
239
+ if e.fn1 != nil && e.ev1 == event {
240
+ f1 = e.fn1
241
+ }
242
+ // events.errorMonitor sees an 'error' before the error listeners,
243
+ // whatever their registration order
244
+ if event == "error" {
245
+ if e.fn0 != nil && e.ev0 == TnErrorMonitor {
246
+ TnInvoke(e.fn0, args...)
247
+ }
248
+ if e.fn1 != nil && e.ev1 == TnErrorMonitor {
249
+ TnInvoke(e.fn1, args...)
250
+ }
251
+ }
252
+ if f0 == nil && f1 == nil {
253
+ if event == "error" {
254
+ if len(args) > 0 {
255
+ if _, ok := args[0].(error); ok {
256
+ panic(args[0])
257
+ }
258
+ panic(TnNodeError("Error", "ERR_UNHANDLED_ERROR", "Unhandled error. ("+fmt.Sprint(args[0])+")"))
259
+ }
260
+ panic(TnNodeError("Error", "ERR_UNHANDLED_ERROR", "Unhandled error."))
261
+ }
262
+ return false
263
+ }
264
+ if f0 != nil {
265
+ TnInvoke(f0, args...)
266
+ }
267
+ if f1 != nil {
268
+ TnInvoke(f1, args...)
269
+ }
270
+ return true
271
+ }
173
272
  s := e.st()
174
273
  s.mu.Lock()
175
274
  // (listener lists are copy-on-write — adds append past this length, removals
@@ -288,6 +387,16 @@ func TnAddAbortListener(signal *TnAbortSignal, listener interface{}) *TnDisposab
288
387
  }
289
388
 
290
389
  func (e *TnEventEmitter) listeners(event string) []interface{} {
390
+ if e.state == nil {
391
+ out := []interface{}{}
392
+ if e.fn0 != nil && e.ev0 == event {
393
+ out = append(out, e.fn0)
394
+ }
395
+ if e.fn1 != nil && e.ev1 == event {
396
+ out = append(out, e.fn1)
397
+ }
398
+ return out
399
+ }
291
400
  s := e.st()
292
401
  s.mu.Lock()
293
402
  defer s.mu.Unlock()
@@ -301,6 +410,21 @@ func (e *TnEventEmitter) listeners(event string) []interface{} {
301
410
  func (e *TnEventEmitter) rawListeners(event string) []interface{} { return e.listeners(event) }
302
411
 
303
412
  func (e *TnEventEmitter) listenerCount(event string, fn ...interface{}) float64 {
413
+ if e.state == nil {
414
+ n := 0
415
+ wantFilter := len(fn) > 0 && fn[0] != nil
416
+ var wantID uintptr
417
+ if wantFilter {
418
+ wantID = tnFuncID(fn[0])
419
+ }
420
+ if e.fn0 != nil && e.ev0 == event && (!wantFilter || tnFuncID(e.fn0) == wantID) {
421
+ n++
422
+ }
423
+ if e.fn1 != nil && e.ev1 == event && (!wantFilter || tnFuncID(e.fn1) == wantID) {
424
+ n++
425
+ }
426
+ return float64(n)
427
+ }
304
428
  s := e.st()
305
429
  s.mu.Lock()
306
430
  defer s.mu.Unlock()
@@ -317,6 +441,16 @@ func (e *TnEventEmitter) listenerCount(event string, fn ...interface{}) float64
317
441
  }
318
442
 
319
443
  func (e *TnEventEmitter) eventNames() []string {
444
+ if e.state == nil {
445
+ out := []string{}
446
+ if e.fn0 != nil {
447
+ out = append(out, e.ev0)
448
+ }
449
+ if e.fn1 != nil && (e.fn0 == nil || e.ev1 != e.ev0) {
450
+ out = append(out, e.ev1)
451
+ }
452
+ return out
453
+ }
320
454
  s := e.st()
321
455
  s.mu.Lock()
322
456
  defer s.mu.Unlock()
@@ -329,10 +463,10 @@ func (e *TnEventEmitter) setMaxListeners(n float64) *TnEventEmitter {
329
463
  }
330
464
 
331
465
  func (e *TnEventEmitter) getMaxListeners() float64 {
332
- if e.st().maxListeners < 0 {
466
+ if e.state == nil || e.state.maxListeners < 0 {
333
467
  return TnDefaultMaxListeners
334
468
  }
335
- return e.st().maxListeners
469
+ return e.state.maxListeners
336
470
  }
337
471
 
338
472
  // events.once(emitter, name): a promise of the event's arguments; 'error' rejects it