Files
SourMesen ad0a1b9849 Lua: Misc. fixes & improvements (#250)
-Fixed getAccessCounters documentation (swapped param order)
-Fixed file watcher not being disabled properly when closing the script window, causing the script to run if changed on the disk, even if the script window is closed
-Prevent file watcher from triggering from writes done by the script window itself (which caused the script to be reloaded 2 or 3 times in a row)
-Fixed UI freezes when using emu.log() on a large table
-Fixed crash when 100k character limit (in the last 500 lines) for script log was reached (out of bounds read/write)
-Added "fps" value to "getState"
-Slightly improved getAccessCounters/getCdlData performance
-Added a new drawPixels API to improve performance when a script needs to draw a lot of individual pixels
2026-08-10 22:42:34 +09:00

176 lines
3.6 KiB
C++

#include "pch.h"
#include "LuaCallHelper.h"
LuaCallHelper::LuaCallHelper(lua_State* lua) : _lua(lua)
{
_stackSize = lua_gettop(lua);
}
void LuaCallHelper::ForceParamCount(int paramCount)
{
while(lua_gettop(_lua) < paramCount) {
lua_pushnil(_lua);
}
}
bool LuaCallHelper::CheckParamCount(int minParamCount)
{
if(minParamCount >= 0 && _stackSize < _paramCount && _stackSize >= minParamCount) {
return true;
}
return CheckSpecificParamCount(_paramCount, minParamCount);
}
bool LuaCallHelper::CheckSpecificParamCount(int count, int minParamCount)
{
if(_stackSize != count) {
string message = string("too ") + (_stackSize < count ? "few" : "many") + " parameters. expected ";
if(minParamCount >= 0) {
message += std::to_string(minParamCount) + " to " + std::to_string(count);
} else {
message += std::to_string(count);
}
message += ", got " + std::to_string(_stackSize);
luaL_error(_lua, message.c_str());
return false;
}
return true;
}
double LuaCallHelper::ReadDouble()
{
_paramCount++;
double value = 0;
if(lua_isnumber(_lua, -1)) {
value = lua_tonumber(_lua, -1);
}
lua_pop(_lua, 1);
return value;
}
bool LuaCallHelper::ReadBool(bool defaultValue)
{
_paramCount++;
bool value = defaultValue;
if(lua_isboolean(_lua, -1)) {
value = lua_toboolean(_lua, -1) != 0;
} else if(lua_isnumber(_lua, -1)) {
value = lua_tonumber(_lua, -1) != 0;
}
lua_pop(_lua, 1);
return value;
}
Nullable<bool> LuaCallHelper::ReadOptionalBool()
{
_paramCount++;
Nullable<bool> result;
if(lua_isboolean(_lua, -1)) {
result.HasValue = true;
result.Value = lua_toboolean(_lua, -1) != 0;
} else if(lua_isnumber(_lua, -1)) {
result.HasValue = true;
result.Value = lua_tonumber(_lua, -1) != 0;
}
lua_pop(_lua, 1);
return result;
}
Nullable<int32_t> LuaCallHelper::ReadOptionalInteger()
{
_paramCount++;
Nullable<int32_t> result;
if(lua_isinteger(_lua, -1)) {
result.HasValue = true;
result.Value = (int32_t)lua_tointeger(_lua, -1);
} else if(lua_isnumber(_lua, -1)) {
result.HasValue = true;
result.Value = (int32_t)lua_tonumber(_lua, -1);
}
lua_pop(_lua, 1);
return result;
}
uint32_t LuaCallHelper::ReadIntegerFromIndex(int32_t index)
{
_paramCount++;
uint32_t value = 0;
if(lua_isinteger(_lua, index)) {
value = (uint32_t)lua_tointeger(_lua, index);
} else if(lua_isnumber(_lua, -1)) {
value = (uint32_t)lua_tonumber(_lua, index);
}
return value;
}
uint32_t LuaCallHelper::ReadInteger(uint32_t defaultValue)
{
_paramCount++;
uint32_t value = defaultValue;
if(lua_isinteger(_lua, -1)) {
value = (uint32_t)lua_tointeger(_lua, -1);
} else if(lua_isnumber(_lua, -1)) {
value = (uint32_t)lua_tonumber(_lua, -1);
}
lua_pop(_lua, 1);
return value;
}
string LuaCallHelper::ReadString()
{
_paramCount++;
size_t len;
string str;
if(lua_isstring(_lua, -1)) {
const char* cstr = lua_tolstring(_lua, -1, &len);
str = string(cstr, len);
}
lua_pop(_lua, 1);
return str;
}
int LuaCallHelper::GetReference()
{
_paramCount++;
if(lua_isfunction(_lua, -1)) {
return luaL_ref(_lua, LUA_REGISTRYINDEX);
} else {
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(uint64_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;
}