diff --git epan/wslua/init_wslua.c epan/wslua/init_wslua.c index d7f2e3a..4407eb4 100644 --- epan/wslua/init_wslua.c +++ epan/wslua/init_wslua.c @@ -130,12 +130,11 @@ int dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) { } -static void iter_table_and_call(lua_State* LS, int env, const gchar* table_name, lua_CFunction error_handler) { +static void iter_table_and_call(lua_State* LS, const gchar* table_name, lua_CFunction error_handler) { lua_settop(LS,0); lua_pushcfunction(LS,error_handler); - lua_pushstring(LS, table_name); - lua_gettable(LS, env); + lua_getglobal(LS, table_name); if (!lua_istable(LS, 2)) { report_failure("Lua: either `%s' does not exist or it is not a table!\n",table_name); @@ -183,7 +182,7 @@ static void wslua_init_routine(void) { } if (L) { - iter_table_and_call(L, LUA_GLOBALSINDEX, WSLUA_INIT_ROUTINES,init_error_handler); + iter_table_and_call(L, WSLUA_INIT_ROUTINES,init_error_handler); } } @@ -239,7 +238,11 @@ static gboolean lua_load_script(const gchar* filename) { lua_pushcfunction(L,lua_main_error_handler); +#if LUA_VERSION_NUM >= 502 + error = lua_load(L,getF,file,filename,NULL); +#else error = lua_load(L,getF,file,filename); +#endif switch (error) { case 0: lua_pcall(L,0,0,1); @@ -254,6 +257,10 @@ static gboolean lua_load_script(const gchar* filename) { report_failure("Lua: memory allocation error during execution of %s",filename); fclose(file); return FALSE; + default: + report_failure("Lua: unspecified error during execution of %s", filename); + fclose(file); + return FALSE; } report_failure("Lua: unknown error during execution of %s: %d",filename,error); @@ -348,9 +355,8 @@ int wslua_init(register_cb cb, gpointer client_data) { lua_atpanic(L,wslua_panic); /* the init_routines table (accessible by the user) */ - lua_pushstring(L, WSLUA_INIT_ROUTINES); lua_newtable (L); - lua_settable(L, LUA_GLOBALSINDEX); + lua_setglobal(L, WSLUA_INIT_ROUTINES); /* the dissectors table goes in the registry (not accessible) */ lua_newtable (L); @@ -374,8 +380,7 @@ int wslua_init(register_cb cb, gpointer client_data) { filename = NULL; /* check if lua is to be disabled */ - lua_pushstring(L,"disable_lua"); - lua_gettable(L, LUA_GLOBALSINDEX); + lua_getglobal(L,"disable_lua"); if (lua_isboolean(L,-1) && lua_toboolean(L,-1)) { /* disable lua */ @@ -388,8 +393,7 @@ int wslua_init(register_cb cb, gpointer client_data) { lua_load_plugins(get_plugin_dir()); /* check whether we should run other scripts even if running superuser */ - lua_pushstring(L,"run_user_scripts_when_superuser"); - lua_gettable(L, LUA_GLOBALSINDEX); + lua_getglobal(L,"run_user_scripts_when_superuser"); if (lua_isboolean(L,-1) && lua_toboolean(L,-1)) { run_anyway = TRUE; @@ -424,9 +428,8 @@ int wslua_init(register_cb cb, gpointer client_data) { * after this point it is too late to register a menu * disable the function to avoid weirdness */ - lua_pushstring(L, "register_menu"); lua_pushcfunction(L, wslua_not_register_menu); - lua_settable(L, LUA_GLOBALSINDEX); + lua_setglobal(L, "register_menu"); /* set up some essential globals */ lua_pinfo = NULL; diff --git epan/wslua/lua_bitop.c epan/wslua/lua_bitop.c index 5fb7789..e602615 100644 --- epan/wslua/lua_bitop.c +++ epan/wslua/lua_bitop.c @@ -81,7 +81,7 @@ static UBits barg(lua_State *L, int idx) #error "Unknown number type, check LUA_NUMBER_* in luaconf.h" #endif if (b == 0 && !lua_isnumber(L, idx)) - luaL_typerror(L, idx, "number"); + luaL_error(L, "bad argument %d (number expected, got %s)", idx, lua_typename(L, lua_type(L, idx))); return b; } @@ -174,7 +174,14 @@ LUALIB_API int luaopen_bit(lua_State *L) msg = "arithmetic right-shift broken"; luaL_error(L, "bit library self-test failed (%s)", msg); } + +#if LUA_VERSION_NUM >= 502 + luaL_newlib(L, bit_funcs); + lua_setglobal(L, "bit"); +#else luaL_register(L, "bit", bit_funcs); +#endif + return 1; } diff --git epan/wslua/make-taps.pl epan/wslua/make-taps.pl index c916d86..0ca8e46 100755 --- epan/wslua/make-taps.pl +++ epan/wslua/make-taps.pl @@ -195,14 +195,14 @@ TBLFTR for my $ename (sort keys %enums) { - print CFILE "\n\t/*\n\t * $ename\n\t */\n\tlua_pushstring(L,\"$ename\"); lua_newtable(L);\n"; + print CFILE "\n\t/*\n\t * $ename\n\t */\n\tlua_newtable(L);\n"; for my $a (@{$enums{$ename}}) { print CFILE <<"ENUMELEM"; - lua_pushstring(L,"$a"); lua_pushnumber(L,(lua_Number)$a); lua_settable(L,LUA_GLOBALSINDEX); + lua_pushnumber(L,(lua_Number)$a); lua_setglobal(L,"$a"); lua_pushnumber(L,(lua_Number)$a); lua_pushstring(L,"$a"); lua_settable(L,-3); ENUMELEM } - print CFILE "\tlua_settable(L,LUA_GLOBALSINDEX);\n"; + print CFILE "\tlua_setglobal(L,\"$ename\");\n"; } print CFILE <<"TAIL"; diff --git epan/wslua/wslua.h epan/wslua/wslua.h index b593b7e..a919543 100644 --- epan/wslua/wslua.h +++ epan/wslua/wslua.h @@ -258,7 +258,7 @@ typedef struct _wslua_private_table* PrivateTable; #define WSLUA_CLASS_DEFINE(C,check_code,push_code) \ C to##C(lua_State* L, int idx) { \ C* v = (C*)lua_touserdata (L, idx); \ - if (!v) luaL_typerror(L,idx,#C); \ + if (!v) luaL_error(L, "bad argument %d (%s expected, got %s)", idx, #C, lua_typename(L, lua_type(L, idx))); \ return v ? *v : NULL; \ } \ C check##C(lua_State* L, int idx) { \ @@ -299,6 +299,29 @@ typedef int dummy##C #ifdef HAVE_LUA_5_1 +#if LUA_VERSION_NUM >= 502 +#define WSLUA_REGISTER_CLASS(C) { \ + int lib_idx, meta_idx; \ + lua_createtable(L, 0, 0); \ + lib_idx = lua_gettop(L); \ + luaL_newmetatable(L, #C); \ + meta_idx = lua_gettop(L); \ + luaL_setfuncs(L, C ## _meta, 0); \ + luaL_newlib(L, C ## _methods); \ + lua_setfield(L, meta_idx, "__index"); \ + luaL_newlib(L, C ## _meta); \ + lua_setfield(L, meta_idx, "__metatable"); \ + lua_setmetatable(L, lib_idx); \ + lua_setglobal(L, #C); \ +} + +#define WSLUA_REGISTER_META(C) { \ + luaL_newmetatable (L, #C); \ + luaL_setfuncs (L, C ## _meta, 0); \ + lua_pop(L,1); \ +} + +#else #define WSLUA_REGISTER_CLASS(C) { \ luaL_register (L, #C, C ## _methods); \ luaL_newmetatable (L, #C); \ @@ -317,6 +340,7 @@ typedef int dummy##C luaL_register (L, NULL, C ## _meta); \ lua_pop(L,1); \ } +#endif #define WSLUA_INIT(L) \ luaL_openlibs(L); \ @@ -326,7 +350,9 @@ typedef int dummy##C #endif #define WSLUA_FUNCTION extern int -#define WSLUA_REGISTER_FUNCTION(name) { lua_pushstring(L, #name); lua_pushcfunction(L, wslua_## name); lua_settable(L, LUA_GLOBALSINDEX); } + +#define WSLUA_REGISTER_FUNCTION(name) { lua_pushcfunction(L, wslua_## name); lua_setglobal(L, #name); } + #define WSLUA_REGISTER extern int #define WSLUA_METHOD static int @@ -335,17 +361,17 @@ typedef int dummy##C #define WSLUA_ATTR_GET static int #define WSLUA_METAMETHOD static int -#define WSLUA_METHODS static const luaL_reg -#define WSLUA_META static const luaL_reg +#define WSLUA_METHODS static const luaL_Reg +#define WSLUA_META static const luaL_Reg #define WSLUA_CLASS_FNREG(class,name) { #name, class##_##name } #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; } #define WSLUA_OPTARG_ERROR(name,attr,error) { luaL_argerror(L,WSLUA_OPTARG_##name##_ ##attr, #name ": " error); return 0; } -#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushstring(L,n); lua_pushboolean(L,v); lua_settable(L, LUA_GLOBALSINDEX); } -#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,n); lua_pushstring(L,v); lua_settable(L, LUA_GLOBALSINDEX); } -#define WSLUA_REG_GLOBAL_NUMBER(L,n,v) { lua_pushstring(L,n); lua_pushnumber(L,v); lua_settable(L, LUA_GLOBALSINDEX); } +#define WSLUA_REG_GLOBAL_BOOL(L,n,v) { lua_pushboolean(L,v); lua_setglobal(L,n); } +#define WSLUA_REG_GLOBAL_STRING(L,n,v) { lua_pushstring(L,v); lua_setglobal(L,n); } +#define WSLUA_REG_GLOBAL_NUMBER(L,n,v) { lua_pushnumber(L,v); lua_setglobal(L,n); } #define WSLUA_RETURN(i) return (i); diff --git epan/wslua/wslua_field.c epan/wslua/wslua_field.c index b9505b7..7a380e0 100644 --- epan/wslua/wslua_field.c +++ epan/wslua/wslua_field.c @@ -208,7 +208,7 @@ static int FieldInfo_get_name(lua_State* L) { return 1; } -static const luaL_reg FieldInfo_get[] = { +static const luaL_Reg FieldInfo_get[] = { /* {"data_source", FieldInfo_get_data_source }, */ {"range", FieldInfo_get_range}, /* {"hidden", FieldInfo_get_hidden}, */ @@ -232,7 +232,7 @@ static int FieldInfo__index(lua_State* L) { Other attributes: */ const gchar* idx = luaL_checkstring(L,2); - const luaL_reg* r; + const luaL_Reg* r; checkFieldInfo(L,1); @@ -294,7 +294,7 @@ WSLUA_METAMETHOD FieldInfo__lt(lua_State* L) { } -static const luaL_reg FieldInfo_meta[] = { +static const luaL_Reg FieldInfo_meta[] = { {"__tostring", FieldInfo__tostring}, {"__call", FieldInfo__call}, {"__index", FieldInfo__index}, @@ -469,12 +469,12 @@ WSLUA_METAMETHOD Field_tostring(lua_State* L) { return 1; } -static const luaL_reg Field_methods[] = { +static const luaL_Reg Field_methods[] = { {"new", Field_new}, { NULL, NULL } }; -static const luaL_reg Field_meta[] = { +static const luaL_Reg Field_meta[] = { {"__tostring", Field_tostring}, {"__call", Field__call}, { NULL, NULL } diff --git epan/wslua/wslua_listener.c epan/wslua/wslua_listener.c index 6e43f6b..fb04a4c 100644 --- epan/wslua/wslua_listener.c +++ epan/wslua/wslua_listener.c @@ -297,13 +297,13 @@ static int Listener_newindex(lua_State* L) { } -static const luaL_reg Listener_methods[] = { +static const luaL_Reg Listener_methods[] = { {"new", Listener_new}, {"remove", Listener_remove}, { NULL, NULL } }; -static const luaL_reg Listener_meta[] = { +static const luaL_Reg Listener_meta[] = { {"__tostring", Listener_tostring}, {"__newindex", Listener_newindex}, { NULL, NULL } diff --git epan/wslua/wslua_pinfo.c epan/wslua/wslua_pinfo.c index 435b890..f61b14c 100644 --- epan/wslua/wslua_pinfo.c +++ epan/wslua/wslua_pinfo.c @@ -301,9 +301,9 @@ WSLUA_META NSTime_meta[] = { int NSTime_register(lua_State* L) { WSLUA_REGISTER_META(NSTime); - lua_pushstring(L, "NSTime"); lua_pushcfunction(L, NSTime_new); - lua_settable(L, LUA_GLOBALSINDEX); + lua_setglobal(L, "NSTime"); + return 1; } @@ -809,7 +809,7 @@ static int Columns_gc(lua_State* L) { } -static const luaL_reg Columns_meta[] = { +static const luaL_Reg Columns_meta[] = { {"__tostring", Columns__tostring }, {"__newindex", Columns__newindex }, {"__index", Columns_index}, @@ -1444,7 +1444,7 @@ static int Pinfo_gc(lua_State* L) { } -static const luaL_reg Pinfo_meta[] = { +static const luaL_Reg Pinfo_meta[] = { {"__index", Pinfo_index}, {"__newindex",Pinfo_setindex}, {"__tostring", Pinfo_tostring}, diff --git epan/wslua/wslua_proto.c epan/wslua/wslua_proto.c index 439a91c..384f83f 100644 --- epan/wslua/wslua_proto.c +++ epan/wslua/wslua_proto.c @@ -1095,7 +1095,7 @@ static int ProtoField_gc(lua_State* L) { return 0; } -static const luaL_reg ProtoField_methods[] = { +static const luaL_Reg ProtoField_methods[] = { {"new", ProtoField_new}, {"uint8",ProtoField_uint8}, {"uint16",ProtoField_uint16}, @@ -1126,7 +1126,7 @@ static const luaL_reg ProtoField_methods[] = { { NULL, NULL } }; -static const luaL_reg ProtoField_meta[] = { +static const luaL_Reg ProtoField_meta[] = { {"__tostring", ProtoField__tostring }, {"__gc", ProtoField_gc }, { NULL, NULL } @@ -1281,12 +1281,21 @@ static int Proto_set_init(lua_State* L) { if (lua_isfunction(L,3)) { /* insert the dissector into the dissectors table */ lua_pushstring(L, WSLUA_INIT_ROUTINES); +#if LUA_VERSION_NUM >= 502 + lua_pushglobaltable(L); +#else lua_gettable(L, LUA_GLOBALSINDEX); +#endif lua_replace(L, 1); lua_pushstring(L,proto->name); lua_replace(L, 2); lua_settable(L,1); +#if LUA_VERSION_NUM >= 502 + /* remove the global environment table from the stack */ + lua_pop(L,1); +#endif + return 0; } else { luaL_argerror(L,3,"The initializer of a protocol must be a function"); @@ -1423,7 +1432,7 @@ static int Proto_newindex(lua_State* L) { return 0; } -static const luaL_reg Proto_meta[] = { +static const luaL_Reg Proto_meta[] = { {"__tostring", Proto_tostring}, {"__index", Proto_index}, {"__newindex", Proto_newindex}, @@ -1437,10 +1446,9 @@ int Proto_register(lua_State* L) { lua_newtable(L); protocols_table_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushstring(L, "Proto"); - lua_pushcfunction(L, Proto_new); - lua_settable(L, LUA_GLOBALSINDEX); - + lua_pushcfunction(L, Proto_new); + lua_setglobal(L, "Proto"); + Pref_register(L); Prefs_register(L); @@ -1539,13 +1547,13 @@ WSLUA_METAMETHOD Dissector_tostring(lua_State* L) { return 1; } -static const luaL_reg Dissector_methods[] = { +static const luaL_Reg Dissector_methods[] = { {"get", Dissector_get }, {"call", Dissector_call }, { NULL, NULL } }; -static const luaL_reg Dissector_meta[] = { +static const luaL_Reg Dissector_meta[] = { {"__tostring", Dissector_tostring}, { NULL, NULL } }; @@ -1825,7 +1833,7 @@ WSLUA_METAMETHOD DissectorTable_tostring(lua_State* L) { return 1; } -static const luaL_reg DissectorTable_methods[] = { +static const luaL_Reg DissectorTable_methods[] = { {"new", DissectorTable_new }, {"get", DissectorTable_get }, {"add", DissectorTable_add }, @@ -1835,7 +1843,7 @@ static const luaL_reg DissectorTable_methods[] = { { NULL, NULL } }; -static const luaL_reg DissectorTable_meta[] = { +static const luaL_Reg DissectorTable_meta[] = { {"__tostring", DissectorTable_tostring}, { NULL, NULL } }; diff --git epan/wslua/wslua_tree.c epan/wslua/wslua_tree.c index 88270d3..18592b2 100644 --- epan/wslua/wslua_tree.c +++ epan/wslua/wslua_tree.c @@ -419,7 +419,7 @@ static int TreeItem_gc(lua_State* L) { return 0; } -static const luaL_reg TreeItem_methods[] = { +static const luaL_Reg TreeItem_methods[] = { {"add_packet_field", TreeItem_add_packet_field}, {"add", TreeItem_add}, {"add_le", TreeItem_add_le}, @@ -433,7 +433,7 @@ static const luaL_reg TreeItem_methods[] = { { NULL, NULL } }; -static const luaL_reg TreeItem_meta[] = { +static const luaL_Reg TreeItem_meta[] = { {"__gc", TreeItem_gc}, { NULL, NULL } }; diff --git epan/wslua/wslua_tvb.c epan/wslua/wslua_tvb.c index 6ba756e..db5f757 100644 --- epan/wslua/wslua_tvb.c +++ epan/wslua/wslua_tvb.c @@ -287,7 +287,7 @@ static int ByteArray_tostring(lua_State* L) { static int ByteArray_tvb (lua_State *L); -static const luaL_reg ByteArray_methods[] = { +static const luaL_Reg ByteArray_methods[] = { {"new", ByteArray_new}, {"len", ByteArray_len}, {"prepend", ByteArray_prepend}, @@ -300,7 +300,7 @@ static const luaL_reg ByteArray_methods[] = { { NULL, NULL } }; -static const luaL_reg ByteArray_meta[] = { +static const luaL_Reg ByteArray_meta[] = { {"__tostring", ByteArray_tostring}, {"__gc", ByteArray_gc}, {"__concat", ByteArray__concat}, @@ -623,7 +623,7 @@ WSLUA_METHOD Tvb_range(lua_State* L) { return 0; } -static const luaL_reg Tvb_methods[] = { +static const luaL_Reg Tvb_methods[] = { {"range", Tvb_range}, {"len", Tvb_len}, {"offset", Tvb_offset}, @@ -632,7 +632,7 @@ static const luaL_reg Tvb_methods[] = { { NULL, NULL } }; -static const luaL_reg Tvb_meta[] = { +static const luaL_Reg Tvb_meta[] = { {"__call", Tvb_range}, {"__tostring", Tvb__tostring}, {"__gc", Tvb__gc}, @@ -1314,7 +1314,7 @@ WSLUA_METAMETHOD TvbRange__tostring(lua_State* L) { return 1; } -static const luaL_reg TvbRange_methods[] = { +static const luaL_Reg TvbRange_methods[] = { {"uint", TvbRange_uint}, {"le_uint", TvbRange_le_uint}, {"int", TvbRange_int}, @@ -1345,7 +1345,7 @@ static const luaL_reg TvbRange_methods[] = { { NULL, NULL } }; -static const luaL_reg TvbRange_meta[] = { +static const luaL_Reg TvbRange_meta[] = { {"__tostring", TvbRange__tostring}, {"__concat", wslua__concat}, {"__call", TvbRange_range}, @@ -1386,11 +1386,11 @@ static int Int64__gc(lua_State* L) { return 0; } -static const luaL_reg Int64_methods[] = { +static const luaL_Reg Int64_methods[] = { { NULL, NULL } }; -static const luaL_reg Int64_meta[] = { +static const luaL_Reg Int64_meta[] = { {"__tostring", Int64__tostring}, {"__concat", wslua__concat}, {"__gc", Int64__gc}, @@ -1422,11 +1422,11 @@ static int UInt64__gc(lua_State* L) { return 0; } -static const luaL_reg UInt64_methods[] = { +static const luaL_Reg UInt64_methods[] = { { NULL, NULL } }; -static const luaL_reg UInt64_meta[] = { +static const luaL_Reg UInt64_meta[] = { {"__tostring", UInt64__tostring}, {"__concat", wslua__concat}, {"__gc", UInt64__gc}, diff --git epan/wslua/wslua_util.c epan/wslua/wslua_util.c index 8dd2002..a6f1bf5 100644 --- epan/wslua/wslua_util.c +++ epan/wslua/wslua_util.c @@ -373,13 +373,13 @@ static int wslua_Dir__gc(lua_State* L) { return 0; } -static const luaL_reg Dir_methods[] = { +static const luaL_Reg Dir_methods[] = { {"open", Dir_open}, {"close", Dir_close}, { NULL, NULL } }; -static const luaL_reg Dir_meta[] = { +static const luaL_Reg Dir_meta[] = { {"__call", Dir__call}, {"__gc", wslua_Dir__gc}, { NULL, NULL }