Add Lua 5.2 compatibility.

This commit is contained in:
topkecleon
2016-09-06 20:34:57 -04:00
parent fa3e6d449f
commit 1fd125aa2c
6 changed files with 51 additions and 34 deletions

View File

@@ -17,6 +17,12 @@ function luarun:init(config)
return JSON.encode(t, {indent=true})
end
end
-- Lua 5.2 compatibility.
-- "loadstring" was renamed "load" in 5.3.
luarun.loadstring = load or loadstring
luarun.err_msg = function(x)
return 'Error:\n' .. tostring(x)
end
end
function luarun:action(msg, config)
@@ -35,8 +41,8 @@ function luarun:action(msg, config)
input = 'return ' .. input
end
local output, success =
load("local bot = require('otouto.bot')\n\z
local output, success = luarun.loadstring(
"local bot = require('otouto.bot')\n\z
local bindings = require('otouto.bindings')\n\z
local utilities = require('otouto.utilities')\n\z
local drua = require('otouto.drua-tg')\n\z
@@ -44,16 +50,13 @@ function luarun:action(msg, config)
local URL = require('socket.url')\n\z
local HTTP = require('socket.http')\n\z
local HTTPS = require('ssl.https')\n\z
return function (self, msg, config)\n" .. input .. "\nend")
local function err_msg(x)
return "Error:\n" .. tostring(x)
end
return function (self, msg, config)\n" .. input .. "\nend"
)
if output == nil then
output = success
else
success, output = xpcall(output(), err_msg, self, msg, config)
success, output = xpcall(output(), luarun.err_msg, self, msg, config)
end
if output == nil then

View File

@@ -26,6 +26,9 @@ local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('dkjson')
local bindings = require('otouto.bindings')
-- Lua 5.2 compatibility.
-- If no built-in utf8 is available, load the library.
local utf8 = utf8 or require('lua-utf8')
-- For the sake of ease to new contributors and familiarity to old contributors,
-- we'll provide a couple of aliases to real bindings here.
@@ -324,8 +327,7 @@ end
-- Converts a gross string back into proper UTF-8.
-- Useful for fixing improper encoding caused by bad JSON escaping.
function utilities.fix_utf8(str)
return string.char(utf8.codepoint(str, 1, -1))
return string.char(utf8.codepoint(str, 1, -1))
end
return utilities