lua: added support for testing object type
Dieser Commit ist enthalten in:
Ursprung
d78b217b7f
Commit
b15cc45286
222
server/src/lua/class.cpp
Normale Datei
222
server/src/lua/class.cpp
Normale Datei
@ -0,0 +1,222 @@
|
||||
/**
|
||||
* server/src/lua/classid.cpp
|
||||
*
|
||||
* This file is part of the traintastic source code.
|
||||
*
|
||||
* Copyright (C) 2021 Reinder Feenstra
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "class.hpp"
|
||||
#include "push.hpp"
|
||||
#include "object.hpp"
|
||||
|
||||
#include "../board/board.hpp"
|
||||
#include "../board/boardlist.hpp"
|
||||
|
||||
#include "../board/tile/rail/sensorrailtile.hpp"
|
||||
#include "../board/tile/rail/straightrailtile.hpp"
|
||||
#include "../board/tile/rail/cross45railtile.hpp"
|
||||
#include "../board/tile/rail/tunnelrailtile.hpp"
|
||||
#include "../board/tile/rail/blockrailtile.hpp"
|
||||
#include "../board/tile/rail/curve45railtile.hpp"
|
||||
#include "../board/tile/rail/bridge45rightrailtile.hpp"
|
||||
#include "../board/tile/rail/cross90railtile.hpp"
|
||||
#include "../board/tile/rail/bridge45leftrailtile.hpp"
|
||||
#include "../board/tile/rail/bufferstoprailtile.hpp"
|
||||
#include "../board/tile/rail/curve90railtile.hpp"
|
||||
#include "../board/tile/rail/bridge90railtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutsinglesliprailtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutleft90railtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutright45railtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutleft45railtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutright90railtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutdoublesliprailtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutwyerailtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutleftcurvedrailtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnout3wayrailtile.hpp"
|
||||
#include "../board/tile/rail/turnout/turnoutrightcurvedrailtile.hpp"
|
||||
#include "../board/tile/rail/signal/signal2aspectrailtile.hpp"
|
||||
#include "../board/tile/rail/signal/signal3aspectrailtile.hpp"
|
||||
|
||||
#include "../clock/clock.hpp"
|
||||
|
||||
#include "../hardware/controller/wlanmaus.hpp"
|
||||
#include "../hardware/controller/controllerlist.hpp"
|
||||
#ifdef USB_XPRESSNET
|
||||
#include "../hardware/controller/usbxpressnetcontroller.hpp"
|
||||
#endif
|
||||
|
||||
#include "../hardware/commandstation/rocoz21.hpp"
|
||||
#include "../hardware/commandstation/virtualcommandstation.hpp"
|
||||
#include "../hardware/commandstation/loconettcpbinary.hpp"
|
||||
#ifdef USB_XPRESSNET
|
||||
#include "../hardware/commandstation/usbxpressnetinterface.hpp"
|
||||
#endif
|
||||
#include "../hardware/commandstation/xpressnetserial.hpp"
|
||||
#include "../hardware/commandstation/dccplusplusserial.hpp"
|
||||
#include "../hardware/commandstation/commandstationlist.hpp"
|
||||
#include "../hardware/commandstation/loconetserial.hpp"
|
||||
|
||||
#include "../hardware/protocol/loconet/loconetlist.hpp"
|
||||
#include "../hardware/protocol/loconet/loconet.hpp"
|
||||
#include "../hardware/protocol/loconet/loconetlisttablemodel.hpp"
|
||||
#include "../hardware/protocol/xpressnet/xpressnetlist.hpp"
|
||||
#include "../hardware/protocol/xpressnet/xpressnetlisttablemodel.hpp"
|
||||
#include "../hardware/protocol/xpressnet/xpressnet.hpp"
|
||||
#include "../hardware/protocol/dccplusplus/dccplusplus.hpp"
|
||||
|
||||
#include "../hardware/decoder/decoderfunction.hpp"
|
||||
#include "../hardware/decoder/decoderlist.hpp"
|
||||
#include "../hardware/decoder/decoder.hpp"
|
||||
#include "../hardware/decoder/decoderfunctions.hpp"
|
||||
#include "../hardware/decoder/decoderlisttablemodel.hpp"
|
||||
|
||||
#include "../hardware/input/loconetinput.hpp"
|
||||
#include "../hardware/input/monitor/xpressnetinputmonitor.hpp"
|
||||
#include "../hardware/input/monitor/loconetinputmonitor.hpp"
|
||||
#include "../hardware/input/xpressnetinput.hpp"
|
||||
#include "../hardware/input/list/inputlist.hpp"
|
||||
#include "../hardware/input/list/inputlisttablemodel.hpp"
|
||||
#include "../hardware/input/map/blockinputmap.hpp"
|
||||
#include "../hardware/input/map/blockinputmapitem.hpp"
|
||||
|
||||
#include "../hardware/output/list/outputlist.hpp"
|
||||
#include "../hardware/output/list/outputlisttablemodel.hpp"
|
||||
#include "../hardware/output/keyboard/loconetoutputkeyboard.hpp"
|
||||
#include "../hardware/output/map/outputmapoutputaction.hpp"
|
||||
#include "../hardware/output/map/signaloutputmap.hpp"
|
||||
#include "../hardware/output/map/turnoutoutputmap.hpp"
|
||||
#include "../hardware/output/map/turnoutoutputmapitem.hpp"
|
||||
#include "../hardware/output/map/signaloutputmapitem.hpp"
|
||||
#include "../hardware/output/loconetoutput.hpp"
|
||||
|
||||
#include "../vehicle/rail/railvehiclelist.hpp"
|
||||
#include "../vehicle/rail/locomotive.hpp"
|
||||
#include "../vehicle/rail/freightcar.hpp"
|
||||
|
||||
#include "../train/train.hpp"
|
||||
#include "../train/trainlist.hpp"
|
||||
|
||||
#include "../world/world.hpp"
|
||||
|
||||
namespace Lua {
|
||||
|
||||
static const char* metaTableName = "class";
|
||||
|
||||
using IsInstance = bool(*)(const ::ObjectPtr&);
|
||||
|
||||
template<class T>
|
||||
static bool isInstance(const ::ObjectPtr& object)
|
||||
{
|
||||
return dynamic_cast<T*>(object.get());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline static void setField(lua_State* L, std::string_view key)
|
||||
{
|
||||
static_assert(std::is_base_of_v<::Object, T>);
|
||||
push(L, key);
|
||||
*reinterpret_cast<IsInstance*>(lua_newuserdata(L, sizeof(IsInstance))) = isInstance<T>;
|
||||
luaL_newmetatable(L, metaTableName);
|
||||
lua_setmetatable(L, -2);
|
||||
lua_settable(L, -3);
|
||||
}
|
||||
|
||||
void Class::registerValues(lua_State* L)
|
||||
{
|
||||
assert(lua_istable(L, -1));
|
||||
|
||||
setField<Board>(L, "BOARD");
|
||||
setField<BoardList>(L, "BOARD_LIST");
|
||||
|
||||
setField<StraightRailTile>(L, "STRAIGHT_RAIL_TILE");
|
||||
setField<TunnelRailTile>(L, "TUNNEL_RAIL_TILE");
|
||||
setField<BufferStopRailTile>(L, "BUFFER_STOP_RAIL_TILE");
|
||||
setField<Curve45RailTile>(L, "CURVE_45_RAIL_TILE");
|
||||
setField<Curve90RailTile>(L, "CURVE_90_RAIL_TILE");
|
||||
setField<Cross45RailTile>(L, "CROSS_45_RAIL_TILE");
|
||||
setField<Cross90RailTile>(L, "CROSS_90_RAIL_TILE");
|
||||
setField<Bridge45RightRailTile>(L, "BRIDGE_45_RIGHT_RAIL_TILE");
|
||||
setField<Bridge45LeftRailTile>(L, "BRIDGE_45_LEFT_RAIL_TILE");
|
||||
setField<Bridge90RailTile>(L, "BRIDGE_90_RAIL_TILE");
|
||||
setField<TurnoutSingleSlipRailTile>(L, "TURNOUT_SINGLE_SLIP_RAIL_TILE");
|
||||
setField<TurnoutLeft90RailTile>(L, "TURNOUT_LEFT_90_RAIL_TILE");
|
||||
setField<TurnoutRight45RailTile>(L, "TURNOUT_RIGHT_45_RAIL_TILE");
|
||||
setField<TurnoutLeft45RailTile>(L, "TURNOUT_LEFT_45_RAIL_TILE");
|
||||
setField<TurnoutRight90RailTile>(L, "TURNOUT_RIGHT_90_RAIL_TILE");
|
||||
setField<TurnoutDoubleSlipRailTile>(L, "TURNOUT_DOUBLE_SLIP_RAIL_TILE");
|
||||
setField<TurnoutWyeRailTile>(L, "TURNOUT_WYE_RAIL_TILE");
|
||||
setField<TurnoutLeftCurvedRailTile>(L, "TURNOUT__RAIL_TILE");
|
||||
setField<Turnout3WayRailTile>(L, "TURNOUT_3WAY_RAIL_TILE");
|
||||
setField<TurnoutRightCurvedRailTile>(L, "TURNOUT_RIGHT_CURVED_RAIL_TILE");
|
||||
setField<Signal2AspectRailTile>(L, "SIGNAL_2_ASPECT_RAIL_TILE");
|
||||
setField<Signal3AspectRailTile>(L, "SIGNAL_3_ASPECT_RAIL_TILE");
|
||||
setField<SensorRailTile>(L, "SENSOR_RAIL_TILE");
|
||||
setField<BlockRailTile>(L, "BLOCK_RAIL_TILE");
|
||||
|
||||
setField<Clock>(L, "CLOCK");
|
||||
|
||||
setField<WLANmaus>(L, "WLANMAUS_CONTROLLER");
|
||||
setField<ControllerList>(L, "CONTROLLER_LIST");
|
||||
#ifdef USB_XPRESSNET
|
||||
setField<USBXpressNetController>(L, "USB_XPRESSNET_CONTROLLER");
|
||||
#endif
|
||||
|
||||
setField<RocoZ21>(L, "Z21_COMMAND_STATION");
|
||||
setField<VirtualCommandStation>(L, "VIRTUAL_COMMAND_STATION");
|
||||
setField<LocoNetTCPBinary>(L, "LOCONET_TCP_BINARY_COMMAND_STATION");
|
||||
#ifdef USB_XPRESSNET
|
||||
setField<USBXpressNetInterface>(L, "USB_XPRESSNET_INTERFACE");
|
||||
#endif
|
||||
setField<XpressNetSerial>(L, "XPRESSNET_SERIAL_COMMAND_STATION");
|
||||
setField<DCCPlusPlusSerial>(L, "DCCPLUSPLUS_SERIAL_COMMAND_STATION");
|
||||
setField<CommandStationList>(L, "COMMAND_STATION_LIST");
|
||||
setField<LocoNetSerial>(L, "LOCONET_SERIAL_COMMAND_STATION");
|
||||
|
||||
setField<DecoderFunction>(L, "DECODER_FUNCTION");
|
||||
setField<DecoderList>(L, "DECODER_LIST");
|
||||
setField<Decoder>(L, "DECODER");
|
||||
setField<DecoderFunctions>(L, "DECODER_FUNCTIONS");
|
||||
|
||||
setField<LocoNetInput>(L, "LOCONET_INPUT");
|
||||
setField<XpressNetInput>(L, "XPRESSNET_INPUT");
|
||||
setField<InputList>(L, "INPUT_LIST");
|
||||
|
||||
setField<OutputList>(L, "OUTPUT_LIST");
|
||||
setField<LocoNetOutput>(L, "LOCONET_OUTPUT");
|
||||
|
||||
setField<RailVehicleList>(L, "RAIL_VEHICLE_LIST");
|
||||
setField<Locomotive>(L, "LOCOMOTIVE");
|
||||
setField<FreightCar>(L, "FREIGHT_CAR");
|
||||
|
||||
setField<Train>(L, "TRAIN");
|
||||
setField<TrainList>(L, "TRAIN_LIST");
|
||||
|
||||
setField<World>(L, "WORLD");
|
||||
}
|
||||
|
||||
int Class::isInstance(lua_State* L)
|
||||
{
|
||||
if(lua_gettop(L) != 2)
|
||||
errorExpectedNArgumentsGotN(L, 2, lua_gettop(L));
|
||||
|
||||
push(L, (*reinterpret_cast<IsInstance*>(luaL_checkudata(L, 2, metaTableName)))(Object::test(L, 1)));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
40
server/src/lua/class.hpp
Normale Datei
40
server/src/lua/class.hpp
Normale Datei
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* server/src/lua/classid.hpp
|
||||
*
|
||||
* This file is part of the traintastic source code.
|
||||
*
|
||||
* Copyright (C) 2021 Reinder Feenstra
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef TRAINTASTIC_SERVER_LUA_CLASS_HPP
|
||||
#define TRAINTASTIC_SERVER_LUA_CLASS_HPP
|
||||
|
||||
#include <lua.hpp>
|
||||
#include "../core/objectptr.hpp"
|
||||
|
||||
namespace Lua {
|
||||
|
||||
struct Class
|
||||
{
|
||||
static void registerValues(lua_State* L);
|
||||
|
||||
static int isInstance(lua_State* L);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -25,6 +25,7 @@
|
||||
#include "object.hpp"
|
||||
#include "method.hpp"
|
||||
#include "log.hpp"
|
||||
#include "class.hpp"
|
||||
#include <traintastic/utils/str.hpp>
|
||||
#include <traintastic/codename.hpp>
|
||||
#include "../world/world.hpp"
|
||||
@ -93,6 +94,16 @@ SandboxPtr Sandbox::create(Script& script)
|
||||
Log::push(L);
|
||||
lua_setfield(L, -2, "log");
|
||||
|
||||
// add is_instance function:
|
||||
lua_pushcfunction(L, Class::isInstance);
|
||||
lua_setfield(L, -2, "is_instance");
|
||||
|
||||
// add class types:
|
||||
lua_newtable(L);
|
||||
Class::registerValues(L);
|
||||
ReadOnlyTable::wrap(L, -1);
|
||||
lua_setfield(L, -2, "class");
|
||||
|
||||
// add enum values:
|
||||
lua_newtable(L);
|
||||
Enum<DecoderProtocol>::registerValues(L);
|
||||
|
||||
156
server/test/lua/isinstance.cpp
Normale Datei
156
server/test/lua/isinstance.cpp
Normale Datei
@ -0,0 +1,156 @@
|
||||
/**
|
||||
* server/test/lua/isinstance.cpp
|
||||
*
|
||||
* This file is part of the traintastic test suite.
|
||||
*
|
||||
* Copyright (C) 2021 Reinder Feenstra
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include "../../src/lua/class.hpp"
|
||||
#include "../../src/lua/object.hpp"
|
||||
#include "../../src/world/world.hpp"
|
||||
|
||||
TEST_CASE("Lua is_instance", "[lua][lua-isinstance]")
|
||||
{
|
||||
static const char* is_instance = "is_instance";
|
||||
static const char* WORLD = "WORLD";
|
||||
static const char* DECODER = "DECODER";
|
||||
|
||||
lua_State* L = luaL_newstate();
|
||||
|
||||
// add in_instance function
|
||||
lua_pushcfunction(L, Lua::Class::isInstance);
|
||||
lua_setglobal(L, is_instance);
|
||||
|
||||
// add classes
|
||||
lua_newtable(L);
|
||||
Lua::Class::registerValues(L);
|
||||
|
||||
// register object type
|
||||
Lua::Object::registerType(L);
|
||||
|
||||
// nil
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushnil(L);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// boolean: false
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushboolean(L, 0);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// boolean: true
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushboolean(L, 1);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// integer: 42
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushinteger(L, 42);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// number: 3.14
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushnumber(L, 3.14);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// string
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_pushstring(L, "traintastic");
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// table
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_newtable(L);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// userdata
|
||||
lua_getglobal(L, is_instance);
|
||||
lua_newuserdata(L, sizeof(void*));
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// World
|
||||
auto world = World::create();
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, world);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, world);
|
||||
lua_getfield(L, -3, DECODER);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
// Decoder
|
||||
auto decoder = world->decoders->add();
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, decoder);
|
||||
lua_getfield(L, -3, WORLD);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE_FALSE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_getglobal(L, is_instance);
|
||||
Lua::Object::push(L, decoder);
|
||||
lua_getfield(L, -3, DECODER);
|
||||
REQUIRE(lua_pcall(L, 2, 1, 0) == LUA_OK);
|
||||
REQUIRE(lua_isboolean(L, -1));
|
||||
REQUIRE(lua_toboolean(L, -1));
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_close(L);
|
||||
}
|
||||
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren