NodeJS module to expose Lua bindings to JavaScript.
npm install luajs
const luajs = require('luajs');
let lua = new luajs.LuaState();
If you want to have multiple LuaState
instances simultaneously, you need to pass a name to each state:
new luajs.LuaState('name');
.
Using Promises:
lua.doString('return 5/2;').then(result => {
console.log(`Result: ${result}`);
}).catch(error => {
// Handle error
});
lua.doFile('./script.lua').then(result => {
console.log(`Result: ${result}`);
}).catch(error => {
// Handle error
});
lua.setGlobal('name', 'Lukas');
let me = lua.getGlobal('name');
luajs.LuaState#doString
and luajs.LuaState#doFile
also have a syncronous API:
let result1 = lua.doStringSync('return 5/2;');
let result2 = lua.doFileSync('./script.lua');
(This will throw if lua encounters an error running the code)
You should always close a LuaState
instance once you're done using it:
// Close state
lua.close();
// Reset state (this is equal to closing and re-initializing)
lua.reset();