mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
110 lines
2.3 KiB
C++
110 lines
2.3 KiB
C++
// Luanti
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
// Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
|
|
|
#include "irrTypes.h"
|
|
extern "C" {
|
|
#include <lauxlib.h>
|
|
}
|
|
|
|
#include "helper.h"
|
|
#include <cmath>
|
|
#include <irr_v2d.h>
|
|
#include <irr_v3d.h>
|
|
#include <string_view>
|
|
#include "c_converter.h"
|
|
|
|
/*
|
|
* Read template functions
|
|
*/
|
|
template <>
|
|
bool LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
return lua_toboolean(L, index) != 0;
|
|
}
|
|
|
|
template <>
|
|
s16 LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
return luaL_checkinteger(L, index);
|
|
}
|
|
|
|
template <>
|
|
int LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
return luaL_checkinteger(L, index);
|
|
}
|
|
|
|
template <>
|
|
f32 LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
f64 v = luaL_checknumber(L, index);
|
|
return static_cast<f32>(v);
|
|
}
|
|
|
|
template <>
|
|
f64 LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
return luaL_checknumber(L, index);
|
|
}
|
|
|
|
template <>
|
|
f32 LuaHelper::readFiniteParam(lua_State *L, int index)
|
|
{
|
|
f64 original_value = luaL_checknumber(L, index);
|
|
f32 v = static_cast<f32>(original_value);
|
|
if (std::isfinite(v))
|
|
return v;
|
|
if (std::isnan(original_value))
|
|
luaL_argerror(L, index, "number is NaN");
|
|
if (!std::isfinite(original_value))
|
|
luaL_argerror(L, index, "number is not finite");
|
|
assert(!std::isfinite(v));
|
|
luaL_argerror(L, index, "number is out-of-bounds for a 32-bit float");
|
|
IRR_CODE_UNREACHABLE();
|
|
}
|
|
|
|
template <>
|
|
f64 LuaHelper::readFiniteParam(lua_State *L, int index)
|
|
{
|
|
f64 v = luaL_checknumber(L, index);
|
|
if (std::isfinite(v))
|
|
return v;
|
|
if (std::isnan(v))
|
|
luaL_argerror(L, index, "number is NaN");
|
|
luaL_argerror(L, index, "number is not finite");
|
|
IRR_CODE_UNREACHABLE();
|
|
}
|
|
|
|
template <>
|
|
v2s16 LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
return read_v2s16(L, index);
|
|
}
|
|
|
|
template <>
|
|
v2f LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
return check_v2f(L, index);
|
|
}
|
|
|
|
template <>
|
|
v3f LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
return check_v3f(L, index);
|
|
}
|
|
|
|
template <>
|
|
std::string_view LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
size_t length;
|
|
const char *str = luaL_checklstring(L, index, &length);
|
|
return std::string_view(str, length);
|
|
}
|
|
|
|
template <>
|
|
std::string LuaHelper::readParam(lua_State *L, int index)
|
|
{
|
|
auto sv = readParam<std::string_view>(L, index);
|
|
return std::string(sv); // a copy
|
|
}
|