lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

It was thus said that the Great Farzad Sadeghi once stated:
> Hi,
> I needed to turn a bunch of C structures into Lua tables for a tool
> I'm working on so I started looking for tutorials on how to do that
> but the tutorials were either old or were too simple and didn't cover
> my use case. So I looked around(including the lua mailist itself) and
> finally made a tutorial for that.
> Here's the link for the github gist:
> Source Code:
> I'm a Lua noob, so any comments are appreciated on how to make the
> tutorial better.

  Here are some quick comments on just looking over the code.
1) You can remove pop_a_t() as the function luaL_checkudata() already does
the error checking for you.  Yes, it's in the manual, it's the [-0,+0,v] bit
past the function prototype in section 4.8 (there's a similar notation for
each function in that section).  The 'v' means it will throw an error.

2) There's no need to call lua_checkstack() as Lua will have at least 20
stack slots available (section 4.2) unless Lua has been compiled with a
different value.

3) I'm personally not a fan of getters and setters, and I would create a
function for __index that would allow me to avoid having to call a function
to set/get a value from the userdata.  You can see an example in my own
network interface [1] for both sockets and network addresses.  But that's

4) I think there's a bug with your use of LUA_REGISTRYINDEX, but it's not
easy to follow the code.  The only place you set the registry is in
push_a_t() and that just uses the pointer to an a_t.  You later attempt get
info out of the registry in getter_a_pp(), but at no point do you save the
address to a c_t in the registry.  Did you try running the code you wrote in
the tutorial?

  Having written code for Lua 5.1 through 5.3, an old tutorial should still
be mostly fine (a few API calls have changed).  Also, what use case do you
have?  You don't spell that out in the tutorial and it seems overly complex
for what it actually done in the code.



	I wrote the code so I could do:
		net = require ""
		sock = net.socket('ip','tcp')
		sock.nonblock  = true
		sock.reuseaddr = true
	instead of the (in my opinion) clumsier:
		net = require ""
		sock = net.socket('ip','tcp')

		net = require ""
		sock = net.socket('ip','tcp')