lua-users home
lua-l archive

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


Sorry I did not watch your stack sufficiently for you.  The advice still
stands however.

Hopefully the following (commented) code section will help.

// -------------------------------------
// Explanation of Notation:
// S = X, Y
// Means that X is at the top of the stack (location -1),
// and Y is just below that (location -2).
// This is not necessarily a complete description
// of the stack, just of the top locations.
// -------------------------------------
 luaopen_string (luaVM);  //S = String Lib
 luaopen_math (luaVM);   //S = Math Lib, String Lib
// String Library and Math Library are also now in the
// GLOBAL environment as tables string and math respectively.

 lua_newtable(luaVM); //S = NewTable, Math Lib, String Lib
 lua_pushstring(luaVM, "core"); //S = "core", NewTable
 lua_pushvalue(luaVM, -2); //S = NewTable, "core", NewTable

/* register it with given name */
 lua_settable(luaVM, LUA_GLOBALSINDEX); //S = NewTable, Math Lib

// Attempt to set the environment of the Math Library to NewTable.
// This will of course fail as you should set the environment of
// functions (more accurately, of "chunks"), NOT of tables.
//
// As a side note if this had set the environment of a function to
// NewTable that environment would of course be empty at
// this point, so the chunk with this environment would not
// have access to any libraries, etc. unless they were added
// later.
 lua_setfenv(luaVM, -2);
// -------------------------------------

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Dmitriy
Iassenev
Sent: Thursday, September 25, 2003 2:51 AM
To: Lua list
Subject: Re: How to load buffer from C to the particular Lua
namespace(table)


hi,

I changed my code and now string and math loaded before lua_newtable(...),
but it changed nothing, I get the following error message
"attempt to index global 'string' (a nil value)", i.e. module string is
invisible inside the module "extension.lua". What am I doing wrong?

Best regards,
Dmitriy Iassenev


> Hey watch your stack!
>
> -------------------------------------
> lua_newtable(luaVM);
>
> luaopen_string (luaVM);
> luaopen_math (luaVM);
>
> lua_pushstring(luaVM, "core");
> lua_pushvalue(luaVM, -2);
> lua_settable(luaVM, LUA_GLOBALSINDEX); /* register it with given name */
> lua_setfenv(luaVM, -2);
> -------------------------------------
>
> is vastly different from
>
> -------------------------------------
> luaopen_string (luaVM);
> luaopen_math (luaVM);
>
> lua_newtable(luaVM);
> lua_pushstring(luaVM, "core");
> lua_pushvalue(luaVM, -2);
> lua_settable(luaVM, LUA_GLOBALSINDEX); /* register it with given name */
> lua_setfenv(luaVM, -2);
> -------------------------------------