Update README.wmem, release notes for emem removal
[metze/wireshark/wip.git] / doc / README.wslua
1 README.wslua
2
3 This is a HOWTO for adding support for new Lua hooks/functions/abilities in
4 Wireshark.   If you see any errors or have any improvements, submit patches -
5 free software is a community effort....
6
7 This is NOT a guide for how to write Lua plugins - that's documented already
8 on the Wireshark webpages.
9
10 Contributors to this README:
11 Hadriel Kaplan <hadrielk[AT]yahoo.com>
12
13 ==============================================================================
14
15 Overview:
16
17 The way Wireshark exposes functions for Lua is generally based on a
18 callback/event model, letting Lua plugins register their custom Lua functions
19 into event callbacks.  C-based "objects" are exposed as Lua tables with
20 typical Lua USERDATA pointer dispatching, plain C-functions are registered as
21 such in Lua, and C-based enums/variables are registered into Lua as table
22 key=value (usually... though rarely they're registered as array indexed
23 values).  All of that is very typical for applications that expose things
24 into a Lua scripting environment.
25
26 The details that make it a little different are (1) the process by which the
27 code is bound/registered into Lua, and (2) the API documentation generator.
28 Wireshark uses C-macros liberally, both for the usual reasons as well as for
29 the binding generator and documentation generator scripts.  The macros are
30 described within this document.
31
32 The API documentation is auto-generated from a Perl script called 'make-
33 wsluarm.pl', which searches C-files for the known macros and generates
34 appropriate HTML documentation from them.  This includes using the C-comments
35 after the macros for the API document info.
36
37 Likewise, another Perl script called 'make-reg.pl' generates the C-files
38 'register_wslua.c' and 'declare_wslua.h', based on the C-macros it searches
39 for in existing source files.  The code this Perl script auto-generates is
40 what actually registers some classes/functions into Lua - you don't have to
41 write your own registration functions to get your new functions/classes into
42 Lua tables. (you can do so, but it's not advisable)
43
44 Both of the perl scripts above are given the C-source files to search through
45 by the make process, generated from the lists in epan/wslua/CMakeLists.txt.
46 Naturally if you add new source files, you need to add them to the list in
47 epan/wslua/CMakeLists.txt, as well as epan/wslua/Makefile.am and
48 epan/wslua/Makefile.nmake. You also have to add the module name into
49 docbook/user-guide.xml and docbook/wsluarm.xml, and the source files into
50 docbook/CMakeLists.txt and docbook/Makefile.common, to get it to be generated
51 in the user guide.
52
53 Another Perl script is used as well, called 'make-init-lua.pl', which
54 generates the init.lua script.  A large part of it deals with exposing #define
55 values into the Lua global table, or sub-tables.  Unfortunately not all of
56 them are put in sub-tables, which means the global Lua table is quite polluted
57 now.  If you add new ones in here, please think of putting them in a subtable,
58 as they are for wtap, ftypes, and base.  For example, there are several put in
59 as 'PI_' prefixed names, such as 'PI_SEVERITY_MASK = 15728640'.  The fact they
60 all have a common 'PI_' prefix should be an indicator they can be put in a
61 table named PI, or PacketInfo.  Just because C-code doesn't have namespaces,
62 doesn't mean Lua can't. This has now been fixed, and the PI_* names are now in
63 two separate subtables of a table named 'expert', as 'expert.group' and
64 'expert.severity' subtables. Follow that model in 'make-init-lua.pl'.
65
66
67 Due to those documentation and registration scripts, you MUST follow some very
68 specific conventions in the functions you write to expose C-side code to Lua,
69 as described in this document.
70
71 Naming conventions/rules:
72
73 Class/object names must be UpperCamelCase, no numbers/underscores.
74 Function and method names must be lower_underscore_case, no numbers.
75 Constants/enums must be ALLCAPS, and can have numbers.
76
77 The above rules are more than merely conventions - the Perl scripts which
78 auto-generate stuff use regex patterns that require the naming syntax to be
79 followed.
80
81 ==============================================================================
82
83 Documenting things for the API docs:
84
85 As explained previously, the API documentation is auto-generated from a
86 Perl script called 'make-wsluarm.pl', which searches C-files for the known
87 macros and generates appropriate HTML documentation from them. This includes
88 using the C-comments after the macros for the API document info. The comments
89 are extremely important, because the API documentation is what most Lua script
90 authors will see - do *not* expect them to go looking through the C-source code
91 to figure things out.
92
93 Please make sure to at least use the '@since' version notification markup
94 in your comments, to let users know when the new class/function/etc. you
95 created became available.
96
97 Because documentation is so important, the make-wsluarm.pl script supports
98 specific markup syntax in comments, and converts them to XML and ultimately
99 into the various documentation formats. The markup syntax is documented in
100 the top comments in make-wsluarm.pl, but are repeated here as well:
101   - two (or more) line breaks in comments result in separate paragraphs
102   - all '&' are converted into their entity names, except inside urls
103   - all '<', and '>' are converted into their entity names everywhere
104   - any word(s) wrapped in one star, e.g., *foo bar*, become italics
105   - any word(s) wrapped in two stars, e.g., **foo bar**, become bold
106   - any word(s) wrapped in backticks, e.g., `foo bar`, become bold (for now)
107   - any word(s) wrapped in two backticks, e.g., ``foo bar``, become one backtick
108   - any "[[url]]" becomes an XML ulink with the url as both the url and text
109   - any "[[url|text]]" becomes an XML ulink with the url as the url and text as text
110   - any indent with a single leading star '*' followed by space is a bulleted list item
111     reducing indent or having an extra linebreak stops the list
112   - any indent with a leading digits-dot followed by space, i.e. "1. ", is a numbered list item
113     reducing indent or having an extra linebreak stops the list
114   - supports meta-tagged info inside comment descriptions as follows:
115     * a line starting with "@note" or "Note:" becomes an XML note line
116     * a line starting with "@warning" or "Warning:" becomes an XML warning line
117     * a line starting with "@version" or "@since" becomes a "Since:" line
118     * a line starting with "@code" and ending with "@endcode" becomes an
119       XML programlisting block, with no indenting/parsing within the block
120     The above '@' commands are based on Doxygen commands
121
122
123 ==============================================================================
124
125 Some implementation details:
126
127 Creating new C-classes for Lua:
128
129 Explaining the Lua class/object model and how it's bound to C-code functions
130 and data types is beyond the scope of this document; if you don't already know
131 how that works, I suggest you start reading lua-users.org's wiki, and
132 lua.org's free reference manual.
133
134 Wireshark generally uses a model close to the typical binding
135 model: 'registering' class methods and metamethods, pushing objects into Lua
136 by applying the class' metatable to the USERDATA, etc.  This latter part is
137 mostly handled for you by the C-macro's created by WSLUA_CLASS_DEFINE, such as
138 push/check, described later in this document.
139
140 The actual way methods are dispatched is a little different from normal Lua
141 bindings, because attributes are supported as well (see next section). The
142 details won't be covered in this document - they're documented in the code
143 itself in: wslua_internals.c above the wslua_reg_attributes function.
144
145 Registering a class requires you to write some code: a WSLUA_METHODS table,
146 a WSLUA_META table, and a registration function.  The WSLUA_METHODS table is an
147 array of luaL_Reg structs, which map a string name that will be the function's
148 name in Lua, to a C-function pointer which is the C-function to be invoked by
149 Lua when the user calls the name.  Instead of defining this array of structs
150 explicitly using strings and function names, you should use the WSLUA_METHODS
151 macro name for the array, and use WSLUA_CLASS_FNREG macro for each entry.
152 The WSLUA_META table follows the same behavior, with the WSLUA_CLASS_MTREG
153 macro for each entry. Make sure your C-function names use two underscores
154 instead of one.
155
156 Once you've created the appropriate array tables, define a registration
157 function named 'ClassName_register', where 'ClassName'is your class name, the
158 same one used in WSLUA_CLASS_DEFINE.  The make-reg.pl Perl script will search
159 your file for WSLUA_CLASS_DEFINE, and it generates a register_wslua.c which
160 will call your ClassName_register function during Wireshark initialization.
161 Inside your ClassName_register function, use either the WSLUA_REGISTER_CLASS
162 or the WSLUA_REGISTER_META macros with the class name as the argument.  That
163 will automatically register the methods/meta tables into Lua.  Use
164 WSLUA_REGISTER_CLASS if your class has methods and optionally metamethods, or
165 use WSLUA_REGISTER_META if it only has metamethods - do *not* use both.  Note
166 that your class does not need to have a WSLUA_METHODS nor WSLUA_META table.
167 Also, you should read the 'Memory management model' section later in this
168 document.
169
170 Class member variable attributes (getters/setters):
171
172 The current implementation does not follow a single/common class-variable
173 attribute accessor model for the Lua API: some class member values are
174 populated/retrieved when a table field attribute is used that triggers the
175 __index or __newindex metamethods, and others are accessed through explicit
176 getter/setter method functions.  In other words from a Lua code perspective
177 some class object variables are retrieves as 'foo = myObj.var', while others
178 are done as 'foo = myObj.getVar()'.
179
180 From the C-side code perspective, some classes register no real method
181 functions but just have attributes (and use the WSLUA_ATTRIBUTE documentation
182 model for them). For example the FieldInfo class in wslua_field.c does this.
183 Other classes provide access to member variable through getter/setter method
184 functions (and thus use the WSLUA_METHOD documentation model). For example
185 the TvbRange class in wslua_tvb.c does this. Using the latter model of having
186 a getter/setter method function allows one to pass multiple arguments, whereas
187 the former __index/__newindex metamethod model does not. Both models are
188 fairly common in Lua APIs, although having a mixture of both in the same API
189 probably isn't.  There is even a third model in use: pre-loading the member
190 fields of the class table with the values, instead of waiting for the Lua
191 script to access a particular one to retrieve it; for example the Listener tap
192 extractors table is pre-populated (see files 'wslua_listener.c' and 'taps'
193 which through the make-taps.pl perl script creates 'taps_wslua.c'). The
194 downside of that approach is the performance impact, filling fields the Lua
195 script may never access.  Lastly, the Field, FieldInfo, and Tvb's ByteArray
196 type each provide a __call metamethod as an accessor - I strongly suggest you
197 do NOT do that, as it's not a common model and will confuse people since it
198 doesn't follow the model of the other classes in Wireshark.
199
200 The way attribute accessing is handled is a bit too complicated to discuss
201 here, but is documented in wslua_internals.c above the wslua_reg_attributes
202 function definition. All you need to know is how to write the C-code to
203 register attributes, and the code to provide getter/setters for them. To
204 create them, you create an array table similar to the WSLUA_METHODS and
205 WSLUA_META tables, except using the macro name WSLUA_ATTRIBUTES. Inside this
206 array, each entry should use one of the following macros: WSLUA_ATTRIBUTE_ROREG,
207 WSLUA_ATTRIBUTE_WOREG, or WSLUA_ATTRIBUTE_RWREG. Those provide the hooks for
208 a getter-only, setter-only, or both getter and setter function. The functions
209 themselves need to follow a naming scheme of ClassName_get_attributename(),
210 or ClassName_set_attributename(), for the respective getter vs. setter function.
211 Trivial getters/setters have macros provided to make this automatic, for things
212 such as getting numbers, strings, etc. The macros are in wslua.h. For example,
213 the WSLUA_ATTRIBUTE_NAMED_BOOLEAN_GETTER(Foo,bar,choo) macro creates a getter
214 function to get the boolean value of the Class Foo's choo member variable, as
215 the Lua attribute named 'bar'.
216
217 To register the attributes, your Class registration function must call the
218 WSLUA_REGISTER_ATTRIBUTES(ClassName) macro, after it calls either the
219 WSLUA_REGISTER_META(ClassName) macro or the WSLUA_REGISTER_CLASS(ClassName)
220 one.
221
222 Callback function registration:
223
224 For some callbacks, there are register_* Lua global functions, which take a
225 user-defined Lua function name as the argument - the one to be hooked into
226 that event.  Unlike in most Lua APIs, there's a unique register_foo() function
227 for each event type, instead of a single register() with the event as an
228 argument.  For example there's a register_postdissector() function.  In some
229 cases the Lua functions are invoked based on a pre-defined function-name model
230 instead of explicit register_foo(), whereby a C-object looks for a defined
231 member variable in the Registry that repesents a Lua function created by the
232 plugin.  This would be the case if the Lua plugin had defined a pre-defined
233 member key of its object's table in Lua, for that purpose.  For example if the
234 Lua plugin sets the 'reset' member of the Listener object table to a function,
235 then Wireshark creates a Registry entry for that Lua function, and executes
236 that Lua function when the Listener resets. (see the example Listener Lua
237 script in the online docs) That model is only useful if the object can only be
238 owned by one plugin so only one function is ever hooked, obviously, and thus
239 only if it's created by the Lua plugin (e.g., Listener.new()).
240
241 Creating new Listener tap types:
242
243 The Listener object is one of the more complicated ones.  When the Lua script
244 creates a Listener (using Listener.new()), the code creates and returns a tap
245 object.  The type of tap is based on the passed-in argument to Listener.new(),
246 and it creates a Lua table of the tap member variables.  That happens in
247 taps_wslua.c, which is an auto-generated file from make-taps.pl.  That Perl
248 script reads from a file called 'taps', which identifies every struct name
249 (and associated enum name) that should be exposed as a tap type.  The Perl
250 script then generates the taps_wslua.c to push those whenever the Listener
251 calls for a tap; and it also generates a taps.tx file documenting them all.
252 So to add a new type, add the info to the taps file (or uncomment an existing
253 one), and make sure every member of the tap struct you're exposing is of a
254 type that make-taps.pl has in its Perl %types and %comments associative
255 arrays.
256
257 Note on Lua versions:
258
259 Wireshark supports both Lua 5.1 and 5.2, which are defined as LUA_VERSION_NUM
260 values 501 and 502 respectively.  When exposing things into Lua, make sure to
261 use ifdef wrappers for things which changed between the versions of Lua.  See
262 this for details: http://www.lua.org/manual/5.2/manual.html#8.3
263
264 ==============================================================================
265
266 Defined Macros for Lua-API C-files:
267
268 WSLUA_MODULE - this isn't actually used in real C-code, but rather only
269 appears in C-comments at the top of .c files.  That's because it's purely used
270 for documentation, and it makes a new section in the API documentation.
271
272 For example, this appears near the top of the wslua_gui.c file:
273
274     /* WSLUA_MODULE Gui GUI support */
275
276 That makes the API documentation have a section titled 'GUI support' (it's
277 currently section 11.7 in the API docs).  It does NOT mean there's any Lua
278 table named 'Gui' (in fact there isn't).  It's just for documentation.
279 If you look at the documentation, you'll see there is 'ProgDlg', 'TextWindow',
280 etc. in that 'GUI support' section.  That's because both ProgDlg and
281 TextWindow are defined in that same wslua_gui.c file using the
282 'WSLUA_CLASS_DEFINE' macro. (see description of that later)  make-wsluarm.pl
283 created those in the same documentation section because they're in the same c
284 file as that WSLUA_MODULE comment.  You'll also note the documentation
285 includes a sub-section for 'Non Method Functions', which it auto-generated
286 from anything with a 'WSLUA_FUNCTION' macro (as opposed to class member
287 functions, which use the 'WSLUA_METHOD' and 'WSLUA_CONSTRUCTOR' macros). Also,
288 to make new wslua files generate documentation, it is not sufficient to just
289 add this macro to a new file and add the file to the CMakeLists.txt; you also
290 have to add the module name into docbook/user-guide.xml, and docbook/wsluarm.xml.
291
292 WSLUA_ATTRIBUTE - this is another documentation-only "macro", only used within
293 comments.  It makes the API docs generate documentation for a member variable
294 of a class, i.e. a key of a Lua table that is not called as a function in Lua,
295 but rather just retrieved or set.  The 'WSLUA_ATTRIBUTE' token is followed by
296 a 'RO', 'WO', or 'RW' token, for Read-Only, Write-Only, or Read-Write. (ie,
297 whether the variable can be retrieved, written to, or both)  This read/write
298 mode indication gets put into the API documentation. After that comes the name
299 of the attribute, which must be the class name followed by the specific
300 attribute name.
301
302 Example:
303
304     /* WSLUA_ATTRIBUTE Pinfo_rel_ts RO Number of seconds passed since beginning of capture */
305
306
307
308 WSLUA_FUNCTION - this is used for any global Lua function (functions put into
309 the global table) you want to expose, but not for object-style methods (that's
310 the 'WSLUA_METHOD' macro), nor static functions within an object (that's
311 WSLUA_CONSTRUCTOR).  Unlike many of the macros here, the function name must
312 begin with 'wslua_'.  Everything after that prefix will be the name of the
313 function in Lua.  You can ONLY use lower-case letters and the underscore
314 character in this function name.  For example 'WSLUA_FUNCTION
315 wslua_get_foo(lua_State* L)' will become a Lua function named 'get_foo'.
316 Documentation for it will also be automatically generated, as it is for the
317 other macros.  Although from a Lua perspective it is a global function (not in
318 any class' table), the documentation will append it to the documentation page
319 of the module/file its source code is in, in a "Non Method Functions" section.
320 Descriptive text about the function must be located after the '{' and optional
321 whitespace, within a '\*' '*\' comment block on one line.
322
323 Example:
324
325     WSLUA_FUNCTION wslua_gui_enabled(lua_State* L) { /* Checks whether the GUI facility is enabled. */
326         lua_pushboolean(L,GPOINTER_TO_INT(ops && ops->add_button));
327         WSLUA_RETURN(1); /* A boolean: true if it is enabled, false if it isn't. */
328     }
329
330 WSLUA_CLASS_DEFINE - this is used to define/create a new Lua class type (i.e.,
331 table with methods).  A Class name must begin with an uppercase letter,
332 followed by any upper or lower case letters but not underscores; in other
333 words, UpperCamelCase without numbers.  The macro is expanded to create a
334 bunch of helper functions - see wslua.h.  Documentation for it will also be
335 automatically generated, as it is for the other macros.
336
337 Example:
338
339     WSLUA_CLASS_DEFINE(ProgDlg,NOP,NOP); /* Manages a progress bar dialog. */
340
341 WSLUA_CONSTRUCTOR - this is used to define a function of a class that is a
342 static function rather than a per-object method; i.e., from a Lua perspective
343 the function is called as 'myObj.func()' instead of 'myObj:func()'.  From a
344 C-code perspective the code generated by make-reg.pl does not treat this
345 differently than a WSLUA_METHOD, the only real difference being that the code
346 you write within the function won't be checking the object instance as the
347 first passed-in argument on the Lua-API stack.  But from a documentation
348 perspective this macro correctly documents the usage using a '.' period rather
349 than ':' colon.  This can also be used within comments, but then it's
350 '_WSLUA_CONSTRUCTOR_'.  The name of the function must use the Class name
351 first, followed by underscore, and then the specific lower_underscore name
352 that will end up being the name of the function in Lua.
353
354 Example:
355
356     WSLUA_CONSTRUCTOR Dissector_get (lua_State *L) {
357         /* Obtains a dissector reference by name */
358     #define WSLUA_ARG_Dissector_get_NAME 1 /* The name of the dissector */
359         const gchar* name = luaL_checkstring(L,WSLUA_ARG_Dissector_get_NAME);
360         Dissector d;
361
362         if (!name)
363             WSLUA_ARG_ERROR(Dissector_get,NAME,"must be a string");
364
365         if ((d = find_dissector(name))) {
366             pushDissector(L, d);
367             WSLUA_RETURN(1); /* The Dissector reference */
368         } else
369             WSLUA_ARG_ERROR(Dissector_get,NAME,"No such dissector");
370     }
371
372
373 WSLUA_METHOD - this is used for object-style class method definitions.  The
374 documentation will use the colon syntax, and it will be called as
375 'myObj:func()' in Lua, so your function needs to check the first argument of
376 the stack for the object pointer.  Two helper functions are automatically made
377 for this purpose, from the macro expansion of WSLUA_CLASS_DEFINE, of the
378 signatures 'MyObj toMyObj(lua_State* L, int idx)' and 'MyObj
379 checkMyObj(lua_State* L, int idx)'.  They do the same thing, but the former
380 generates a Lua Error on failure, while the latter does not.
381
382 Example:
383
384     WSLUA_METHOD Listener_remove(lua_State* L) {
385         /* Removes a tap listener */
386         Listener tap = checkListener(L,1);
387         if (!tap) return 0;
388         remove_tap_listener(tap);
389         return 0;
390     }
391
392
393 WSLUA_METAMETHOD - this is used for defining object metamethods (ie, Lua
394 metatable functions).  The documentation will describe these as well, although
395 currently it doesn't specify they're metamethods but rather makes them appear
396 as regular object methods.  The name of it must be the class name followed by
397 *two* underscores, or else it will not appear in the documentation.
398
399 Example:
400
401     WSLUA_METAMETHOD NSTime__eq(lua_State* L) { /* Compares two NSTimes */
402         NSTime time1 = checkNSTime(L,1);
403         NSTime time2 = checkNSTime(L,2);
404         gboolean result = FALSE;
405
406         if (!time1 || !time2)
407           WSLUA_ERROR(FieldInfo__eq,"Data source must be the same for both fields");
408
409         if (nstime_cmp(time1, time2) == 0)
410             result = TRUE;
411
412         lua_pushboolean(L,result);
413
414         return 1;
415     }
416
417
418 WSLUA_ARG_ - the prefix used in a #define statement, for a required
419 function/method argument (ie, one without a default value).  It is defined to
420 an integer representing the index slot number of the Lua stack it will be at,
421 when calling the appropriate lua_check/lua_opt routine to get it from the
422 stack.  The make_wsluarm.pl Perl script will generate API documentation with
423 this argument name for the function/method, removing the 'WSLUA_ARG_' prefix.
424 The name following the 'WSLUA_ARG_' prefix must be the same name as the
425 function it's an argument for, followed by an underscore and then an ALLCAPS
426 argument name (including numbers is ok).  Although this last part is in
427 ALLCAPS, it is documented in lowercase.  The argument name itself is
428 meaningless since it does not exist in Lua or C code.
429
430 Example: see the example in WSLUA_CONSTRUCTOR above, where
431 WSLUA_ARG_Dissector_get_NAME is used.
432
433
434 WSLUA_OPTARG_ - the prefix used in a #define statement, for an optional
435 function/method argument (ie, one with a default value).  It is defined to an
436 integer representing the index slot number of the Lua stack it will be at,
437 when calling the appropriate lua_check/lua_opt routine to get it from the
438 stack.  The make_wsluarm.pl Perl script will generate API documentation with
439 this argument name for the function/method, removing the 'WSLUA_OPTARG_'
440 prefix.  The rules for the name of the argument after the prefix are the same
441 as for 'WSLUA_ARG_' above.
442
443 Example:
444
445     #define WSLUA_OPTARG_Dumper_new_FILETYPE 2 /* The type of the file to be created */
446
447
448 WSLUA_MOREARGS - a documentation-only macro used to document that more
449 arguments are expected/supported.  This is useful when the number of
450 argumeents is not fixed, i.e., a vararg model.  The macro is followed by the
451 name of the function it's an argument for (without the 'wslua_' prefix if the
452 function is a WSLUA_FUNCTION type), and then followed by descriptive text.
453
454 Example:
455
456     WSLUA_FUNCTION wslua_critical( lua_State* L ) { /* Will add a log entry with critical severity*/
457     /* WSLUA_MOREARGS critical objects to be printed    */
458         wslua_log(L,G_LOG_LEVEL_CRITICAL);
459         return 0;
460     }
461
462
463 WSLUA_RETURN - a macro with parentheses containing the number of return
464 values, meaning the number of items pushed back to Lua.  Lua supports mutliple
465 return values, although Wireshark usually just returns 0 or 1 value.  The
466 argument can be an integer or a variable of the integer, and is not actually
467 documented.  The API documentation will use the comments after this macro for
468 the return description.  This macro can also be within comments, but is then
469 '_WSLUA_RETURNS_'.
470
471 Example:
472
473     WSLUA_RETURN(1); /* The ethernet pseudoheader */
474
475
476 WSLUA_ERROR - this C macro takes arguments, and expands to call luaL_error()
477 using them, and returns 0.  The arguments it takes is the full function name
478 and a string describing the error.  For documentation, it uses the string
479 argument and displays it with the function it's associated to.
480
481 Example:
482     if (!wtap_dump_can_write_encap(filetype, encap))
483         WSLUA_ERROR(Dumper_new,"Not every filetype handles every encap");
484
485
486 WSLUA_ARG_ERROR - this is a pure C macro and does not generate any
487 documentation.  It is used for errors in type/value of function/method
488 arguments.
489
490 Example: see the example in thr WSLUA_CONSTRUCTOR above.
491
492
493 ==============================================================================
494
495 Memory management model:
496
497 Lua uses a garbage collection model, which for all intents and purposes can
498 collect garbage at any time once an item is no longer referenced by something
499 in Lua.  When C-malloc'ed values are pushed into Lua, the Lua library has to
500 let you decide whether to try to free them or not.  This is done through the
501 '__gc' metamethod, so every Wireshark class created by WSLUA_CLASS_DEFINE must
502 implement a metamethod function to handle this.  The name of the function must
503 be 'ClassName__gc', where 'ClassName' is the same name as the class.  Even if
504 you decide to do nothing, you still have to define the function or it will
505 fail to compile - as of this writing, which changed it to do so, in order to
506 make the programmer think about it and not forget.
507
508 The thing to think about is the lifetime of the object/value.  If C-code
509 controls/manages the object after pushing it into Lua, then C-code MUST NOT
510 free it until it knows Lua has garbage collected it, which is only known by
511 the __gc metamethod being invoked.  Otherwise you run the risk of the Lua
512 script trying to use it later, which will dereference a pointer to something
513 that has been free'd, and crash.  There are known ways to avoid this, but
514 those ways are not currently used in Wireshark's Lua API implementation;
515 except Tvb and TvbRange do implement a simple model of reference counting to
516 protect against this.
517
518 If possible/reasonable, the best model is to malloc the object when you push
519 it into Lua, usually in a class function (not method) named 'new', and then
520 free it in the __gc metamethod.  But if that's not reasonable, then the next
521 best model is to have a boolean member of the class called something like
522 'expired', which is set to true if the C-code decides it is dead/no-longer-
523 useful, and then have every Lua-to-C accessor method for that class type check
524 that boolean before trying to use it, and have the __gc metamethod set
525 expired=true or free it if it's already expired by C-side code; and vice-versa
526 for the C-side code.
527
528 In some cases the class is exposed with a specific method to free/remove it,
529 typically called 'remove'; the Listener class does this, for example.  When
530 the Lua script calls myListener:remove(), the C-code for that class method
531 free's the Listener that was malloc'ed previously in Listener.new().  The
532 Listener__gc() metamethod does not do anything, since it's hopefully already
533 been free'd.  The downside with this approach is if the script never calls
534 remove(), then it leaks memory; and if the script ever tries to use the
535 Listener userdata object after it called remove(), then Wireshark crashes.  Of
536 course either case would be a Lua script programming error, and easily
537 fixable, so it's not a huge deal.
538
539 ==============================================================================
540