lua-users home
lua-l archive

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


Hi all,

I'm having difficulty getting examples using LUA_ENVIRONINDEX to work.  I've been trying to use code posted previously on this list, as well as from the article in Game Programming Gems 6.  In all cases, I get a SIGBUS error at the call lua_replace(L, LUA_ENVIRONINDEX).  Has anyone else had this issue?  Is there something else I can look at to diagnose the problem?

Here's a minimal example (main.cpp):

extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

int main(int argc, char* argv[])
{

lua_State * L = lua_open();
luaL_openlibs(L);
lua_settop(L, 0);


luaL_dostring(L, "print(_VERSION)"); // Lua 5.1


lua_newtable(L);

lua_replace(L, LUA_ENVIRONINDEX); // "has exited due to signal 10 (SIGBUS)."

lua_close(L);
    return 0;
}

FWIW OS 10.4.7, Xcode 2.4, MacBookPro (Intel).  

Xcode isn't able to debug this very well.  It appears to crash at an index2adr call is the culprit.  Here's what I get:

tty /dev/ttyp2
Program loaded.
sharedlibrary apply-load-rules all
run
[Switching to process 942 local thread 0xf03]
Running…
Lua version 501Program received signal:  "EXC_BAD_ACCESS".

asm index2addr 000023a3a:1:
0x00023a3a  <+0138>  mov    12(%eax),%eax
0x00023a3d  <+0141>  mov    %eax,80(%ecx)
0x00023a40  <+0144>  movl   $0x5,8(%edx)
0x00023a47  <+0151>  mov    %edx,%eax
0x00023a49  <+0153>  pop    %ebx
0x00023a4a  <+0154>  pop    %esi
0x00023a4b  <+0155>  pop    %ebp
0x00023a4c  <+0156>  ret    
0x00023a4d  <+0157>  mov    16(%eax),%eax
0x00023a50  <+0160>  add    $0x5c,%eax
0x00023a53  <+0163>  pop    %ebx
0x00023a54  <+0164>  pop    %esi
0x00023a55  <+0165>  pop    %ebp
0x00023a56  <+0166>  ret 






On Aug 29, 2006, at 9:51 AM, David Jones wrote:


On 29 Aug 2006, at 07:57, Jose Luis Hidalgo Valiño wrote:


El 29/08/2006, a las 2:58, Mildred escribió:


For example,
could lua_environindex be used to hold data locally( or private ) to
a C function that can be accessed in successive calls?

I don't know about LUA_ENVIRONINDEX but if you want to keep some
variables between the function call (like static variables in C) you
may find closures useful.

I'm not sure if closures are mutable or not (but it's name suggest they are not mutable), if you want a mutable variable, that can be accessed by a function, you should use one of these pseudo indices:
    - LUA_REGISTRY --> accessible by C code only
    - LUA_GLOBALSINDEX ---> the thread environment, for global variables.
    - LUA_ENVIRONINDEX --> that's new to lua 5.1 and I'm not sure how to use it.

LUA_ENVIRONINDEX seems a way to hold data locally to a set of functions ( or only one ) but without loosing the possibility of access the full global environment (the thread environment) through LUA_GLOBALSINDEX.

In lua 5.1 lua_setfenv "changes" the behavior of LUA_ENVIRONINDEX or LUA_GLOBALSINDEX ? I'm not sure.

It depends whether you apply lua_setfenv to a Lua function or a C function.  It has completely different meanings.

applied to a Lua function it changes where that Lua function finds global variables;

applied to a C function it changes its LUA_ENVIRONINDEX table.  As if lua_replace(L, LUA_ENVIRONINDEX) was called.  This has, of course, got nothing to do with global variables (or environment in the usual sense of the world).

Look at the source code for lua_setfenv, and lua_replace in lapi.c; it's instructive.

drj