Mesen-SX/Core/LuaCallHelper.cpp

172 lines
3 KiB
C++
Raw Normal View History

2019-05-12 21:18:05 -04:00
#include "stdafx.h"
2019-07-02 19:56:00 -04:00
#ifndef LIBRETRO
2019-05-12 21:18:05 -04:00
#include "LuaCallHelper.h"
2020-12-19 23:30:09 +03:00
LuaCallHelper::LuaCallHelper(lua_State* lua) : _lua(lua)
2019-05-12 21:18:05 -04:00
{
_stackSize = lua_gettop(lua);
}
void LuaCallHelper::ForceParamCount(int paramCount)
{
2020-12-19 23:30:09 +03:00
while (lua_gettop(_lua) < paramCount)
{
2019-05-12 21:18:05 -04:00
lua_pushnil(_lua);
}
}
bool LuaCallHelper::CheckParamCount(int minParamCount)
{
2020-12-19 23:30:09 +03:00
if (minParamCount >= 0 && _stackSize < _paramCount && _stackSize >= minParamCount)
{
2019-05-12 21:18:05 -04:00
return true;
}
2020-12-19 23:30:09 +03:00
if (_stackSize != _paramCount)
{
string message = string("too ") + (_stackSize < _paramCount ? "few" : "many") + " parameters. expected " +
std::to_string(_paramCount) + " got " + std::to_string(_stackSize);
2019-05-12 21:18:05 -04:00
luaL_error(_lua, message.c_str());
return false;
}
return true;
}
double LuaCallHelper::ReadDouble()
{
_paramCount++;
double value = 0;
2020-12-19 23:30:09 +03:00
if (lua_isnumber(_lua, -1))
{
2019-05-12 21:18:05 -04:00
value = lua_tonumber(_lua, -1);
}
lua_pop(_lua, 1);
return value;
}
bool LuaCallHelper::ReadBool(bool defaultValue)
{
_paramCount++;
bool value = defaultValue;
2020-12-19 23:30:09 +03:00
if (lua_isboolean(_lua, -1))
{
2019-05-12 21:18:05 -04:00
value = lua_toboolean(_lua, -1) != 0;
2020-12-19 23:30:09 +03:00
}
else if (lua_isnumber(_lua, -1))
{
2019-05-12 21:18:05 -04:00
value = lua_tonumber(_lua, -1) != 0;
}
lua_pop(_lua, 1);
return value;
}
Nullable<bool> LuaCallHelper::ReadOptionalBool()
{
_paramCount++;
Nullable<bool> result;
2020-12-19 23:30:09 +03:00
if (lua_isboolean(_lua, -1))
{
2019-05-12 21:18:05 -04:00
result.HasValue = true;
result.Value = lua_toboolean(_lua, -1) != 0;
2020-12-19 23:30:09 +03:00
}
else if (lua_isnumber(_lua, -1))
{
2019-05-12 21:18:05 -04:00
result.HasValue = true;
result.Value = lua_tonumber(_lua, -1) != 0;
}
lua_pop(_lua, 1);
return result;
}
Nullable<uint32_t> LuaCallHelper::ReadOptionalInteger()
{
_paramCount++;
Nullable<uint32_t> result;
2020-12-19 23:30:09 +03:00
if (lua_isinteger(_lua, -1))
{
2019-05-12 21:18:05 -04:00
result.HasValue = true;
result.Value = (uint32_t)lua_tointeger(_lua, -1);
2020-12-19 23:30:09 +03:00
}
else if (lua_isnumber(_lua, -1))
{
2019-05-12 21:18:05 -04:00
result.HasValue = true;
result.Value = (uint32_t)lua_tonumber(_lua, -1);
}
lua_pop(_lua, 1);
return result;
}
uint32_t LuaCallHelper::ReadInteger(uint32_t defaultValue)
{
_paramCount++;
uint32_t value = defaultValue;
2020-12-19 23:30:09 +03:00
if (lua_isinteger(_lua, -1))
{
2019-05-12 21:18:05 -04:00
value = (uint32_t)lua_tointeger(_lua, -1);
2020-12-19 23:30:09 +03:00
}
else if (lua_isnumber(_lua, -1))
{
2019-05-12 21:18:05 -04:00
value = (uint32_t)lua_tonumber(_lua, -1);
}
lua_pop(_lua, 1);
return value;
}
string LuaCallHelper::ReadString()
{
_paramCount++;
size_t len;
string str;
2020-12-19 23:30:09 +03:00
if (lua_isstring(_lua, -1))
{
2019-05-12 21:18:05 -04:00
const char* cstr = lua_tolstring(_lua, -1, &len);
str = string(cstr, len);
}
lua_pop(_lua, 1);
return str;
}
int LuaCallHelper::GetReference()
{
_paramCount++;
2020-12-19 23:30:09 +03:00
if (lua_isfunction(_lua, -1))
{
2019-05-12 21:18:05 -04:00
return luaL_ref(_lua, LUA_REGISTRYINDEX);
2020-12-19 23:30:09 +03:00
}
else
{
2019-05-12 21:18:05 -04:00
lua_pop(_lua, 1);
return LUA_NOREF;
}
}
void LuaCallHelper::Return(bool value)
{
lua_pushboolean(_lua, value);
_returnCount++;
}
void LuaCallHelper::Return(int value)
{
lua_pushinteger(_lua, value);
_returnCount++;
}
void LuaCallHelper::Return(uint32_t value)
{
lua_pushinteger(_lua, value);
_returnCount++;
}
void LuaCallHelper::Return(string value)
{
lua_pushlstring(_lua, value.c_str(), value.size());
_returnCount++;
}
int LuaCallHelper::ReturnCount()
{
return _returnCount;
}
2019-07-02 19:56:00 -04:00
2020-12-19 23:30:09 +03:00
#endif