lua-users home
lua-l archive

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




Am 07.04.2019 um 14:04 schrieb Marc Balmer <marc@msys.ch>:



Am 07.04.2019 um 13:48 schrieb Viacheslav Usov <via.usov@gmail.com>:

On Sun, Apr 7, 2019 at 1:05 PM Marc Balmer <marc@msys.ch> wrote:

> Are both variants valid?

Userdata is the only option here, because Lua strings are immutable, and this should not be violated by clever C code.

I somewhat agree.  But the string copy is only used within my C function, so it does not really matter if it is modified, since Lua code never sees or uses this string.  Therefore my question if this has any "technical" side/bad effects.


Frankly, the whole approach, which would then involve creating more Lua strings and calling luaL_checkoption on them, seems heavy-footed to me.

I am not pushing the strings and using luaL_checkoption, but rather a checkoption function that takes the name as const char * argument instead of getting it from the Lua stack.


If you need this to pass optional flags, I would instead consider passing them as multiple arguments, so that the Lua code could do this:

dlopen('whatever') -- no flags
dlopen('whatever ', 'lazy')
dlopen('whatever ', 'lazy', 'global')  

This leaves the entire question of parsing to Lua's parser, the user does not have to learn your syntax, and all you need to do is something like this:

int flags = 0, top = lua_gettop(L);
for (int i = 2; i <= top; ++i)
    flags |= (1 << luaL_checkoption(L, i, 0, list));


I did this before, but then I came accross a situation where additional parameters are specified after the flags:

func('abc', 'def', 'ghi, 'bar')

(def and ghi are or-able flags)

This would then beome

func('abc', 'def, ghi', 'bar')

of course, are third variant seems possible, using tables:

func('abc', { 'def', 'ghi' }, 'bar'}

I think it is best to check where a argument is a single string and then use luaL_checkoption _or_ if it is a table and then iterate over the table values.  That only uses Lua idioms and does not introduce a new syntax for options:

dlopen('library', 'lazy')

or 

dlopen('library, { 'lazy', 'global' })

or is this still to much "automagic"?


I am looking for the way that is the most user friendly.

Cheers,
V.