Skip to content

v2.8.0

Latest

Choose a tag to compare

@leafo leafo released this 10 Oct 04:50
v2.8.0

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 _transform on the original object
  • codegen.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 diffed
  • codegen.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 = true to run the interpreted type on failure to get the exact message
  • types.proxy is resolved once at compile time, and types.one_of only 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_transform and _compile_inner hooks. 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 pairs order. 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.shape after 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 to types.number + types.string / tonumber * types.number
  • types.equivalent exposes the comparison as values_equivalent on the class

Extensions

  • tableshape.ext.json_schema: to_json_schema has been rewritten as a plain recursive walk instead of pattern matching with tableshape types. Conversion is significantly faster. Empty shapes and partials now convert correctly
  • tableshape.ext.with_args: fixed a tagged type that transforms to nil being 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