Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Wireshark-dev: Re: [Wireshark-dev] [Wireshark-users] bit operations aremissingfrom lua bindings

From: Sam Roberts <vieuxtech@xxxxxxxxx>
Date: Tue, 9 Jun 2009 16:06:12 -0700
On Mon, Jun 8, 2009 at 2:26 PM, Bálint Réczey<balint.reczey@xxxxxxxxxxxx> wrote:
> I forgot to add the __gc.
> Feel free to add the __gc or change the userdata to contain uint itself.
> I can't do it myself because I'm on holiday, far from my development machine.

It's easier to work with 64-bit userdata if it doesn't need the extra
malloc/__gc steps.

Here's a patch, mostly code removal.

Thank you for including the range and bitfield.

Will this be in the next release?

Cheers,
Sam
Index: wslua_field.c
===================================================================
--- wslua_field.c	(revision 28680)
+++ wslua_field.c	(working copy)
@@ -81,15 +81,11 @@
 			lua_pushnumber(L,(lua_Number)fvalue_get_floating(&(fi->value)));
 			return 1;
 		case FT_INT64: {
-			Int64 num = g_malloc(sizeof(gint64));
-			*num = fvalue_get_integer64(&(fi->value));
-			pushInt64(L,num);
+			pushInt64(L,fvalue_get_integer64(&fi->value));
 			return 1;
 		}
 		case FT_UINT64: {
-			UInt64 num = g_malloc(sizeof(guint64));
-			*num = fvalue_get_integer64(&(fi->value));
-			pushUInt64(L,num);
+			pushUInt64(L,fvalue_get_integer64(&fi->value));
 			return 1;
 		}
 		case FT_ETHER: {
Index: wslua.h
===================================================================
--- wslua.h	(revision 28680)
+++ wslua.h	(working copy)
@@ -219,8 +219,8 @@
 typedef struct _wslua_pinfo* Pinfo;
 typedef struct _wslua_treeitem* TreeItem;
 typedef address* Address;
-typedef gint64* Int64;
-typedef guint64* UInt64;
+typedef gint64 Int64;
+typedef guint64 UInt64;
 typedef header_field_info** Field;
 typedef field_info* FieldInfo;
 typedef struct _wslua_tap* Listener;
@@ -255,7 +255,7 @@
     luaL_checktype(L,index,LUA_TUSERDATA); \
     p = (C*)luaL_checkudata(L, index, #C); \
     check_code; \
-    return p ? *p : NULL; \
+    return p ? *p : 0; \
 } \
 C* push##C(lua_State* L, C v) { \
     C* p; \
@@ -276,13 +276,13 @@
 } \
 C shift##C(lua_State* L,int i) { \
     C* p; \
-	if(!lua_isuserdata(L,i)) return NULL; \
+	if(!lua_isuserdata(L,i)) return 0; \
 	p = lua_touserdata(L, i); \
 	lua_getfield(L, LUA_REGISTRYINDEX, #C); \
 	if (p == NULL || !lua_getmetatable(L, i) || !lua_rawequal(L, -1, -2)) p=NULL; \
 	lua_pop(L, 2); \
 	if (p) { lua_remove(L,i); return *p; }\
-	else return NULL;\
+	else return 0;\
 } \
 typedef int dummy##C
 
Index: wslua_tvb.c
===================================================================
--- wslua_tvb.c	(revision 28680)
+++ wslua_tvb.c	(working copy)
@@ -661,9 +661,7 @@
         case 6:
         case 7:
         case 8: {
-            UInt64 num = g_malloc(sizeof(guint64));
-            *num = tvb_get_ntoh64(tvbr->tvb->ws_tvb,tvbr->offset);
-            pushUInt64(L,num);
+            pushUInt64(L,tvb_get_ntoh64(tvbr->tvb->ws_tvb,tvbr->offset));
             WSLUA_RETURN(1);
         }
         default:
@@ -693,9 +691,7 @@
         case 6:
         case 7:
         case 8: {
-            UInt64 num = g_malloc(sizeof(guint64));
-            *num = tvb_get_ntoh64(tvbr->tvb->ws_tvb,tvbr->offset);
-            pushUInt64(L,num);
+            pushUInt64(L,tvb_get_ntoh64(tvbr->tvb->ws_tvb,tvbr->offset));
             WSLUA_RETURN(1);
         }
         default:
