if (lua_isfunction(L,3)) {
/* insert the dissector into the dissectors table */
- lua_getglobal(L, LUA_DISSECTORS_TABLE);
+ lua_pushstring(L, LUA_DISSECTORS_TABLE);
+ lua_gettable(L, LUA_REGISTRYINDEX);
+
lua_replace(L, 1);
lua_pushstring(L,proto->name);
lua_replace(L, 2);
if (lua_isfunction(L,3)) {
/* insert the dissector into the dissectors table */
- lua_getglobal(L, LUA_INIT_ROUTINES);
+ lua_pushstring(L, LUA_INIT_ROUTINES);
+ lua_gettable(L, LUA_REGISTRYINDEX);
lua_replace(L, 1);
lua_pushstring(L,proto->name);
lua_replace(L, 2);
if (lua_isfunction(L,3)) {
/* insert the dissector into the dissectors table */
- lua_getglobal(L, LUA_HANDOFF_ROUTINES);
+ lua_pushstring(L, LUA_HANDOFF_ROUTINES);
+ lua_gettable(L, LUA_REGISTRYINDEX);
lua_replace(L, 1);
lua_pushstring(L,proto->name);
lua_replace(L, 2);
lua_pushcfunction(L, Proto_register_postdissector);
lua_settable(L, LUA_GLOBALSINDEX);
- lua_pushstring(L, "new_protocol");
+ lua_pushstring(L, PROTO);
lua_pushcfunction(L, Proto_new);
lua_settable(L, LUA_GLOBALSINDEX);
static int DissectorTable_add (lua_State *L) {
DissectorTable dt = checkDissectorTable(L,1);
- Proto p;
ftenum_t type;
Dissector handle;
if (!dt) return 0;
- if(( p = luaL_checkudata(L,3,PROTO) )) {
+ if( isProto(L,3) ) {
+ Proto p;
+ p = toProto(L,3);
handle = p->handle;
- } else if (! ( handle = luaL_checkudata(L,3,DISSECTOR) )) {
+ } else if ( isDissector(L,3) ) {
+ handle = toDissector(L,3);
+ } else {
luaL_argerror(L,3,"Must be either " PROTO " or " DISSECTOR );
return 0;
}
lua_settop(L,0);
- lua_getglobal(L, LUA_DISSECTORS_TABLE);
+ lua_pushstring(L, LUA_DISSECTORS_TABLE);
+ lua_gettable(L, LUA_REGISTRYINDEX);
if (!lua_istable(L, -1)) {
proto_item* pi = proto_tree_add_text(tree,tvb,0,0,"Lua: either `" LUA_DISSECTORS_TABLE "' does not exist or it is not a table!");
lua_settop(LS,0);
lua_pushcfunction(LS,error_handler);
- lua_getglobal(LS, table_name);
-
+ lua_pushstring(LS, table_name);
+ lua_gettable(LS, LUA_REGISTRYINDEX);
+
if (!lua_istable(LS, 2)) {
report_failure("Lua: either `%s' does not exist or it is not a table!\n",table_name);
lua_close(LS);
lua_pushstring(L, "report_failure");
lua_pushcfunction(L, lua_report_failure);
lua_settable(L, LUA_GLOBALSINDEX);
-
+
+
+
lua_pushstring(L, LUA_HANDOFF_ROUTINES);
lua_newtable (L);
- lua_settable(L, LUA_GLOBALSINDEX);
+ lua_settable(L, LUA_REGISTRYINDEX);
lua_pushstring(L, LUA_INIT_ROUTINES);
lua_newtable (L);
- lua_settable(L, LUA_GLOBALSINDEX);
+ lua_settable(L, LUA_REGISTRYINDEX);
lua_pushstring(L, LUA_DISSECTORS_TABLE);
lua_newtable (L);
- lua_settable(L, LUA_GLOBALSINDEX);
+ lua_settable(L, LUA_REGISTRYINDEX);
lua_pushstring(L, LUA_TAP_PACKET);
lua_newtable (L);
#define SUBTREE "SubTree"
typedef int* SubTree;
-#define PROTO "Proto"
+#define PROTO "Protocol"
typedef struct _eth_proto_t* Proto;
#define DISSECTOR_TABLE "DissectorTable"
#define NOP
+/*
+ * toXxx(L,idx) gets a Xxx from an index (Lua Error if fails)
+ * checkXxx(L,idx) gets a Xxx from an index after calling check_code (No Lua Error if it fails)
+ * pushXxx(L,xxx) pushes an Xxx into the stack
+ * isXxx(L,idx) tests whether we have an Xxx at idx
+ */
#define LUA_CLASS_DEFINE(C,CN,check_code) \
C to##C(lua_State* L, int index) { \
C* v = (C*)lua_touserdata (L, index); \
C* p = lua_newuserdata(L,sizeof(C)); *p = v; \
luaL_getmetatable(L, CN); lua_setmetatable(L, -2); \
return p; \
+}\
+gboolean is##C(lua_State* L,int i) { \
+ return (gboolean)(lua_isuserdata(L,i) && luaL_checkudata(L,3,CN)); \
}
+
extern packet_info* lua_pinfo;
extern proto_tree* lua_tree;
extern tvbuff_t* lua_tvb;
extern C to##C(lua_State* L, int index); \
extern C check##C(lua_State* L, int index); \
extern C* push##C(lua_State* L, C v); \
-extern int C##_register(lua_State* L);
+extern int C##_register(lua_State* L); \
+extern gboolean is##C(lua_State* L,int i)
+
LUA_CLASS_DECLARE(Tap,TAP);
LUA_CLASS_DECLARE(Field,FIELD);
extern void lua_prime_all_fields(proto_tree* tree);
void lua_register_subtrees(void);
-#if 0
-#define WARNSTACK(s) {int i; for (i = 1; i <= lua_gettop(L); i++) if (lua_isstring(L,i)) printf("-%s-> %i `%s'\n",s,i , lua_tostring(L,i)); else printf("-%s-> %i %s\n",s,i , lua_typename(L,lua_type(L,i))); }
-#endif
-
#endif