Compiles a schema to a function of straight-line code: compile_guard to
what is answers, and compile_parse to what parse returns. And
to_guard_source writes the guard as the text of a module, for a build to
write to a file.
The compiler reads a schema's kind and def, emits JavaScript source, and
makes a function of it with new Function. The interpreter is the oracle:
a compiled guard gives is's verdict, and a compiled parse gives parse's
result, the same ok, an output that is deep-equal and is the input itself
exactly where the interpreter's is, and on failure the interpreter's issues.
- Issues are never compiled. Compiled code builds no issue. A compiled
parse of an input that fails calls parse, so its issues are the
library's own.
- A kind the compiler has no code for runs in place. A schema of a kind
defined outside the library, or of a library kind whose
def is not what
the kind holds, is run by its own run for that subtree, handed the
object and the key as the library hands them, and the code around it is
compiled. So is, in a compiled parse, a keyed_record, an
object_with_rest, a union that may try an option again without its
key, and a memo_union that can change a value. - A schema is read, never written to, and nothing is kept on one: the
caller holds the function a compile returns. A lazy's function is called
once, at the compile.
- Where
new Function is refused, as under a content security policy
without unsafe-eval, the functions returned run the interpreter. - Where compiled code runs out of stack, the interpreter answers, or
throws its own
RangeError where it runs out too. A schema that holds a
lazy, a memo_union, or a schema of a kind the compiler has no code
for catches a RangeError at its root, any RangeError, and calls is
or parse. Another schema has no catch.
What is refused. A failed parse is the library's walk and so is every
subtree run in place, so compiled code can't bound what the library doesn't,
and the compiler throws on a schema the library can walk in time
exponential in the depth of an input, naming where it is:
- a union on a cycle, two or more options of which reach a cycle and
could take the same array, or the same object unless a literal entry at
one key, with a value of its own in each of those options, comes before
every entry that reaches a cycle. It is the rule by which the Rust crate
keeps a union's verdicts, asked of a union that is on a cycle. What to
write instead: a memo_union, or where each option is an object schema,
a variant or those literal entries first.
- a pipe on a cycle, two or more steps of which reach a cycle. What to
write instead: the pipe as the one option of a memo_union.
A cycle is one only a lazy closes, and one a memo_union is on is none,
as it is asked of a value once and then read back. So a memo_union, which
is_memo_union tells, is never refused, and compiled code keeps its
verdicts for the length of a call, as the library's does while it probes.
The rule refuses some unions the library walks in good time, whose options
differ below their first level. What it can't see is not refused, and is
not bounded: a kind defined outside the library, a function a check or a
transform calls, and a union given a memo_union's own options array,
which is told as one though its own run keeps nothing.
The bound. What compiles is never exponential in the depth of an input
that is a tree, in a guard, a parse that matches, or a parse that fails:
the bound is stated in docs/design.md, under Compiling, with what it is
not of.
What may differ from the interpreter, each said at more length in
https://github.com/fuzdev/fuz_schema/blob/main/docs/design.md, which is
where they are listed:
- How often a user's function runs. A compiled parse of an input that
fails has run each check, transform, and with_default_fn it reached
before the failure, and parse runs them again, as it does for a call
that ran out of stack. A
RangeError one throws under a schema that has
the catch is taken for the stack's, and the interpreter runs the
function again. A compiled memo_union keeps its verdicts from
its first value, where the library's keeps them only while it probes, so
a check under one can run fewer times. - What is read of the input. A parse that matches first reads each
value of the input in the library's order, an object's entries in the
schema's and a union's options in theirs. It reads less often: a copy is
filled from the values its entries ran on, and whether a key is present
is asked only where the answer can change the result, after the entries.
A guard makes no copy, so it reads nothing only a copy reads. A parse that
fails reads the input again, in parse. A getter or a
Proxy can tell. - A schema that takes a library kind's name and a
run of its own is
compiled as the library's kind, by its kind and def alone, but a
uuid, an iso_datetime, and a to_integer, which are run by the
schema's run. - The depth of input at which the stack runs out. Compiled code can
answer an input so deep that the interpreter throws a
RangeError, and
never the reverse, but a guard to_guard_source writes, which has no
interpreter to ask, can throw where the interpreter answers.
It is a module of its own, so a bundle that compiles no schema carries none
of it.