lua-users home
lua-l archive

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

On Apr 3, 2008, at 1:09 AM, Javier Guerra wrote:

On Wed, Apr 2, 2008 at 4:50 PM, Eugen-Andrei Gavriloaie
<> wrote:
On Apr 3, 2008, at 12:27 AM, Javier Guerra wrote:
no, each call to lua_evaluate_expression() would compile and execute a
new script in the same context. it won't 'accumulate' a long script.

Hmmm, that is even worse unfortunately because I don't want to recompile the script over and over again. Some Lua variables may contain valuable
informations and on the first call

it's not _re_compiling.

first you compile and run your config, that sets one or more 'global'
vars with your data.  then you compile and run several *very* short
scripts, each one returns one or a few datums to the stack, where your
C code can easily fetch it/them.


Well, I need to do more reading on this matter. It's obvious that I don't fully understand the concept of loading,compiling and executing Lua scripts.

Here is what I know so far:

1. Instantiate a Lua VM
_pLuaState = lua_newstate(_lua_alloc, NULL);

2. Adding some more standard functionality to the newly created VM

3. Adding custom made functionality represented by c++ functions
lua_pushcfunction(pLuaState, w_cfunc_call);
lua_setglobal(pLuaState, "cfunc");

At this stage we have a fully working Lua VM with some API provided by the standard libraries and custom made c++ functions. We will continue by loading a script into our VM

4. Load a Lua script from disk. This will result in adding 0 or more APIs (functions written in Lua) and 0 or more already initialized variables (like my config file who is a bunch of variables). I think (not 100% sure) that this is only transforming Lua script into bytecode and loading it into VM

luaL_loadfile(pLuaState, STR(fileName));

Next step (who seems that I don't fully understand) is start the VM (give it a kick)

5. lua_pcall(pLuaState, 0, 0, 0)

At this stage, the entire VM is performing it's first step from the rest of it's life cycle. This is represented by initializing the global variables from the script (like my config file) and doing some functions calls (I don't have any in a config file)

6. Now, the cloudy part for me is what is happening when I do a luaL_loadstring(...). From the docs I understood that the string is compiled and transformed into byte code and than added to the VM. But this "adding" process is playing tricks with my mind. What is really happening there? Is permanently/temporarily added? Is only executed and after that discarded?

In my point of view (hopefully correct :) ) loading a script means compiling it into a byte code form and after that make it available to the VM. What would happen if we do a second luaL_loadfile(...) on the same lua_State? replacing the previous one or appending? Same question with the luaL_loadstring(...)

Thank you very much!