[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: index/newindex on a table
- From: "Peter Shook" <pshook@...>
- Date: Fri, 4 Oct 2002 14:54:50 -0400
Try starting from the simplest example and build up. This is what I get:
$ bin/lua
Lua 5.0 (alpha) Copyright (C) 1994-2002 Tecgraf, PUC-Rio
name = _PROMPT
> print( XYZ )
name = XYZ
nil
name = _PROMPT
> XYZ = 1
name = XYZ
value = 1
name = _PROMPT
> print( XYZ )
name = XYZ
nil
name = _PROMPT
>
Here is the code:
/*
* #define LUA_USERCONFIG to this file
* by adding the following to your config file
* USERCONF=-DLUA_USERCONFIG='"$(LUA)/etc/test_mt.c"'
*/
#define lua_userinit(L) openstdlibs(L)+myinit(L)
static int
global_index (lua_State *L)
{
const char *name = luaL_check_string(L, 2);
printf("name = %s\n", name);
return 0;
}
static int
global_newindex (lua_State *L)
{
const char *name = luaL_check_string(L, 2);
const char *value = luaL_check_string(L, 3);
printf("name = %s\nvalue = %s\n", name, value);
return 0;
}
static int
myinit (lua_State *L)
{
lua_newtable(L);
lua_pushstring(L, "__index");
lua_pushcfunction(L, global_index);
lua_settable(L, -3);
lua_pushstring(L, "__newindex");
lua_pushcfunction(L, global_newindex);
lua_settable(L, -3);
lua_setmetatable(L, LUA_GLOBALSINDEX);
return 0;
}