mirror of
https://github.com/minetest/minetest.git
synced 2024-11-27 01:53:45 +01:00
Script: Enforce type checks if not nil (#9748)
* Script: Enforce type checks if not nil
This commit is contained in:
parent
515d38a702
commit
be71e70a91
@ -102,7 +102,8 @@ void read_item_definition(lua_State* L, int index,
|
|||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
|
||||||
lua_getfield(L, index, "sounds");
|
lua_getfield(L, index, "sounds");
|
||||||
if(lua_istable(L, -1)){
|
if (!lua_isnil(L, -1)) {
|
||||||
|
luaL_checktype(L, -1, LUA_TTABLE);
|
||||||
lua_getfield(L, -1, "place");
|
lua_getfield(L, -1, "place");
|
||||||
read_soundspec(L, -1, def.sound_place);
|
read_soundspec(L, -1, def.sound_place);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
@ -182,9 +183,11 @@ void read_object_properties(lua_State *L, int index,
|
|||||||
{
|
{
|
||||||
if(index < 0)
|
if(index < 0)
|
||||||
index = lua_gettop(L) + 1 + index;
|
index = lua_gettop(L) + 1 + index;
|
||||||
if(!lua_istable(L, index))
|
if (lua_isnil(L, index))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
luaL_checktype(L, -1, LUA_TTABLE);
|
||||||
|
|
||||||
int hp_max = 0;
|
int hp_max = 0;
|
||||||
if (getintfield(L, -1, "hp_max", hp_max)) {
|
if (getintfield(L, -1, "hp_max", hp_max)) {
|
||||||
prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
|
prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
|
||||||
@ -1027,8 +1030,10 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
|
|||||||
{
|
{
|
||||||
if(index < 0)
|
if(index < 0)
|
||||||
index = lua_gettop(L) + 1 + index;
|
index = lua_gettop(L) + 1 + index;
|
||||||
if(lua_isnil(L, index)){
|
if (lua_isnil(L, index))
|
||||||
} else if(lua_istable(L, index)){
|
return;
|
||||||
|
|
||||||
|
if (lua_istable(L, index)) {
|
||||||
getstringfield(L, index, "name", spec.name);
|
getstringfield(L, index, "name", spec.name);
|
||||||
getfloatfield(L, index, "gain", spec.gain);
|
getfloatfield(L, index, "gain", spec.gain);
|
||||||
getfloatfield(L, index, "fade", spec.fade);
|
getfloatfield(L, index, "fade", spec.fade);
|
||||||
@ -1055,7 +1060,11 @@ void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
|
|||||||
NodeBox read_nodebox(lua_State *L, int index)
|
NodeBox read_nodebox(lua_State *L, int index)
|
||||||
{
|
{
|
||||||
NodeBox nodebox;
|
NodeBox nodebox;
|
||||||
if(lua_istable(L, -1)){
|
if (lua_isnil(L, -1))
|
||||||
|
return nodebox;
|
||||||
|
|
||||||
|
luaL_checktype(L, -1, LUA_TTABLE);
|
||||||
|
|
||||||
nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
|
nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
|
||||||
ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
|
ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
|
||||||
|
|
||||||
@ -1090,7 +1099,7 @@ NodeBox read_nodebox(lua_State *L, int index)
|
|||||||
NODEBOXREADVEC(nodebox.disconnected_right, "disconnected_right");
|
NODEBOXREADVEC(nodebox.disconnected_right, "disconnected_right");
|
||||||
NODEBOXREADVEC(nodebox.disconnected, "disconnected");
|
NODEBOXREADVEC(nodebox.disconnected, "disconnected");
|
||||||
NODEBOXREADVEC(nodebox.disconnected_sides, "disconnected_sides");
|
NODEBOXREADVEC(nodebox.disconnected_sides, "disconnected_sides");
|
||||||
}
|
|
||||||
return nodebox;
|
return nodebox;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1519,8 +1528,11 @@ void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
void read_groups(lua_State *L, int index, ItemGroupList &result)
|
void read_groups(lua_State *L, int index, ItemGroupList &result)
|
||||||
{
|
{
|
||||||
if (!lua_istable(L,index))
|
if (lua_isnil(L, index))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
luaL_checktype(L, index, LUA_TTABLE);
|
||||||
|
|
||||||
result.clear();
|
result.clear();
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
|
@ -33,10 +33,9 @@ extern "C" {
|
|||||||
#define CHECK_TYPE(index, name, type) { \
|
#define CHECK_TYPE(index, name, type) { \
|
||||||
int t = lua_type(L, (index)); \
|
int t = lua_type(L, (index)); \
|
||||||
if (t != (type)) { \
|
if (t != (type)) { \
|
||||||
std::string traceback = script_get_backtrace(L); \
|
|
||||||
throw LuaError(std::string("Invalid ") + (name) + \
|
throw LuaError(std::string("Invalid ") + (name) + \
|
||||||
" (expected " + lua_typename(L, (type)) + \
|
" (expected " + lua_typename(L, (type)) + \
|
||||||
" got " + lua_typename(L, t) + ").\n" + traceback); \
|
" got " + lua_typename(L, t) + ")."); \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
|
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
|
||||||
@ -457,12 +456,22 @@ size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result
|
|||||||
Table field getters
|
Table field getters
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname)
|
||||||
|
{
|
||||||
|
if (lua_isnil(L, index))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
CHECK_TYPE(index, std::string("field \"") + fieldname + '"', type);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool getstringfield(lua_State *L, int table,
|
bool getstringfield(lua_State *L, int table,
|
||||||
const char *fieldname, std::string &result)
|
const char *fieldname, std::string &result)
|
||||||
{
|
{
|
||||||
lua_getfield(L, table, fieldname);
|
lua_getfield(L, table, fieldname);
|
||||||
bool got = false;
|
bool got = false;
|
||||||
if(lua_isstring(L, -1)){
|
|
||||||
|
if (check_field_or_nil(L, -1, LUA_TSTRING, fieldname)) {
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
const char *ptr = lua_tolstring(L, -1, &len);
|
const char *ptr = lua_tolstring(L, -1, &len);
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
@ -479,7 +488,8 @@ bool getfloatfield(lua_State *L, int table,
|
|||||||
{
|
{
|
||||||
lua_getfield(L, table, fieldname);
|
lua_getfield(L, table, fieldname);
|
||||||
bool got = false;
|
bool got = false;
|
||||||
if(lua_isnumber(L, -1)){
|
|
||||||
|
if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)) {
|
||||||
result = lua_tonumber(L, -1);
|
result = lua_tonumber(L, -1);
|
||||||
got = true;
|
got = true;
|
||||||
}
|
}
|
||||||
@ -492,7 +502,8 @@ bool getboolfield(lua_State *L, int table,
|
|||||||
{
|
{
|
||||||
lua_getfield(L, table, fieldname);
|
lua_getfield(L, table, fieldname);
|
||||||
bool got = false;
|
bool got = false;
|
||||||
if(lua_isboolean(L, -1)){
|
|
||||||
|
if (check_field_or_nil(L, -1, LUA_TBOOLEAN, fieldname)){
|
||||||
result = lua_toboolean(L, -1);
|
result = lua_toboolean(L, -1);
|
||||||
got = true;
|
got = true;
|
||||||
}
|
}
|
||||||
@ -511,17 +522,6 @@ size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
|
|||||||
return num_strings_read;
|
return num_strings_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string checkstringfield(lua_State *L, int table,
|
|
||||||
const char *fieldname)
|
|
||||||
{
|
|
||||||
lua_getfield(L, table, fieldname);
|
|
||||||
CHECK_TYPE(-1, std::string("field \"") + fieldname + '"', LUA_TSTRING);
|
|
||||||
size_t len;
|
|
||||||
const char *s = lua_tolstring(L, -1, &len);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
return std::string(s, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getstringfield_default(lua_State *L, int table,
|
std::string getstringfield_default(lua_State *L, int table,
|
||||||
const char *fieldname, const std::string &default_)
|
const char *fieldname, const std::string &default_)
|
||||||
{
|
{
|
||||||
|
@ -45,13 +45,15 @@ float getfloatfield_default(lua_State *L, int table,
|
|||||||
int getintfield_default(lua_State *L, int table,
|
int getintfield_default(lua_State *L, int table,
|
||||||
const char *fieldname, int default_);
|
const char *fieldname, int default_);
|
||||||
|
|
||||||
|
bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool getintfield(lua_State *L, int table,
|
bool getintfield(lua_State *L, int table,
|
||||||
const char *fieldname, T &result)
|
const char *fieldname, T &result)
|
||||||
{
|
{
|
||||||
lua_getfield(L, table, fieldname);
|
lua_getfield(L, table, fieldname);
|
||||||
bool got = false;
|
bool got = false;
|
||||||
if (lua_isnumber(L, -1)){
|
if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)){
|
||||||
result = lua_tointeger(L, -1);
|
result = lua_tointeger(L, -1);
|
||||||
got = true;
|
got = true;
|
||||||
}
|
}
|
||||||
@ -87,8 +89,6 @@ bool getboolfield(lua_State *L, int table,
|
|||||||
const char *fieldname, bool &result);
|
const char *fieldname, bool &result);
|
||||||
bool getfloatfield(lua_State *L, int table,
|
bool getfloatfield(lua_State *L, int table,
|
||||||
const char *fieldname, float &result);
|
const char *fieldname, float &result);
|
||||||
std::string checkstringfield(lua_State *L, int table,
|
|
||||||
const char *fieldname);
|
|
||||||
|
|
||||||
void setstringfield(lua_State *L, int table,
|
void setstringfield(lua_State *L, int table,
|
||||||
const char *fieldname, const std::string &value);
|
const char *fieldname, const std::string &value);
|
||||||
|
@ -487,7 +487,9 @@ int ModApiInventory::l_get_inventory(lua_State *L)
|
|||||||
{
|
{
|
||||||
InventoryLocation loc;
|
InventoryLocation loc;
|
||||||
|
|
||||||
std::string type = checkstringfield(L, 1, "type");
|
lua_getfield(L, 1, "type");
|
||||||
|
std::string type = luaL_checkstring(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
if(type == "node"){
|
if(type == "node"){
|
||||||
MAP_LOCK_REQUIRED;
|
MAP_LOCK_REQUIRED;
|
||||||
@ -504,11 +506,13 @@ int ModApiInventory::l_get_inventory(lua_State *L)
|
|||||||
|
|
||||||
NO_MAP_LOCK_REQUIRED;
|
NO_MAP_LOCK_REQUIRED;
|
||||||
if (type == "player") {
|
if (type == "player") {
|
||||||
std::string name = checkstringfield(L, 1, "name");
|
lua_getfield(L, 1, "name");
|
||||||
loc.setPlayer(name);
|
loc.setPlayer(luaL_checkstring(L, -1));
|
||||||
|
lua_pop(L, 1);
|
||||||
} else if (type == "detached") {
|
} else if (type == "detached") {
|
||||||
std::string name = checkstringfield(L, 1, "name");
|
lua_getfield(L, 1, "name");
|
||||||
loc.setDetached(name);
|
loc.setDetached(luaL_checkstring(L, -1));
|
||||||
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getServer(L)->getInventory(loc) != NULL)
|
if (getServer(L)->getInventory(loc) != NULL)
|
||||||
|
Loading…
Reference in New Issue
Block a user