lua-users home
lua-l archive

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


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;
}