A question of style for C module writers

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

A question of style for C module writers

Marc Balmer
Hi all

I have a question of style.  It's about using constants in modules.

Let's say we have a fictious C library libfoo that defines a preprocessor symbol INFO with a value of e.g. 42.

Then I write a Lua module foo which exposes libfoo's functionality to Lua.  Should INFO be an integer constant in foo or rather a string that is looked up using luaL_checkoption()?

i.e.

foo.somefunc(foo.INFO, 'test data')

or

foo.somefunc('info', 'test data')

It's probably only a question of style or taste, but what is your opininion (or remarks) on this?

- mb


Reply | Threaded
Open this post in threaded view
|

Re: A question of style for C module writers

Charles Heywood
I think I've seen the second option more than the first outside of autogenerated wrappers, I think because instead of a table lookup you just have a constant string. LuaJIT might optimize it out but for Lua in general I believe strings are faster, although not as friendly to autocompletion and/or linting engines.


From: [hidden email] <[hidden email]> on behalf of Marc Balmer <[hidden email]>
Sent: Sunday, April 7, 2019 2:00:49 AM
To: [hidden email]
Subject: A question of style for C module writers
 
Hi all

I have a question of style.  It's about using constants in modules.

Let's say we have a fictious C library libfoo that defines a preprocessor symbol INFO with a value of e.g. 42.

Then I write a Lua module foo which exposes libfoo's functionality to Lua.  Should INFO be an integer constant in foo or rather a string that is looked up using luaL_checkoption()?

i.e.

foo.somefunc(foo.INFO, 'test data')

or

foo.somefunc('info', 'test data')

It's probably only a question of style or taste, but what is your opininion (or remarks) on this?

- mb


Reply | Threaded
Open this post in threaded view
|

Re: A question of style for C module writers

Sean Conner
In reply to this post by Marc Balmer
It was thus said that the Great Marc Balmer once stated:

> Hi all
>
> I have a question of style.  It's about using constants in modules.
>
> Let's say we have a fictious C library libfoo that defines a preprocessor
> symbol INFO with a value of e.g. 42.
>
> Then I write a Lua module foo which exposes libfoo's functionality to Lua.
> Should INFO be an integer constant in foo or rather a string that is
> looked up using luaL_checkoption()?
>
> i.e.
>
> foo.somefunc(foo.INFO, 'test data')
>
> or
>
> foo.somefunc('info', 'test data')
>
> It's probably only a question of style or taste, but what is your
> opininion (or remarks) on this?

  I use option 2 quite a bit (such constants marked in 'single quotes'):

https://github.com/spc476/lua-conmanorg/blob/master/src/net.c
        sock = org.conman.net.socket('ip','tcp')
        addr = org.conman.net.address2("www.google.com",'any','tcp','http')

https://github.com/spc476/lua-conmanorg/blob/master/src/syslog.c
        syslog.open("myprogram",'daemon')
        syslog('debug',"frobulator=%d",frobvalue)
        syslog('error',"syserr=%d",error)

https://github.com/spc476/lua-conmanorg/blob/master/src/clock.c
        org.conman.clock.sleep(.25,'monotonic')
        now = org.conman.clock.get('realtime')

  -spc

Reply | Threaded
Open this post in threaded view
|

Re: A question of style for C module writers

Philipp Janda
In reply to this post by Marc Balmer
Am 07.04.19 um 09:00 schröbte Marc Balmer:
> Hi all

Hi!

>
> I have a question of style.  It's about using constants in modules.
>
> Let's say we have a fictious C library libfoo that defines a preprocessor symbol INFO with a value of e.g. 42.
>
> Then I write a Lua module foo which exposes libfoo's functionality to Lua.  Should INFO be an integer constant in foo or rather a string that is looked up using luaL_checkoption()?
>
> i.e.
>
> foo.somefunc(foo.INFO, 'test data')
>
> or
>
> foo.somefunc('info', 'test data')
>
> It's probably only a question of style or taste, but what is your opininion (or remarks) on this?

That depends on how INFO is used. If it is passed unmodified to
functions, I use string options. If it can be combined with other
constants using bitwise operations, I use full userdata with interning
for extra type safety. If you are supposed to pass an integer and INFO
is just one particular example of such an argument, I use integers. The
last one hasn't happened for me yet, I think.

>
> - mb

Philipp