summaryrefslogtreecommitdiff
path: root/epan/wslua
diff options
context:
space:
mode:
authorHadriel Kaplan <hadrielk@yahoo.com>2014-02-19 03:22:55 -0500
committerEvan Huus <eapache@gmail.com>2014-02-21 02:05:35 +0000
commit9246a709bf826f2cc64581b25fbf1ebafa330db6 (patch)
treeb63eb4c55d7e1673f98a371d8787de906807baaf /epan/wslua
parentc65fbffc95b5e6b0caff6952faea8e8c9350cf7d (diff)
downloadwireshark-9246a709bf826f2cc64581b25fbf1ebafa330db6.tar.gz
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')
-rw-r--r--epan/wslua/CMakeLists.txt3
-rw-r--r--epan/wslua/Makefile.am3
-rw-r--r--epan/wslua/wslua.h144
-rw-r--r--epan/wslua/wslua_dumper.c4
-rw-r--r--epan/wslua/wslua_field.c174
-rw-r--r--epan/wslua/wslua_gui.c85
-rw-r--r--epan/wslua/wslua_int64.c162
-rw-r--r--epan/wslua/wslua_internals.c302
-rw-r--r--epan/wslua/wslua_listener.c84
-rw-r--r--epan/wslua/wslua_pinfo.c815
-rw-r--r--epan/wslua/wslua_proto.c315
-rw-r--r--epan/wslua/wslua_struct.c6
-rw-r--r--epan/wslua/wslua_tree.c104
-rw-r--r--epan/wslua/wslua_tvb.c178
-rw-r--r--epan/wslua/wslua_util.c62
15 files changed, 1081 insertions, 1360 deletions
diff --git a/epan/wslua/CMakeLists.txt b/epan/wslua/CMakeLists.txt
index 8968938281..e65daeccd9 100644
--- a/epan/wslua/CMakeLists.txt
+++ b/epan/wslua/CMakeLists.txt
@@ -34,8 +34,9 @@ set(WSLUA_MODULES
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_gui.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_util.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_field.c
- ${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_struct.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_struct.c
${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_dumper.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/wslua/wslua_internals.c
)
set(WSLUA_FILES
diff --git a/epan/wslua/Makefile.am b/epan/wslua/Makefile.am
index 241a2a63ad..ec4afd2cd9 100644
--- a/epan/wslua/Makefile.am
+++ b/epan/wslua/Makefile.am
@@ -41,7 +41,8 @@ wslua_modules = \
$(srcdir)/wslua_util.c \
$(srcdir)/wslua_field.c \
$(srcdir)/wslua_struct.c \
- $(srcdir)/wslua_dumper.c
+ $(srcdir)/wslua_dumper.c \
+ $(srcdir)/wslua_internals.c
libwslua_la_SOURCES = \
$(wslua_modules) \
diff --git a/epan/wslua/wslua.h b/epan/wslua/wslua.h
index 6deef65468..1b6011b8bc 100644
--- a/epan/wslua/wslua.h
+++ b/epan/wslua/wslua.h
@@ -198,7 +198,7 @@ struct _wslua_tap {
lua_State* L;
int packet_ref;
int draw_ref;
- int init_ref;
+ int reset_ref;
};
# define DIRECTORY_T GDir
@@ -312,8 +312,39 @@ C shift##C(lua_State* L,int i) { \
} \
typedef int dummy##C
+typedef struct _wslua_attribute_table {
+ const gchar *fieldname;
+ lua_CFunction getfunc;
+ lua_CFunction setfunc;
+} wslua_attribute_table;
+extern int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, gboolean is_getter);
+
+#define WSLUA_TYPEOF_FIELD "__typeof"
+
#ifdef HAVE_LUA
+#define WSLUA_REGISTER_META(C) { \
+ /* check for existing metatable in registry */ \
+ luaL_getmetatable(L, #C); \
+ if (!lua_isnil (L, -1)) { \
+ fprintf(stderr, "ERROR: Attempt to register metatable '%s' which already exists in Lua registry\n", #C); \
+ exit(1); \
+ } \
+ lua_pop (L, 1); \
+ /* create a new metatable and register metamethods into it */ \
+ luaL_newmetatable (L, #C); \
+ wslua_setfuncs (L, C ## _meta, 0); \
+ /* add a metatable field named '__typeof' = the class name, this is used by the typeof() Lua func */ \
+ lua_pushstring(L, #C); \
+ lua_setfield(L, -2, WSLUA_TYPEOF_FIELD); \
+ /* add the '__gc' metamethod with a C-function named Class__gc */ \
+ /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
+ lua_pushcfunction(L, C ## __gc); \
+ lua_setfield(L, -2, "__gc"); \
+ /* pop the metatable */ \
+ lua_pop(L, 1); \
+}
+
#define WSLUA_REGISTER_CLASS(C) { \
/* check for existing class table in global */ \
lua_getglobal (L, #C); \
@@ -327,14 +358,10 @@ typedef int dummy##C
wslua_setfuncs (L, C ## _methods, 0); \
/* add a method-table field named '__typeof' = the class name, this is used by the typeof() Lua func */ \
lua_pushstring(L, #C); \
- lua_setfield(L, -2, "__typeof"); \
- /* create a new metatable and register metamethods into it */ \
- luaL_newmetatable (L, #C); \
- wslua_setfuncs (L, C ## _meta, 0); \
- /* add the '__gc' metamethod with a C-function named Class__gc */ \
- /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
- lua_pushcfunction(L, C ## __gc); \
- lua_setfield(L, -2, "__gc"); \
+ lua_setfield(L, -2, WSLUA_TYPEOF_FIELD); \
+ /* setup the meta table */ \
+ WSLUA_REGISTER_META(C); \
+ luaL_getmetatable(L, #C); \
/* push a copy of the class methods table, and set it to be the metatable's __index field */ \
lua_pushvalue (L, -2); \
lua_setfield (L, -2, "__index"); \
@@ -344,26 +371,18 @@ typedef int dummy##C
lua_setglobal (L, #C); \
}
-#define WSLUA_REGISTER_META(C) { \
- /* check for existing metatable in registry */ \
+/* see comments for wslua_reg_attributes and wslua_attribute_dispatcher to see how this attribute stuff works */
+#define WSLUA_REGISTER_ATTRIBUTES(C) { \
+ /* get metatable from Lua registry */ \
luaL_getmetatable(L, #C); \
- if (!lua_isnil (L, -1)) { \
- fprintf(stderr, "ERROR: Attempt to register metatable '%s' which already exists in Lua registry\n", #C); \
+ if (lua_isnil(L, -1)) { \
+ fprintf(stderr, "ERROR: Attempt to register attributes without a pre-existing metatable for '%s' in Lua registry\n", #C); \
exit(1); \
} \
- lua_pop (L, 1); \
- /* create a new metatable and register metamethods into it */ \
- luaL_newmetatable (L, #C); \
- wslua_setfuncs (L, C ## _meta, 0); \
- /* add a metatable field named '__typeof' = the class name, this is used by the typeof() Lua func */ \
- lua_pushstring(L, #C); \
- lua_setfield(L, -2, "__typeof"); \
- /* add the '__gc' metamethod with a C-function named Class__gc */ \
- /* this will force ALL wslua classes to have a Class__gc function defined, which is good */ \
- lua_pushcfunction(L, C ## __gc); \
- lua_setfield(L, -2, "__gc"); \
- /* pop the metatable */ \
- lua_pop(L, 1); \
+ /* register the getters/setters in their tables */ \
+ wslua_reg_attributes(L, C##_attributes, TRUE); \
+ wslua_reg_attributes(L, C##_attributes, FALSE); \
+ lua_pop(L, 1); /* pop the metatable */ \
}
#define WSLUA_INIT(L) \
@@ -389,6 +408,68 @@ typedef int dummy##C
#define WSLUA_META static const luaL_Reg
#define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name }
#define WSLUA_CLASS_FNREG_ALIAS(class,aliasname,name) { #aliasname, class##_##name }
+#define WSLUA_CLASS_MTREG(class,name) { "__" #name, class##__##name }
+
+#define WSLUA_ATTRIBUTES static const wslua_attribute_table
+/* following are useful macros for the rows in the array created by above */
+#define WSLUA_ATTRIBUTE_RWREG(class,name) { #name, class##_get_##name, class##_set_##name }
+#define WSLUA_ATTRIBUTE_ROREG(class,name) { #name, class##_get_##name, NULL }
+#define WSLUA_ATTRIBUTE_WOREG(class,name) { #name, NULL, class##_set_##name }
+
+#define WSLUA_ATTRIBUTE_FUNC_SETTER(C,field) \
+ static int C##_set_##field (lua_State* L) { \
+ C obj = check##C (L,1); \
+ if (! lua_isfunction(L,-1) ) \
+ return luaL_error(L, "%s's attribute `%s' must be a function", #C , #field ); \
+ obj->field##_ref = luaL_ref(L, LUA_REGISTRYINDEX); \
+ return 0; \
+ }
+
+#define WSLUA_ATTRIBUTE_GET(C,name,block) \
+ static int C##_get_##name (lua_State* L) { \
+ C obj = check##C (L,1); \
+ block \
+ return 1; \
+ }
+
+#define WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(C,name,member) \
+ WSLUA_ATTRIBUTE_GET(C,name,{lua_pushboolean(L, obj->member );})
+
+#define WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(C,name,member) \
+ WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(obj->member));})
+
+#define WSLUA_ATTRIBUTE_NUMBER_GETTER(C,member) \
+ WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(C,member,member)
+
+#define WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(C,name,block) \
+ WSLUA_ATTRIBUTE_GET(C,name,{lua_pushnumber(L,(lua_Number)(block));})
+
+#define WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,name,member) \
+ WSLUA_ATTRIBUTE_GET(C,name, { \
+ lua_pushstring(L,obj->member); /* this pushes nil if obj->member is null */ \
+ })
+
+#define WSLUA_ATTRIBUTE_STRING_GETTER(C,member) \
+ WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(C,member,member)
+
+
+#define WSLUA_ATTRIBUTE_SET(C,name,block) \
+ static int C##_set_##name (lua_State* L) { \
+ C obj = check##C (L,1); \
+ block; \
+ return 0; \
+ }
+
+#define WSLUA_ATTRIBUTE_NAMED_NUMBER_SETTER(C,name,member,cast) \
+ WSLUA_ATTRIBUTE_SET(C,name, { \
+ if (! lua_isnumber(L,-1) ) \
+ return luaL_error(L, "%s's attribute `%s' must be a number", #C , #name ); \
+ obj->member = (cast) lua_tointeger(L,-1); \
+ })
+
+#define WSLUA_ATTRIBUTE_NUMBER_SETTER(C,member,cast) \
+ WSLUA_ATTRIBUTE_NAMED_NUMBER_SETTER(C,member,member,cast)
+
#define WSLUA_ERROR(name,error) { luaL_error(L, ep_strdup_printf("%s%s", #name ": " ,error) ); return 0; }
#define WSLUA_ARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_ARG_ ## name ## _ ## attr, #name ": " error); return 0; }
@@ -403,7 +484,12 @@ typedef int dummy##C
#define WSLUA_API extern
#define NOP
-#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,s)
+#define FAIL_ON_NULL(s) if (! *p) luaL_argerror(L,idx,"null " s)
+#define FAIL_ON_NULL_OR_EXPIRED(s) if (!*p) { \
+ luaL_argerror(L,idx,"null " s); \
+ } else if ((*p)->expired) { \
+ luaL_argerror(L,idx,"expired " s); \
+ }
/* Clears or marks references that connects Lua to Wireshark structures */
#define CLEAR_OUTSTANDING(C, marker, marker_val) void clear_outstanding_##C(void) { \
@@ -440,8 +526,12 @@ extern lua_State* wslua_state(void);
extern int wslua__concat(lua_State* L);
extern gboolean wslua_optbool(lua_State* L, int n, gboolean def);
+extern int wslua_optboolint(lua_State* L, int n, int def);
extern const gchar* lua_shiftstring(lua_State* L,int idx);
extern void wslua_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
+extern const gchar* wslua_typeof_unknown;
+extern const gchar* wslua_typeof(lua_State *L, int idx);
+extern void wslua_assert_table_field_new(lua_State *L, int idx, const gchar *name);
extern int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data);
extern void wslua_prefs_changed(void);
extern void proto_register_lua(void);
diff --git a/epan/wslua/wslua_dumper.c b/epan/wslua/wslua_dumper.c
index b60c17701c..6da3f4003e 100644
--- a/epan/wslua/wslua_dumper.c
+++ b/epan/wslua/wslua_dumper.c
@@ -452,11 +452,11 @@ WSLUA_METHODS Dumper_methods[] = {
WSLUA_CLASS_FNREG(Dumper,flush),
WSLUA_CLASS_FNREG(Dumper,dump),
WSLUA_CLASS_FNREG(Dumper,dump_current),
- {0, 0}
+ { NULL, NULL }
};
WSLUA_META Dumper_meta[] = {
- {0, 0}
+ { NULL, NULL }
};
int Dumper_register(lua_State* L) {
diff --git a/epan/wslua/wslua_field.c b/epan/wslua/wslua_field.c
index 55ca858dcf..b6d1b3d606 100644
--- a/epan/wslua/wslua_field.c
+++ b/epan/wslua/wslua_field.c
@@ -36,7 +36,8 @@
#include "wslua.h"
-WSLUA_CLASS_DEFINE(FieldInfo,FAIL_ON_NULL("null FieldInfo"),NOP);
+/* any call to checkFieldInfo() will now error on null or expired, so no need to check again */
+WSLUA_CLASS_DEFINE(FieldInfo,FAIL_ON_NULL_OR_EXPIRED("FieldInfo"),NOP);
/*
An extracted Field
*/
@@ -47,50 +48,35 @@ static GPtrArray* outstanding_FieldInfo = NULL;
CLEAR_OUTSTANDING(FieldInfo,expired,TRUE)
+/* WSLUA_ATTRIBUTE FieldInfo_len RO The length of this field */
WSLUA_METAMETHOD FieldInfo__len(lua_State* L) {
/*
Obtain the Length of the field
*/
FieldInfo fi = checkFieldInfo(L,1);
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
lua_pushnumber(L,fi->ws_fi->length);
return 1;
}
+/* WSLUA_ATTRIBUTE FieldInfo_offset RO The offset of this field */
WSLUA_METAMETHOD FieldInfo__unm(lua_State* L) {
/*
Obtain the Offset of the field
*/
FieldInfo fi = checkFieldInfo(L,1);
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
lua_pushnumber(L,fi->ws_fi->start);
return 1;
}
+/* WSLUA_ATTRIBUTE FieldInfo_value RO The value of this field */
WSLUA_METAMETHOD FieldInfo__call(lua_State* L) {
/*
Obtain the Value of the field
*/
FieldInfo fi = checkFieldInfo(L,1);
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
switch(fi->ws_fi->hfinfo->type) {
case FT_BOOLEAN:
lua_pushboolean(L,(int)fvalue_get_uinteger(&(fi->ws_fi->value)));
@@ -193,16 +179,11 @@ WSLUA_METAMETHOD FieldInfo__call(lua_State* L) {
}
}
+/* WSLUA_ATTRIBUTE FieldInfo_label RO The string representing this field */
WSLUA_METAMETHOD FieldInfo__tostring(lua_State* L) {
/* The string representation of the field */
FieldInfo fi = checkFieldInfo(L,1);
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
if (fi->ws_fi->value.ftype->val_to_string_repr) {
gchar* repr = fvalue_to_string_repr(&fi->ws_fi->value,FTREPR_DISPLAY,NULL);
if (repr) {
@@ -222,19 +203,14 @@ WSLUA_METAMETHOD FieldInfo__tostring(lua_State* L) {
return 1;
}
-static int FieldInfo_display(lua_State* L) {
+/* WSLUA_ATTRIBUTE FieldInfo_display RO The string display of this field as seen in GUI */
+static int FieldInfo_get_display(lua_State* L) {
/* The display string of this field as seen in GUI */
FieldInfo fi = checkFieldInfo(L,1);
gchar label_str[ITEM_LABEL_LENGTH+1];
gchar *label_ptr;
gchar *value_ptr;
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
if (!fi->ws_fi->rep) {
label_ptr = label_str;
proto_item_fill_label(fi->ws_fi, label_str);
@@ -255,16 +231,11 @@ static int FieldInfo_display(lua_State* L) {
return 1;
}
+/* WSLUA_ATTRIBUTE FieldInfo_range RO The TvbRange covering this field */
static int FieldInfo_get_range(lua_State* L) {
/* The TvbRange covering this field */
FieldInfo fi = checkFieldInfo(L,1);
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
if (push_TvbRange (L, fi->ws_fi->ds_tvb, fi->ws_fi->start, fi->ws_fi->length)) {
return 1;
}
@@ -272,86 +243,29 @@ static int FieldInfo_get_range(lua_State* L) {
return 0;
}
+/* WSLUA_ATTRIBUTE FieldInfo_generated RO Whether this field was marked as generated (boolean) */
static int FieldInfo_get_generated(lua_State* L) {
/* Whether this field was marked as generated. */
FieldInfo fi = checkFieldInfo(L,1);
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
lua_pushboolean(L,FI_GET_FLAG(fi->ws_fi, FI_GENERATED));
return 1;
}
+/* WSLUA_ATTRIBUTE FieldInfo_name RO The name of this field */
static int FieldInfo_get_name(lua_State* L) {
/* The filter name of this field. */
FieldInfo fi = checkFieldInfo(L,1);
- if (!fi) return 0;
- if (fi->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
lua_pushstring(L,fi->ws_fi->hfinfo->abbrev);
return 1;
}
-static const luaL_Reg FieldInfo_get[] = {
- /* {"data_source", FieldInfo_get_data_source }, */
- {"range", FieldInfo_get_range},
- /* {"hidden", FieldInfo_get_hidden}, */
- {"generated", FieldInfo_get_generated},
-
- /* WSLUA_ATTRIBUTE FieldInfo_name RO The name of this field */
- {"name", FieldInfo_get_name},
- /* WSLUA_ATTRIBUTE FieldInfo_label RO The string representing this field */
- {"label", FieldInfo__tostring},
- /* WSLUA_ATTRIBUTE FieldInfo_value RO The value of this field */
- {"value", FieldInfo__call},
- /* WSLUA_ATTRIBUTE FieldInfo_tvb RO A tvbrange covering this field */
- {"tvb", FieldInfo_get_range},
- /* WSLUA_ATTRIBUTE FieldInfo_len RO The length of this field */
- {"len", FieldInfo__len},
- /* WSLUA_ATTRIBUTE FieldInfo_offset RO The offset of this field */
- {"offset", FieldInfo__unm},
- /* WSLUA_ATTRIBUTE FieldInfo_display RO The string display of this field as seen in GUI */
- {"display", FieldInfo_display},
- { NULL, NULL }
-};
-
-static int FieldInfo__index(lua_State* L) {
- /*
- Other attributes:
- */
- const gchar* idx = luaL_checkstring(L,2);
- const luaL_Reg* r;
-
- if (!idx) return 0;
-
- for (r = FieldInfo_get; r->name; r++) {
- if (g_str_equal(r->name, idx)) {
- return r->func(L);
- }
- }
-
- return 0;
-}
-
WSLUA_METAMETHOD FieldInfo__eq(lua_State* L) {
/* Checks whether lhs is within rhs */
FieldInfo l = checkFieldInfo(L,1);
FieldInfo r = checkFieldInfo(L,2);
- if (!l || !r) return 0;
- if (l->expired || r->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
if (l->ws_fi->ds_tvb != r->ws_fi->ds_tvb)
WSLUA_ERROR(FieldInfo__eq,"Data source must be the same for both fields");
@@ -368,12 +282,6 @@ WSLUA_METAMETHOD FieldInfo__le(lua_State* L) {
FieldInfo l = checkFieldInfo(L,1);
FieldInfo r = checkFieldInfo(L,2);
- if (!l || !r) return 0;
- if (l->expired || r->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
if (l->ws_fi->ds_tvb != r->ws_fi->ds_tvb)
WSLUA_ERROR(FieldInfo__le,"Data source must be the same for both fields");
@@ -390,12 +298,6 @@ WSLUA_METAMETHOD FieldInfo__lt(lua_State* L) {
FieldInfo l = checkFieldInfo(L,1);
FieldInfo r = checkFieldInfo(L,2);
- if (!l || !r) return 0;
- if (l->expired || r->expired) {
- luaL_error(L, "expired FieldInfo");
- return 0;
- }
-
if (l->ws_fi->ds_tvb != r->ws_fi->ds_tvb)
WSLUA_ERROR(FieldInfo__lt,"Data source must be the same for both fields");
@@ -409,7 +311,7 @@ WSLUA_METAMETHOD FieldInfo__lt(lua_State* L) {
/* Gets registered as metamethod automatically by WSLUA_REGISTER_META */
static int FieldInfo__gc(lua_State* L _U_) {
- FieldInfo fi = checkFieldInfo(L,1);
+ FieldInfo fi = toFieldInfo(L,1);
if (!fi) return 0;
@@ -422,20 +324,37 @@ static int FieldInfo__gc(lua_State* L _U_) {
return 0;
}
-static const luaL_Reg FieldInfo_meta[] = {
- {"__tostring", FieldInfo__tostring},
- {"__call", FieldInfo__call},
- {"__index", FieldInfo__index},
- {"__len", FieldInfo__len},
- {"__unm", FieldInfo__unm},
- {"__eq", FieldInfo__eq},
- {"__le", FieldInfo__le},
- {"__lt", FieldInfo__lt},
+/* This table is ultimately registered as a sub-table of the class' metatable,
+ * and if __index/__newindex is invoked then it calls the appropriate function
+ * from this table for getting/setting the members.
+ */
+WSLUA_ATTRIBUTES FieldInfo_attributes[] = {
+ WSLUA_ATTRIBUTE_ROREG(FieldInfo,range),
+ WSLUA_ATTRIBUTE_ROREG(FieldInfo,generated),
+ WSLUA_ATTRIBUTE_ROREG(FieldInfo,name),
+ WSLUA_ATTRIBUTE_ROREG(FieldInfo,display),
+ { "label", FieldInfo__tostring, NULL },
+ { "value", FieldInfo__call, NULL },
+ { "tvb", FieldInfo_get_range, NULL },
+ { "len", FieldInfo__len, NULL },
+ { "offset", FieldInfo__unm, NULL },
+ { NULL, NULL, NULL }
+};
+
+WSLUA_META FieldInfo_meta[] = {
+ WSLUA_CLASS_MTREG(FieldInfo,tostring),
+ WSLUA_CLASS_MTREG(FieldInfo,call),
+ WSLUA_CLASS_MTREG(FieldInfo,len),
+ WSLUA_CLASS_MTREG(FieldInfo,unm),
+ WSLUA_CLASS_MTREG(FieldInfo,eq),
+ WSLUA_CLASS_MTREG(FieldInfo,le),
+ WSLUA_CLASS_MTREG(FieldInfo,lt),
{ NULL, NULL }
};
int FieldInfo_register(lua_State* L) {
WSLUA_REGISTER_META(FieldInfo);
+ WSLUA_REGISTER_ATTRIBUTES(FieldInfo);
return 0;
}
@@ -473,7 +392,7 @@ WSLUA_FUNCTION wslua_all_field_infos(lua_State* L) {
return items_found;
}
-WSLUA_CLASS_DEFINE(Field,NOP,NOP);
+WSLUA_CLASS_DEFINE(Field,FAIL_ON_NULL("Field"),NOP);
/*
A Field extractor to to obtain field values.
*/
@@ -614,11 +533,6 @@ WSLUA_METAMETHOD Field__tostring(lua_State* L) {
/* Obtain a string with the field name */
Field f = checkField(L,1);
- if ( !(f && *f) ) {
- luaL_error(L,"invalid Field");
- return 0;
- }
-
if (wanted_fields) {
lua_pushstring(L,*((gchar**)f));
} else {
@@ -633,14 +547,14 @@ static int Field__gc(lua_State* L _U_) {
return 0;
}
-static const luaL_Reg Field_methods[] = {
- {"new", Field_new},
+WSLUA_METHODS Field_methods[] = {
+ WSLUA_CLASS_FNREG(Field,new),
{ NULL, NULL }
};
-static const luaL_Reg Field_meta[] = {
- {"__tostring", Field__tostring},
- {"__call", Field__call},
+WSLUA_META Field_meta[] = {
+ WSLUA_CLASS_MTREG(Field,tostring),
+ WSLUA_CLASS_MTREG(Field,call),
{ NULL, NULL }
};
diff --git a/epan/wslua/wslua_gui.c b/epan/wslua/wslua_gui.c
index fc421caec7..81475d9bf0 100644
--- a/epan/wslua/wslua_gui.c
+++ b/epan/wslua/wslua_gui.c
@@ -281,7 +281,7 @@ WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* Pops up a new dialog */
-WSLUA_CLASS_DEFINE(ProgDlg,NOP,NOP); /* Manages a progress bar dialog. */
+WSLUA_CLASS_DEFINE(ProgDlg,FAIL_ON_NULL("ProgDlg"),NOP); /* Manages a progress bar dialog. */
WSLUA_CONSTRUCTOR ProgDlg_new(lua_State* L) { /* Creates a new TextWindow. */
#define WSLUA_OPTARG_ProgDlg_new_TITLE 2 /* Title of the new window, defaults to "Progress". */
@@ -313,10 +313,6 @@ WSLUA_METHOD ProgDlg_update(lua_State* L) { /* Appends text */
WSLUA_ERROR(ProgDlg_update,"GUI not available");
}
- if (!pd) {
- WSLUA_ERROR(ProgDlg_update,"Cannot be called for something not a ProgDlg");
- }
-
g_free(pd->task);
pd->task = g_strdup(task);
@@ -337,10 +333,6 @@ WSLUA_METHOD ProgDlg_update(lua_State* L) { /* Appends text */
WSLUA_METHOD ProgDlg_stopped(lua_State* L) { /* Checks wheher the user has pressed the stop button. */
ProgDlg pd = checkProgDlg(L,1);
- if (!pd) {
- WSLUA_ERROR(ProgDlg_stopped,"Cannot be called for something not a ProgDlg");
- }
-
lua_pushboolean(L,pd->stopped);
WSLUA_RETURN(1); /* true if the user has asked to stop the progress. */
@@ -355,10 +347,6 @@ WSLUA_METHOD ProgDlg_close(lua_State* L) { /* Appends text */
WSLUA_ERROR(ProgDlg_close,"GUI not available");
}
- if (!pd) {
- WSLUA_ERROR(ProgDlg_update,"Cannot be called for something not a ProgDlg");
- }
-
if (pd->pw) {
ops->destroy_progress_window(pd->pw);
pd->pw = NULL;
@@ -370,18 +358,14 @@ WSLUA_METHOD ProgDlg_close(lua_State* L) { /* Appends text */
static int ProgDlg__tostring(lua_State* L) {
ProgDlg pd = checkProgDlg(L,1);
- if (pd) {
- lua_pushstring(L,ep_strdup_printf("%sstopped",pd->stopped?"":"not "));
- } else {
- luaL_error(L, "ProgDlg__tostring has being passed something else!");
- }
+ lua_pushstring(L,ep_strdup_printf("%sstopped",pd->stopped?"":"not "));
return 0;
}
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int ProgDlg__gc(lua_State* L) {
- ProgDlg pd = checkProgDlg(L,1);
+ ProgDlg pd = toProgDlg(L,1);
if (pd) {
if (pd->pw && ops->destroy_progress_window) {
@@ -402,12 +386,12 @@ WSLUA_METHODS ProgDlg_methods[] = {
WSLUA_CLASS_FNREG(ProgDlg,update),
WSLUA_CLASS_FNREG(ProgDlg,stopped),
WSLUA_CLASS_FNREG(ProgDlg,close),
- {0, 0}
+ { NULL, NULL }
};
WSLUA_META ProgDlg_meta[] = {
- {"__tostring", ProgDlg__tostring},
- {0, 0}
+ WSLUA_CLASS_MTREG(ProgDlg,tostring),
+ { NULL, NULL }
};
int ProgDlg_register(lua_State* L) {
@@ -421,7 +405,7 @@ int ProgDlg_register(lua_State* L) {
-WSLUA_CLASS_DEFINE(TextWindow,NOP,NOP); /* Manages a text window. */
+WSLUA_CLASS_DEFINE(TextWindow,FAIL_ON_NULL_OR_EXPIRED("TextWindow"),NOP); /* Manages a text window. */
/* XXX: button and close callback data is being leaked */
/* XXX: lua callback function and TextWindow are not garbage collected because
@@ -466,9 +450,6 @@ WSLUA_METHOD TextWindow_set_atclose(lua_State* L) { /* Set the function that wil
WSLUA_ERROR(TextWindow_set_atclose,"GUI not available");
}
- if (!tw)
- WSLUA_ERROR(TextWindow_at_close,"Cannot be called for something not a TextWindow");
-
lua_settop(L,2);
if (! lua_isfunction(L,2))
@@ -494,12 +475,6 @@ WSLUA_METHOD TextWindow_set(lua_State* L) { /* Sets the text. */
if (!ops->set_text)
WSLUA_ERROR(TextWindow_set,"GUI not available");
- if (!tw)
- WSLUA_ERROR(TextWindow_set,"Cannot be called for something not a TextWindow");
-
- if (tw->expired)
- WSLUA_ERROR(TextWindow_set,"Expired TextWindow");
-
if (!text)
WSLUA_ARG_ERROR(TextWindow_set,TEXT,"Must be a string");
@@ -516,12 +491,6 @@ WSLUA_METHOD TextWindow_append(lua_State* L) { /* Appends text */
if (!ops->append_text)
WSLUA_ERROR(TextWindow_append,"GUI not available");
- if (!tw)
- WSLUA_ERROR(TextWindow_append,"Cannot be called for something not a TextWindow");
-
- if (tw->expired)
- WSLUA_ERROR(TextWindow_append,"Expired TextWindow");
-
if (!text)
WSLUA_ARG_ERROR(TextWindow_append,TEXT,"Must be a string");
@@ -538,12 +507,6 @@ WSLUA_METHOD TextWindow_prepend(lua_State* L) { /* Prepends text */
if (!ops->prepend_text)
WSLUA_ERROR(TextWindow_prepend,"GUI not available");
- if (!tw)
- WSLUA_ERROR(TextWindow_prepend,"Cannot be called for something not a TextWindow");
-
- if (tw->expired)
- WSLUA_ERROR(TextWindow_prepend,"Expired TextWindow");
-
if (!text)
WSLUA_ARG_ERROR(TextWindow_prepend,TEXT,"Must be a string");
@@ -558,12 +521,6 @@ WSLUA_METHOD TextWindow_clear(lua_State* L) { /* Erases all text in the window.
if (!ops->clear_text)
WSLUA_ERROR(TextWindow_clear,"GUI not available");
- if (!tw)
- WSLUA_ERROR(TextWindow_clear,"Cannot be called for something not a TextWindow");
-
- if (tw->expired)
- WSLUA_ERROR(TextWindow_clear,"Expired TextWindow");
-
ops->clear_text(tw->ws_tw);
WSLUA_RETURN(1); /* The TextWindow object. */
@@ -576,12 +533,6 @@ WSLUA_METHOD TextWindow_get_text(lua_State* L) { /* Get the text of the window *
if (!ops->get_text)
WSLUA_ERROR(TextWindow_get_text,"GUI not available");
- if (!tw)
- WSLUA_ERROR(TextWindow_get_text,"Cannot be called for something not a TextWindow");
-
- if (tw->expired)
- WSLUA_ERROR(TextWindow_get_text,"Expired TextWindow");
-
text = ops->get_text(tw->ws_tw);
lua_pushstring(L,text);
@@ -590,7 +541,7 @@ WSLUA_METHOD TextWindow_get_text(lua_State* L) { /* Get the text of the window *
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int TextWindow__gc(lua_State* L) {
- TextWindow tw = checkTextWindow(L,1);
+ TextWindow tw = toTextWindow(L,1);
if (!tw)
return 0;
@@ -604,7 +555,6 @@ static int TextWindow__gc(lua_State* L) {
g_free(tw);
}
-
return 0;
}
@@ -617,14 +567,7 @@ WSLUA_METHOD TextWindow_set_editable(lua_State* L) { /* Make this window editabl
if (!ops->set_editable)
WSLUA_ERROR(TextWindow_set_editable,"GUI not available");
- if (!tw)
- WSLUA_ERROR(TextWindow_set_editable,"Cannot be called for something not a TextWindow");
-
- if (tw->expired)
- WSLUA_ERROR(TextWindow_set_editable,"Expired TextWindow");
-
- if (ops->set_editable)
- ops->set_editable(tw->ws_tw,editable);
+ ops->set_editable(tw->ws_tw,editable);
WSLUA_RETURN(1); /* The TextWindow object. */
}
@@ -674,12 +617,6 @@ WSLUA_METHOD TextWindow_add_button(lua_State* L) {
if (!ops->add_button)
WSLUA_ERROR(TextWindow_add_button,"GUI not available");
- if (!tw)
- WSLUA_ERROR(TextWindow_add_button,"Cannot be called for something not a TextWindow");
-
- if (tw->expired)
- WSLUA_ERROR(TextWindow_add_button,"Expired TextWindow");
-
if (! lua_isfunction(L,WSLUA_ARG_TextWindow_add_button_FUNCTION) )
WSLUA_ARG_ERROR(TextWindow_add_button,FUNCTION,"must be a function");
@@ -716,12 +653,12 @@ WSLUA_METHODS TextWindow_methods[] = {
WSLUA_CLASS_FNREG(TextWindow,set_editable),
WSLUA_CLASS_FNREG(TextWindow,get_text),
WSLUA_CLASS_FNREG(TextWindow,add_button),
- {0, 0}
+ { NULL, NULL }
};
WSLUA_META TextWindow_meta[] = {
{"__tostring", TextWindow_get_text},
- {0, 0}
+ { NULL, NULL }
};
int TextWindow_register(lua_State* L) {
diff --git a/epan/wslua/wslua_int64.c b/epan/wslua/wslua_int64.c
index 3534e3a5f3..e781747a52 100644
--- a/epan/wslua/wslua_int64.c
+++ b/epan/wslua/wslua_int64.c
@@ -275,8 +275,9 @@ WSLUA_METAMETHOD Int64__tostring(lua_State* L) {
/* Converts the Int64 into a string of decimal digits */
gint64 num = getInt64(L,1);
gchar s[LUATYPE64_STRING_SIZE];
- if (sprintf(s, "%" G_GINT64_MODIFIER "d", num) < 0)
- luaL_error(L, "Error writing Int64 to a string");
+ if (sprintf(s, "%" G_GINT64_MODIFIER "d", num) < 0) {
+ return luaL_error(L, "Error writing Int64 to a string");
+ }
lua_pushstring(L,s);
WSLUA_RETURN(1); /* The Lua string */
}
@@ -314,8 +315,9 @@ WSLUA_METAMETHOD Int64__div(lua_State* L) {
Trying to divide by zero results in a Lua error. */
Int64 num1 = getInt64(L,1);
Int64 num2 = getInt64(L,2);
- if (num2 == 0)
- luaL_error(L, "Trying to divide Int64 by zero");
+ if (num2 == 0) {
+ return luaL_error(L, "Trying to divide Int64 by zero");
+ }
pushInt64(L, num1 / num2);
WSLUA_RETURN(1); /* The Int64 object */
}
@@ -325,8 +327,9 @@ WSLUA_METAMETHOD Int64__mod(lua_State* L) {
Trying to modulo by zero results in a Lua error. */
Int64 num1 = getInt64(L,1);
Int64 num2 = getInt64(L,2);
- if (num2 == 0)
- luaL_error(L, "Trying to modulo Int64 by zero");
+ if (num2 == 0) {
+ return luaL_error(L, "Trying to modulo Int64 by zero");
+ }
pushInt64(L, num1 % num2);
WSLUA_RETURN(1); /* The Int64 object */
}
@@ -468,43 +471,43 @@ static int Int64__gc(lua_State* L _U_) {
}
static const luaL_Reg Int64_methods[] = {
- { "new", Int64_new },
- { "max", Int64_max },
- { "min", Int64_min },
- { "tonumber", Int64_tonumber },
- { "fromhex", Int64_fromhex },
- { "tohex", Int64_tohex },
- { "higher", Int64_higher },
- { "lower", Int64_lower },
- { "encode", Int64_encode },
- { "decode", Int64_decode },
- { "bnot", Int64_bnot },
- { "band", Int64_band },
- { "bor", Int64_bor },
- { "bxor", Int64_bxor },
- { "lshift", Int64_lshift },
- { "rshift", Int64_rshift },
- { "arshift", Int64_arshift },
- { "rol", Int64_rol },
- { "ror", Int64_ror },
- { "bswap", Int64_bswap },
+ WSLUA_CLASS_FNREG(Int64,new),
+ WSLUA_CLASS_FNREG(Int64,max),
+ WSLUA_CLASS_FNREG(Int64,min),
+ WSLUA_CLASS_FNREG(Int64,tonumber),
+ WSLUA_CLASS_FNREG(Int64,fromhex),
+ WSLUA_CLASS_FNREG(Int64,tohex),
+ WSLUA_CLASS_FNREG(Int64,higher),
+ WSLUA_CLASS_FNREG(Int64,lower),
+ WSLUA_CLASS_FNREG(Int64,encode),
+ WSLUA_CLASS_FNREG(Int64,decode),
+ WSLUA_CLASS_FNREG(Int64,bnot),
+ WSLUA_CLASS_FNREG(Int64,band),
+ WSLUA_CLASS_FNREG(Int64,bor),
+ WSLUA_CLASS_FNREG(Int64,bxor),
+ WSLUA_CLASS_FNREG(Int64,lshift),
+ WSLUA_CLASS_FNREG(Int64,rshift),
+ WSLUA_CLASS_FNREG(Int64,arshift),
+ WSLUA_CLASS_FNREG(Int64,rol),
+ WSLUA_CLASS_FNREG(Int64,ror),
+ WSLUA_CLASS_FNREG(Int64,bswap),
{ NULL, NULL }
};
static const luaL_Reg Int64_meta[] = {
- {"__tostring", Int64__tostring},
- {"__call", Int64__call},
- {"__concat", wslua__concat},
- {"__unm", Int64__unm},
- {"__add", Int64__add},
- {"__sub", Int64__sub},
- {"__mul", Int64__mul},
- {"__div", Int64__div},
- {"__mod", Int64__mod},
- {"__pow", Int64__pow},
- {"__eq", Int64__eq},
- {"__lt", Int64__lt},
- {"__le", Int64__le},
+ WSLUA_CLASS_MTREG(Int64,tostring),
+ WSLUA_CLASS_MTREG(Int64,call),
+ WSLUA_CLASS_MTREG(wslua,concat),
+ WSLUA_CLASS_MTREG(Int64,unm),
+ WSLUA_CLASS_MTREG(Int64,add),
+ WSLUA_CLASS_MTREG(Int64,sub),
+ WSLUA_CLASS_MTREG(Int64,mul),
+ WSLUA_CLASS_MTREG(Int64,div),
+ WSLUA_CLASS_MTREG(Int64,mod),
+ WSLUA_CLASS_MTREG(Int64,pow),
+ WSLUA_CLASS_MTREG(Int64,eq),
+ WSLUA_CLASS_MTREG(Int64,lt),
+ WSLUA_CLASS_MTREG(Int64,le),
{ NULL, NULL }
};
@@ -682,8 +685,9 @@ WSLUA_METAMETHOD UInt64__tostring(lua_State* L) {
/* Converts the UInt64 into a string */
guint64 num = getUInt64(L,1);
gchar s[LUATYPE64_STRING_SIZE];
- if (sprintf(s, "%" G_GINT64_MODIFIER "u",(guint64)num) < 0)
- luaL_error(L, "Error writing UInt64 to a string");
+ if (sprintf(s, "%" G_GINT64_MODIFIER "u",(guint64)num) < 0) {
+ return luaL_error(L, "Error writing UInt64 to a string");
+ }
lua_pushstring(L,s);
WSLUA_RETURN(1); /* The Lua string */
}
@@ -761,8 +765,9 @@ WSLUA_METAMETHOD UInt64__div(lua_State* L) {
Trying to divide by zero results in a Lua error. */
UInt64 num1 = getUInt64(L,1);
UInt64 num2 = getUInt64(L,2);
- if (num2 == 0)
- luaL_error(L, "Trying to divide UInt64 by zero");
+ if (num2 == 0) {
+ return luaL_error(L, "Trying to divide UInt64 by zero");
+ }
pushUInt64(L, num1 / num2);
WSLUA_RETURN(1); /* The UInt64 result */
}
@@ -772,8 +777,9 @@ WSLUA_METAMETHOD UInt64__mod(lua_State* L) {
Trying to modulo by zero results in a Lua error. */
UInt64 num1 = getUInt64(L,1);
UInt64 num2 = getUInt64(L,2);
- if (num2 == 0)
- luaL_error(L, "Trying to modulo UInt64 by zero");
+ if (num2 == 0) {
+ return luaL_error(L, "Trying to modulo UInt64 by zero");
+ }
pushUInt64(L, num1 % num2);
WSLUA_RETURN(1); /* The UInt64 result */
}
@@ -900,43 +906,43 @@ static int UInt64__gc(lua_State* L _U_) {
}
static const luaL_Reg UInt64_methods[] = {
- { "new", UInt64_new },
- { "max", UInt64_max },
- { "min", UInt64_min },
- { "tonumber", UInt64_tonumber },
- { "fromhex", UInt64_fromhex },
- { "tohex", UInt64_tohex },
- { "higher", UInt64_higher },
- { "lower", UInt64_lower },
- { "encode", UInt64_encode },
- { "decode", UInt64_decode },
- { "bnot", UInt64_bnot },
- { "band", UInt64_band },
- { "bor", UInt64_bor },
- { "bxor", UInt64_bxor },
- { "lshift", UInt64_lshift },
- { "rshift", UInt64_rshift },
- { "arshift", UInt64_arshift },
- { "rol", UInt64_rol },
- { "ror", UInt64_ror },
- { "bswap", UInt64_bswap },
+ WSLUA_CLASS_FNREG(UInt64,new),
+ WSLUA_CLASS_FNREG(UInt64,max),
+ WSLUA_CLASS_FNREG(UInt64,min),
+ WSLUA_CLASS_FNREG(UInt64,tonumber),
+ WSLUA_CLASS_FNREG(UInt64,fromhex),
+ WSLUA_CLASS_FNREG(UInt64,tohex),
+ WSLUA_CLASS_FNREG(UInt64,higher),
+ WSLUA_CLASS_FNREG(UInt64,lower),
+ WSLUA_CLASS_FNREG(UInt64,encode),
+ WSLUA_CLASS_FNREG(UInt64,decode),
+ WSLUA_CLASS_FNREG(UInt64,bnot),
+ WSLUA_CLASS_FNREG(UInt64,band),
+ WSLUA_CLASS_FNREG(UInt64,bor),
+ WSLUA_CLASS_FNREG(UInt64,bxor),
+ WSLUA_CLASS_FNREG(UInt64,lshift),
+ WSLUA_CLASS_FNREG(UInt64,rshift),
+ WSLUA_CLASS_FNREG(UInt64,arshift),
+ WSLUA_CLASS_FNREG(UInt64,rol),
+ WSLUA_CLASS_FNREG(UInt64,ror),
+ WSLUA_CLASS_FNREG(UInt64,bswap),
{ NULL, NULL }
};
static const luaL_Reg UInt64_meta[] = {
- {"__tostring", UInt64__tostring},
- {"__call", UInt64__call},
- {"__concat", wslua__concat},
- {"__unm", UInt64__unm},
- {"__add", UInt64__add},
- {"__sub", UInt64__sub},
- {"__mul", UInt64__mul},
- {"__div", UInt64__div},
- {"__mod", UInt64__mod},
- {"__pow", UInt64__pow},
- {"__eq", UInt64__eq},
- {"__lt", UInt64__lt},
- {"__le", UInt64__le},
+ WSLUA_CLASS_MTREG(UInt64,tostring),
+ WSLUA_CLASS_MTREG(UInt64,call),
+ WSLUA_CLASS_MTREG(wslua,concat),
+ WSLUA_CLASS_MTREG(UInt64,unm),
+ WSLUA_CLASS_MTREG(UInt64,add),
+ WSLUA_CLASS_MTREG(UInt64,sub),
+ WSLUA_CLASS_MTREG(UInt64,mul),
+ WSLUA_CLASS_MTREG(UInt64,div),
+ WSLUA_CLASS_MTREG(UInt64,mod),
+ WSLUA_CLASS_MTREG(UInt64,pow),
+ WSLUA_CLASS_MTREG(UInt64,eq),
+ WSLUA_CLASS_MTREG(UInt64,lt),
+ WSLUA_CLASS_MTREG(UInt64,le),
{ NULL, NULL }
};
diff --git a/epan/wslua/wslua_internals.c b/epan/wslua/wslua_internals.c
new file mode 100644
index 0000000000..e1c77b56fc
--- /dev/null
+++ b/epan/wslua/wslua_internals.c
@@ -0,0 +1,302 @@
+/*
+ * wslua_internals.c
+ *
+ * Wireshark's interface to the Lua Programming Language
+ *
+ * This file is for internal WSLUA functions - not ones exposed into Lua.
+ *
+ * (c) 2013, Hadriel Kaplan <hadrielk@yahoo.com>
+ *
+ * $Id: wslua_internals.c 47885 2013-02-25 22:05:28Z hadrielk $
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "wslua.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;
+}
+
+/* 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;
+
+ if ( lua_isnumber(L,n) ) {
+ val = (int)lua_tointeger(L,n);
+ } else if ( lua_isboolean(L,n) ) {
+ val = lua_toboolean(L,n) ? 1 : 0;
+ } else if ( lua_isnil(L,n) || lua_gettop(L) < n ){
+ val = def;
+ } else {
+ luaL_argerror(L,n,"must be a boolean or integer");
+ }
+
+ 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 */
+}
+
+/* identical to lua_getfield but without triggering metamethods */
+WSLUA_API void lua_rawgetfield(lua_State *L, int index, const char *k) {
+ lua_pushstring(L, k);
+ lua_rawget(L, index);
+}
+
+/* identical to lua_setfield but without triggering metamethods */
+WSLUA_API void lua_rawsetfield (lua_State *L, int index, const char *k) {
+ lua_pushstring(L, k);
+ lua_insert(L, -2);
+ lua_rawset(L, index);
+}
+
+WSLUA_API void wslua_print_stack(char* s, lua_State* L) {
+ int i;
+
+ for (i=1;i<=lua_gettop(L);i++) {
+ printf("%s-%i: %s\n",s,i,lua_typename (L,lua_type(L, i)));
+ }
+ printf("\n");
+}
+
+
+/* C-code function equivalent of the typeof() function we created in Lua.
+ * The Lua one is for Lua scripts to use, this one is for C-code to use.
+ */
+const gchar* wslua_typeof_unknown = "UNKNOWN";
+const gchar* wslua_typeof(lua_State *L, int idx) {
+ const gchar *classname = wslua_typeof_unknown;
+ /* we'll try getting the class name for error reporting*/
+ if (luaL_getmetafield(L, idx, WSLUA_TYPEOF_FIELD)) {
+ classname = luaL_optstring(L, -1, wslua_typeof_unknown);
+ lua_pop(L,1); /* pop __typeof result */
+ }
+ else if (lua_type(L,idx) == LUA_TTABLE) {
+ lua_rawgetfield(L, idx, WSLUA_TYPEOF_FIELD);
+ classname = luaL_optstring(L, -1, wslua_typeof_unknown);
+ lua_pop(L,1); /* pop __typeof result */
+ }
+ return classname;
+}
+
+/* This verifies/asserts that field 'name' doesn't already exist in table at location idx.
+ * If it does, this EXITS wireshark, because this is a fundamental programming error.
+ * As such, this function is only useful for special circumstances, notably
+ * those that will happen on application start every time, as opposed to
+ * something that could happen only if a Lua script makes it happen.
+ */
+void wslua_assert_table_field_new(lua_State *L, int idx, const gchar *name) {
+ lua_rawgetfield(L, idx, name);
+ if (!lua_isnil (L, -1)) {
+ fprintf(stderr, "ERROR: Field %s already exists!\n", name);
+ exit(1);
+ }
+ lua_pop (L, 1); /* pop the nil */ \
+}
+
+/* This function is an attribute field __index/__newindex (ie, getter/setter) dispatcher.
+ * What the heck does that mean? Well, when a Lua script tries to retrieve a
+ * table/userdata field by doing this:
+ * local foo = myobj.fieldname
+ * if 'fieldname' does not exist in the 'myobj' table/userdata, then Lua calls
+ * the '__index' metamethod of the table/userdata, and puts onto the Lua
+ * stack the table and fieldname string. So this function here handles that,
+ * by dispatching that request to the appropriate getter function in the
+ * __getters table within the metatable of the userdata. That table and
+ * its functions were populated by the WSLUA_REGISTER_ATTRIBUTES() macro, and
+ * really by wslua_reg_attributes().
+ */
+
+static int wslua_attribute_dispatcher (lua_State *L) {
+ lua_CFunction cfunc = NULL;
+ const gchar *fieldname = lua_shiftstring(L,2); /* remove the field name */
+ const gchar *classname = NULL;
+ const gchar *type = NULL;
+
+ /* the userdata object is at index 1, fieldname was at 2 but no longer,
+ now we get the getter/setter table at upvalue 1 */
+ if (!lua_istable(L, lua_upvalueindex(1)))
+ return luaL_error(L, "Accessor dispatcher cannot retrieve the metatable");
+
+ lua_rawgetfield(L, lua_upvalueindex(1), fieldname); /* field's cfunction is now at -1 */
+
+ if (!lua_iscfunction(L, -1)) {
+ lua_pop(L,1); /* pop whatever we got before */
+ /* check if there's a methods table */
+ if (lua_istable(L, lua_upvalueindex(2))) {
+ lua_rawgetfield(L, lua_upvalueindex(2), fieldname);
+ if (lua_iscfunction(L,-1)) {
+ /* we found a method for Lua to call, so give it back to Lua */
+ return 1;
+ }
+ lua_pop(L,1); /* pop whatever we got before */
+ }
+ classname = wslua_typeof(L, 1);
+ type = wslua_typeof(L, lua_upvalueindex(1));
+ lua_pop(L, 1); /* pop the nil/invalid getfield result */
+ return luaL_error(L, "No such '%s' %s attribute/field for object type '%s'", fieldname, type, classname);
+ }
+
+ cfunc = lua_tocfunction(L, -1);
+ lua_pop(L, 1); /* pop the cfunction */
+
+ /* the stack is now as if it had been calling the getter/setter c-function directly, so do it */
+ return (*cfunc)(L);
+}
+
+
+/* This function "registers" attribute functions - i.e., getters/setters for Lua objects.
+ * This way we don't have to write the __index/__newindex function dispatcher for every
+ * wslua class. Instead, your class should use WSLUA_REGISTER_ATTRIBUTES(classname), which
+ * ultimately calls this one - it calls it twice: once to register getters, once to register
+ * setters.
+ *
+ * The way this all works is every wslua class has a metatable. One of the fields of that
+ * metatable is a __index field, used for "getter" access, and a __newindex field used for
+ * "setter" access. If the __index field's _value_ is a Lua table, then Lua looks
+ * up that table, as it does for class methods for example; but if the __index field's
+ * value is a function/cfunction, Lua calls it instead to get/set the field. So
+ * we use that behavior to access our getters/setters, by creating a table of getter
+ * cfunctions, saving that as an upvalue of a dispatcher cfunction, and using that
+ * dispatcher cfunction as the value of the __index field of the metatable of the wslua object.
+ *
+ * In some cases, the metatable _index/__newindex will already be a table; for example if
+ * class methods were registered, then __index will already be a table. In that case, we
+ * move the existing one to be an upvalue of the attribute dispatcher function. The attribute
+ * dispatcher will look for it and return the method, if it doesn't find an attribute field.
+ * The code below makes sure the attribute names don't overlap with method names.
+ *
+ * This function assumes there's a class metatable on top of the stack when it's initially called,
+ * and leaves it on top when done.
+ */
+int wslua_reg_attributes(lua_State *L, const wslua_attribute_table *t, gboolean is_getter) {
+ int midx = lua_gettop(L);
+ const gchar *metafield = is_getter ? "__index" : "__newindex";
+ int idx;
+ int nup = 1; /* number of upvalues */
+
+ if (!lua_istable(L, midx)) {
+ fprintf(stderr, "No metatable in the Lua stack when registering attributes!\n");
+ exit(1);
+ }
+
+ /* check if there's a __index/__newindex table already - could be if this class has methods */
+ lua_rawgetfield(L, midx, metafield);
+ if (lua_isnil(L, -1)) {
+ /* there isn't one, pop the nil */
+ lua_pop(L,1);
+ }
+ else if (lua_istable(L, -1)) {
+ /* there is one, so make it be the attribute dispatchers upvalue #2 table */
+ nup = 2;
+ }
+ else {
+ fprintf(stderr, "'%s' field is not a table in the Lua stack when registering attributes!\n", metafield);
+ exit(1);
+ }
+
+ /* make our new getter/setter table - we don't need to pop it later */
+ lua_newtable(L);
+ idx = lua_gettop(L);
+
+ /* fill the getter/setter table with given functions */
+ for (; t->fieldname != NULL; t++) {
+ lua_CFunction cfunc = is_getter ? t->getfunc : t->setfunc;
+ if (cfunc) {
+ /* if there's a previous methods table, make sure this attribute name doesn't collide */
+ if (nup > 1) {
+ lua_rawgetfield(L, -2, t->fieldname);
+ if (!lua_isnil(L,-1)) {
+ fprintf(stderr, "'%s' attribute name already exists as method name for the class\n", t->fieldname);
+ exit(1);
+ }
+ lua_pop(L,1); /* pop the nil */
+ }
+ lua_pushcfunction(L, cfunc);
+ lua_rawsetfield(L, idx, t->fieldname);
+ }
+ }
+
+ /* push the getter/setter table name into its table, for error reporting purposes */
+ lua_pushstring(L, (is_getter ? "getter" : "setter"));
+ lua_rawsetfield(L, idx, WSLUA_TYPEOF_FIELD);
+
+ /* copy table into the class's metatable, for introspection */
+ lua_pushvalue(L, idx);
+ lua_rawsetfield(L, midx, (is_getter ? "__getters" : "__setters"));
+
+ if (nup > 1) {
+ /* we've got more than one upvalue, so move the new getter/setter to the bottom-most of those */
+ lua_insert(L,-nup);
+ }
+
+ /* we should now be back to having gettter/setter table at -1 (or -2 if there was a previous methods table) */
+ /* create upvalue of getter/setter table for wslua_attribute_dispatcher function */
+ lua_pushcclosure(L, wslua_attribute_dispatcher, nup); /* pushes cfunc with upvalue, removes getter/setter table */
+ lua_rawsetfield(L, midx, metafield); /* sets this dispatch function as __index/__newindex field of metatable */
+
+ /* we should now be back to real metatable being on top */
+ return 0;
+}
diff --git a/epan/wslua/wslua_listener.c b/epan/wslua/wslua_listener.c
index 33f4ddc595..ec4d838db8 100644
--- a/epan/wslua/wslua_listener.c
+++ b/epan/wslua/wslua_listener.c
@@ -36,7 +36,7 @@
#include "wslua.h"
-WSLUA_CLASS_DEFINE(Listener,NOP,NOP);
+WSLUA_CLASS_DEFINE(Listener,FAIL_ON_NULL("Listener"),NOP);
/*
A Listener, is called once for every packet that matches a certain filter or has a certain tap.
It can read the tree, the packet's Tvb eventually the tapped data but it cannot
@@ -143,10 +143,10 @@ static int tap_reset_cb_error_handler(lua_State* L) {
static void lua_tap_reset(void *tapdata) {
Listener tap = (Listener)tapdata;
- if (tap->init_ref == LUA_NOREF) return;
+ if (tap->reset_ref == LUA_NOREF) return;
lua_pushcfunction(tap->L,tap_reset_cb_error_handler);
- lua_rawgeti(tap->L, LUA_REGISTRYINDEX, tap->init_ref);
+ lua_rawgeti(tap->L, LUA_REGISTRYINDEX, tap->reset_ref);
switch ( lua_pcall(tap->L,0,0,1) ) {
case 0:
@@ -206,7 +206,7 @@ WSLUA_CONSTRUCTOR Listener_new(lua_State* L) {
tap->L = L;
tap->packet_ref = LUA_NOREF;
tap->draw_ref = LUA_NOREF;
- tap->init_ref = LUA_NOREF;
+ tap->reset_ref = LUA_NOREF;
/*
* XXX - do all Lua taps require the protocol tree? If not, it might
@@ -234,8 +234,6 @@ WSLUA_METHOD Listener_remove(lua_State* L) {
/* Removes a tap listener */
Listener tap = checkListener(L,1);
- if (!tap) return 0;
-
remove_tap_listener(tap);
return 0;
@@ -246,8 +244,6 @@ WSLUA_METAMETHOD Listener__tostring(lua_State* L) {
Listener tap = checkListener(L,1);
gchar* str;
- if (!tap) return 0;
-
str = ep_strdup_printf("Listener(%s) filter: %s",tap->name, tap->filter ? tap->filter : "NONE");
lua_pushstring(L,str);
@@ -255,69 +251,59 @@ WSLUA_METAMETHOD Listener__tostring(lua_State* L) {
}
-static int Listener__newindex(lua_State* L) {
- /* WSLUA_ATTRIBUTE Listener_packet WO A function that will be called once every packet matches the Listener listener filter.
+/* WSLUA_ATTRIBUTE Listener_packet WO A function that will be called once every packet matches the Listener listener filter.
- function tap.packet(pinfo,tvb,tapinfo) ... end
- Note: tapinfo is a table of info based on the Listener's type, or nil.
- */
- /* WSLUA_ATTRIBUTE Listener_draw WO A function that will be called once every few seconds to redraw the gui objects;
- in tshark this funtion is called only at the very end of the capture file.
+ function tap.packet(pinfo,tvb,tapinfo) ... end
+ Note: tapinfo is a table of info based on the Listener's type, or nil.
+*/
+WSLUA_ATTRIBUTE_FUNC_SETTER(Listener,packet);
- function tap.draw() ... end
- */
- /* WSLUA_ATTRIBUTE Listener_reset WO A function that will be called at the end of the capture run.
- function tap.reset() ... end
- */
- Listener tap = shiftListener(L,1);
- const gchar* idx = lua_shiftstring(L,1);
- int* refp = NULL;
+/* WSLUA_ATTRIBUTE Listener_draw WO A function that will be called once every few seconds to redraw the gui objects;
+ in tshark this funtion is called only at the very end of the capture file.
- if (!idx) return 0;
+ function tap.draw() ... end
+*/
+WSLUA_ATTRIBUTE_FUNC_SETTER(Listener,draw);
- if (g_str_equal(idx,"packet")) {
- refp = &(tap->packet_ref);
- } else if (g_str_equal(idx,"draw")) {
- refp = &(tap->draw_ref);
- } else if (g_str_equal(idx,"reset")) {
- refp = &(tap->init_ref);
- } else {
- luaL_error(L,"No such attribute `%s' for a tap",idx);
- return 0;
- }
+/* WSLUA_ATTRIBUTE Listener_reset WO A function that will be called at the end of the capture run.
- if (! lua_isfunction(L,1)) {
- luaL_error(L,"Listener's attribute `%s' must be a function");
- return 0;
- }
+ function tap.reset() ... end
+*/
+WSLUA_ATTRIBUTE_FUNC_SETTER(Listener,reset);
- lua_pushvalue(L, 1);
- *refp = luaL_ref(L, LUA_REGISTRYINDEX);
-
- return 0;
-}
static int Listener__gc(lua_State* L _U_) {
/* do NOT free Listener, it's never free'd */
return 0;
}
-static const luaL_Reg Listener_methods[] = {
- {"new", Listener_new},
- {"remove", Listener_remove},
+/* This table is ultimately registered as a sub-table of the class' metatable,
+ * and if __index/__newindex is invoked then it calls the appropriate function
+ * from this table for getting/setting the members.
+ */
+WSLUA_ATTRIBUTES Listener_attributes[] = {
+ WSLUA_ATTRIBUTE_WOREG(Listener,packet),
+ WSLUA_ATTRIBUTE_WOREG(Listener,draw),
+ WSLUA_ATTRIBUTE_WOREG(Listener,reset),
+ { NULL, NULL, NULL }
+};
+
+WSLUA_METHODS Listener_methods[] = {
+ WSLUA_CLASS_FNREG(Listener,new),
+ WSLUA_CLASS_FNREG(Listener,remove),
{ NULL, NULL }
};
-static const luaL_Reg Listener_meta[] = {
- {"__tostring", Listener__tostring},
- {"__newindex", Listener__newindex},
+WSLUA_META Listener_meta[] = {
+ WSLUA_CLASS_MTREG(Listener,tostring),
{ NULL, NULL }
};
int Listener_register(lua_State* L) {
wslua_set_tap_enums(L);
WSLUA_REGISTER_CLASS(Listener);
+ WSLUA_REGISTER_ATTRIBUTES(Listener);
return 0;
}
diff --git a/epan/wslua/wslua_pinfo.c b/epan/wslua/wslua_pinfo.c
index 4f0b29981f..fd5df90418 100644
--- a/epan/wslua/wslua_pinfo.c
+++ b/epan/wslua/wslua_pinfo.c
@@ -71,7 +71,7 @@ Pinfo* push_Pinfo(lua_State* L, packet_info* ws_pinfo) {
#define PUSH_COLUMNS(L,c) {g_ptr_array_add(outstanding_Columns,c);pushColumns(L,c);}
#define PUSH_PRIVATE_TABLE(L,c) {g_ptr_array_add(outstanding_PrivateTable,c);pushPrivateTable(L,c);}
-WSLUA_CLASS_DEFINE(NSTime,NOP,NOP);
+WSLUA_CLASS_DEFINE(NSTime,FAIL_ON_NULL("NSTime"),NOP);
/* NSTime represents a nstime_t. This is an object with seconds and nano seconds. */
WSLUA_CONSTRUCTOR NSTime_new(lua_State *L) {
@@ -90,11 +90,16 @@ WSLUA_CONSTRUCTOR NSTime_new(lua_State *L) {
WSLUA_RETURN(1); /* The new NSTime object. */
}
+WSLUA_METAMETHOD NSTime__call(lua_State* L) { /* Creates a NSTime object */
+#define WSLUA_OPTARG_NSTime__call_SECONDS 1 /* Seconds */
+#define WSLUA_OPTARG_NSTime__call_NSECONDS 2 /* Nano seconds */
+ lua_remove(L,1); /* remove the table */
+ WSLUA_RETURN(NSTime_new(L)); /* The new NSTime object. */
+}
+
WSLUA_METAMETHOD NSTime__tostring(lua_State* L) {
NSTime nstime = checkNSTime(L,1);
- if (!nstime) return 0;
-
lua_pushstring(L,ep_strdup_printf("%ld.%09d", (long)nstime->secs, nstime->nsecs));
WSLUA_RETURN(1); /* The string representing the nstime. */
@@ -137,9 +142,6 @@ WSLUA_METAMETHOD NSTime__eq(lua_State* L) { /* Compares two NSTimes */
NSTime time2 = checkNSTime(L,2);
gboolean result = FALSE;
- if (!time1 || !time2)
- WSLUA_ERROR(NSTime__eq,"Both values must be a NSTime");
-
if (nstime_cmp(time1, time2) == 0)
result = TRUE;
@@ -153,9 +155,6 @@ WSLUA_METAMETHOD NSTime__le(lua_State* L) { /* Compares two NSTimes */
NSTime time2 = checkNSTime(L,2);
gboolean result = FALSE;
- if (!time1 || !time2)
- WSLUA_ERROR(NSTime__le,"Both values must be a NSTime");
-
if (nstime_cmp(time1, time2) <= 0)
result = TRUE;
@@ -169,9 +168,6 @@ WSLUA_METAMETHOD NSTime__lt(lua_State* L) { /* Compares two NSTimes */
NSTime time2 = checkNSTime(L,2);
gboolean result = FALSE;
- if (!time1 || !time2)
- WSLUA_ERROR(NSTime__lt,"Both values must be a NSTime");
-
if (nstime_cmp(time1, time2) < 0)
result = TRUE;
@@ -180,104 +176,18 @@ WSLUA_METAMETHOD NSTime__lt(lua_State* L) { /* Compares two NSTimes */
return 1;
}
-typedef struct {
- const gchar* name;
- lua_CFunction get;
- lua_CFunction set;
-} nstime_actions_t;
-
-static int NSTime_get_secs(lua_State* L) {
- NSTime nstime = toNSTime(L,1);
-
- lua_pushnumber (L,(lua_Number)(nstime->secs));
-
- return 1;
-}
-
-static int NSTime_set_secs(lua_State* L)
- {
- NSTime nstime = toNSTime(L,1);
- time_t secs = luaL_checkint(L,3);
-
- nstime->secs = secs;
-
- return 0;
-}
-
-static int NSTime_get_nsecs(lua_State* L) {
- NSTime nstime = toNSTime(L,1);
-
- lua_pushnumber (L,(lua_Number)(nstime->nsecs));
-
- return 1;
-}
-
-static int NSTime_set_nsecs(lua_State* L) {
- NSTime nstime = toNSTime(L,1);
- int nsecs = luaL_checkint(L,3);
-
- nstime->nsecs = nsecs;
-
- return 0;
-}
-
-static const nstime_actions_t nstime_actions[] = {
- /* WSLUA_ATTRIBUTE NSTime_secs RW The NSTime seconds */
- {"secs", NSTime_get_secs, NSTime_set_secs},
-
- /* WSLUA_ATTRIBUTE NSTime_nsecs RW The NSTime nano seconds */
- {"nsecs", NSTime_get_nsecs, NSTime_set_nsecs},
-
- {NULL,NULL,NULL}
-};
-
-static int NSTime__index(lua_State* L) {
- NSTime nstime = checkNSTime(L,1);
- const gchar* name = luaL_checkstring(L,2);
- const nstime_actions_t* pa;
-
- if (! (nstime && name) ) return 0;
-
- for (pa = nstime_actions; pa->name; pa++) {
- if ( g_str_equal(name,pa->name) ) {
- if (pa->get) {
- return pa->get(L);
- } else {
- luaL_error(L,"You cannot get the `%s' attribute of a nstime",name);
- return 0;
- }
- }
- }
- luaL_error(L,"A protocol doesn't have a `%s' nstime",name);
- return 0;
-}
+/* WSLUA_ATTRIBUTE NSTime_secs RW The NSTime seconds */
+WSLUA_ATTRIBUTE_NUMBER_GETTER(NSTime,secs);
+WSLUA_ATTRIBUTE_NUMBER_SETTER(NSTime,secs,time_t);
-static int NSTime__newindex(lua_State* L) {
- NSTime nstime = checkNSTime(L,1);
- const gchar* name = luaL_checkstring(L,2);
- const nstime_actions_t* pa;
-
- if (! (nstime && name) ) return 0;
-
- for (pa = nstime_actions; pa->name; pa++) {
- if ( g_str_equal(name,pa->name) ) {
- if (pa->set) {
- return pa->set(L);
- } else {
- luaL_error(L,"You cannot set the `%s' attribute of a nstime",name);
- return 0;
- }
- }
- }
-
- luaL_error(L,"A protocol doesn't have a `%s' nstime",name);
- return 0;
-}
+/* WSLUA_ATTRIBUTE NSTime_nsecs RW The NSTime nano seconds */
+WSLUA_ATTRIBUTE_NUMBER_GETTER(NSTime,nsecs);
+WSLUA_ATTRIBUTE_NUMBER_SETTER(NSTime,nsecs,int);
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int NSTime__gc(lua_State* L) {
- NSTime nstime = checkNSTime(L,1);
+ NSTime nstime = toNSTime(L,1);
if (!nstime) return 0;
@@ -285,29 +195,40 @@ static int NSTime__gc(lua_State* L) {
return 0;
}
-WSLUA_META NSTime_meta[] = {
- {"__index", NSTime__index},
- {"__newindex", NSTime__newindex},
- {"__tostring", NSTime__tostring},
- {"__add", NSTime__add},
- {"__sub", NSTime__sub},
- {"__unm", NSTime__unm},
- {"__eq", NSTime__eq},
- {"__le", NSTime__le},
- {"__lt", NSTime__lt},
- { NULL, NULL}
+/* This table is ultimately registered as a sub-table of the class' metatable,
+ * and if __index/__newindex is invoked then it calls the appropriate function
+ * from this table for getting/setting the members.
+ */
+WSLUA_ATTRIBUTES NSTime_attributes[] = {
+ WSLUA_ATTRIBUTE_RWREG(NSTime,secs),
+ WSLUA_ATTRIBUTE_RWREG(NSTime,nsecs),
+ { NULL, NULL, NULL }
};
-int NSTime_register(lua_State* L) {
- WSLUA_REGISTER_META(NSTime);
+WSLUA_METHODS NSTime_methods[] = {
+ WSLUA_CLASS_FNREG(NSTime,new),
+ { NULL, NULL }
+};
- lua_pushcfunction(L, NSTime_new);
- lua_setglobal(L, "NSTime");
+WSLUA_META NSTime_meta[] = {
+ WSLUA_CLASS_MTREG(NSTime,tostring),
+ WSLUA_CLASS_MTREG(NSTime,add),
+ WSLUA_CLASS_MTREG(NSTime,sub),
+ WSLUA_CLASS_MTREG(NSTime,unm),
+ WSLUA_CLASS_MTREG(NSTime,eq),
+ WSLUA_CLASS_MTREG(NSTime,le),
+ WSLUA_CLASS_MTREG(NSTime,lt),
+ WSLUA_CLASS_MTREG(NSTime,call),
+ { NULL, NULL }
+};
+int NSTime_register(lua_State* L) {
+ WSLUA_REGISTER_CLASS(NSTime);
+ WSLUA_REGISTER_ATTRIBUTES(NSTime);
return 0;
}
-WSLUA_CLASS_DEFINE(Address,NOP,NOP); /* Represents an address */
+WSLUA_CLASS_DEFINE(Address,FAIL_ON_NULL("Address"),NOP); /* Represents an address */
WSLUA_CONSTRUCTOR Address_ip(lua_State* L) {
/* Creates an Address Object representing an IP address. */
@@ -452,7 +373,7 @@ WSLUA_METHODS Address_methods[] = {
WSLUA_CLASS_FNREG(Address,uri),
WSLUA_CLASS_FNREG(Address,tipc),
#endif
- {0,0}
+ { NULL, NULL }
};
WSLUA_METAMETHOD Address__tostring(lua_State* L) {
@@ -465,7 +386,7 @@ WSLUA_METAMETHOD Address__tostring(lua_State* L) {
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int Address__gc(lua_State* L) {
- Address addr = checkAddress(L,1);
+ Address addr = toAddress(L,1);
if (addr) {
g_free((void*)addr->data);
@@ -515,11 +436,11 @@ WSLUA_METAMETHOD Address__lt(lua_State* L) { /* Compares two Addresses */
}
WSLUA_META Address_meta[] = {
- {"__tostring", Address__tostring },
- {"__eq",Address__eq},
- {"__le",Address__le},
- {"__lt",Address__lt},
- {0,0}
+ WSLUA_CLASS_MTREG(Address,tostring),
+ WSLUA_CLASS_MTREG(Address,eq),
+ WSLUA_CLASS_MTREG(Address,le),
+ WSLUA_CLASS_MTREG(Address,lt),
+ { NULL, NULL }
};
@@ -529,7 +450,7 @@ int Address_register(lua_State *L) {
}
-WSLUA_CLASS_DEFINE(Column,FAIL_ON_NULL("expired column"),NOP); /* A Column in the packet list */
+WSLUA_CLASS_DEFINE(Column,FAIL_ON_NULL("Column"),NOP); /* A Column in the packet list */
struct col_names_t {
const gchar* name;
@@ -610,10 +531,7 @@ WSLUA_METAMETHOD Column__tostring(lua_State *L) {
Column c = checkColumn(L,1);
const gchar* text;
- if (!c) {
- lua_pushstring(L,"(nil)");
- }
- else if (!c->cinfo) {
+ if (!c->cinfo) {
text = col_id_to_name(c->col);
lua_pushfstring(L, "(%s)", text ? text : "unknown");
}
@@ -627,7 +545,7 @@ WSLUA_METAMETHOD Column__tostring(lua_State *L) {
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS */
static int Column__gc(lua_State* L) {
- Column col = checkColumn(L,1);
+ Column col = toColumn(L,1);
if (!col) return 0;
@@ -644,7 +562,7 @@ WSLUA_METHOD Column_clear(lua_State *L) {
/* Clears a Column */
Column c = checkColumn(L,1);
- if (!(c && c->cinfo)) return 0;
+ if (!(c->cinfo)) return 0;
col_clear(c->cinfo, c->col);
@@ -657,7 +575,7 @@ WSLUA_METHOD Column_set(lua_State *L) {
Column c = checkColumn(L,1);
const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_set_TEXT);
- if (!(c && c->cinfo))
+ if (!(c->cinfo))
return 0;
if (!s) WSLUA_ARG_ERROR(Column_set,TEXT,"must be a string");
@@ -673,7 +591,7 @@ WSLUA_METHOD Column_append(lua_State *L) {
Column c = checkColumn(L,1);
const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_append_TEXT);
- if (!(c && c->cinfo))
+ if (!(c->cinfo))
return 0;
if (!s) WSLUA_ARG_ERROR(Column_append,TEXT,"must be a string");
@@ -689,7 +607,7 @@ WSLUA_METHOD Column_prepend(lua_State *L) {
Column c = checkColumn(L,1);
const gchar* s = luaL_checkstring(L,WSLUA_ARG_Column_prepend_TEXT);
- if (!(c && c->cinfo))
+ if (!(c->cinfo))
return 0;
if (!s) WSLUA_ARG_ERROR(Column_prepend,TEXT,"must be a string");
@@ -703,21 +621,21 @@ WSLUA_METHOD Column_fence(lua_State *L) {
/* Sets Column text fence, to prevent overwriting */
Column c = checkColumn(L,1);
- if (c && c->cinfo)
+ if (c->cinfo)
col_set_fence(c->cinfo, c->col);
return 0;
-}
+}
WSLUA_METHOD Column_clear_fence(lua_State *L) {
/* Clear Column text fence */
Column c = checkColumn(L,1);
- if (c && c->cinfo)
+ if (c->cinfo)
col_clear_fence(c->cinfo, c->col);
return 0;
-}
+}
WSLUA_METHODS Column_methods[] = {
@@ -728,13 +646,13 @@ WSLUA_METHODS Column_methods[] = {
WSLUA_CLASS_FNREG_ALIAS(Column,preppend,prepend),
WSLUA_CLASS_FNREG(Column,fence),
WSLUA_CLASS_FNREG(Column,clear_fence),
- {0,0}
+ { NULL, NULL }
};
WSLUA_META Column_meta[] = {
- {"__tostring", Column__tostring },
- {0,0}
+ WSLUA_CLASS_MTREG(Column,tostring),
+ { NULL, NULL }
};
@@ -757,7 +675,7 @@ WSLUA_METAMETHOD Columns__tostring(lua_State *L) {
/* The string "Columns", no real use, just for debugging purposes. */
}
-/*
+/*
* To document this is very odd - it won't make sense to a person reading the
* API docs to see this metamethod as a method, but oh well.
*/
@@ -789,7 +707,7 @@ WSLUA_METAMETHOD Columns__newindex(lua_State *L) {
WSLUA_ARG_ERROR(Columns__newindex,COLUMN,"the column name must be a valid column");
}
-WSLUA_METAMETHOD Columns_index(lua_State *L) {
+WSLUA_METAMETHOD Columns__index(lua_State *L) {
Columns cols = checkColumns(L,1);
const struct col_names_t* cn;
const char* colname = luaL_checkstring(L,2);
@@ -798,7 +716,7 @@ WSLUA_METAMETHOD Columns_index(lua_State *L) {
Column c = (Column)g_malloc(sizeof(struct _wslua_col_info));
c->cinfo = NULL;
c->col = col_name_to_id(colname);
- c->expired = FALSE;
+ c->expired = FALSE;
PUSH_COLUMN(L,c);
return 1;
@@ -817,7 +735,7 @@ WSLUA_METAMETHOD Columns_index(lua_State *L) {
Column c = (Column)g_malloc(sizeof(struct _wslua_col_info));
c->cinfo = cols->cinfo;
c->col = col_name_to_id(colname);
- c->expired = FALSE;
+ c->expired = FALSE;
PUSH_COLUMN(L,c);
return 1;
@@ -829,7 +747,7 @@ WSLUA_METAMETHOD Columns_index(lua_State *L) {
/* Gets registered as metamethod automatically by WSLUA_REGISTER_META */
static int Columns__gc(lua_State* L) {
- Columns cols = checkColumns(L,1);
+ Columns cols = toColumns(L,1);
if (!cols) return 0;
@@ -843,10 +761,10 @@ static int Columns__gc(lua_State* L) {
}
-static const luaL_Reg Columns_meta[] = {
- {"__tostring", Columns__tostring },
- {"__newindex", Columns__newindex },
- {"__index", Columns_index},
+WSLUA_META Columns_meta[] = {
+ WSLUA_CLASS_MTREG(Columns,tostring),
+ WSLUA_CLASS_MTREG(Columns,newindex),
+ WSLUA_CLASS_MTREG(Columns,index),
{ NULL, NULL }
};
@@ -856,11 +774,11 @@ int Columns_register(lua_State *L) {
return 0;
}
-WSLUA_CLASS_DEFINE(PrivateTable,NOP,NOP);
+WSLUA_CLASS_DEFINE(PrivateTable,FAIL_ON_NULL_OR_EXPIRED("PrivateTable"),NOP);
/* PrivateTable represents the pinfo->private_table. */
WSLUA_METAMETHOD PrivateTable__tostring(lua_State* L) {
- PrivateTable priv = checkPrivateTable(L,1);
+ PrivateTable priv = toPrivateTable(L,1);
GString *key_string;
GList *keys, *key;
@@ -891,13 +809,6 @@ static int PrivateTable__index(lua_State* L) {
const gchar* name = luaL_checkstring(L,2);
const gchar* string;
- if (! (priv && name) ) return 0;
-
- if (priv->expired) {
- luaL_error(L,"expired private_table");
- return 0;
- }
-
string = (const gchar *)g_hash_table_lookup (priv->table, (gpointer) name);
if (string) {
@@ -915,13 +826,6 @@ static int PrivateTable__newindex(lua_State* L) {
const gchar* name = luaL_checkstring(L,2);
const gchar* string = NULL;
- if (! (priv && name) ) return 0;
-
- if (priv->expired) {
- luaL_error(L,"expired private_table");
- return 0;
- }
-
if (lua_isstring(L,3)) {
/* This also catches numbers, which is converted to string */
string = luaL_checkstring(L,3);
@@ -944,7 +848,7 @@ static int PrivateTable__newindex(lua_State* L) {
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int PrivateTable__gc(lua_State* L) {
- PrivateTable priv = checkPrivateTable(L,1);
+ PrivateTable priv = toPrivateTable(L,1);
if (!priv) return 0;
@@ -961,10 +865,10 @@ static int PrivateTable__gc(lua_State* L) {
}
WSLUA_META PrivateTable_meta[] = {
- {"__index", PrivateTable__index},
- {"__newindex", PrivateTable__newindex},
- {"__tostring", PrivateTable__tostring},
- { NULL, NULL}
+ WSLUA_CLASS_MTREG(PrivateTable,index),
+ WSLUA_CLASS_MTREG(PrivateTable,newindex),
+ WSLUA_CLASS_MTREG(PrivateTable,tostring),
+ { NULL, NULL }
};
int PrivateTable_register(lua_State* L) {
@@ -973,45 +877,38 @@ int PrivateTable_register(lua_State* L) {
}
-WSLUA_CLASS_DEFINE(Pinfo,FAIL_ON_NULL("expired pinfo"),NOP);
+WSLUA_CLASS_DEFINE(Pinfo,FAIL_ON_NULL_OR_EXPIRED("Pinfo"),NOP);
/* Packet information */
-static int Pinfo_tostring(lua_State *L) { lua_pushstring(L,"a Pinfo"); return 1; }
-
-#define PINFO_GET(name,block) static int name(lua_State *L) { \
- Pinfo pinfo = checkPinfo(L,1); \
- if (!pinfo) return 0;\
- if (pinfo->expired) { \
- luaL_error(L,"expired_pinfo"); \
- return 0; \
- } \
- block \
- return 1;\
-}
-
-#define PINFO_GET_BOOLEAN(name,val) \
- PINFO_GET(name,{lua_pushboolean(L,val);})
-
-#define PINFO_GET_NUMBER(name,val) \
- PINFO_GET(name,{lua_pushnumber(L,(lua_Number)(val));})
+static int Pinfo__tostring(lua_State *L) { lua_pushstring(L,"a Pinfo"); return 1; }
-#define PINFO_GET_STRING(name,val) \
- PINFO_GET(name, { \
- const gchar* value; \
- value = val; \
- if (value) lua_pushstring(L,(const char*)(value)); else lua_pushnil(L); \
+#define PINFO_ADDRESS_GETTER(name) \
+ WSLUA_ATTRIBUTE_GET(Pinfo,name, { \
+ Address addr = g_new(address,1); \
+ COPY_ADDRESS(addr, &(obj->ws_pinfo->name)); \
+ pushAddress(L,addr); \
})
-#define PINFO_GET_ADDRESS(name,role) \
- PINFO_GET(name, { \
- Address addr; \
- addr = g_new(address,1); \
- COPY_ADDRESS(addr, &(pinfo->ws_pinfo->role)); \
- pushAddress(L,addr); \
+#define PINFO_ADDRESS_SETTER(name) \
+ WSLUA_ATTRIBUTE_SET(Pinfo,name, { \
+ const address* from = checkAddress(L,-1); \
+ COPY_ADDRESS(&(obj->ws_pinfo->name),from); \
})
-#define PINFO_GET_LIGHTUSERDATA(name, val) \
- PINFO_GET(name,{lua_pushlightuserdata(L, (void *) (val));})
+#define PINFO_NAMED_BOOLEAN_GETTER(name,member) \
+ WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(Pinfo,name,ws_pinfo->member)
+
+#define PINFO_NUMBER_GETTER(name) \
+ WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(Pinfo,name,ws_pinfo->name)
+
+#define PINFO_NAMED_NUMBER_GETTER(name,member) \
+ WSLUA_ATTRIBUTE_NAMED_NUMBER_GETTER(Pinfo,name,ws_pinfo->member)
+
+#define PINFO_NUMBER_SETTER(name,cast) \
+ WSLUA_ATTRIBUTE_NAMED_NUMBER_SETTER(Pinfo,name,ws_pinfo->name,cast)
+
+#define PINFO_NAMED_NUMBER_SETTER(name,member,cast) \
+ WSLUA_ATTRIBUTE_NAMED_NUMBER_SETTER(Pinfo,name,ws_pinfo->member,cast)
static double
lua_nstime_to_sec(const nstime_t *nstime)
@@ -1028,47 +925,107 @@ lua_delta_nstime_to_sec(const Pinfo pinfo, const frame_data *fd, guint32 prev_nu
return lua_nstime_to_sec(&del);
}
-PINFO_GET_BOOLEAN(Pinfo_fragmented,pinfo->ws_pinfo->fragmented)
-PINFO_GET_BOOLEAN(Pinfo_in_error_pkt,pinfo->ws_pinfo->flags.in_error_pkt)
-PINFO_GET_BOOLEAN(Pinfo_visited,pinfo->ws_pinfo->fd->flags.visited)
-
-PINFO_GET_NUMBER(Pinfo_number,pinfo->ws_pinfo->fd->num)
-PINFO_GET_NUMBER(Pinfo_len,pinfo->ws_pinfo->fd->pkt_len)
-PINFO_GET_NUMBER(Pinfo_caplen,pinfo->ws_pinfo->fd->cap_len)
-PINFO_GET_NUMBER(Pinfo_abs_ts,lua_nstime_to_sec(&pinfo->ws_pinfo->fd->abs_ts))
-PINFO_GET_NUMBER(Pinfo_rel_ts,lua_nstime_to_sec(&pinfo->ws_pinfo->rel_ts))
-PINFO_GET_NUMBER(Pinfo_delta_ts,lua_delta_nstime_to_sec(pinfo, pinfo->ws_pinfo->fd, pinfo->ws_pinfo->fd->num - 1))
-PINFO_GET_NUMBER(Pinfo_delta_dis_ts,lua_delta_nstime_to_sec(pinfo, pinfo->ws_pinfo->fd, pinfo->ws_pinfo->fd->prev_dis_num))
-PINFO_GET_NUMBER(Pinfo_ipproto,pinfo->ws_pinfo->ipproto)
-PINFO_GET_NUMBER(Pinfo_circuit_id,pinfo->ws_pinfo->circuit_id)
-PINFO_GET_NUMBER(Pinfo_can_desegment,pinfo->ws_pinfo->can_desegment)
-PINFO_GET_NUMBER(Pinfo_desegment_len,pinfo->ws_pinfo->desegment_len)
-PINFO_GET_NUMBER(Pinfo_desegment_offset,pinfo->ws_pinfo->desegment_offset)
-PINFO_GET_NUMBER(Pinfo_ptype,pinfo->ws_pinfo->ptype)
-PINFO_GET_NUMBER(Pinfo_src_port,pinfo->ws_pinfo->srcport)
-PINFO_GET_NUMBER(Pinfo_dst_port,pinfo->ws_pinfo->destport)
-PINFO_GET_NUMBER(Pinfo_match_uint,pinfo->ws_pinfo->match_uint)
-
-PINFO_GET_STRING(Pinfo_curr_proto,pinfo->ws_pinfo->current_proto)
-PINFO_GET_STRING(Pinfo_match_string,pinfo->ws_pinfo->match_string)
-
-PINFO_GET_ADDRESS(Pinfo_net_src,net_src)
-PINFO_GET_ADDRESS(Pinfo_net_dst,net_dst)
-PINFO_GET_ADDRESS(Pinfo_dl_src,dl_src)
-PINFO_GET_ADDRESS(Pinfo_dl_dst,dl_dst)
-PINFO_GET_ADDRESS(Pinfo_src,src)
-PINFO_GET_ADDRESS(Pinfo_dst,dst)
-
-PINFO_GET_LIGHTUSERDATA(Pinfo_private_data, pinfo->ws_pinfo->private_data)
-
-static int Pinfo_match(lua_State *L) {
- Pinfo pinfo = checkPinfo(L,1);
- if (!pinfo) return 0;
- if (pinfo->expired) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
+/* WSLUA_ATTRIBUTE Pinfo_visited RO Whether this packet hass been already visited */
+PINFO_NAMED_BOOLEAN_GETTER(visited,fd->flags.visited);
+
+/* WSLUA_ATTRIBUTE Pinfo_number RO The number of this packet in the current file */
+PINFO_NAMED_NUMBER_GETTER(number,fd->num);
+
+/* WSLUA_ATTRIBUTE Pinfo_len RO The length of the frame */
+PINFO_NAMED_NUMBER_GETTER(len,fd->pkt_len);
+
+/* WSLUA_ATTRIBUTE Pinfo_caplen RO The captured length of the frame */
+PINFO_NAMED_NUMBER_GETTER(caplen,fd->cap_len);
+
+/* WSLUA_ATTRIBUTE Pinfo_abs_ts RO When the packet was captured */
+WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(Pinfo,abs_ts,lua_nstime_to_sec(&obj->ws_pinfo->fd->abs_ts));
+
+/* WSLUA_ATTRIBUTE Pinfo_rel_ts RO Number of seconds passed since beginning of capture */
+WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(Pinfo,rel_ts,lua_nstime_to_sec(&obj->ws_pinfo->rel_ts));
+
+/* WSLUA_ATTRIBUTE Pinfo_delta_ts RO Number of seconds passed since the last captured packet */
+WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(Pinfo,delta_ts,lua_delta_nstime_to_sec(obj, obj->ws_pinfo->fd, obj->ws_pinfo->fd->num - 1));
+
+/* WSLUA_ATTRIBUTE Pinfo_delta_dis_ts RO Number of seconds passed since the last displayed packet */
+WSLUA_ATTRIBUTE_BLOCK_NUMBER_GETTER(Pinfo,delta_dis_ts,lua_delta_nstime_to_sec(obj, obj->ws_pinfo->fd, obj->ws_pinfo->fd->prev_dis_num));
+
+/* WSLUA_ATTRIBUTE Pinfo_ipproto RO IP Protocol id */
+PINFO_NUMBER_GETTER(ipproto);
+
+/* WSLUA_ATTRIBUTE Pinfo_circuit_id RW For circuit based protocols */
+PINFO_NUMBER_GETTER(circuit_id);
+PINFO_NUMBER_SETTER(circuit_id,guint32);
+
+/* WSLUA_ATTRIBUTE Pinfo_curr_proto RO Which Protocol are we dissecting */
+WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(Pinfo,curr_proto,ws_pinfo->current_proto);
+
+/* WSLUA_ATTRIBUTE Pinfo_can_desegment RW Set if this segment could be desegmented */
+PINFO_NUMBER_GETTER(can_desegment);
+PINFO_NUMBER_SETTER(can_desegment,guint16);
+
+/* WSLUA_ATTRIBUTE Pinfo_desegment_len RW Estimated number of additional bytes required for completing the PDU */
+PINFO_NUMBER_GETTER(desegment_len);
+PINFO_NUMBER_SETTER(desegment_len,guint32);
+
+/* WSLUA_ATTRIBUTE Pinfo_desegment_offset RW Offset in the tvbuff at which the dissector will continue processing when next called*/
+PINFO_NUMBER_GETTER(desegment_offset);
+PINFO_NUMBER_SETTER(desegment_offset,int);
+
+/* WSLUA_ATTRIBUTE Pinfo_private_data RO Access to private data */
+WSLUA_ATTRIBUTE_GET(Pinfo,private_data, {lua_pushlightuserdata(L,(void *)(obj->ws_pinfo->private_data));});
+
+/* WSLUA_ATTRIBUTE Pinfo_fragmented RO If the protocol is only a fragment */
+PINFO_NAMED_BOOLEAN_GETTER(fragmented,fragmented);
+
+/* WSLUA_ATTRIBUTE Pinfo_in_error_pkt RO If we're inside an error packet */
+PINFO_NAMED_BOOLEAN_GETTER(in_error_pkt,flags.in_error_pkt);
+
+/* WSLUA_ATTRIBUTE Pinfo_match_uint RO Matched uint for calling subdissector from table */
+PINFO_NUMBER_GETTER(match_uint);
+
+/* WSLUA_ATTRIBUTE Pinfo_match_string RO Matched string for calling subdissector from table */
+WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(Pinfo,match_string,ws_pinfo->match_string);
+
+/* WSLUA_ATTRIBUTE Pinfo_port_type RW Type of Port of .src_port and .dst_port */
+PINFO_NAMED_NUMBER_GETTER(port_type,ptype);
+
+/* WSLUA_ATTRIBUTE Pinfo_src_port RW Source Port of this Packet */
+PINFO_NAMED_NUMBER_GETTER(src_port,srcport);
+PINFO_NAMED_NUMBER_SETTER(src_port,srcport,guint32);
+
+/* WSLUA_ATTRIBUTE Pinfo_dst_port RW Source Address of this Packet */
+PINFO_NAMED_NUMBER_GETTER(dst_port,destport);
+PINFO_NAMED_NUMBER_SETTER(dst_port,destport,guint32);
+
+/* WSLUA_ATTRIBUTE Pinfo_dl_src RW Data Link Source Address of this Packet */
+PINFO_ADDRESS_GETTER(dl_src);
+PINFO_ADDRESS_SETTER(dl_src);
+
+/* WSLUA_ATTRIBUTE Pinfo_dl_dst RW Data Link Destination Address of this Packet */
+PINFO_ADDRESS_GETTER(dl_dst);
+PINFO_ADDRESS_SETTER(dl_dst);
+
+/* WSLUA_ATTRIBUTE Pinfo_net_src RW Network Layer Source Address of this Packet */
+PINFO_ADDRESS_GETTER(net_src);
+PINFO_ADDRESS_SETTER(net_src);
+
+/* WSLUA_ATTRIBUTE Pinfo_net_dst RW Network Layer Destination Address of this Packet */
+PINFO_ADDRESS_GETTER(net_dst);
+PINFO_ADDRESS_SETTER(net_dst);
+
+/* WSLUA_ATTRIBUTE Pinfo_src RW Source Address of this Packet */
+PINFO_ADDRESS_GETTER(src);
+PINFO_ADDRESS_SETTER(src);
+
+/* WSLUA_ATTRIBUTE Pinfo_dst RW Destination Address of this Packet */
+PINFO_ADDRESS_GETTER(dst);
+PINFO_ADDRESS_SETTER(dst);
+
+
+/* WSLUA_ATTRIBUTE Pinfo_match RO Port/Data we are matching */
+static int Pinfo_get_match(lua_State *L) {
+ Pinfo pinfo = checkPinfo(L,1);
if (pinfo->ws_pinfo->match_string) {
lua_pushstring(L,pinfo->ws_pinfo->match_string);
@@ -1079,16 +1036,13 @@ static int Pinfo_match(lua_State *L) {
return 1;
}
-static int Pinfo_columns(lua_State *L) {
+/* WSLUA_ATTRIBUTE Pinfo_columns RO Accesss to the packet list columns */
+/* WSLUA_ATTRIBUTE Pinfo_cols RO Accesss to the packet list columns (equivalent to pinfo.columns) */
+static int Pinfo_get_columns(lua_State *L) {
Columns cols = NULL;
Pinfo pinfo = checkPinfo(L,1);
const gchar* colname = luaL_optstring(L,2,NULL);
- if (pinfo->expired) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
-
cols = (Columns)g_malloc(sizeof(struct _wslua_cols));
cols->cinfo = pinfo->ws_pinfo->cinfo;
cols->expired = FALSE;
@@ -1099,24 +1053,18 @@ static int Pinfo_columns(lua_State *L) {
lua_settop(L,0);
PUSH_COLUMNS(L,cols);
lua_pushstring(L,colname);
- return Columns_index(L);
+ return Columns__index(L);
}
return 1;
}
-static int Pinfo_private(lua_State *L) {
+/* WSLUA_ATTRIBUTE Pinfo_private RO Access to the private table entries */
+static int Pinfo_get_private(lua_State *L) {
PrivateTable priv = NULL;
Pinfo pinfo = checkPinfo(L,1);
const gchar* privname = luaL_optstring(L,2,NULL);
gboolean is_allocated = FALSE;
- if (!pinfo) return 0;
-
- if (pinfo->expired) {
- luaL_error(L,"expired private_table");
- return 0;
- }
-
if (!pinfo->ws_pinfo->private_table) {
pinfo->ws_pinfo->private_table = g_hash_table_new(g_str_hash,g_str_equal);
is_allocated = TRUE;
@@ -1138,121 +1086,11 @@ static int Pinfo_private(lua_State *L) {
return 1;
}
-typedef enum {
- PARAM_NONE,
- PARAM_ADDR_SRC,
- PARAM_ADDR_DST,
- PARAM_ADDR_DL_SRC,
- PARAM_ADDR_DL_DST,
- PARAM_ADDR_NET_SRC,
- PARAM_ADDR_NET_DST,
- PARAM_PORT_SRC,
- PARAM_PORT_DST,
- PARAM_CIRCUIT_ID,
- PARAM_CAN_DESEGMENT,
- PARAM_DESEGMENT_LEN,
- PARAM_DESEGMENT_OFFSET,
- PARAM_PORT_TYPE
-} pinfo_param_type_t;
-
-static int pushnil_param(lua_State* L, packet_info* pinfo _U_, pinfo_param_type_t pt _U_ ) {
- lua_pushnil(L);
- return 1;
-}
-
-static int Pinfo_set_addr(lua_State* L, packet_info* pinfo, pinfo_param_type_t pt) {
- const address* from = checkAddress(L,1);
- address* to;
-
- if (! from ) {
- luaL_error(L,"Not an OK address");
- return 0;
- }
-
- if (!pinfo) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
-
- switch(pt) {
- case PARAM_ADDR_SRC:
- to = &(pinfo->src);
- break;
- case PARAM_ADDR_DST:
- to = &(pinfo->dst);
- break;
- case PARAM_ADDR_DL_SRC:
- to = &(pinfo->dl_src);
- break;
- case PARAM_ADDR_DL_DST:
- to = &(pinfo->dl_dst);
- break;
- case PARAM_ADDR_NET_SRC:
- to = &(pinfo->net_src);
- break;
- case PARAM_ADDR_NET_DST:
- to = &(pinfo->net_dst);
- break;
- default:
- g_assert(!"BUG: A bad parameter");
- return 0;
- }
-
- COPY_ADDRESS(to,from);
- return 0;
-}
-
-static int Pinfo_set_int(lua_State* L, packet_info* pinfo, pinfo_param_type_t pt) {
- gint64 v = luaL_checkint(L,1);
-
- if (!pinfo) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
-
- switch(pt) {
- case PARAM_PORT_SRC:
- pinfo->srcport = (guint32)v;
- return 0;
- case PARAM_PORT_DST:
- pinfo->destport = (guint32)v;
- return 0;
- case PARAM_CIRCUIT_ID:
- pinfo->circuit_id = (guint32)v;
- return 0;
- case PARAM_CAN_DESEGMENT:
- pinfo->can_desegment = (guint16)v;
- return 0;
- case PARAM_DESEGMENT_LEN:
- pinfo->desegment_len = (guint32)v;
- return 0;
- case PARAM_DESEGMENT_OFFSET:
- pinfo->desegment_offset = (int)v;
- return 0;
- default:
- g_assert(!"BUG: A bad parameter");
- }
-
- return 0;
-}
-
-typedef struct _pinfo_method_t {
- const gchar* name;
- lua_CFunction get;
- int (*set)(lua_State*, packet_info*, pinfo_param_type_t);
- pinfo_param_type_t param;
-} pinfo_method_t;
-
-static int Pinfo_hi(lua_State *L) {
+/* WSLUA_ATTRIBUTE Pinfo_hi RW higher Address of this Packet */
+static int Pinfo_get_hi(lua_State *L) {
Pinfo pinfo = checkPinfo(L,1);
Address addr;
- if (!pinfo) return 0;
- if (pinfo->expired) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
-
addr = (Address)g_malloc(sizeof(address));
if (CMP_ADDRESS(&(pinfo->ws_pinfo->src), &(pinfo->ws_pinfo->dst) ) >= 0) {
COPY_ADDRESS(addr, &(pinfo->ws_pinfo->src));
@@ -1264,16 +1102,11 @@ static int Pinfo_hi(lua_State *L) {
return 1;
}
-static int Pinfo_lo(lua_State *L) {
+/* WSLUA_ATTRIBUTE Pinfo_lo RO lower Address of this Packet */
+static int Pinfo_get_lo(lua_State *L) {
Pinfo pinfo = checkPinfo(L,1);
Address addr;
- if (!pinfo) return 0;
- if (pinfo->expired) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
-
addr = (Address)g_malloc(sizeof(address));
if (CMP_ADDRESS(&(pinfo->ws_pinfo->src), &(pinfo->ws_pinfo->dst) ) < 0) {
COPY_ADDRESS(addr, &(pinfo->ws_pinfo->src));
@@ -1285,177 +1118,9 @@ static int Pinfo_lo(lua_State *L) {
return 1;
}
-
-static const pinfo_method_t Pinfo_methods[] = {
-
- /* WSLUA_ATTRIBUTE Pinfo_number RO The number of this packet in the current file */
- {"number", Pinfo_number, pushnil_param, PARAM_NONE},
-
- /* WSLUA_ATTRIBUTE Pinfo_len RO The length of the frame */
- {"len", Pinfo_len, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_caplen RO The captured length of the frame */
- {"caplen", Pinfo_caplen, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_abs_ts RO When the packet was captured */
- {"abs_ts",Pinfo_abs_ts, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_rel_ts RO Number of seconds passed since beginning of capture */
- {"rel_ts",Pinfo_rel_ts, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_delta_ts RO Number of seconds passed since the last captured packet */
- {"delta_ts",Pinfo_delta_ts, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_delta_dis_ts RO Number of seconds passed since the last displayed packet */
- {"delta_dis_ts",Pinfo_delta_dis_ts, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_visited RO Whether this packet hass been already visited */
- {"visited",Pinfo_visited, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_src RW Source Address of this Packet */
- {"src", Pinfo_src, Pinfo_set_addr, PARAM_ADDR_SRC },
-
- /* WSLUA_ATTRIBUTE Pinfo_dst RW Destination Address of this Packet */
- {"dst", Pinfo_dst, Pinfo_set_addr, PARAM_ADDR_DST },
-
- /* WSLUA_ATTRIBUTE Pinfo_lo RO lower Address of this Packet */
- {"lo", Pinfo_lo, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_hi RW higher Address of this Packet */
- {"hi", Pinfo_hi, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_dl_src RW Data Link Source Address of this Packet */
- {"dl_src", Pinfo_dl_src, Pinfo_set_addr, PARAM_ADDR_DL_SRC },
-
- /* WSLUA_ATTRIBUTE Pinfo_dl_dst RW Data Link Destination Address of this Packet */
- {"dl_dst", Pinfo_dl_dst, Pinfo_set_addr, PARAM_ADDR_DL_DST },
-
- /* WSLUA_ATTRIBUTE Pinfo_net_src RW Network Layer Source Address of this Packet */
- {"net_src", Pinfo_net_src, Pinfo_set_addr, PARAM_ADDR_NET_SRC },
-
- /* WSLUA_ATTRIBUTE Pinfo_net_dst RW Network Layer Destination Address of this Packet */
- {"net_dst", Pinfo_net_dst, Pinfo_set_addr, PARAM_ADDR_NET_DST },
-
- /* WSLUA_ATTRIBUTE Pinfo_ptype RW Type of Port of .src_port and .dst_port */
- {"port_type", Pinfo_ptype, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_src_port RW Source Port of this Packet */
- {"src_port", Pinfo_src_port, Pinfo_set_int, PARAM_PORT_SRC },
-
- /* WSLUA_ATTRIBUTE Pinfo_dst_port RW Source Address of this Packet */
- {"dst_port", Pinfo_dst_port, Pinfo_set_int, PARAM_PORT_DST },
-
- /* WSLUA_ATTRIBUTE Pinfo_ipproto RO IP Protocol id */
- {"ipproto", Pinfo_ipproto, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_circuit_id RO For circuit based protocols */
- {"circuit_id", Pinfo_circuit_id, Pinfo_set_int, PARAM_CIRCUIT_ID },
-
- /* WSLUA_ATTRIBUTE Pinfo_match RO Port/Data we are matching */
- {"match", Pinfo_match, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_curr_proto RO Which Protocol are we dissecting */
- {"curr_proto", Pinfo_curr_proto, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_columns RO Accesss to the packet list columns */
- {"columns", Pinfo_columns, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_cols RO Accesss to the packet list columns (equivalent to pinfo.columns) */
- {"cols", Pinfo_columns, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_can_desegment RW Set if this segment could be desegmented */
- {"can_desegment", Pinfo_can_desegment, Pinfo_set_int, PARAM_CAN_DESEGMENT },
-
- /* WSLUA_ATTRIBUTE Pinfo_desegment_len RW Estimated number of additional bytes required for completing the PDU */
- {"desegment_len", Pinfo_desegment_len, Pinfo_set_int, PARAM_DESEGMENT_LEN },
-
- /* WSLUA_ATTRIBUTE Pinfo_desegment_offset RW Offset in the tvbuff at which the dissector will continue processing when next called*/
- {"desegment_offset", Pinfo_desegment_offset, Pinfo_set_int, PARAM_DESEGMENT_OFFSET },
-
- /* WSLUA_ATTRIBUTE Pinfo_private_data RO Access to private data */
- {"private_data", Pinfo_private_data, pushnil_param, PARAM_NONE},
-
- /* WSLUA_ATTRIBUTE Pinfo_private RW Access to the private table entries */
- {"private", Pinfo_private, pushnil_param, PARAM_NONE},
-
- /* WSLUA_ATTRIBUTE Pinfo_fragmented RO If the protocol is only a fragment */
- {"fragmented", Pinfo_fragmented, pushnil_param, PARAM_NONE},
-
- /* WSLUA_ATTRIBUTE Pinfo_in_error_pkt RO If we're inside an error packet */
- {"in_error_pkt", Pinfo_in_error_pkt, pushnil_param, PARAM_NONE},
-
- /* WSLUA_ATTRIBUTE Pinfo_match_uint RO Matched uint for calling subdissector from table */
- {"match_uint", Pinfo_match_uint, pushnil_param, PARAM_NONE },
-
- /* WSLUA_ATTRIBUTE Pinfo_match_string RO Matched string for calling subdissector from table */
- {"match_string", Pinfo_match_string, pushnil_param, PARAM_NONE },
-
- {NULL,NULL,NULL,PARAM_NONE}
-};
-
-
-static int pushnil(lua_State* L) {
- lua_pushnil(L);
- return 1;
-}
-
-static int Pinfo_index(lua_State* L) {
- Pinfo pinfo = checkPinfo(L,1);
- const gchar* name = luaL_checkstring(L,2);
- lua_CFunction method = pushnil;
- const pinfo_method_t* curr;
-
- if (! (pinfo && name) ) {
- lua_pushnil(L);
- return 1;
- }
- if (pinfo->expired) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
-
- for (curr = Pinfo_methods ; curr->name ; curr++) {
- if (g_str_equal(curr->name,name)) {
- method = curr->get;
- break;
- }
- }
-
- lua_settop(L,1);
- return method(L);
-}
-
-static int Pinfo_setindex(lua_State* L) {
- Pinfo pinfo = checkPinfo(L,1);
- const gchar* name = luaL_checkstring(L,2);
- int (*method)(lua_State*, packet_info* pinfo, pinfo_param_type_t) = pushnil_param;
- const pinfo_method_t* curr;
- pinfo_param_type_t param_type = PARAM_NONE;
-
- if (! (pinfo && name) ) {
- return 0;
- }
- if (pinfo->expired) {
- luaL_error(L,"expired_pinfo");
- return 0;
- }
-
- for (curr = Pinfo_methods ; curr->name ; curr++) {
- if (g_str_equal(curr->name,name)) {
- method = curr->set;
- param_type = curr->param;
- break;
- }
- }
-
- lua_remove(L,1);
- lua_remove(L,1);
- return method(L,pinfo->ws_pinfo,param_type);
-}
-
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int Pinfo__gc(lua_State* L) {
- Pinfo pinfo = checkPinfo(L,1);
+ Pinfo pinfo = toPinfo(L,1);
if (!pinfo) return 0;
@@ -1468,15 +1133,57 @@ static int Pinfo__gc(lua_State* L) {
}
-static const luaL_Reg Pinfo_meta[] = {
- {"__index", Pinfo_index},
- {"__newindex",Pinfo_setindex},
- {"__tostring", Pinfo_tostring},
+/* This table is ultimately registered as a sub-table of the class' metatable,
+ * and if __index/__newindex is invoked then it calls the appropriate function
+ * from this table for getting/setting the members.
+ */
+WSLUA_ATTRIBUTES Pinfo_attributes[] = {
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,number),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,len),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,caplen),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,abs_ts),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,rel_ts),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,delta_ts),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,delta_dis_ts),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,visited),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,src),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,dst),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,lo),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,hi),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,dl_src),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,dl_dst),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,net_src),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,net_dst),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,port_type),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,src_port),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,dst_port),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,ipproto),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,circuit_id),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,match),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,curr_proto),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,columns),
+ { "cols", Pinfo_get_columns, NULL },
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,can_desegment),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,desegment_len),
+ WSLUA_ATTRIBUTE_RWREG(Pinfo,desegment_offset),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,private_data),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,private),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,fragmented),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,in_error_pkt),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,fragmented),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,match_uint),
+ WSLUA_ATTRIBUTE_ROREG(Pinfo,match_string),
+ { NULL, NULL, NULL }
+};
+
+WSLUA_META Pinfo_meta[] = {
+ WSLUA_CLASS_MTREG(Pinfo,tostring),
{ NULL, NULL }
};
int Pinfo_register(lua_State* L) {
WSLUA_REGISTER_META(Pinfo);
+ WSLUA_REGISTER_ATTRIBUTES(Pinfo);
outstanding_Pinfo = g_ptr_array_new();
outstanding_Column = g_ptr_array_new();
outstanding_Columns = g_ptr_array_new();
diff --git a/epan/wslua/wslua_proto.c b/epan/wslua/wslua_proto.c
index be2a01677a..6d91c4f5d5 100644
--- a/epan/wslua/wslua_proto.c
+++ b/epan/wslua/wslua_proto.c
@@ -253,11 +253,11 @@ WSLUA_METHODS Pref_methods[] = {
WSLUA_CLASS_FNREG(Pref,enum),
WSLUA_CLASS_FNREG(Pref,range),
WSLUA_CLASS_FNREG(Pref,statictext),
- {0,0}
+ { NULL, NULL }
};
WSLUA_META Pref_meta[] = {
- {0,0}
+ { NULL, NULL }
};
@@ -420,9 +420,9 @@ static int Prefs__gc(lua_State* L _U_) {
}
WSLUA_META Prefs_meta[] = {
- {"__newindex", Prefs__newindex},
- {"__index", Prefs__index},
- {0,0}
+ WSLUA_CLASS_MTREG(Prefs,newindex),
+ WSLUA_CLASS_MTREG(Prefs,index),
+ { NULL, NULL }
};
WSLUA_REGISTER Prefs_register(lua_State* L) {
@@ -1297,41 +1297,41 @@ static int ProtoField__gc(lua_State* L) {
return 0;
}
-static const luaL_Reg ProtoField_methods[] = {
- {"new", ProtoField_new},
- {"uint8",ProtoField_uint8},
- {"uint16",ProtoField_uint16},
- {"uint24",ProtoField_uint24},
- {"uint32",ProtoField_uint32},
- {"uint64",ProtoField_uint64},
- {"int8",ProtoField_int8},
- {"int16",ProtoField_int16},
- {"int24",ProtoField_int24},
- {"int32",ProtoField_int32},
- {"int64",ProtoField_int64},
- {"framenum",ProtoField_framenum},
- {"ipv4",ProtoField_ipv4},
- {"ipv6",ProtoField_ipv6},
- {"ipx",ProtoField_ipx},
- {"ether",ProtoField_ether},
- {"bool",ProtoField_bool},
- {"float",ProtoField_float},
- {"double",ProtoField_double},
- {"absolute_time",ProtoField_absolute_time},
- {"relative_time",ProtoField_relative_time},
- {"string",ProtoField_string},
- {"stringz",ProtoField_stringz},
- {"bytes",ProtoField_bytes},
- {"ubytes",ProtoField_ubytes},
- {"guid",ProtoField_guid},
- {"oid",ProtoField_oid},
- {"rel_oid",ProtoField_rel_oid},
- {"systemid",ProtoField_systemid},
+WSLUA_METHODS ProtoField_methods[] = {
+ WSLUA_CLASS_FNREG(ProtoField,new),
+ WSLUA_CLASS_FNREG(ProtoField,uint8),
+ WSLUA_CLASS_FNREG(ProtoField,uint16),
+ WSLUA_CLASS_FNREG(ProtoField,uint24),
+ WSLUA_CLASS_FNREG(ProtoField,uint32),
+ WSLUA_CLASS_FNREG(ProtoField,uint64),
+ WSLUA_CLASS_FNREG(ProtoField,int8),
+ WSLUA_CLASS_FNREG(ProtoField,int16),
+ WSLUA_CLASS_FNREG(ProtoField,int24),
+ WSLUA_CLASS_FNREG(ProtoField,int32),
+ WSLUA_CLASS_FNREG(ProtoField,int64),
+ WSLUA_CLASS_FNREG(ProtoField,framenum),
+ WSLUA_CLASS_FNREG(ProtoField,ipv4),
+ WSLUA_CLASS_FNREG(ProtoField,ipv6),
+ WSLUA_CLASS_FNREG(ProtoField,ipx),
+ WSLUA_CLASS_FNREG(ProtoField,ether),
+ WSLUA_CLASS_FNREG(ProtoField,bool),
+ WSLUA_CLASS_FNREG(ProtoField,float),
+ WSLUA_CLASS_FNREG(ProtoField,double),
+ WSLUA_CLASS_FNREG(ProtoField,absolute_time),
+ WSLUA_CLASS_FNREG(ProtoField,relative_time),
+ WSLUA_CLASS_FNREG(ProtoField,string),
+ WSLUA_CLASS_FNREG(ProtoField,stringz),
+ WSLUA_CLASS_FNREG(ProtoField,bytes),
+ WSLUA_CLASS_FNREG(ProtoField,ubytes),
+ WSLUA_CLASS_FNREG(ProtoField,guid),
+ WSLUA_CLASS_FNREG(ProtoField,oid),
+ WSLUA_CLASS_FNREG(ProtoField,rel_oid),
+ WSLUA_CLASS_FNREG(ProtoField,systemid),
{ NULL, NULL }
};
-static const luaL_Reg ProtoField_meta[] = {
- {"__tostring", ProtoField__tostring },
+WSLUA_META ProtoField_meta[] = {
+ WSLUA_CLASS_MTREG(ProtoField,tostring),
{ NULL, NULL }
};
@@ -1340,7 +1340,7 @@ int ProtoField_register(lua_State* L) {
return 0;
}
-WSLUA_CLASS_DEFINE(Proto,NOP,NOP);
+WSLUA_CLASS_DEFINE(Proto,FAIL_ON_NULL("Proto"),NOP);
/*
A new protocol in wireshark. Protocols have more uses, the main one is to dissect
a protocol. But they can be just dummies used to register preferences for
@@ -1405,7 +1405,14 @@ WSLUA_CONSTRUCTOR Proto_new(lua_State* L) {
return 0;
}
-static int Proto_tostring(lua_State* L) {
+WSLUA_METAMETHOD Proto__call(lua_State* L) { /* Creates a Proto object */
+#define WSLUA_ARG_Proto__call_NAME 1 /* The name of the protocol */
+#define WSLUA_ARG_Proto__call_DESC 2 /* A Long Text description of the protocol (usually lowercase) */
+ lua_remove(L,1); /* remove the table */
+ WSLUA_RETURN(Proto_new(L)); /* The new Proto object. */
+}
+
+static int Proto__tostring(lua_State* L) {
Proto proto = checkProto(L,1);
gchar* s;
@@ -1436,8 +1443,10 @@ WSLUA_FUNCTION wslua_register_postdissector(lua_State* L) {
return 0;
}
+/* WSLUA_ATTRIBUTE Proto_dissector RW The protocol's dissector, a function you define.
+ The called dissector function will be given three arguments of (1) a Tvb object, (2) a Pinfo object, and (3) a TreeItem object. */
static int Proto_get_dissector(lua_State* L) {
- Proto proto = toProto(L,1);
+ Proto proto = checkProto(L,1);
if (proto->handle) {
pushDissector(L,proto->handle);
@@ -1449,105 +1458,90 @@ static int Proto_get_dissector(lua_State* L) {
}
static int Proto_set_dissector(lua_State* L) {
- Proto proto = toProto(L,1);
+ Proto proto = checkProto(L,1);
- if (lua_isfunction(L,3)) {
+ if (lua_isfunction(L,2)) {
/* insert the dissector into the dissectors table */
gchar* loname = g_ascii_strdown(proto->name, -1);
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_dissectors_table_ref);
lua_replace(L, 1);
lua_pushstring(L,proto->name);
- lua_replace(L, 2);
+ lua_insert(L, 2); /* function is now at 3 */
lua_settable(L,1);
proto->handle = new_create_dissector_handle(dissect_lua, proto->hfid);
new_register_dissector(loname, dissect_lua, proto->hfid);
-
- return 0;
} else {
- luaL_argerror(L,3,"The dissector of a protocol must be a function");
- return 0;
+ luaL_argerror(L,2,"The dissector of a protocol must be a function");
}
+ return 0;
}
+/* WSLUA_ATTRIBUTE Proto_prefs RO The preferences of this dissector */
static int Proto_get_prefs(lua_State* L) {
- Proto proto = toProto(L,1);
+ Proto proto = checkProto(L,1);
pushPrefs(L,&proto->prefs);
return 1;
}
+/* WSLUA_ATTRIBUTE Proto_prefs_changed WO The preferences changed routine of this dissector, a function you define. */
static int Proto_set_prefs_changed(lua_State* L) {
- Proto proto = toProto(L,1);
+ Proto proto = checkProto(L,1);
- if (lua_isfunction(L,3)) {
+ if (lua_isfunction(L,2)) {
/* insert the prefs changed callback into the prefs_changed table */
lua_getglobal(L, WSLUA_PREFS_CHANGED);
lua_replace(L, 1);
lua_pushstring(L,proto->name);
- lua_replace(L, 2);
+ lua_insert(L, 2); /* function is now at 3 */
lua_settable(L,1);
-
} else {
- luaL_argerror(L,3,"The prefs of a protocol must be a function");
+ luaL_argerror(L,2,"The prefs of a protocol must be a function");
}
return 0;
}
+/* WSLUA_ATTRIBUTE Proto_init WO The init routine of this dissector, a function you define.
+ The called init function is passed no arguments. */
static int Proto_set_init(lua_State* L) {
- Proto proto = toProto(L,1);
+ Proto proto = checkProto(L,1);
- if (lua_isfunction(L,3)) {
+ if (lua_isfunction(L,2)) {
/* insert the init routine into the init_routines table */
lua_getglobal(L, WSLUA_INIT_ROUTINES);
lua_replace(L, 1);
lua_pushstring(L,proto->name);
- lua_replace(L, 2);
+ lua_insert(L, 2); /* function is now at 3 */
lua_settable(L,1);
-
- return 0;
} else {
- luaL_argerror(L,3,"The initializer of a protocol must be a function");
- return 0;
+ luaL_argerror(L,2,"The initializer of a protocol must be a function");
}
+ return 0;
}
-static int Proto_get_name(lua_State* L) {
- Proto proto = toProto(L,1);
- lua_pushstring(L,proto->name);
- return 1;
-}
+/* WSLUA_ATTRIBUTE Proto_name RO The name given to this dissector */
+WSLUA_ATTRIBUTE_STRING_GETTER(Proto,name);
-static int Proto_get_description(lua_State* L) {
- Proto proto = toProto(L,1);
- lua_pushstring(L,proto->desc);
- return 1;
-}
+/* WSLUA_ATTRIBUTE Proto_description RO The description given to this dissector */
+WSLUA_ATTRIBUTE_NAMED_STRING_GETTER(Proto,description,desc);
+/* WSLUA_ATTRIBUTE Proto_fields RW The Fields Table of this dissector */
static int Proto_get_fields(lua_State* L) {
- Proto proto = toProto(L,1);
+ Proto proto = checkProto(L,1);
lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields);
return 1;
}
-void wslua_print_stack(char* s, lua_State* L) {
- int i;
-
- for (i=1;i<=lua_gettop(L);i++) {
- printf("%s-%i: %s\n",s,i,lua_typename (L,lua_type(L, i)));
- }
- printf("\n");
-}
-
static int Proto_set_fields(lua_State* L) {
- Proto proto = toProto(L,1);
+ Proto proto = checkProto(L,1);
#define FIELDS_TABLE 2
#define NEW_TABLE 3
#define NEW_FIELD 3
lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields);
- lua_replace(L,FIELDS_TABLE);
+ lua_insert(L,FIELDS_TABLE);
if( lua_istable(L,NEW_TABLE)) {
for (lua_pushnil(L); lua_next(L, NEW_TABLE); ) {
@@ -1570,106 +1564,45 @@ static int Proto_set_fields(lua_State* L) {
return 1;
}
-typedef struct {
- const gchar* name;
- lua_CFunction get;
- lua_CFunction set;
-} proto_actions_t;
-
-static const proto_actions_t proto_actions[] = {
- /* WSLUA_ATTRIBUTE Proto_dissector RW The protocol's dissector, a function you define.
- The called dissector function will be given three arguments of (1) a Tvb object, (2) a Pinfo object, and (3) a TreeItem object. */
- {"dissector", Proto_get_dissector, Proto_set_dissector},
-
- /* WSLUA_ATTRIBUTE Proto_fields RO The Fields Table of this dissector */
- {"fields", Proto_get_fields, Proto_set_fields},
-
- /* WSLUA_ATTRIBUTE Proto_prefs RO The preferences of this dissector */
- {"prefs", Proto_get_prefs, NULL},
-
- /* WSLUA_ATTRIBUTE Proto_prefs_changed WO The preferences changed routine of this dissector, a function you define. */
- {"prefs_changed", NULL, Proto_set_prefs_changed},
-
- /* WSLUA_ATTRIBUTE Proto_init WO The init routine of this dissector, a function you define.
- The called init function is passed no arguments. */
- {"init", NULL, Proto_set_init},
-
- /* WSLUA_ATTRIBUTE Proto_name RO The name given to this dissector */
- {"name", Proto_get_name, NULL},
-
- /* WSLUA_ATTRIBUTE Proto_description RO The description given to this dissector */
- {"description", Proto_get_description, NULL},
-
- {NULL,NULL,NULL}
-};
-
-static int Proto_index(lua_State* L) {
- Proto proto = checkProto(L,1);
- const gchar* name = luaL_checkstring(L,2);
- const proto_actions_t* pa;
-
- if (! (proto && name) ) return 0;
-
- for (pa = proto_actions; pa->name; pa++) {
- if ( g_str_equal(name,pa->name) ) {
- if (pa->get) {
- return pa->get(L);
- } else {
- luaL_error(L,"You cannot get the `%s' attribute of a protocol",name);
- return 0;
- }
- }
- }
-
- luaL_error(L,"A protocol doesn't have a `%s' attribute",name);
- return 0;
-}
-
-static int Proto_newindex(lua_State* L) {
- Proto proto = checkProto(L,1);
- const gchar* name = luaL_checkstring(L,2);
- const proto_actions_t* pa;
-
- if (! (proto && name) ) return 0;
-
- for (pa = proto_actions; pa->name; pa++) {
- if ( g_str_equal(name,pa->name) ) {
- if (pa->set) {
- return pa->set(L);
- } else {
- luaL_error(L,"You cannot set the `%s' attribute of a protocol",name);
- return 0;
- }
- }
- }
-
- luaL_error(L,"A protocol doesn't have a `%s' attribute",name);
- return 0;
-}
-
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int Proto__gc(lua_State* L _U_) {
/* do NOT free Proto, it's never free'd */
return 0;
}
-static const luaL_Reg Proto_meta[] = {
- {"__tostring", Proto_tostring},
- {"__index", Proto_index},
- {"__newindex", Proto_newindex},
+/* This table is ultimately registered as a sub-table of the class' metatable,
+ * and if __index/__newindex is invoked then it calls the appropriate function
+ * from this table for getting/setting the members.
+ */
+WSLUA_ATTRIBUTES Proto_attributes[] = {
+ WSLUA_ATTRIBUTE_RWREG(Proto,dissector),
+ WSLUA_ATTRIBUTE_RWREG(Proto,fields),
+ WSLUA_ATTRIBUTE_ROREG(Proto,prefs),
+ WSLUA_ATTRIBUTE_WOREG(Proto,prefs_changed),
+ WSLUA_ATTRIBUTE_WOREG(Proto,init),
+ WSLUA_ATTRIBUTE_ROREG(Proto,name),
+ WSLUA_ATTRIBUTE_ROREG(Proto,description),
+ { NULL, NULL, NULL }
+};
+
+WSLUA_METHODS Proto_methods[] = {
+ WSLUA_CLASS_FNREG(Proto,new),
{ NULL, NULL }
};
-int Proto_register(lua_State* L) {
+WSLUA_META Proto_meta[] = {
+ WSLUA_CLASS_MTREG(Proto,tostring),
+ WSLUA_CLASS_MTREG(Proto,call),
+ { NULL, NULL }
+};
- WSLUA_REGISTER_META(Proto);
+int Proto_register(lua_State* L) {
+ WSLUA_REGISTER_CLASS(Proto);
+ WSLUA_REGISTER_ATTRIBUTES(Proto);
lua_newtable(L);
protocols_table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
- lua_pushcfunction(L, Proto_new);
- lua_setglobal(L, "Proto");
-
return 0;
}
@@ -1683,9 +1616,9 @@ int wslua_is_field_available(lua_State* L, const char* field_abbr) {
while (lua_next(L, -2)) {
Proto proto;
proto = checkProto(L, -1);
-
+
lua_rawgeti(L, LUA_REGISTRYINDEX, proto->fields);
-
+
lua_pushnil(L);
while (lua_next(L, -2)) {
ProtoField f = checkProtoField(L, -1);
@@ -1699,7 +1632,7 @@ int wslua_is_field_available(lua_State* L, const char* field_abbr) {
lua_pop(L, 2); /* proto->fields and table value */
}
lua_pop(L, 1); /* protocols_table_ref */
-
+
return 0;
}
@@ -1804,14 +1737,14 @@ static int Dissector__gc(lua_State* L _U_) {
return 0;
}
-static const luaL_Reg Dissector_methods[] = {
- {"get", Dissector_get },
- {"call", Dissector_call },
+WSLUA_METHODS Dissector_methods[] = {
+ WSLUA_CLASS_FNREG(Dissector,get),
+ WSLUA_CLASS_FNREG(Dissector,call),
{ NULL, NULL }
};
-static const luaL_Reg Dissector_meta[] = {
- {"__tostring", Dissector__tostring},
+WSLUA_META Dissector_meta[] = {
+ WSLUA_CLASS_MTREG(Dissector,tostring),
{ NULL, NULL }
};
@@ -1904,7 +1837,7 @@ WSLUA_METHOD DissectorTable_add (lua_State *L) {
if( isProto(L,WSLUA_ARG_DissectorTable_add_DISSECTOR) ) {
Proto p;
- p = toProto(L,WSLUA_ARG_DissectorTable_add_DISSECTOR);
+ p = checkProto(L,WSLUA_ARG_DissectorTable_add_DISSECTOR);
handle = p->handle;
if (! handle)
@@ -1957,7 +1890,7 @@ WSLUA_METHOD DissectorTable_set (lua_State *L) {
if( isProto(L,WSLUA_ARG_DissectorTable_set_DISSECTOR) ) {
Proto p;
- p = toProto(L,WSLUA_ARG_DissectorTable_set_DISSECTOR);
+ p = checkProto(L,WSLUA_ARG_DissectorTable_set_DISSECTOR);
handle = p->handle;
if (! handle)
@@ -2013,7 +1946,7 @@ WSLUA_METHOD DissectorTable_remove (lua_State *L) {
if( isProto(L,WSLUA_ARG_DissectorTable_remove_DISSECTOR) ) {
Proto p;
- p = toProto(L,WSLUA_ARG_DissectorTable_remove_DISSECTOR);
+ p = checkProto(L,WSLUA_ARG_DissectorTable_remove_DISSECTOR);
handle = p->handle;
} else if ( isDissector(L,WSLUA_ARG_DissectorTable_remove_DISSECTOR) ) {
@@ -2058,7 +1991,7 @@ WSLUA_METHOD DissectorTable_remove_all (lua_State *L) {
if( isProto(L,WSLUA_ARG_DissectorTable_remove_all_DISSECTOR) ) {
Proto p;
- p = toProto(L,WSLUA_ARG_DissectorTable_remove_all_DISSECTOR);
+ p = checkProto(L,WSLUA_ARG_DissectorTable_remove_all_DISSECTOR);
handle = p->handle;
} else if ( isDissector(L,WSLUA_ARG_DissectorTable_remove_all_DISSECTOR) ) {
@@ -2202,20 +2135,20 @@ static int DissectorTable__gc(lua_State* L _U_) {
return 0;
}
-static const luaL_Reg DissectorTable_methods[] = {
- {"new", DissectorTable_new },
- {"get", DissectorTable_get },
- {"add", DissectorTable_add },
- {"set", DissectorTable_set },
- {"remove", DissectorTable_remove },
- {"remove_all", DissectorTable_remove_all },
- {"try", DissectorTable_try },
- {"get_dissector", DissectorTable_get_dissector },
+WSLUA_METHODS DissectorTable_methods[] = {
+ WSLUA_CLASS_FNREG(DissectorTable,new),
+ WSLUA_CLASS_FNREG(DissectorTable,get),
+ WSLUA_CLASS_FNREG(DissectorTable,add),
+ WSLUA_CLASS_FNREG(DissectorTable,set),
+ WSLUA_CLASS_FNREG(DissectorTable,remove),
+ WSLUA_CLASS_FNREG(DissectorTable,remove_all),
+ WSLUA_CLASS_FNREG(DissectorTable,try),
+ WSLUA_CLASS_FNREG(DissectorTable,get_dissector),
{ NULL, NULL }
};
-static const luaL_Reg DissectorTable_meta[] = {
- {"__tostring", DissectorTable__tostring},
+WSLUA_META DissectorTable_meta[] = {
+ WSLUA_CLASS_MTREG(DissectorTable,tostring),
{ NULL, NULL }
};
diff --git a/epan/wslua/wslua_struct.c b/epan/wslua/wslua_struct.c
index 4c4642ed51..b809831ded 100644
--- a/epan/wslua/wslua_struct.c
+++ b/epan/wslua/wslua_struct.c
@@ -509,14 +509,14 @@ static int Struct__gc(lua_State* L _U_) {
return 0;
}
-static const luaL_Reg Struct_methods[] = {
+WSLUA_METHODS Struct_methods[] = {
{"pack", Struct_pack},
{"unpack", Struct_unpack},
{"size", Struct_size},
- {NULL, NULL}
+ { NULL, NULL }
};
-static const luaL_Reg Struct_meta[] = {
+WSLUA_META Struct_meta[] = {
{ NULL, NULL }
};
diff --git a/epan/wslua/wslua_tree.c b/epan/wslua/wslua_tree.c
index 14a5cb6836..0440fe85e5 100644
--- a/epan/wslua/wslua_tree.c
+++ b/epan/wslua/wslua_tree.c
@@ -49,7 +49,7 @@ TreeItem* push_TreeItem(lua_State*L, TreeItem t) {
CLEAR_OUTSTANDING(TreeItem, expired, TRUE)
-WSLUA_CLASS_DEFINE(TreeItem,NOP,NOP);
+WSLUA_CLASS_DEFINE(TreeItem,FAIL_ON_NULL_OR_EXPIRED("TreeItem"),NOP);
/* TreeItems represent information in the packet-details pane.
A root TreeItem is passed to dissectors as the third argument. */
@@ -306,17 +306,9 @@ WSLUA_METHOD TreeItem_set_text(lua_State *L) {
/* Sets the text of the label */
#define WSLUA_ARG_TreeItem_set_text_TEXT 2 /* The text to be used. */
TreeItem ti = checkTreeItem(L,1);
- const gchar* s;
+ const gchar* s = luaL_checkstring(L,WSLUA_ARG_TreeItem_set_text_TEXT);
- if (ti) {
- if (ti->expired) {
- luaL_error(L,"expired TreeItem");
- return 0;
- }
-
- s = luaL_checkstring(L,WSLUA_ARG_TreeItem_set_text_TEXT);
- proto_item_set_text(ti->item,"%s",s);
- }
+ proto_item_set_text(ti->item,"%s",s);
return 0;
}
@@ -325,17 +317,10 @@ WSLUA_METHOD TreeItem_append_text(lua_State *L) {
/* Appends text to the label */
#define WSLUA_ARG_TreeItem_append_text_TEXT 2 /* The text to be appended. */
TreeItem ti = checkTreeItem(L,1);
- const gchar* s;
+ const gchar* s = luaL_checkstring(L,WSLUA_ARG_TreeItem_append_text_TEXT);
+
+ proto_item_append_text(ti->item,"%s",s);
- if (ti) {
- if (ti->expired) {
- luaL_error(L,"expired TreeItem");
- return 0;
- }
-
- s = luaL_checkstring(L,WSLUA_ARG_TreeItem_append_text_TEXT);
- proto_item_append_text(ti->item,"%s",s);
- }
return 0;
}
@@ -343,17 +328,10 @@ WSLUA_METHOD TreeItem_prepend_text(lua_State *L) {
/* Prepends text to the label */
#define WSLUA_ARG_TreeItem_prepend_text_TEXT 2 /* The text to be prepended */
TreeItem ti = checkTreeItem(L,1);
- const gchar* s;
+ const gchar* s = luaL_checkstring(L,WSLUA_ARG_TreeItem_prepend_text_TEXT);
- if (ti) {
- if (ti->expired) {
- luaL_error(L,"expired TreeItem");
- return 0;
- }
+ proto_item_prepend_text(ti->item,"%s",s);
- s = luaL_checkstring(L,WSLUA_ARG_TreeItem_prepend_text_TEXT);
- proto_item_prepend_text(ti->item,"%s",s);
- }
return 0;
}
@@ -367,13 +345,7 @@ WSLUA_METHOD TreeItem_add_expert_info(lua_State *L) {
int severity = luaL_optint(L,WSLUA_OPTARG_TreeItem_add_expert_info_SEVERITY,PI_CHAT);
const gchar* str = luaL_optstring(L,WSLUA_OPTARG_TreeItem_add_expert_info_TEXT,"Expert Info");
- if ( ti && ti->item ) {
- if (ti->expired) {
- luaL_error(L,"expired TreeItem");
- return 0;
- }
- expert_add_info_format_internal(lua_pinfo, ti->item, group, severity, "%s", str);
- }
+ expert_add_info_format_internal(lua_pinfo, ti->item, group, severity, "%s", str);
return 0;
}
@@ -381,13 +353,9 @@ WSLUA_METHOD TreeItem_add_expert_info(lua_State *L) {
WSLUA_METHOD TreeItem_set_generated(lua_State *L) {
/* Marks the TreeItem as a generated field (with data infered but not contained in the packet). */
TreeItem ti = checkTreeItem(L,1);
- if (ti) {
- if (ti->expired) {
- luaL_error(L,"expired TreeItem");
- return 0;
- }
- PROTO_ITEM_SET_GENERATED(ti->item);
- }
+
+ PROTO_ITEM_SET_GENERATED(ti->item);
+
return 0;
}
@@ -395,13 +363,9 @@ WSLUA_METHOD TreeItem_set_generated(lua_State *L) {
WSLUA_METHOD TreeItem_set_hidden(lua_State *L) {
/* Should not be used */
TreeItem ti = checkTreeItem(L,1);
- if (ti) {
- if (ti->expired) {
- luaL_error(L,"expired TreeItem");
- return 0;
- }
- PROTO_ITEM_SET_HIDDEN(ti->item);
- }
+
+ PROTO_ITEM_SET_HIDDEN(ti->item);
+
return 0;
}
@@ -409,24 +373,16 @@ WSLUA_METHOD TreeItem_set_len(lua_State *L) {
/* Set TreeItem's length inside tvb, after it has already been created. */
#define WSLUA_ARG_TreeItem_set_len_LEN 2 /* The length to be used. */
TreeItem ti = checkTreeItem(L,1);
- gint len;
+ gint len = luaL_checkint(L,WSLUA_ARG_TreeItem_set_len_LEN);
- if (ti) {
- if (ti->expired) {
- luaL_error(L,"expired TreeItem");
- return 0;
- }
-
- len = luaL_checkint(L,WSLUA_ARG_TreeItem_set_len_LEN);
- proto_item_set_len(ti->item, len);
- }
+ proto_item_set_len(ti->item, len);
return 0;
}
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int TreeItem__gc(lua_State* L) {
- TreeItem ti = checkTreeItem(L,1);
+ TreeItem ti = toTreeItem(L,1);
if (!ti) return 0;
if (!ti->expired)
ti->expired = TRUE;
@@ -435,21 +391,21 @@ static int TreeItem__gc(lua_State* L) {
return 0;
}
-static const luaL_Reg TreeItem_methods[] = {
- {"add_packet_field", TreeItem_add_packet_field},
- {"add", TreeItem_add},
- {"add_le", TreeItem_add_le},
- {"set_text", TreeItem_set_text},
- {"append_text", TreeItem_append_text},
- {"prepend_text", TreeItem_prepend_text},
- {"add_expert_info", TreeItem_add_expert_info},
- {"set_generated", TreeItem_set_generated},
- {"set_hidden", TreeItem_set_hidden},
- {"set_len", TreeItem_set_len},
+WSLUA_METHODS TreeItem_methods[] = {
+ WSLUA_CLASS_FNREG(TreeItem,add_packet_field),
+ WSLUA_CLASS_FNREG(TreeItem,add),
+ WSLUA_CLASS_FNREG(TreeItem,add_le),
+ WSLUA_CLASS_FNREG(TreeItem,set_text),
+ WSLUA_CLASS_FNREG(TreeItem,append_text),
+ WSLUA_CLASS_FNREG(TreeItem,prepend_text),
+ WSLUA_CLASS_FNREG(TreeItem,add_expert_info),
+ WSLUA_CLASS_FNREG(TreeItem,set_generated),
+ WSLUA_CLASS_FNREG(TreeItem,set_hidden),
+ WSLUA_CLASS_FNREG(TreeItem,set_len),
{ NULL, NULL }
};
-static const luaL_Reg TreeItem_meta[] = {
+WSLUA_META TreeItem_meta[] = {
{ NULL, NULL }
};
diff --git a/epan/wslua/wslua_tvb.c b/epan/wslua/wslua_tvb.c
index a101fd56e8..2a2c5bcdc7 100644
--- a/epan/wslua/wslua_tvb.c
+++ b/epan/wslua/wslua_tvb.c
@@ -37,7 +37,7 @@
#include "wslua.h"
#include "wsutil/base64.h"
-WSLUA_CLASS_DEFINE(ByteArray,FAIL_ON_NULL("null bytearray"),NOP);
+WSLUA_CLASS_DEFINE(ByteArray,FAIL_ON_NULL("ByteArray"),NOP);
WSLUA_CONSTRUCTOR ByteArray_new(lua_State* L) { /* Creates a ByteArray Object */
#define WSLUA_OPTARG_ByteArray_new_HEXBYTES 1 /* A string consisting of hexadecimal bytes like "00 B1 A2" or "1a2b3c4d" */
@@ -84,7 +84,7 @@ WSLUA_CONSTRUCTOR ByteArray_new(lua_State* L) { /* Creates a ByteArray Object */
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int ByteArray__gc(lua_State* L) {
- ByteArray ba = checkByteArray(L,1);
+ ByteArray ba = toByteArray(L,1);
if (!ba) return 0;
@@ -101,9 +101,6 @@ WSLUA_METAMETHOD ByteArray__concat(lua_State* L) {
ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray__cat_SECOND);
ByteArray ba;
- if (! (ba1 && ba2) )
- WSLUA_ERROR(ByteArray__cat,"Both arguments must be ByteArrays");
-
ba = g_byte_array_new();
g_byte_array_append(ba,ba1->data,ba1->len);
g_byte_array_append(ba,ba2->data,ba2->len);
@@ -118,9 +115,6 @@ WSLUA_METHOD ByteArray_prepend(lua_State* L) {
ByteArray ba = checkByteArray(L,1);
ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray_prepend_PREPENDED);
- if (! (ba && ba2) )
- WSLUA_ERROR(ByteArray_prepend,"Both arguments must be ByteArrays");
-
g_byte_array_prepend(ba,ba2->data,ba2->len);
return 0;
@@ -132,9 +126,6 @@ WSLUA_METHOD ByteArray_append(lua_State* L) {
ByteArray ba = checkByteArray(L,1);
ByteArray ba2 = checkByteArray(L,WSLUA_ARG_ByteArray_append_APPENDED);
- if (! (ba && ba2) )
- WSLUA_ERROR(ByteArray_append,"Both arguments must be ByteArrays");
-
g_byte_array_append(ba,ba2->data,ba2->len);
return 0;
@@ -148,7 +139,6 @@ WSLUA_METHOD ByteArray_set_size(lua_State* L) {
int siz = luaL_checkint(L,WSLUA_ARG_ByteArray_set_size_SIZE);
guint8* padding;
- if (!ba) return 0;
if (siz < 0)
WSLUA_ERROR(ByteArray_set_size,"ByteArray size must be non-negative");
@@ -170,8 +160,6 @@ WSLUA_METHOD ByteArray_set_index(lua_State* L) {
int idx = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_INDEX);
int v = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_VALUE);
- if (!ba) return 0;
-
if (idx == 0 && ! g_str_equal(luaL_optstring(L,2,""),"0") ) {
luaL_argerror(L,2,"bad index");
return 0;
@@ -199,8 +187,6 @@ WSLUA_METHOD ByteArray_get_index(lua_State* L) {
ByteArray ba = checkByteArray(L,1);
int idx = luaL_checkint(L,WSLUA_ARG_ByteArray_get_index_INDEX);
- if (!ba) return 0;
-
if (idx == 0 && ! g_str_equal(luaL_optstring(L,2,""),"0") ) {
luaL_argerror(L,2,"bad index");
return 0;
@@ -219,8 +205,6 @@ WSLUA_METHOD ByteArray_len(lua_State* L) {
/* Obtain the length of a ByteArray */
ByteArray ba = checkByteArray(L,1);
- if (!ba) return 0;
-
lua_pushnumber(L,(lua_Number)ba->len);
WSLUA_RETURN(1); /* The length of the ByteArray. */
@@ -235,8 +219,6 @@ WSLUA_METHOD ByteArray_subset(lua_State* L) {
int len = luaL_checkint(L,WSLUA_ARG_ByteArray_set_index_LENGTH);
ByteArray sub;
- if (!ba) return 0;
-
if ((offset + len) > (int)ba->len || offset < 0 || len < 1) {
luaL_error(L,"Out Of Bounds");
return 0;
@@ -257,8 +239,6 @@ WSLUA_METHOD ByteArray_base64_decode(lua_State* L) {
gchar *data;
size_t len;
- if (!ba) return 0;
-
ba2 = g_byte_array_new();
data = (gchar*)g_malloc (ba->len + 1);
memcpy(data, ba->data, ba->len);
@@ -312,23 +292,23 @@ WSLUA_METAMETHOD ByteArray__tostring(lua_State* L) {
static int ByteArray_tvb (lua_State *L);
-static const luaL_Reg ByteArray_methods[] = {
- {"new", ByteArray_new},
- {"len", ByteArray_len},
- {"prepend", ByteArray_prepend},
- {"append", ByteArray_append},
- {"subset", ByteArray_subset},
- {"set_size", ByteArray_set_size},
- {"tvb", ByteArray_tvb},
- {"base64_decode", ByteArray_base64_decode},
- {"get_index", ByteArray_get_index},
- {"set_index", ByteArray_set_index},
+WSLUA_METHODS ByteArray_methods[] = {
+ WSLUA_CLASS_FNREG(ByteArray,new),
+ WSLUA_CLASS_FNREG(ByteArray,len),
+ WSLUA_CLASS_FNREG(ByteArray,prepend),
+ WSLUA_CLASS_FNREG(ByteArray,append),
+ WSLUA_CLASS_FNREG(ByteArray,subset),
+ WSLUA_CLASS_FNREG(ByteArray,set_size),
+ WSLUA_CLASS_FNREG(ByteArray,tvb),
+ WSLUA_CLASS_FNREG(ByteArray,base64_decode),
+ WSLUA_CLASS_FNREG(ByteArray,get_index),
+ WSLUA_CLASS_FNREG(ByteArray,set_index),
{ NULL, NULL }
};
-static const luaL_Reg ByteArray_meta[] = {
- {"__tostring", ByteArray__tostring},
- {"__concat", ByteArray__concat},
+WSLUA_META ByteArray_meta[] = {
+ WSLUA_CLASS_MTREG(ByteArray,tostring),
+ WSLUA_CLASS_MTREG(ByteArray,concat),
{"__call",ByteArray_subset},
{ NULL, NULL }
};
@@ -362,7 +342,7 @@ int ByteArray_register(lua_State* L) {
*
*/
-WSLUA_CLASS_DEFINE(Tvb,FAIL_ON_NULL("expired tvb"),NOP);
+WSLUA_CLASS_DEFINE(Tvb,FAIL_ON_NULL_OR_EXPIRED("Tvb"),NOP);
/* A Tvb represents the packet's buffer. It is passed as an argument to listeners and dissectors,
and can be used to extract information (via TvbRange) from the packet's data. Beware that Tvbs are usable only by the current
listener or dissector call and are destroyed as soon as the listener/dissector returns, so references
@@ -435,8 +415,6 @@ WSLUA_CONSTRUCTOR ByteArray_tvb (lua_State *L) {
guint8* data;
Tvb tvb;
- if (!ba) return 0;
-
if (!lua_tvb) {
luaL_error(L,"Tvbs can only be created and used in dissectors");
return 0;
@@ -487,12 +465,6 @@ WSLUA_METAMETHOD Tvb__tostring(lua_State* L) {
int len;
gchar* str;
- if (!tvb) return 0;
- if (tvb->expired) {
- luaL_error(L,"expired tvb");
- return 0;
- }
-
len = tvb_length(tvb->ws_tvb);
str = ep_strdup_printf("TVB(%i) : %s",len,tvb_bytes_to_ep_str(tvb->ws_tvb,0,len));
lua_pushstring(L,str);
@@ -501,7 +473,7 @@ WSLUA_METAMETHOD Tvb__tostring(lua_State* L) {
/* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
static int Tvb__gc(lua_State* L) {
- Tvb tvb = checkTvb(L,1);
+ Tvb tvb = toTvb(L,1);
free_Tvb(tvb);
@@ -513,12 +485,6 @@ WSLUA_METHOD Tvb_reported_len(lua_State* L) {
/* Obtain the reported length of a TVB */
Tvb tvb = checkTvb(L,1);
- if (!tvb) return 0;
- if (tvb->expired) {
- luaL_error(L,"expired tvb");
- return 0;
- }
-
lua_pushnumber(L,tvb_reported_length(tvb->ws_tvb));
WSLUA_RETURN(1); /* The length of the Tvb. */
}
@@ -527,12 +493,6 @@ WSLUA_METHOD Tvb_len(lua_State* L) {
/* Obtain the length of a TVB */
Tvb tvb = checkTvb(L,1);
- if (!tvb) return 0;
- if (tvb->expired) {
- luaL_error(L,"expired tvb");
- return 0;
- }
-
lua_pushnumber(L,tvb_length(tvb->ws_tvb));
WSLUA_RETURN(1); /* The length of the Tvb. */
}
@@ -543,12 +503,6 @@ WSLUA_METHOD Tvb_reported_length_remaining(lua_State* L) {
Tvb tvb = checkTvb(L,1);
int offset = luaL_optint(L, Tvb_reported_length_remaining_OFFSET, 0);
- if (!tvb) return 0;
- if (tvb->expired) {
- luaL_error(L,"expired tvb");
- return 0;
- }
-
lua_pushnumber(L,tvb_reported_length_remaining(tvb->ws_tvb, offset));
WSLUA_RETURN(1); /* The length of the Tvb. */
}
@@ -557,12 +511,6 @@ WSLUA_METHOD Tvb_offset(lua_State* L) {
/* Returns the raw offset (from the beginning of the source Tvb) of a sub Tvb. */
Tvb tvb = checkTvb(L,1);
- if (!tvb) return 0;
- if (tvb->expired) {
- luaL_error(L,"expired tvb");
- return 0;
- }
-
lua_pushnumber(L,tvb_raw_offset(tvb->ws_tvb));
WSLUA_RETURN(1); /* The raw offset of the Tvb. */
}
@@ -575,7 +523,7 @@ WSLUA_METAMETHOD Tvb__call(lua_State* L) {
}
#endif
-WSLUA_CLASS_DEFINE(TvbRange,FAIL_ON_NULL("expired tvbrange"),NOP);
+WSLUA_CLASS_DEFINE(TvbRange,FAIL_ON_NULL("TvbRange"),NOP);
/*
A TvbRange represents a usable range of a Tvb and is used to extract data from the Tvb that generated it
TvbRanges are created by calling a tvb (e.g. tvb(offset,length)). If the TvbRange span is outside the
@@ -624,12 +572,6 @@ WSLUA_METHOD Tvb_range(lua_State* L) {
int offset = luaL_optint(L,WSLUA_OPTARG_Tvb_range_OFFSET,0);
int len = luaL_optint(L,WSLUA_OPTARG_Tvb_range_LENGTH,-1);
- if (!tvb) return 0;
- if (tvb->expired) {
- luaL_error(L,"expired tvb");
- return 0;
- }
-
if (push_TvbRange(L,tvb->ws_tvb,offset,len)) {
WSLUA_RETURN(1); /* The TvbRange */
}
@@ -637,18 +579,18 @@ WSLUA_METHOD Tvb_range(lua_State* L) {
return 0;
}
-static const luaL_Reg Tvb_methods[] = {
- {"range", Tvb_range},
- {"len", Tvb_len},
- {"offset", Tvb_offset},
- {"reported_len", Tvb_reported_len},
- {"reported_length_remaining", Tvb_reported_length_remaining},
+WSLUA_METHODS Tvb_methods[] = {
+ WSLUA_CLASS_FNREG(Tvb,range),
+ WSLUA_CLASS_FNREG(Tvb,len),
+ WSLUA_CLASS_FNREG(Tvb,offset),
+ WSLUA_CLASS_FNREG(Tvb,reported_len),
+ WSLUA_CLASS_FNREG(Tvb,reported_length_remaining),
{ NULL, NULL }
};
-static const luaL_Reg Tvb_meta[] = {
+WSLUA_META Tvb_meta[] = {
+ WSLUA_CLASS_MTREG(Tvb,tostring),
{"__call", Tvb_range},
- {"__tostring", Tvb__tostring},
{ NULL, NULL }
};
@@ -1435,42 +1377,42 @@ WSLUA_METAMETHOD TvbRange__tostring(lua_State* L) {
return 1;
}
-static const luaL_Reg TvbRange_methods[] = {
- {"uint", TvbRange_uint},
- {"le_uint", TvbRange_le_uint},
- {"int", TvbRange_int},
- {"le_int", TvbRange_le_int},
- {"uint64", TvbRange_uint64},
- {"le_uint64", TvbRange_le_uint64},
- {"int64", TvbRange_int64},
- {"le_int64", TvbRange_le_int64},
- {"float", TvbRange_float},
- {"le_float", TvbRange_le_float},
- {"ether", TvbRange_ether},
- {"ipv4", TvbRange_ipv4},
- {"le_ipv4", TvbRange_le_ipv4},
- {"nstime", TvbRange_nstime},
- {"le_nstime", TvbRange_le_nstime},
- {"string", TvbRange_string},
- {"stringz", TvbRange_stringz},
- {"strsize", TvbRange_strsize},
- {"bytes", TvbRange_bytes},
- {"bitfield", TvbRange_bitfield},
- {"range", TvbRange_range},
- {"len", TvbRange_len},
- {"offset", TvbRange_offset},
- {"tvb", TvbRange_tvb},
- {"le_ustring", TvbRange_le_ustring},
- {"ustring", TvbRange_ustring},
- {"le_ustringz", TvbRange_le_ustringz},
- {"ustringz", TvbRange_ustringz},
- {"uncompress", TvbRange_uncompress},
+WSLUA_METHODS TvbRange_methods[] = {
+ WSLUA_CLASS_FNREG(TvbRange,uint),
+ WSLUA_CLASS_FNREG(TvbRange,le_uint),
+ WSLUA_CLASS_FNREG(TvbRange,int),
+ WSLUA_CLASS_FNREG(TvbRange,le_int),
+ WSLUA_CLASS_FNREG(TvbRange,uint64),
+ WSLUA_CLASS_FNREG(TvbRange,le_uint64),
+ WSLUA_CLASS_FNREG(TvbRange,int64),
+ WSLUA_CLASS_FNREG(TvbRange,le_int64),
+ WSLUA_CLASS_FNREG(TvbRange,float),
+ WSLUA_CLASS_FNREG(TvbRange,le_float),
+ WSLUA_CLASS_FNREG(TvbRange,ether),
+ WSLUA_CLASS_FNREG(TvbRange,ipv4),
+ WSLUA_CLASS_FNREG(TvbRange,le_ipv4),
+ WSLUA_CLASS_FNREG(TvbRange,nstime),
+ WSLUA_CLASS_FNREG(TvbRange,le_nstime),
+ WSLUA_CLASS_FNREG(TvbRange,string),
+ WSLUA_CLASS_FNREG(TvbRange,stringz),
+ WSLUA_CLASS_FNREG(TvbRange,strsize),
+ WSLUA_CLASS_FNREG(TvbRange,bytes),
+ WSLUA_CLASS_FNREG(TvbRange,bitfield),
+ WSLUA_CLASS_FNREG(TvbRange,range),
+ WSLUA_CLASS_FNREG(TvbRange,len),
+ WSLUA_CLASS_FNREG(TvbRange,offset),
+ WSLUA_CLASS_FNREG(TvbRange,tvb),
+ WSLUA_CLASS_FNREG(TvbRange,le_ustring),
+ WSLUA_CLASS_FNREG(TvbRange,ustring),
+ WSLUA_CLASS_FNREG(TvbRange,le_ustringz),
+ WSLUA_CLASS_FNREG(TvbRange,ustringz),
+ WSLUA_CLASS_FNREG(TvbRange,uncompress),
{ NULL, NULL }
};
-static const luaL_Reg TvbRange_meta[] = {
- {"__tostring", TvbRange__tostring},
- {"__concat", wslua__concat},
+WSLUA_META TvbRange_meta[] = {
+ WSLUA_CLASS_MTREG(TvbRange,tostring),
+ WSLUA_CLASS_MTREG(wslua,concat),
{"__call", TvbRange_range},
{ NULL, NULL }
};
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);