diff options
author | Hadriel Kaplan <hadrielk@yahoo.com> | 2014-02-19 03:22:55 -0500 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2014-02-21 02:05:35 +0000 |
commit | 9246a709bf826f2cc64581b25fbf1ebafa330db6 (patch) | |
tree | b63eb4c55d7e1673f98a371d8787de906807baaf /epan/wslua/wslua_util.c | |
parent | c65fbffc95b5e6b0caff6952faea8e8c9350cf7d (diff) |
Cleanup on aisle 5: normalizes the Lua code to follow common schema/model
Over time the various wslua classes/functions have gotten moldy, with different
ways of doing similar things. Some of it can't be changed without breaking
backwards compatibility for Lua scripts, so I didn't do that. But I did what
I could. The biggest change is a refactoring of how accessors/attributes
are handled in the code, so that most of them work the same way using the
same code.
Specific changes made:
* Added null/expired checking macro to class declarations for many classes
* Removed extraneous pointer/expired checking, since checkFoo() does that already
* Fixed "errors" reported by clang static analyzer; they were false positives, but it was easier to get it to stop complaining by changing the code
* Moved internal wslua functions from wslua_utils.c into a new 'wslua_internals.c' file
* Changed Listener/NSTime/Pinfo/Proto to use a common setter/getter accessor/attribute code model, instead of each of them doing their own
* Fixed some API doc mistakes, mostly around attributes that were documented as read-only but were actually read-write
Change-Id: Idddafc5fbd3545ebff29e063acc767e1c743a1a9
Reviewed-on: https://code.wireshark.org/review/271
Reviewed-by: Evan Huus <eapache@gmail.com>
Tested-by: Evan Huus <eapache@gmail.com>
Diffstat (limited to 'epan/wslua/wslua_util.c')
-rw-r--r-- | epan/wslua/wslua_util.c | 62 |
1 files changed, 4 insertions, 58 deletions
diff --git a/epan/wslua/wslua_util.c b/epan/wslua/wslua_util.c index 9e95d557f4..775c65f0e8 100644 --- a/epan/wslua/wslua_util.c +++ b/epan/wslua/wslua_util.c @@ -33,57 +33,6 @@ #include <epan/stat_cmd_args.h> -WSLUA_API int wslua__concat(lua_State* L) { - /* Concatenate two objects to a string */ - if (!luaL_callmeta(L,1,"__tostring")) - lua_pushvalue(L,1); - if (!luaL_callmeta(L,2,"__tostring")) - lua_pushvalue(L,2); - - lua_concat(L,2); - - return 1; -} - -WSLUA_API gboolean wslua_optbool(lua_State* L, int n, gboolean def) { - gboolean val = FALSE; - - if ( lua_isboolean(L,n) ) { - val = lua_toboolean(L,n); - } else if ( lua_isnil(L,n) || lua_gettop(L) < n ){ - val = def; - } else { - luaL_argerror(L,n,"must be a boolean"); - } - - return val; -} - - -WSLUA_API const gchar* lua_shiftstring(lua_State* L, int i) { - const gchar* p = luaL_checkstring(L, i); - - if (p) { - lua_remove(L,i); - return p; - } else { - return NULL; - } -} - -/* following is based on the luaL_setfuncs() from Lua 5.2, so we can use it in pre-5.2 */ -WSLUA_API void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_setfield(L, -(nup + 2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ -} - WSLUA_FUNCTION wslua_get_version(lua_State* L) { /* Get Wireshark version */ const gchar* str = VERSION; lua_pushstring(L,str); @@ -301,7 +250,7 @@ WSLUA_FUNCTION wslua_datafile_path(lua_State* L) { } -WSLUA_CLASS_DEFINE(Dir,NOP,NOP); /* A Directory */ +WSLUA_CLASS_DEFINE(Dir,FAIL_ON_NULL("Dir"),NOP); /* A Directory */ WSLUA_CONSTRUCTOR Dir_open(lua_State* L) { /* Usage: for filename in Dir.open(path) do ... end */ @@ -349,11 +298,6 @@ WSLUA_METAMETHOD Dir__call(lua_State* L) { const gchar* filename; const char* ext; - if (!dir) { - luaL_argerror(L,1,"must be a Dir"); - return 0; - } - if (!dir->dir) { return 0; } @@ -401,7 +345,9 @@ WSLUA_METHOD Dir_close(lua_State* L) { /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */ static int Dir__gc(lua_State* L) { - Dir dir = checkDir(L,1); + Dir dir = toDir(L,1); + + if(!dir) return 0; if (dir->dir) { CLOSEDIR_OP(dir->dir); |