mudgangster

Log | Files | Refs

commit 6dac021378d58d51872f0f0b95d47fd3531e98d3
parent 2a40f89ea041333f186cd85ba429096ba7467f48
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon,  3 Sep 2018 17:33:15 +0300

Update to Lua 5.3, still supports Lua 5.1

Diffstat:
main.lua | 2++
script.lua | 34+++++++++++++++++++---------------
src/script.cc | 20+++++++++++---------
utils.lua | 2+-
4 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/main.lua b/main.lua @@ -2,6 +2,8 @@ mud = { connected = false, } +table.unpack = table.unpack or unpack + require( "utils" ) socket = require( "socket" ) diff --git a/script.lua b/script.lua @@ -26,14 +26,6 @@ local function loadScript( name, path, padding ) return end - local script, err = loadfile( mainPath ) - - if not script then - throw( err ) - - return - end - local env = setmetatable( { require = function( requirePath, ... ) @@ -51,17 +43,19 @@ local function loadScript( name, path, padding ) file:close() end ) - local settings = loadfile( settingsPath ) + local settingsEnv = setmetatable( { }, { + __newindex = defaults, + } ) + + local settings = loadfile( settingsPath, "t", settingsEnv ) if not settings then return defaults end - local settingsEnv = setmetatable( { }, { - __newindex = defaults, - } ) - - setfenv( settings, settingsEnv ) + if _VERSION == "Lua 5.1" then + setfenv( settings, settingsEnv ) + end pcall( settings ) @@ -74,7 +68,17 @@ local function loadScript( name, path, padding ) } ) - setfenv( script, env ) + local script, err = loadfile( mainPath, "t", env ) + + if not script then + throw( err ) + + return + end + + if _VERSION == "Lua 5.1" then + setfenv( script, env ) + end local loaded, err = pcall( script ) diff --git a/src/script.cc b/src/script.cc @@ -8,6 +8,10 @@ #include "common.h" #include "ui.h" +#if LUA_VERSION_NUM < 502 +#define luaL_len lua_objlen +#endif + static lua_State * lua; static int inputHandlerIdx = LUA_NOREF; @@ -43,7 +47,7 @@ void script_handleClose() { lua_call( lua, 0, 0 ); } -// namespace { +namespace { extern "C" int mud_handleXEvents( lua_State * ) { ui_handleXEvents(); @@ -52,7 +56,7 @@ extern "C" int mud_handleXEvents( lua_State * ) { static void generic_print( TextBox * tb, lua_State * L ) { const char* str = luaL_checkstring( L, 1 ); - size_t len = lua_objlen( L, 1 ); + size_t len = luaL_len( L, 1 ); Colour fg = Colour( luaL_checkinteger( L, 2 ) ); Colour bg = Colour( luaL_checkinteger( L, 3 ) ); @@ -94,7 +98,7 @@ extern "C" int mud_drawChat( lua_State * L ) { extern "C" int mud_setStatus( lua_State * L ) { luaL_argcheck( L, lua_type( L, 1 ) == LUA_TTABLE, 1, "expected function" ); - size_t len = lua_objlen( L, 1 ); + size_t len = luaL_len( L, 1 ); ui_statusClear(); @@ -151,20 +155,19 @@ extern "C" int mud_urgent( lua_State * L ) { return 0; } -// } // anon namespace +} // anon namespace void script_init() { mud_handleXEvents( NULL ); // TODO: why is this here? - lua = lua_open(); + lua = luaL_newstate(); luaL_openlibs( lua ); lua_getglobal( lua, "debug" ); lua_getfield( lua, -1, "traceback" ); lua_remove( lua, -2 ); - if( luaL_loadfile( lua, "main.lua" ) ) - { + if( luaL_loadfile( lua, "main.lua" ) ) { printf( "Error reading main.lua: %s\n", lua_tostring( lua, -1 ) ); exit( 1 ); @@ -187,8 +190,7 @@ void script_init() { lua_pushcfunction( lua, mud_setStatus ); - if( lua_pcall( lua, 11, 0, -13 ) ) - { + if( lua_pcall( lua, 11, 0, -13 ) ) { printf( "Error running main.lua: %s\n", lua_tostring( lua, -1 ) ); exit( 1 ); diff --git a/utils.lua b/utils.lua @@ -1,6 +1,6 @@ getmetatable( "" ).__mod = function( self, form ) if type( form ) == "table" then - return self:format( unpack( form ) ) + return self:format( table.unpack( form ) ) end return self:format( form )