Lua for Data Representation citrons.xyz · 19 points · 2 comments · HN thread TL;...

@best

Lua for Data Representation citrons.xyz · 19 points · 2 comments · HN thread

TL;DR The author argues Lua is a strong general-purpose language whose embedding-oriented design makes it well suited to data and configuration formats. The post covers Lua syntax niceties (long strings with variable equal signs), three styles of Lua-based data representation (return-a-table, environment-captured globals, and imperative function-call DSLs), and how to sandbox untrusted input by controlling the environment, limiting denial-of-service via debug.sethook, and rejecting bytecode with load mode "t".

  • Lua's original design goal was extending and embedding into existing programs, which the author believes also makes it a good general-purpose language.
  • Lua's multiline strings can use any number of equal signs (e.g. [===[ ... ]===]), and a short function can generate a safe delimiter for arbitrary text.
  • Three data styles are shown: a chunk returning a table (JSON-like), a chunk run with an empty environment whose globals are captured (INI-like), and imperative calls without parentheses (e.g. config { ... }).
  • For untrusted data, the author advises against this approach, but suggests an empty or data-constructor-only environment, avoiding the standard library, and using debug.sethook against infinite loops.
  • Bytecode must be rejected for untrusted input, since load accepts it by default unless the mode is "t".
  • Using a full interpreter is overkill for simple data, but worthwhile when Lua is already in a project or when extensible custom syntax is needed.

Discussion The thread is small, with only two top-level comments, and both are constructive and appreciative. They focus on practical uses of the technique rather than debating the article's claims. One commenter points to CorsixTH, a game that uses Lua for its config, translation and update-check files. The other shows how a bare table following a verb reads like a constructor, giving a DSL-like syntax for building structured data. The mood is positive and technical.

  • Lua-as-config is practical and already used in real projects (some): A commenter cites CorsixTH, which uses Lua for its config format, translation files and update check, as evidence the approach works in production. They note it is not necessarily a good thing ("for better or worse").
  • Verb-plus-table syntax enables type-like DSLs (a few): A commenter notes that Lua's implicit call syntax (a name followed by a table) lets you write expressions like Dog { ... } that read as typed constructors, useful for building structured data declaratively.

Links from the comments

#lua #configuration #dataFormats #sandboxing #embedding