aboutsummaryrefslogtreecommitdiff
path: root/src/bitflags.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bitflags.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/bitflags.c b/src/bitflags.c
index 39f8af0..e397918 100644
--- a/src/bitflags.c
+++ b/src/bitflags.c
@@ -46,6 +46,32 @@ LSBF_BITFLAG lsbf_checkbitflags(lua_State *L, int index) {
46 return obj->flags; 46 return obj->flags;
47} 47}
48 48
49// Validates that the given index is a table containing a field 'fieldname'
50// which is a bitflag object and returns its value.
51// If the index is not a table or the field is not a bitflag object, a Lua
52// error is raised. If the bitflag is not present, the default value is returned.
53// The stack remains unchanged.
54LSBF_BITFLAG lsbf_checkbitflagsfield(lua_State *L, int index, const char *fieldname, LSBF_BITFLAG default_value) {
55 luaL_checktype(L, index, LUA_TTABLE);
56 lua_getfield(L, index, fieldname);
57
58 // if null, return default value
59 if (lua_isnil(L, -1)) {
60 lua_pop(L, 1);
61 return default_value;
62 }
63
64 // check to bitflags
65 LS_BitFlags *obj = luaL_testudata(L, -1, BITFLAGS_MT_NAME);
66 if (obj == NULL) {
67 lua_pop(L, 1);
68 return luaL_error(L, "bad argument #%d, field '%s' must be a bitflag object", index, fieldname);
69 }
70 LSBF_BITFLAG value = obj->flags;
71 lua_pop(L, 1);
72 return value;
73}
74
49/*** 75/***
50Creates a new bitflag object from the given value. 76Creates a new bitflag object from the given value.
51@function system.bitflag 77@function system.bitflag