hn.today

Lua for Data Representation

citrons.xyz103 points56 comments
Screenshot of Lua for Data Representation

Lua’s suitability as a data and configuration format follows from the same simplicity and flexibility that make it effective as an embedded language. Its parser can handle very large source files, and its long-string syntax can represent text or Lua code neatly without cumbersome escaping. Three styles make use of these strengths: returning a table resembles JSON; assigning globals in a controlled environment resembles INI; and function-call syntax supports an imperative, domain-specific configuration language. The table-returning approach is convenient, but it is still executable code: function values can run arbitrary actions, so it should not be treated as safe just because it looks like data.

Executing untrusted Lua is dangerous, and an empty environment - at most exposing data constructors - can prevent access to I/O and the host program’s state. Removing standard-library functions is safest; execution hooks can help limit infinite loops, though excessive memory use remains difficult to prevent. Bytecode must be rejected by loading text mode only. Despite these risks and the cost of adding an interpreter, Lua is useful when a project already uses it or needs configuration more expressive than JSON or INI. It can support custom types and grow into a purpose-built language, an approach that places Lua alongside Lisp and Forth in the tradition of creating small languages for particular needs.

Read on citrons.xyz56 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.