@@ -893,8 +889,7 @@
         lua_pushnumber(L,tvb_get_bits32(tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, FALSE));
         return 1;
     } else if (len <= 64) {
-        UInt64 num = g_malloc(sizeof(guint64));
-        *num = tvb_get_bits64(tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, FALSE);
+        UInt64 num = tvb_get_bits64(tvbr->tvb->ws_tvb,tvbr->offset*8 + pos, len, FALSE);
         pushUInt64(L,num);
         WSLUA_RETURN(1); /* The bitfield value */
     } else {
@@ -1007,7 +1002,7 @@
     return 1;
 }
 
-WSLUA_CLASS_DEFINE(Int64,FAIL_ON_NULL("null int64"),NOP);
+WSLUA_CLASS_DEFINE(Int64,NOP,NOP);
 /*
   Int64 represents a 64 bit integer.
   Lua uses one single number representation which can be chosen at compile time and since
@@ -1017,29 +1012,18 @@
  */
 
 WSLUA_METAMETHOD Int64__tostring(lua_State* L) {
-	/* Converts the Int64 into a string */
-    Int64 num = checkInt64(L,1);
-    lua_pushstring(L,ep_strdup_printf("%" G_GINT64_MODIFIER "d",(gint64)*(num)));
+    /* Converts the Int64 into a string */
+    gint64 num = checkInt64(L,1);
+    lua_pushstring(L,ep_strdup_printf("%" G_GINT64_MODIFIER "d",num));
     return 1;
 }
 
-static int Int64__gc(lua_State* L) {
-    Int64 num = checkInt64(L,1);
-
-    if (!num) return 0;
-
-    g_free(num);
-
-    return 0;
-}
-
 static const luaL_reg Int64_methods[] = {
     { NULL, NULL }
 };
 
 static const luaL_reg Int64_meta[] = {
     {"__tostring", Int64__tostring},
-    {"__gc", Int64__gc},
     { NULL, NULL }
 };
 
@@ -1048,33 +1032,22 @@
     return 1;
 }
 
-WSLUA_CLASS_DEFINE(UInt64,FAIL_ON_NULL("null uint64"),NOP);
-	/* UInt64 represents a 64 bit unsigned integer. */
+WSLUA_CLASS_DEFINE(UInt64,NOP,NOP);
+    /* UInt64 represents a 64 bit unsigned integer. */
 
 WSLUA_METAMETHOD UInt64__tostring(lua_State* L) {
-	/* Converts the UInt64 into a string */
-    UInt64 num = checkUInt64(L,1);
-    lua_pushstring(L,ep_strdup_printf("%" G_GINT64_MODIFIER "u",(guint64)*(num)));
+    /* Converts the UInt64 into a string */
+    guint64 num = checkUInt64(L,1);
+    lua_pushstring(L,ep_strdup_printf("%" G_GINT64_MODIFIER "u",num));
     return 1;
 }
 
-static int UInt64__gc(lua_State* L) {
-    UInt64 num = checkUInt64(L,1);
-
-    if (!num) return 0;
-
-    g_free(num);
-
-    return 0;
-}
-
 static const luaL_reg UInt64_methods[] = {
     { NULL, NULL }
 };
 
 static const luaL_reg UInt64_meta[] = {
     {"__tostring", UInt64__tostring},
-    {"__gc", UInt64__gc},
     { NULL, NULL }
 };
 
Index: wslua_tree.c
===================================================================
--- wslua_tree.c	(revision 28680)
+++ wslua_tree.c	(working copy)
@@ -124,10 +124,10 @@
                     item = proto_tree_add_bytes(tree_item->tree,hfid,tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len, (const guint8*) luaL_checkstring(L,1));
                     break;
                 case FT_UINT64:
-                    item = proto_tree_add_uint64(tree_item->tree,hfid,tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len,*(UInt64)checkUInt64(L,1));
+                    item = proto_tree_add_uint64(tree_item->tree,hfid,tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len,checkUInt64(L,1));
                     break;
                 case FT_INT64:
-                    item = proto_tree_add_int64(tree_item->tree,hfid,tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len,*(Int64)checkInt64(L,1));
+                    item = proto_tree_add_int64(tree_item->tree,hfid,tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len,checkInt64(L,1));
                     break;
                 case FT_IPv4:
                     item = proto_tree_add_ipv4(tree_item->tree,hfid,tvbr->tvb->ws_tvb,tvbr->offset,tvbr->len,*((guint32*)(checkAddress(L,1)->data)));