aboutsummaryrefslogtreecommitdiffstats
path: root/epan/wslua/wslua_internals.c
diff options
context:
space:
mode:
authorHadriel Kaplan <hadrielk@yahoo.com>2014-03-27 16:35:48 -0400
committerAnders Broman <a.broman58@gmail.com>2014-03-28 04:27:43 +0000
commit2c1e673fa2e361c73858709c7a06396c6df470ef (patch)
treeeed3464aa681e031c1c7eb7f0ae7e38633b001e4 /epan/wslua/wslua_internals.c
parent0ffc690da40edf31acb95b2a15033d43a3f35ebd (diff)
Allow chained calls with Lua TreeItem functions, and fix a couple of minor errors.
A common Lua idiom is to use chained calls, i.e. tree:foo():bar():choo(). This actually works for tree:add() because it returns the new child tree item which is then the one being applied to the next chained call. But it doesn't work beyond that for things like set_generated() and so on. So this commit fixes that. This also fixes the Lua tree:add() function for the FT_BOOL type to let it be a Lua boolean value. And it reverts a previous change to Struct.tohex() to allow coercion of the argument. Change-Id: I10f819d363163914ba320c87d4bedebe5b50cacf Reviewed-on: https://code.wireshark.org/review/851 Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'epan/wslua/wslua_internals.c')
-rw-r--r--epan/wslua/wslua_internals.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/epan/wslua/wslua_internals.c b/epan/wslua/wslua_internals.c
index c18bdc6441..0d0e61388c 100644
--- a/epan/wslua/wslua_internals.c
+++ b/epan/wslua/wslua_internals.c
@@ -43,6 +43,36 @@ WSLUA_API int wslua__concat(lua_State* L) {
return 1;
}
+/* like lua_toboolean, except only coerces int, nil, and bool, and errors on other types.
+ note that normal lua_toboolean returns 1 for any Lua value different from false and
+ nil; otherwise it returns 0. So a string would give a 0, as would a number of 1.
+ This function errors if the arg is a string, and sets the boolean to 1 for any
+ number other than 0. Like toboolean, this returns FALSE if the arg was missing. */
+WSLUA_API gboolean wslua_toboolean(lua_State* L, int n) {
+ gboolean val = FALSE;
+
+ if ( lua_isboolean(L,n) || lua_isnil(L,n) || lua_gettop(L) < n ) {
+ val = lua_toboolean(L,n);
+ } else if ( lua_type(L,n) == LUA_TNUMBER ) {
+ int num = luaL_checkint(L,n);
+ val = num != 0 ? TRUE : FALSE;
+ } else {
+ luaL_argerror(L,n,"must be a boolean or number");
+ }
+
+ return val;
+}
+
+/* like luaL_checkint, except for booleans - this does not coerce other types */
+WSLUA_API gboolean wslua_checkboolean(lua_State* L, int n) {
+
+ if (!lua_isboolean(L,n) ) {
+ luaL_argerror(L,n,"must be a boolean");
+ }
+
+ return lua_toboolean(L,n);;
+}
+
WSLUA_API gboolean wslua_optbool(lua_State* L, int n, gboolean def) {
gboolean val = FALSE;
@@ -57,6 +87,24 @@ WSLUA_API gboolean wslua_optbool(lua_State* L, int n, gboolean def) {
return val;
}
+/* like lua_tointeger, except only coerces int, nil, and bool, and errors on other types.
+ note that normal lua_tointeger does not coerce nil or bool, but does coerce strings. */
+WSLUA_API lua_Integer wslua_tointeger(lua_State* L, int n) {
+ lua_Integer val = 0;
+
+ if ( lua_type(L,n) == LUA_TNUMBER) {
+ val = lua_tointeger(L,n);
+ } else if ( lua_isboolean(L,n) ) {
+ val = (lua_Integer) (lua_toboolean(L,n));
+ } else if ( lua_isnil(L,n) ) {
+ val = 0;
+ } else {
+ luaL_argerror(L,n,"must be a integer, boolean or nil");
+ }
+
+ return val;
+}
+
/* like luaL_optint, except converts/handles Lua booleans as well */
WSLUA_API int wslua_optboolint(lua_State* L, int n, int def) {
int val = 0;