Repository navigation
Changes
This release adds tableshape.codegen, which compiles a type into a single generated Lua function. A plain shape runs 5-10x faster compiled, a shape with tags or transforms around 3-4x. Shapes also now check their fields in a sorted order, so transforms and tags that thread state behave the same across Lua versions.
New: tableshape.codegen
codegen.compile(type, opts)returns a compiled type that works like any other type checker (check_value,transform, operators, etc.) but runs the generated code. Any type can be compiled, types the compiler doesn't know about fall back to calling_transformon the original objectcodegen.generate_module(type)returns the source of a standalone Lua module for a type with no dependencies, not even tableshape. Output is deterministic so it can be checked in and diffedcodegen.generate_code(type, opts)returns the raw chunk source and the list of runtime references it needs, useful for inspecting the output- Compiled types don't generate error messages, a failure returns a generic message built from the type's description. Pass
rerun_errors = trueto run the interpreted type on failure to get the exact message types.proxyis resolved once at compile time, andtypes.one_ofonly runs its type checker options against the value rather than also comparing by identity- Custom types can opt into optimized compilation through
_compile_pure,_compile_predicate,_compile_transformand_compile_innerhooks. The MoonScript type checkers implement these. This interface is experimental - Generated code works around the Lua 5.1 limits on locals and upvalues, so very large shapes compile fine
See the README section on compiling types for details.
Core changes
- Shapes check and transform their fields in a sorted order instead of
pairsorder. Previously a shape whose fields used transforms with state or tags could give different results depending on the Lua version. Error messages that list keys (_describe, extra fields) are also sorted now - Shapes snapshot their field list on construction. Mutating the table passed to
types.shapeafter construction is not supported type:on_repair(t)now accepts a type in addition to a function.types.number:on_repair(types.string / tonumber)is equivalent totypes.number + types.string / tonumber * types.numbertypes.equivalentexposes the comparison asvalues_equivalenton the class
Extensions
tableshape.ext.json_schema:to_json_schemahas been rewritten as a plain recursive walk instead of pattern matching with tableshape types. Conversion is significantly faster. Empty shapes and partials now convert correctlytableshape.ext.with_args: fixed a tagged type that transforms tonilbeing reported as a failure, and fixed transforms that produce a value for an omitted trailing argument (eg.types.nil / "default") not reaching the function
Full Changelog: v2.7.0...v2.8.0