add Lua 5.2 and 5.3 support

This commit is contained in:
Grzegorz Kowalski 2023-02-05 22:34:21 +01:00
parent 71673c8ede
commit 6cf75b0da6
15 changed files with 102 additions and 20 deletions

View File

@ -2,3 +2,5 @@
luTango is a module providing access to [Tango Control System](https://www.tango-controls.org/) with Lua.
In it's current state, a basic subset of client API is available (DeviceProxy, AttributeProxy), although not all Tango types are supported.
The library has been tested with Lua 5.1, 5.2 and 5.3.

View File

@ -34,7 +34,7 @@ void set_log_level(LogLevel level)
void lut_lua_register_log(lua_State* L)
{
lua_newtable(L);
luaL_register(L, NULL, lut_log);
va_lua_register(L, lut_log);
lua_setfield(L, -2, "log");
LUT_LOG(INFO, "Registered log table");
}

View File

@ -18,6 +18,7 @@
#include <stdarg.h>
#include <core/lua.h>
#include <core/lua/version_agnostic.h>
#define LUT_LOG(level, msg, ...) _log(level, msg, __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
@ -42,7 +43,7 @@ void set_log_level(LogLevel level);
void lut_lua_register_log(lua_State* L);
int lut_log_set_log_level(lua_State* L);
static const luaL_reg lut_log[] =
static const luaL_Reg lut_log[] =
{
{ "set_log_level", lut_log_set_log_level },
{ NULL, NULL }

View File

@ -38,7 +38,7 @@ std::vector<bool> pop_bool_table(lua_State* L, int idx)
LUT_LOG(TRACE, "Popping boolean table from stack");
std::vector<bool> v;
lua_settop(L, idx);
for(int i = 1; i <= lua_objlen(L, idx); i++)
for(int i = 1; i <= va_lua_objlen(L, idx); i++)
{
lua_rawgeti(L, idx, i);
bool vi = lua_toboolean(L, idx+1);
@ -53,7 +53,7 @@ std::vector<std::string> pop_string_table(lua_State* L, int idx)
LUT_LOG(TRACE, "Popping string table from stack");
std::vector<std::string> v;
lua_settop(L, idx);
for(int i = 1; i <= lua_objlen(L, idx); i++)
for(int i = 1; i <= va_lua_objlen(L, idx); i++)
{
lua_rawgeti(L, idx, i);
std::string vi = luaL_checkstring(L, idx+1);

View File

@ -19,6 +19,7 @@
#include <vector>
#include <core/lua.h>
#include <core/lua/version_agnostic.h>
#include <core/logging/log.h>
void push_bool_table(lua_State* L, std::vector<bool> value);
@ -52,7 +53,7 @@ std::vector<T> pop_number_table(lua_State* L, int idx)
LUT_LOG(TRACE, "Popping number table from stack");
std::vector<T> v;
lua_settop(L, idx);
for(int i = 1; i <= lua_objlen(L, idx); i++)
for(int i = 1; i <= va_lua_objlen(L, idx); i++)
{
lua_rawgeti(L, idx, i);
T vi = luaL_checknumber(L, idx+1);

View File

@ -0,0 +1,25 @@
//-----------------------------------------------------------------------------
//
// luTango - Lua binding for Tango
//
// Copyright (C) 2023 Grzegorz Kowalski
// See LICENSE for legal information
//
// file: version_agnostic.cpp
//
// Facilitate different versions of Lua
//
//-----------------------------------------------------------------------------
#include "version_agnostic.h"
void va_lua_register(lua_State* L, const luaL_Reg* reg)
{
# if (LUA_VERSION_NUM > 501)
// Lua > 5.1
luaL_setfuncs(L, reg, 0);
# else
// Lua 5.1
luaL_register(L, NULL, reg);
# endif
}

View File

@ -0,0 +1,29 @@
//-----------------------------------------------------------------------------
//
// luTango - Lua binding for Tango
//
// Copyright (C) 2023 Grzegorz Kowalski
// See LICENSE for legal information
//
// file: version_agnostic.h
//
// Facilitate different versions of Lua
//
//-----------------------------------------------------------------------------
#ifndef __VERSION_AGNOSTIC_H__
# define __VERSION_AGNOSTIC_H__
#include <core/lua.h>
#if (LUA_VERSION_NUM > 501)
// Lua > 5.1
# define va_lua_objlen lua_rawlen
#else
// Lua 5.1
# define va_lua_objlen lua_objlen
#endif
void va_lua_register(lua_State* L, const luaL_Reg* reg);
#endif /* __VERSION_AGNOSTIC_H__ */

View File

@ -16,7 +16,7 @@
void lut_lua_register_sys(lua_State* L)
{
lua_newtable(L);
luaL_register(L, NULL, lut_sys);
va_lua_register(L, lut_sys);
lua_setfield(L, -2, "sys");
LUT_LOG(INFO, "Registered sys table");
}

View File

@ -17,6 +17,7 @@
#include <core/lua.h>
#include <core/tango.h>
#include <core/logging/log.h>
#include <core/lua/version_agnostic.h>
// Lua API
void lut_lua_register_sys(lua_State* L);
@ -24,7 +25,7 @@ int lut_sys_version(lua_State* L);
int lut_sys_tango_version(lua_State* L);
int lut_sys_cpp_version(lua_State* L);
static const luaL_reg lut_sys[] =
static const luaL_Reg lut_sys[] =
{
{ "version", lut_sys_version },
{ "tango_version", lut_sys_tango_version },

View File

@ -76,7 +76,7 @@ void lut_lua_register_AttributeProxy(lua_State* L)
{
// create table, register funcs and add it as AttributeProxy to lutango's table
lua_newtable(L);
luaL_register(L, NULL, lut_AttributeProxy);
va_lua_register(L, lut_AttributeProxy);
lua_setfield(L, -2, "AttributeProxy");
LUT_LOG(INFO, "Registered AttributeProxy table");
@ -101,7 +101,7 @@ int lut_AttributeProxy_create(lua_State* L)
// set metatable, that allows userdata proper garbage collection
luaL_newmetatable(L, LUT_ATTRIBUTEPROXY);
luaL_register(L, NULL, lut_AttributeProxy_userdata);
va_lua_register(L, lut_AttributeProxy_userdata);
lua_setmetatable(L, -2);
return 1;

View File

@ -22,6 +22,7 @@
#include <core/utils.h>
#include <core/lua/object.h>
#include <core/lua/version_agnostic.h>
#include <core/logging/log.h>
#include <core/tango/attrdata.h>
@ -53,7 +54,7 @@ int lut_AttributeProxy_create(lua_State* L);
int lut_AttributeProxy_destroy(lua_State* L);
int lut_AttributeProxy_call(lua_State* L);
static const luaL_reg lut_AttributeProxy[] =
static const luaL_Reg lut_AttributeProxy[] =
{
// Lua API
{ "create", lut_AttributeProxy_create },
@ -66,7 +67,7 @@ static const luaL_reg lut_AttributeProxy[] =
{ NULL, NULL }
};
static const luaL_reg lut_AttributeProxy_userdata[] =
static const luaL_Reg lut_AttributeProxy_userdata[] =
{
{ "__gc", lut_AttributeProxy_destroy },
{ NULL, NULL }

View File

@ -130,7 +130,7 @@ void lut_lua_register_DeviceProxy(lua_State* L)
{
// create table, register funcs and add it as DeviceProxy to lutango's table
lua_newtable(L);
luaL_register(L, NULL, lut_DeviceProxy);
va_lua_register(L, lut_DeviceProxy);
lua_setfield(L, -2, "DeviceProxy");
LUT_LOG(INFO, "Registered DeviceProxy table");
@ -155,7 +155,7 @@ int lut_DeviceProxy_create(lua_State* L)
// set metatable, that allows userdata proper garbage collection
luaL_newmetatable(L, LUT_DEVICEPROXY);
luaL_register(L, NULL, lut_DeviceProxy_userdata);
va_lua_register(L, lut_DeviceProxy_userdata);
lua_setmetatable(L, -2);
return 1;

View File

@ -22,6 +22,7 @@
#include <core/utils.h>
#include <core/lua/object.h>
#include <core/lua/version_agnostic.h>
#include <core/logging/log.h>
#include <core/tango/attrdata.h>
@ -56,7 +57,7 @@ int lut_DeviceProxy_destroy(lua_State* L);
int lut_DeviceProxy_index(lua_State* L);
int lut_DeviceProxy_newindex(lua_State* L);
static const luaL_reg lut_DeviceProxy[] =
static const luaL_Reg lut_DeviceProxy[] =
{
// Lua API
{ "create", lut_DeviceProxy_create },
@ -71,7 +72,7 @@ static const luaL_reg lut_DeviceProxy[] =
{ NULL, NULL }
};
static const luaL_reg lut_DeviceProxy_userdata[] =
static const luaL_Reg lut_DeviceProxy_userdata[] =
{
{ "__gc", lut_DeviceProxy_destroy },
{ NULL, NULL }

View File

@ -11,6 +11,8 @@
--
-------------------------------------------------------------------------------
require "lutango.version_agnostic"
local function default_handler(e)
io.stderr:write("\n=================================== EXCEPTION ===================================\n")
if e.is_DevFailed then
@ -28,11 +30,9 @@ local function default_handler(e)
io.stderr:write("\n=================================================================================\n\n")
end
local function try(...)
local f = arg[1]
table.remove(arg, 1)
local ok, ret = xpcall(function() return f(unpack(arg)) end, default_handler)
local function try(f, ...)
local f_arg = {...}
local ok, ret = xpcall(function() return f(va_unpack(f_arg)) end, default_handler)
return ret
end

21
src/version_agnostic.lua Normal file
View File

@ -0,0 +1,21 @@
-------------------------------------------------------------------------------
--
-- luTango - Lua binding for Tango
--
-- Copyright (C) 2023 Grzegorz Kowalski
-- See LICENSE for legal information
--
-- file: version_agnostic.lua
--
-- Facilitate different versions of Lua
-- WARNING! This module sets globals!
--
-------------------------------------------------------------------------------
local version = tonumber(_VERSION:match("%d.%d"))
if version > 5.1 then
va_unpack = table.unpack
else
va_unpack = unpack
end