[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How to avoid global variables in a lua module (using C api)
- From: Sebastien Lai <237482@...>
- Date: Fri, 30 Sep 2011 21:41:06 +0200
You have two choices here:
Store your values in the Lua registry (as Ezequiel said), or
Use a C++ Binding (Like LuaPlusPlus:
http://github.com/beelzebub/luapp), and use a class to properly store
The choice is yours, both come with pros and contras.
PS: If you do decide to use the Lua registry to store your values,
consider storing the data in a struct that is passed as userdata to
other C functions, that way you don't have to unwind thousands of
values everytime you want to access your values.
2011/9/30 Ezequiel García <firstname.lastname@example.org>:
> I wrote a Lua 5.1 module (in C) to test a certain C library. I mantain a few
> 'state' global variables (in the C fashion):
> static int _traceEnabled;
> static int _updateMode;
> // such...
> This is not a __very__ bad thing since it's just a testing purpose module
> and it is pretty much self-contained.
> However, since I have Lua ways to avoid doing this (using environment,
> upvalues, and such) I would like to remove the global vars and
> replace them with non-local or something.
> I would like to have an environment value easily accesible local
> to the module or local to a specific function but I am not sure how
> this is done.
> Maybe lua_setfenv()? but I can't find clear documentation (Besides
> it seems it will be removed in 5.2).
> Any guidance?
> Thanks in advance,