zod-compiler 1.0.16 → 1.0.18
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 +38 -34
- package/dist/cli/emitter.js +1 -1
- package/dist/cli/emitter.js.map +1 -1
- package/dist/core/codegen/context.d.ts +21 -6
- package/dist/core/codegen/context.d.ts.map +1 -1
- package/dist/core/codegen/context.js +26 -4
- package/dist/core/codegen/context.js.map +1 -1
- package/dist/core/codegen/emit-issue.d.ts +2 -2
- package/dist/core/codegen/emit-issue.js +18 -18
- package/dist/core/codegen/index.d.ts.map +1 -1
- package/dist/core/codegen/index.js +36 -11
- package/dist/core/codegen/index.js.map +1 -1
- package/dist/core/codegen/issue-decls.d.ts +17 -17
- package/dist/core/codegen/issue-decls.js +26 -26
- package/dist/core/codegen/schemas/array.js +2 -2
- package/dist/core/codegen/schemas/array.js.map +1 -1
- package/dist/core/codegen/schemas/catch.js +1 -1
- package/dist/core/codegen/schemas/catch.js.map +1 -1
- package/dist/core/codegen/schemas/effect.d.ts.map +1 -1
- package/dist/core/codegen/schemas/effect.js +4 -3
- package/dist/core/codegen/schemas/effect.js.map +1 -1
- package/dist/core/codegen/schemas/fallback.js +1 -1
- package/dist/core/codegen/schemas/fallback.js.map +1 -1
- package/dist/core/codegen/schemas/number.js +5 -5
- package/dist/core/codegen/schemas/number.js.map +1 -1
- package/dist/core/codegen/schemas/object.d.ts.map +1 -1
- package/dist/core/codegen/schemas/object.js +3 -3
- package/dist/core/codegen/schemas/object.js.map +1 -1
- package/dist/core/codegen/schemas/string.js +3 -3
- package/dist/core/codegen/schemas/string.js.map +1 -1
- package/dist/core/codegen/schemas/union.js +4 -4
- package/dist/core/codegen/schemas/union.js.map +1 -1
- package/dist/core/codegen/well-known-regex.d.ts +3 -3
- package/dist/core/codegen/well-known-regex.js +16 -16
- package/dist/core/iife.d.ts +19 -12
- package/dist/core/iife.d.ts.map +1 -1
- package/dist/core/iife.js +20 -13
- package/dist/core/iife.js.map +1 -1
- package/dist/unplugin/hoist-compile.d.ts +1 -1
- package/dist/unplugin/hoist-compile.js +1 -1
- package/dist/unplugin/transform.js +11 -11
- package/dist/unplugin/transform.js.map +1 -1
- package/dist/unplugin/virtual.d.ts +1 -1
- package/dist/unplugin/virtual.js +5 -5
- package/dist/unplugin/virtual.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**Compile Zod schemas into zero-overhead validation functions at build time.**
|
|
4
4
|
|
|
5
|
-
Keep your existing Zod schemas. Get **2-
|
|
5
|
+
Keep your existing Zod schemas. Get **2-74x faster** validation. No code changes required.
|
|
6
6
|
|
|
7
7
|
```typescript
|
|
8
8
|
// vite.config.ts — add one line
|
|
@@ -23,7 +23,7 @@ export const UserSchema = z.object({
|
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
// Use it anywhere — tRPC, Hono, React Hook Form, etc.
|
|
26
|
-
// At build time, zod-compiler compiles it to a 2-
|
|
26
|
+
// At build time, zod-compiler compiles it to a 2-74x faster validator.
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
## Install
|
|
@@ -212,8 +212,8 @@ Combinator chains on imported schemas also qualify: bases matching
|
|
|
212
212
|
|
|
213
213
|
### Bundle Size & Cross-File Dedup
|
|
214
214
|
|
|
215
|
-
Generated validators share a small runtime helper layer (`
|
|
216
|
-
wrapper, issue factories like `
|
|
215
|
+
Generated validators share a small runtime helper layer (`__zcMkv` validator
|
|
216
|
+
wrapper, issue factories like `__zcTS`/`__zcIT`, and well-known regexes for
|
|
217
217
|
`email`, `uuid`, `cuid`, `ipv4`, etc.).
|
|
218
218
|
|
|
219
219
|
On every supported bundler the plugin imports these helpers from a single
|
|
@@ -395,7 +395,7 @@ npx zod-compiler check src/schemas.ts --json --fail-under 80
|
|
|
395
395
|
|
|
396
396
|
## What Gets Compiled
|
|
397
397
|
|
|
398
|
-
### Fully Compiled (2-
|
|
398
|
+
### Fully Compiled (2-74x faster)
|
|
399
399
|
|
|
400
400
|
`string`, `number`, `bigint`, `boolean`, `null`, `undefined`, `any`, `unknown`, `literal`, `enum`, `stringbool`, `date`, `file`, `object`, `strictObject` / `.strict()`, `looseObject`, `array`, `tuple`, `record`, `set`, `map`, `union`, `discriminatedUnion`, `intersection`, `pipe` (non-transform), `optional`, `nullable`, `readonly`, `default`, `catch`, `coerce`, `templateLiteral`, `symbol`, `void`, `nan`, `never`, `lazy` (self-recursive), `transform` / `refine` (zero-capture — see below)
|
|
401
401
|
|
|
@@ -429,35 +429,39 @@ compiles; `z.string().transform((s) => s + suffix)` falls back (it captures
|
|
|
429
429
|
|
|
430
430
|
5-way comparison: **Zod v3** vs **Zod v4** vs **zod-compiler** vs **[Typia](https://typia.io/)** vs **[AJV](https://ajv.js.org/)**
|
|
431
431
|
|
|
432
|
-
| Scenario
|
|
433
|
-
|
|
|
434
|
-
| simple string
|
|
435
|
-
| string (min/max)
|
|
436
|
-
| number (int+positive)
|
|
437
|
-
| enum
|
|
438
|
-
| bigint (min/max)
|
|
439
|
-
| tuple [string, int, bool]
|
|
440
|
-
| record\<string, number\>
|
|
441
|
-
| set\<string\> (5 items)
|
|
442
|
-
| set\<string\> (20 items)
|
|
443
|
-
| map\<string, number\> (5 entries)
|
|
444
|
-
| map\<string, number\> (20 entries)
|
|
445
|
-
| pipe (non-transform)
|
|
446
|
-
| discriminatedUnion (3 variants)
|
|
447
|
-
|
|
|
448
|
-
| medium object (
|
|
449
|
-
|
|
|
450
|
-
| large object (
|
|
451
|
-
|
|
|
452
|
-
| recursive tree (
|
|
453
|
-
|
|
|
454
|
-
|
|
|
455
|
-
|
|
|
456
|
-
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
432
|
+
| Scenario | Zod v3 | Zod v4 | **zod-compiler** | Typia | AJV | vs Zod v4 |
|
|
433
|
+
| ------------------------------------------------- | ------ | ------ | ---------------- | ----- | ----- | --------- |
|
|
434
|
+
| simple string | 12.9M | 14.4M | **16.6M** | 17.0M | 17.5M | 1.2x |
|
|
435
|
+
| string (min/max) | 12.0M | 7.4M | **16.2M** | 17.0M | 14.5M | 2.2x |
|
|
436
|
+
| number (int+positive) | 11.7M | 7.8M | **16.4M** | 16.9M | 17.0M | 2.1x |
|
|
437
|
+
| enum | 11.5M | 11.9M | **15.3M** | 15.1M | 16.0M | 1.3x |
|
|
438
|
+
| bigint (min/max) | 11.7M | 7.7M | **16.1M** | — | — | 2.1x |
|
|
439
|
+
| tuple [string, int, bool] | 5.9M | 5.1M | **16.9M** | 17.8M | 16.6M | 3.3x |
|
|
440
|
+
| record\<string, number\> | 3.2M | 2.8M | **10.8M** | 12.1M | 15.7M | 3.9x |
|
|
441
|
+
| set\<string\> (5 items) | 3.7M | 2.2M | **14.4M** | — | — | 6.7x |
|
|
442
|
+
| set\<string\> (20 items) | 1.1M | 667K | **11.1M** | — | — | **17x** |
|
|
443
|
+
| map\<string, number\> (5 entries) | 1.8M | 1.2M | **10.4M** | — | — | 8.7x |
|
|
444
|
+
| map\<string, number\> (20 entries) | 582K | 315K | **8.1M** | — | — | **26x** |
|
|
445
|
+
| pipe (non-transform) | 8.8M | 5.8M | **16.6M** | — | — | 2.9x |
|
|
446
|
+
| discriminatedUnion (3 variants) | 3.1M | 3.9M | **15.1M** | 14.9M | 7.3M | 3.9x |
|
|
447
|
+
| strict object (DB row) | 1.8M | 3.2M | **7.2M** | — | — | 2.3x |
|
|
448
|
+
| medium object (valid) | 1.9M | 2.4M | **9.7M** | 10.5M | 7.4M | 4.0x |
|
|
449
|
+
| medium object (invalid) | 518K | 76K | **5.6M** | 2.9M | 7.7M | **74x** |
|
|
450
|
+
| large object (10 items) | 117K | 166K | **7.6M** | 5.8M | 1.0M | **46x** |
|
|
451
|
+
| large object (100 items) | 13K | 18K | **1.3M** | 1.2M | 110K | **72x** |
|
|
452
|
+
| recursive tree (7 nodes) | 572K | 2.1M | **12.9M** | 12.1M | 4.7M | 6.2x |
|
|
453
|
+
| recursive tree (121 nodes) | 31K | 140K | **1.9M** | 1.9M | 353K | **14x** |
|
|
454
|
+
| event log (combined) | 364K | 536K | **5.9M** | — | — | **11x** |
|
|
455
|
+
| object with transform (zero-capture) | 1.0M | 1.8M | **5.7M** | — | — | 3.2x |
|
|
456
|
+
| array 10 × transform (zero-capture) | 104K | 188K | **2.9M** | — | — | **15x** |
|
|
457
|
+
| array 50 × transform (zero-capture) | 24K | 43K | **737K** | — | — | **17x** |
|
|
458
|
+
| object with captured transform (partial fallback) | 1.4M | 6.1M | **6.5M** | — | — | 1.1x |
|
|
459
|
+
|
|
460
|
+
_ops/s, higher is better. "—" = not supported by the library. Measured with `vitest bench` on Apple M-series (zod 4.3.6, zod v3 3.23.8, typia 12, ajv 8)._
|
|
461
|
+
|
|
462
|
+
Performance scales with schema complexity. Nested objects and arrays see the biggest gains because zod-compiler eliminates per-node traversal overhead. `discriminatedUnion` uses O(1) `switch` dispatch instead of Zod's sequential trial. The invalid-input row is large because failed `safeParse` defers error materialization until `.error` is read. Zero-capture `transform`/`refine` callbacks are compiled (3-17x); schemas with captured callbacks fall back per-field and roughly match Zod.
|
|
463
|
+
|
|
464
|
+
`parse()` (throwing API) rides a zero-allocation fast path: medium object 2.2M → 9.1M ops/s (4.2x), large object (100 items) 18K → 1.4M ops/s (76x).
|
|
461
465
|
|
|
462
466
|
```bash
|
|
463
467
|
pnpm bench # run locally
|
package/dist/cli/emitter.js
CHANGED
|
@@ -8,7 +8,7 @@ export function generateCompiledFileContent(schemas, sourceRelPath, options) {
|
|
|
8
8
|
}
|
|
9
9
|
const zodCompat = options?.zodCompat !== false;
|
|
10
10
|
// A schema needs the source import when:
|
|
11
|
-
// - zodCompat: true (
|
|
11
|
+
// - zodCompat: true (__zcMkv installs compiled methods on the schema)
|
|
12
12
|
// - has refEntries (fallback schemas referenced via __rf[])
|
|
13
13
|
const schemasNeedingImport = schemas.filter((s) => zodCompat || s.refEntries.length > 0);
|
|
14
14
|
const exports = schemas.map((schema) => {
|
package/dist/cli/emitter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emitter.js","sourceRoot":"","sources":["../../src/cli/emitter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,QAAQ,EACR,iBAAiB,EACjB,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAe3B,MAAM,UAAU,2BAA2B,CACzC,OAA6B,EAC7B,aAAqB,EACrB,OAAwB;IAExB,IAAI,UAAU,GAAG,aAAa,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAC;IAC9D,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAChC,UAAU,GAAG,KAAK,UAAU,EAAE,CAAC;IACjC,CAAC;IAED,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,KAAK,KAAK,CAAC;IAE/C,yCAAyC;IACzC,
|
|
1
|
+
{"version":3,"file":"emitter.js","sourceRoot":"","sources":["../../src/cli/emitter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,QAAQ,EACR,iBAAiB,EACjB,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAe3B,MAAM,UAAU,2BAA2B,CACzC,OAA6B,EAC7B,aAAqB,EACrB,OAAwB;IAExB,IAAI,UAAU,GAAG,aAAa,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAC;IAC9D,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAChC,UAAU,GAAG,KAAK,UAAU,EAAE,CAAC;IACjC,CAAC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iB,CAAC,SAAiB,EAAE,UAA8B;IACjF,IAAI,UAAU,EAAE,CAAC;QACf,gFAAgF;QAChF,IAAI,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAC9D,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;YACnE,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,QAAQ,cAAc,CAAC,CAAC;QAC1D,CAAC;QACD,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,yCAAyC;IACzC,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;IACnE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,cAAc,CAAC,CAAC;AACnD,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,UAAkB,EAAE,OAAe;IACzE,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACrC,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAClD,MAAM,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,UAAU,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -7,14 +7,14 @@ export interface CodeGenResult {
|
|
|
7
7
|
/** Number of fallback schemas referenced by __rf[N] in the generated code. 0 = no fallbacks. */
|
|
8
8
|
refCount: number;
|
|
9
9
|
/**
|
|
10
|
-
* Helper names referenced by this schema in lean mode (e.g. "
|
|
10
|
+
* Helper names referenced by this schema in lean mode (e.g. "__zcTS", "__zcReEmail").
|
|
11
11
|
* Used by the unplugin transform to construct the `import { ... } from "virtual:zod-compiler/runtime"` line.
|
|
12
12
|
* Always empty in inline mode.
|
|
13
13
|
*/
|
|
14
14
|
usedHelpers: Set<string>;
|
|
15
15
|
/**
|
|
16
16
|
* Name of the hosted fast-check boolean function in the preamble (null when
|
|
17
|
-
* the schema has no Fast Path). generateIIFE passes it to
|
|
17
|
+
* the schema has no Fast Path). generateIIFE passes it to __zcMkv so parse()
|
|
18
18
|
* and parseAsync() can return valid input without allocating an
|
|
19
19
|
* intermediate SafeParseResult.
|
|
20
20
|
*/
|
|
@@ -38,6 +38,8 @@ export interface CodeGenContext {
|
|
|
38
38
|
* can call it. undefined = schema has no recursion on the fast path.
|
|
39
39
|
*/
|
|
40
40
|
recFastName?: string;
|
|
41
|
+
/** Dedup cache for hosted zero-capture effect functions: source text → preamble var. */
|
|
42
|
+
effectFnCache?: Map<string, string>;
|
|
41
43
|
}
|
|
42
44
|
/** Context object for slow-path (error-collecting) generator functions. */
|
|
43
45
|
export interface SlowGen {
|
|
@@ -94,19 +96,32 @@ export interface FastGen {
|
|
|
94
96
|
export type FastGenerator<T extends SchemaIR = SchemaIR> = (ir: T, g: FastGen) => string | null;
|
|
95
97
|
/** Allocate a fresh `__${prefix}_${n}` identifier and bump the shared counter. */
|
|
96
98
|
export declare function emitTemp(ctx: CodeGenContext, prefix: string): string;
|
|
99
|
+
/**
|
|
100
|
+
* Host a zero-capture effect function (refine predicate, transform,
|
|
101
|
+
* overwrite) in the preamble and return its variable name. The inline
|
|
102
|
+
* `(${source})(x)` form evaluates the function expression — allocating a
|
|
103
|
+
* function object — on EVERY parse at every effect site, including inside
|
|
104
|
+
* the "zero-allocation" fast chain. V8's escape analysis erases that in
|
|
105
|
+
* optimized frames, but interpreter/baseline/deopt frames pay it, and the
|
|
106
|
+
* full source text re-parses as bytecode at each site. Zero-capture sources
|
|
107
|
+
* reference only their own parameters and safe globals by construction, so
|
|
108
|
+
* a single preamble binding is semantically identical. Deduped per schema
|
|
109
|
+
* by source text.
|
|
110
|
+
*/
|
|
111
|
+
export declare function emitEffectFn(ctx: CodeGenContext, source: string): string;
|
|
97
112
|
/**
|
|
98
113
|
* Pristine fallback delegate: declare `var __rfp_N=__rf[N].safeParse.bind(__rf[N]);`
|
|
99
114
|
* in the preamble and return the variable name. Generated code must NEVER read
|
|
100
|
-
* `__rf[N].safeParse` at parse time: `
|
|
115
|
+
* `__rf[N].safeParse` at parse time: `__zcMkv` installs the compiled safeParse as
|
|
101
116
|
* an OWN property on the original schema object, and whenever `__rf[N]` is that
|
|
102
117
|
* same object the read resolves to the compiled delegate itself — infinite
|
|
103
|
-
* recursion (RangeError on every parse). The fallback entry and the
|
|
118
|
+
* recursion (RangeError on every parse). The fallback entry and the __zcMkv
|
|
104
119
|
* target ARE the same object in compile mode (schemaExpr is the compile()
|
|
105
120
|
* argument identifier) and the CLI emitter ((__src_X as any).schema); in
|
|
106
121
|
* autoDiscover mode they are two textually identical constructions that any
|
|
107
122
|
* downstream CSE/dedup transform (babel-plugin-zod-hoist in a field incident)
|
|
108
123
|
* collapses back into one. Capturing at IIFE evaluation — before the trailing
|
|
109
|
-
* `return
|
|
124
|
+
* `return __zcMkv(...)` mutates anything — pins zod's own implementation; the
|
|
110
125
|
* worst case under cross-validator merges is delegating to an equivalent
|
|
111
126
|
* compiled validator (whose own delegates were captured even earlier), never
|
|
112
127
|
* a cycle.
|
|
@@ -146,7 +161,7 @@ export declare function escapeString(s: string | number): string;
|
|
|
146
161
|
*/
|
|
147
162
|
export declare function literalToJs(v: string | number | boolean | null | bigint | undefined): string;
|
|
148
163
|
/**
|
|
149
|
-
* Reference a shared runtime helper (e.g.
|
|
164
|
+
* Reference a shared runtime helper (e.g. __zcFsr) from generated code.
|
|
150
165
|
* Lean mode: registers it for the `virtual:zod-compiler/runtime` import.
|
|
151
166
|
* Inline mode: declares it once in the per-IIFE preamble.
|
|
152
167
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/core/codegen/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,OAAO,EACP,eAAe,EACf,WAAW,EACX,QAAQ,EACR,UAAU,EACX,MAAM,aAAa,CAAC;AAOrB,qKAAqK;AACrK,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE5C,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,gGAAgG;IAChG,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACzB;;;;;OAKG;IACH,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,6FAA6F;AAC7F,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,sFAAsF;IACtF,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,2BAA2B;IAC3B,IAAI,EAAE,WAAW,CAAC;IAClB,uGAAuG;IACvG,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACzB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/core/codegen/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,OAAO,EACP,eAAe,EACf,WAAW,EACX,QAAQ,EACR,UAAU,EACX,MAAM,aAAa,CAAC;AAOrB,qKAAqK;AACrK,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE5C,MAAM,WAAW,aAAa;IAC5B,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AED,qDAAqD;AACrD,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/E;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAmDjD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,CAAC,EAAE,OAAO,GAAG,aAAa,GAAG,WAAW,GAAG,UAAU,EACrD,CAAC,EAAE,OAAO,GAAG,aAAa,GAAG,WAAW,GAAG,UAAU,GACpD,MAAM,CAER;AAED;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CACzC,CAAC,SAAS,OAAO,GAAG,eAAe,GAAG,aAAa,GAAG,WAAW,EACjE,MAAM,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAsClB"}
|
|
@@ -4,19 +4,41 @@ import { lookupFastRegexSource, lookupWellKnownRegex, wellKnownRegexSourceName,
|
|
|
4
4
|
export function emitTemp(ctx, prefix) {
|
|
5
5
|
return `__${prefix}_${ctx.counter++}`;
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* Host a zero-capture effect function (refine predicate, transform,
|
|
9
|
+
* overwrite) in the preamble and return its variable name. The inline
|
|
10
|
+
* `(${source})(x)` form evaluates the function expression — allocating a
|
|
11
|
+
* function object — on EVERY parse at every effect site, including inside
|
|
12
|
+
* the "zero-allocation" fast chain. V8's escape analysis erases that in
|
|
13
|
+
* optimized frames, but interpreter/baseline/deopt frames pay it, and the
|
|
14
|
+
* full source text re-parses as bytecode at each site. Zero-capture sources
|
|
15
|
+
* reference only their own parameters and safe globals by construction, so
|
|
16
|
+
* a single preamble binding is semantically identical. Deduped per schema
|
|
17
|
+
* by source text.
|
|
18
|
+
*/
|
|
19
|
+
export function emitEffectFn(ctx, source) {
|
|
20
|
+
ctx.effectFnCache ??= new Map();
|
|
21
|
+
const cached = ctx.effectFnCache.get(source);
|
|
22
|
+
if (cached !== undefined)
|
|
23
|
+
return cached;
|
|
24
|
+
const name = `__ef_${ctx.counter++}`;
|
|
25
|
+
ctx.preamble.push(`var ${name}=(${source});`);
|
|
26
|
+
ctx.effectFnCache.set(source, name);
|
|
27
|
+
return name;
|
|
28
|
+
}
|
|
7
29
|
/**
|
|
8
30
|
* Pristine fallback delegate: declare `var __rfp_N=__rf[N].safeParse.bind(__rf[N]);`
|
|
9
31
|
* in the preamble and return the variable name. Generated code must NEVER read
|
|
10
|
-
* `__rf[N].safeParse` at parse time: `
|
|
32
|
+
* `__rf[N].safeParse` at parse time: `__zcMkv` installs the compiled safeParse as
|
|
11
33
|
* an OWN property on the original schema object, and whenever `__rf[N]` is that
|
|
12
34
|
* same object the read resolves to the compiled delegate itself — infinite
|
|
13
|
-
* recursion (RangeError on every parse). The fallback entry and the
|
|
35
|
+
* recursion (RangeError on every parse). The fallback entry and the __zcMkv
|
|
14
36
|
* target ARE the same object in compile mode (schemaExpr is the compile()
|
|
15
37
|
* argument identifier) and the CLI emitter ((__src_X as any).schema); in
|
|
16
38
|
* autoDiscover mode they are two textually identical constructions that any
|
|
17
39
|
* downstream CSE/dedup transform (babel-plugin-zod-hoist in a field incident)
|
|
18
40
|
* collapses back into one. Capturing at IIFE evaluation — before the trailing
|
|
19
|
-
* `return
|
|
41
|
+
* `return __zcMkv(...)` mutates anything — pins zod's own implementation; the
|
|
20
42
|
* worst case under cross-validator merges is delegating to an equivalent
|
|
21
43
|
* compiled validator (whose own delegates were captured even earlier), never
|
|
22
44
|
* a cycle.
|
|
@@ -139,7 +161,7 @@ export function literalToJs(v) {
|
|
|
139
161
|
return JSON.stringify(v);
|
|
140
162
|
}
|
|
141
163
|
/**
|
|
142
|
-
* Reference a shared runtime helper (e.g.
|
|
164
|
+
* Reference a shared runtime helper (e.g. __zcFsr) from generated code.
|
|
143
165
|
* Lean mode: registers it for the `virtual:zod-compiler/runtime` import.
|
|
144
166
|
* Inline mode: declares it once in the per-IIFE preamble.
|
|
145
167
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/core/codegen/context.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/core/codegen/context.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,uBAAuB,CAAC;AA+G/B,gFAAgF;AAEhF,kFAAkF;AAClF,MAAM,UAAU,QAAQ,CAAC,GAAmB,EAAE,MAAc;IAC1D,OAAO,KAAK,MAAM,IAAI,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC;AACxC,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAAC,GAAmB,EAAE,MAAc;IAC9D,GAAG,CAAC,aAAa,KAAK,IAAI,GAAG,EAAE,CAAC;IAChC,MAAM,MAAM,GAAG,GAAG,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC7C,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC;IACxC,MAAM,IAAI,GAAG,QAAQ,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC;IACrC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,IAAI,KAAK,MAAM,IAAI,CAAC,CAAC;IAC9C,GAAG,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACpC,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,cAAc,CAAC,GAAmB,EAAE,QAAgB;IAClE,MAAM,IAAI,GAAG,SAAS,QAAQ,EAAE,CAAC;IACjC,MAAM,IAAI,GAAG,OAAO,IAAI,SAAS,QAAQ,yBAAyB,QAAQ,KAAK,CAAC;IAChF,IAAI,CAAC,GA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,CAAC;QACpB,CAAC;aAAM,CAAC;YACN,UAAU,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,EAAE,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;IAED,sDAAsD;IACtD,UAAU,CAAC,IAAI,CACb,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CACtF,CAAC;IAEF,qDAAqD;IACrD,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;YAC5B,MAAM,KAAK,GAAG,UAAU,CAAC,EAAE,EAAE,CAAgC,CAAC;YAC9D,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* caller-supplied issues variable). Two emit modes:
|
|
6
6
|
*
|
|
7
7
|
* - inline (CLI .compiled.ts): emits the literal `{code:"too_small",...}` object.
|
|
8
|
-
* - lean (unplugin): emits a call like `
|
|
8
|
+
* - lean (unplugin): emits a call like `__zcTS(min,origin,inclusive,input,path)`
|
|
9
9
|
* and registers the helper in `ctx.usedHelpers` so the transform layer can
|
|
10
10
|
* construct the corresponding `import` from `"virtual:zod-compiler/runtime"`.
|
|
11
11
|
*
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* Helpers default per the most common call-site kind; size-issue emitters
|
|
23
23
|
* accept `useTypeMsg: true` for node-level uses (tuple), and invalidValue
|
|
24
24
|
* accepts `useTypeMsg: false` for check-level uses (file mime).
|
|
25
|
-
* When no message lands, the
|
|
25
|
+
* When no message lands, the __zcFin finalizer applies the locale default.
|
|
26
26
|
*/
|
|
27
27
|
import type { CodeGenContext } from "./context.js";
|
|
28
28
|
/** Common slim slice of SlowGen + FastGen needed for issue emission. */
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* caller-supplied issues variable). Two emit modes:
|
|
6
6
|
*
|
|
7
7
|
* - inline (CLI .compiled.ts): emits the literal `{code:"too_small",...}` object.
|
|
8
|
-
* - lean (unplugin): emits a call like `
|
|
8
|
+
* - lean (unplugin): emits a call like `__zcTS(min,origin,inclusive,input,path)`
|
|
9
9
|
* and registers the helper in `ctx.usedHelpers` so the transform layer can
|
|
10
10
|
* construct the corresponding `import` from `"virtual:zod-compiler/runtime"`.
|
|
11
11
|
*
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* Helpers default per the most common call-site kind; size-issue emitters
|
|
23
23
|
* accept `useTypeMsg: true` for node-level uses (tuple), and invalidValue
|
|
24
24
|
* accepts `useTypeMsg: false` for check-level uses (file mime).
|
|
25
|
-
* When no message lands, the
|
|
25
|
+
* When no message lands, the __zcFin finalizer applies the locale default.
|
|
26
26
|
*/
|
|
27
27
|
import { escapeString } from "./context.js";
|
|
28
28
|
function pushIssue(g, body) {
|
|
@@ -47,11 +47,11 @@ export function tooSmall(g, minimum, origin, inclusive, options) {
|
|
|
47
47
|
const m = resolveMessage(g, options?.message, options?.useTypeMsg === true);
|
|
48
48
|
if (g.ctx.mode === "lean") {
|
|
49
49
|
if (exact) {
|
|
50
|
-
g.ctx.usedHelpers.add("
|
|
51
|
-
return pushIssue(g, `
|
|
50
|
+
g.ctx.usedHelpers.add("__zcTSx");
|
|
51
|
+
return pushIssue(g, `__zcTSx(${minimum},${escapeString(origin)},${input},${path}${messageArg(m)})`);
|
|
52
52
|
}
|
|
53
|
-
g.ctx.usedHelpers.add("
|
|
54
|
-
return pushIssue(g, `
|
|
53
|
+
g.ctx.usedHelpers.add("__zcTS");
|
|
54
|
+
return pushIssue(g, `__zcTS(${minimum},${escapeString(origin)},${inclusive},${input},${path}${messageArg(m)})`);
|
|
55
55
|
}
|
|
56
56
|
if (exact) {
|
|
57
57
|
return pushIssue(g, `{code:"too_small",minimum:${minimum},origin:${escapeString(origin)},inclusive:true,exact:true${messageProp(m)},input:${input},path:${path}}`);
|
|
@@ -65,11 +65,11 @@ export function tooBig(g, maximum, origin, inclusive, options) {
|
|
|
65
65
|
const m = resolveMessage(g, options?.message, options?.useTypeMsg === true);
|
|
66
66
|
if (g.ctx.mode === "lean") {
|
|
67
67
|
if (exact) {
|
|
68
|
-
g.ctx.usedHelpers.add("
|
|
69
|
-
return pushIssue(g, `
|
|
68
|
+
g.ctx.usedHelpers.add("__zcTBx");
|
|
69
|
+
return pushIssue(g, `__zcTBx(${maximum},${escapeString(origin)},${input},${path}${messageArg(m)})`);
|
|
70
70
|
}
|
|
71
|
-
g.ctx.usedHelpers.add("
|
|
72
|
-
return pushIssue(g, `
|
|
71
|
+
g.ctx.usedHelpers.add("__zcTB");
|
|
72
|
+
return pushIssue(g, `__zcTB(${maximum},${escapeString(origin)},${inclusive},${input},${path}${messageArg(m)})`);
|
|
73
73
|
}
|
|
74
74
|
if (exact) {
|
|
75
75
|
return pushIssue(g, `{code:"too_big",maximum:${maximum},origin:${escapeString(origin)},inclusive:true,exact:true${messageProp(m)},input:${input},path:${path}}`);
|
|
@@ -82,8 +82,8 @@ export function invalidType(g, expected, options) {
|
|
|
82
82
|
// invalid_type is always created by the schema node — schema error applies.
|
|
83
83
|
const m = resolveMessage(g, options?.message, true);
|
|
84
84
|
if (g.ctx.mode === "lean" && !options?.extra) {
|
|
85
|
-
g.ctx.usedHelpers.add("
|
|
86
|
-
return pushIssue(g, `
|
|
85
|
+
g.ctx.usedHelpers.add("__zcIT");
|
|
86
|
+
return pushIssue(g, `__zcIT(${escapeString(expected)},${input},${path}${messageArg(m)})`);
|
|
87
87
|
}
|
|
88
88
|
const extra = options?.extra ? `,${options.extra}` : "";
|
|
89
89
|
return pushIssue(g, `{code:"invalid_type",expected:${escapeString(expected)}${extra}${messageProp(m)},input:${input},path:${path}}`);
|
|
@@ -95,9 +95,9 @@ export function invalidFormat(g, format, options) {
|
|
|
95
95
|
// Format issues are created by check instances — schema error never applies.
|
|
96
96
|
const m = options?.message;
|
|
97
97
|
if (g.ctx.mode === "lean") {
|
|
98
|
-
g.ctx.usedHelpers.add("
|
|
98
|
+
g.ctx.usedHelpers.add("__zcIF");
|
|
99
99
|
const extraArg = options?.extra ? `,{${options.extra}}` : m !== undefined ? ",undefined" : "";
|
|
100
|
-
return pushIssue(g, `
|
|
100
|
+
return pushIssue(g, `__zcIF(${formatExpr},${input},${path}${extraArg}${messageArg(m)})`);
|
|
101
101
|
}
|
|
102
102
|
const extra = options?.extra ? `,${options.extra}` : "";
|
|
103
103
|
return pushIssue(g, `{code:"invalid_format",format:${formatExpr}${extra}${messageProp(m)},input:${input},path:${path}}`);
|
|
@@ -108,8 +108,8 @@ export function unrecognizedKeys(g, keysExpr, options) {
|
|
|
108
108
|
// unrecognized_keys is created by the object schema node — schema error applies.
|
|
109
109
|
const m = resolveMessage(g, options?.message, true);
|
|
110
110
|
if (g.ctx.mode === "lean") {
|
|
111
|
-
g.ctx.usedHelpers.add("
|
|
112
|
-
return pushIssue(g, `
|
|
111
|
+
g.ctx.usedHelpers.add("__zcUK");
|
|
112
|
+
return pushIssue(g, `__zcUK(${keysExpr},${input},${path}${messageArg(m)})`);
|
|
113
113
|
}
|
|
114
114
|
return pushIssue(g, `{code:"unrecognized_keys",keys:${keysExpr}${messageProp(m)},input:${input},path:${path}}`);
|
|
115
115
|
}
|
|
@@ -119,8 +119,8 @@ export function invalidValue(g, valuesExpr, options) {
|
|
|
119
119
|
// Default: enum/literal invalid_value is created by the schema node.
|
|
120
120
|
const m = resolveMessage(g, options?.message, options?.useTypeMsg !== false);
|
|
121
121
|
if (g.ctx.mode === "lean") {
|
|
122
|
-
g.ctx.usedHelpers.add("
|
|
123
|
-
return pushIssue(g, `
|
|
122
|
+
g.ctx.usedHelpers.add("__zcIV");
|
|
123
|
+
return pushIssue(g, `__zcIV(${valuesExpr},${input},${path}${messageArg(m)})`);
|
|
124
124
|
}
|
|
125
125
|
return pushIssue(g, `{code:"invalid_value",values:${valuesExpr}${messageProp(m)},input:${input},path:${path}}`);
|
|
126
126
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/codegen/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAkB,aAAa,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAK/E,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iDAAiD;IACjD,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,QAAQ,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,wBAAwB,GACjC,aAAa,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/codegen/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAkB,aAAa,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAK/E,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iDAAiD;IACjD,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,QAAQ,EACZ,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,wBAAwB,GACjC,aAAa,CAqJf"}
|
|
@@ -24,9 +24,9 @@ export function generateValidator(ir, name, options) {
|
|
|
24
24
|
// Root-level fallback: the whole schema delegates to Zod, so zod's own
|
|
25
25
|
// safeParse result IS the result. Returning it directly skips the issue
|
|
26
26
|
// copy loop (which would force zod's eager ZodError construction), the
|
|
27
|
-
// pointless [].concat(path) rewrites, and the
|
|
27
|
+
// pointless [].concat(path) rewrites, and the __zcFin re-wrap. Delegation
|
|
28
28
|
// goes through the pre-mutation capture (emitRfDelegate) — here __rf[0]
|
|
29
|
-
// and the
|
|
29
|
+
// and the __zcMkv target are routinely the SAME object.
|
|
30
30
|
if (ir.type === "fallback" && ir.refIndex !== undefined) {
|
|
31
31
|
const delegate = emitRfDelegate(ctx, ir.refIndex);
|
|
32
32
|
return {
|
|
@@ -42,7 +42,7 @@ export function generateValidator(ir, name, options) {
|
|
|
42
42
|
let fastExpr = generateFast(ir, fg);
|
|
43
43
|
// Host the fast expression in a named boolean helper. Self-recursive
|
|
44
44
|
// schemas need it so recursive refs can call it; every other eligible
|
|
45
|
-
// schema benefits too:
|
|
45
|
+
// schema benefits too: __zcMkv wires it into parse()/parseAsync(), whose
|
|
46
46
|
// success paths then return the input directly — no intermediate
|
|
47
47
|
// SafeParseResult allocation (the safeParse function body is far past
|
|
48
48
|
// V8's inlining budget, so escape analysis never removes it).
|
|
@@ -54,14 +54,14 @@ export function generateValidator(ir, name, options) {
|
|
|
54
54
|
}
|
|
55
55
|
const sg = createSlowGen("_d", "_d", "[]", "_e", ctx);
|
|
56
56
|
const slowCode = generateSlow(ir, sg);
|
|
57
|
-
const
|
|
57
|
+
const buildCode = () => ["/* zod-compiler */", ...ctx.preamble].join("\n");
|
|
58
58
|
const functionDefParts = [`function ${fnName}(input){`];
|
|
59
59
|
if (fastExpr === "true") {
|
|
60
60
|
// Schema always succeeds (any/unknown) — skip slow path entirely
|
|
61
61
|
functionDefParts.push(`return{success:true,data:input};`);
|
|
62
62
|
functionDefParts.push(`}`);
|
|
63
63
|
return {
|
|
64
|
-
code,
|
|
64
|
+
code: buildCode(),
|
|
65
65
|
functionDef: functionDefParts.join("\n"),
|
|
66
66
|
refCount: options?.refCount ?? 0,
|
|
67
67
|
usedHelpers: ctx.usedHelpers,
|
|
@@ -75,12 +75,33 @@ export function generateValidator(ir, name, options) {
|
|
|
75
75
|
// value presence while the slow path SUCCEEDS by applying the default).
|
|
76
76
|
// The slow path's only output is therefore the issues array, observable
|
|
77
77
|
// solely through `.error` — defer the whole re-walk into the cached
|
|
78
|
-
// accessor (
|
|
78
|
+
// accessor (__zcFinD): a failed safeParse whose `.error` is never read
|
|
79
79
|
// costs the fast check alone.
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
//
|
|
81
|
+
// The walk is HOSTED as a named preamble function rather than a per-call
|
|
82
|
+
// closure, for two measured reasons: (1) a failed safeParse no longer
|
|
83
|
+
// allocates a closure environment + function object before the deferral
|
|
84
|
+
// even starts; (2) the safeParse body shrinks to two statements, putting
|
|
85
|
+
// it within V8's inlining budget — callers in hot loops get the
|
|
86
|
+
// success-path result object escape-analyzed away entirely, which the
|
|
87
|
+
// old shape (slow walk inlined into the body) made impossible.
|
|
88
|
+
ctx.usedHelpers.add("__zcFinD");
|
|
89
|
+
if (slowCode.includes(fnName)) {
|
|
90
|
+
// Self-recursive slow paths call the safeParse function by NAME
|
|
91
|
+
// (slowRecursiveRef) — that binding exists only inside the named
|
|
92
|
+
// function expression under the documented evaluation contract
|
|
93
|
+
// (`new Function(code + "return " + functionDef)`), so the walk stays
|
|
94
|
+
// a per-call closure for recursive schemas. Recursion is the rare
|
|
95
|
+
// shape; everything else gets the hosted walk.
|
|
96
|
+
functionDefParts.push(`if(${fastExpr}){return{success:true,data:input};}`, `return __zcFinD(function(input){`, `var _e=[];`, `var _d=input;`, slowCode, `return _e;`, `},input);`, `}`);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const walkName = `__sw_${ctx.counter++}`;
|
|
100
|
+
ctx.preamble.push(`function ${walkName}(input){var _e=[];\nvar _d=input;\n${slowCode}\nreturn _e;}`);
|
|
101
|
+
functionDefParts.push(`if(${fastExpr}){return{success:true,data:input};}`, `return __zcFinD(${walkName},input);`, `}`);
|
|
102
|
+
}
|
|
82
103
|
return {
|
|
83
|
-
code,
|
|
104
|
+
code: buildCode(),
|
|
84
105
|
functionDef: functionDefParts.join("\n"),
|
|
85
106
|
refCount: options?.refCount ?? 0,
|
|
86
107
|
usedHelpers: ctx.usedHelpers,
|
|
@@ -92,10 +113,14 @@ export function generateValidator(ir, name, options) {
|
|
|
92
113
|
// path must run eagerly — it can succeed where the fast check failed.
|
|
93
114
|
functionDefParts.push(`if(${fastExpr}){return{success:true,data:input};}`);
|
|
94
115
|
}
|
|
95
|
-
|
|
116
|
+
// Success branch inlined at the call site instead of inside __zcFin: the
|
|
117
|
+
// eager path (mutation schemas — coerce/default/trim/transform) returns
|
|
118
|
+
// here on EVERY parse, and the inline literal keeps the hot exit free of a
|
|
119
|
+
// cross-function call; __zcFin is reached only on failure.
|
|
120
|
+
functionDefParts.push(`var _e=[];`, `var _d=input;`, slowCode, `if(_e.length===0){return{success:true,data:_d};}`, `return __zcFin(_e,_d);`, `}`);
|
|
96
121
|
const functionDef = functionDefParts.join("\n");
|
|
97
122
|
return {
|
|
98
|
-
code,
|
|
123
|
+
code: buildCode(),
|
|
99
124
|
functionDef,
|
|
100
125
|
refCount: options?.refCount ?? 0,
|
|
101
126
|
usedHelpers: ctx.usedHelpers,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/codegen/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;A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