logo

TSLua Demo

https://github.com/BenJilks/TSLua

A pure TypeScript reimplementation of the Lua programming language.

This implementation is designed to be as close as possible to the original C implementation, while providing seamless TypeScript/JavaScript interoperability. All 'native' functions are implemented in TypeScript and objects are represented using standard JavaScript objects. Meaning garbage collection is left as the responsibility of the JavaScript engine.

To create a Lua runtime environment, simply instantiate the Engine object within the lua module. It's constructor accepts a Lua script, that can be executed with the run method. Additional code can be loaded using the load method.

Global state is maintained for the whole lifetime of the Engine object. Lua variables can be queried by name using the global method. define and define_table can also be used to interact with the global object. An example is shown below:

const engine = lua.Engine('a = 1 + 2')
engine.run()

const a = engine.global('a')?.number
console.log(a) // --> 3