A tutorial on turning a C structure into a Lua table

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

A tutorial on turning a C structure into a Lua table

Farzad Sadeghi
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:
https://gist.github.com/bloodstalker/91261e541666f16c3b8315d3ff1085d6
Source Code:
https://github.com/bloodstalker/blogstuff/tree/master/src/cstruct2luatbale

I'm a Lua noob, so any comments are appreciated on how to make the
tutorial better.

--
Farzad Sadeghi
project mutator-https://github.com/bloodstalker/mutator

Reply | Threaded
Open this post in threaded view
|

Re: A tutorial on turning a C structure into a Lua table

Sean Conner
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:
> https://gist.github.com/bloodstalker/91261e541666f16c3b8315d3ff1085d6
> Source Code:
> https://github.com/bloodstalker/blogstuff/tree/master/src/cstruct2luatbale
>
> 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
me.

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.

  -spc

[1] https://github.com/spc476/lua-conmanorg/blob/master/src/net.c

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

                net = require "org.conman.net"
                sock = net.socket('ip','tcp')
                sock:setoption(net.NONBLOCK,true)
                sock:setoption(net.REUSEADDR,true)
               

Reply | Threaded
Open this post in threaded view
|

Re: A tutorial on turning a C structure into a Lua table

Sean Conner
In reply to this post by Farzad Sadeghi
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:
> https://gist.github.com/bloodstalker/91261e541666f16c3b8315d3ff1085d6
> Source Code:
> https://github.com/bloodstalker/blogstuff/tree/master/src/cstruct2luatbale
>
> I'm a Lua noob, so any comments are appreciated on how to make the
> tutorial better.

  Oh, I almost forgot---your luatablegen program.  Any reason it's written
in Python and not in Lua?  It seems an odd choice for a Lua tutorial.

  -spc (And what version of Python?  2.7?  3.0?  It makes a difference)




Reply | Threaded
Open this post in threaded view
|

Re: A tutorial on turning a C structure into a Lua table

Jonathan Goble
On Mon, Apr 1, 2019 at 4:07 PM Sean Conner <[hidden email]> wrote:
(And what version of Python?  2.7?  3.0?  It makes a difference)

The shebang on luatablegen.py reads "#!/usr/bin/python3", so it is some version of Python 3.x. The use of print as a function confirms this. I see nothing in the code at a glance (I didn't look to hard as it was painful to read) limiting it to specific Python 3.x versions, other than the argparse dependency which was introduced to the stdlib in 3.2 (although there is a backport on PyPI for 3.1 and 2.x).
Reply | Threaded
Open this post in threaded view
|

Re: A tutorial on turning a C structure into a Lua table

Farzad Sadeghi
In reply to this post by Farzad Sadeghi
Thank you for taking the time and looking through the tutorial.
I did run the code. It works. I guess the confusion reinforces your
point on the tutorial being too complex for what it talks about. This
is pretty much my first time writing a tutorial
The script is in Python 3.0. The script is in Python because I'm a
little bit better in Python than in Lua. Though to be honest, I
realized the ridiculousness of plugging a Python script on the Lua
mailing list in a Lua tutorial after you asked why it is in Python and
not Lua. Sorry about that.

I'm working on a tool that embeds Lua as its scripting language(the
tool is a cli). Right now another code-generator pushes out code that
reads a binary file format(like WASM, ELF,...) and the script I
mentioned will wrap the resulting C structs for Lua.

Regarding the API changes being minimal from 5.1 to 5.3, you know
that, true, but someone new to Lua doesn't. To top it off, you get the
"different Lua versions are really different" line on lua.org, so not
knowing much, I had to assume things were very different. I had to
spend a lot of time to realize all of the things in the tutorial. The
point of the tutorial is to save beginners like myself the time(also
the self-plug tbh) that I had to spend going through old tutorials,
Stackoverflow and the Lua mailist to learn all of that. Besides, for
my particular use-case(structures having pointers to other structures)
I couldn't find any tutorials.

--
Farzad Sadeghi
project mutator-https://github.com/bloodstalker/mutator