Hello I am not sure I understand your exact problem. See attached a code snip to do some table manipulation from C, and a gzipped tarball with my own api to lua, and usage examples. If you like, you can ´dive´ into the luapi.c code, to gain some insight regarding lua interfacing, also, if you like it, you can use or adapt luapi.c to your own taste. A word of caution: luapi and the examples were compiled and linked with the 5.0-alpha release, I am waiting to have some time to try and maybe, adapt it to lua-5.0-beta. But the main core is the same. Sorry, the api is not fully documented. It was the reason because I don´t posted it to the wiki. But seeing the examples ( host.c and udtest.c ) may be helpful. The doc refers to a makefile for hosttest. I excluded it from the tarball because is very broken. Good luck ----- Original Message ----- From: "Andrew Teirney" <andrew@teirney.net> To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br> Sent: Sunday, February 16, 2003 7:12 PM Subject: Setting Global Tables > Hi there, > > I am a fairly new person to lua, the version of lua i am using is > 5.0(beta), since i don't have heaps of time to delve over documents etc i > thought i might post this message to the users list in the hope someone > might be able to explain a few things. > > In my current system which is an embedded project i have what i call a > managment system (written in c), along with a number of support modules. > These support modules will be available to the lua scripts. The intended use > of the lua scripts is to be able to give the software new behaviour without > having the need to compile to native code etc. Lua suit my needs perfectly, > well from what i can see. > > Anyway, one of my support modules is responsible for processing a stream > of text input, it breaks it up into fields as it comes in and so forth. When > a particular sequence of character is received an event is generated. This > event in turn causes either one or multiple scripts to execute depending on > the configuration. The problem i am facing, probably due to my infancy of > knowledge with lua is how can i make these string that the module has split > up avaible to lua. > > So far i have thought of having a global table, lets say called > 'fields'. on getting a new field to add at the c source level somehow poke > it into the lua environment into that 'fields' table. When the scripts have > finished executing then simply assign nil to the 'fields' value and have the > lua strings GC'd etc. I think i can see how this is done if lua calls a c > function. But i am unsure how to do this when lua does not call a c function > from lua land. > > If anyone can provide me with information on how i can do this, this > would be greatly appreciated. Also i forgot to mention that when c code is > running lua code is not, its is a single threaded system, based on > co-operative multi-tasking. Depending on how long lua takes to execute its > scripts it may or maynot become a pre-emptive multi-threaded system, this is > yet to be determined. > > Also on another note, when lua loads a chunk presumably it stores it in > memory, when the chunk has finished loading and then execution has > subsequantly finished is the memory the chunk occupied free'd up? > > Cheers, > > Andrew Stanley Teirney > > >
suppose 'tname' is the table name
'key' is a 'string key' ( remember, tables may be indexed this way )
'value' is a 'string value' for the 'key'
int sp = lua_gettop( L ); // play safe: store the current stack 'pointer'
lua_pushlstring( L, tname, strlen( tname ) );
lua_gettable( L, LUA_GLOBALSINDEX );
// if 'tname' exists in the globals table, the the object is on top of stack
// if not, nil was pushed
// so, check if the object is a LUA_TTABLE
if ( lua_type( L, lua_gettop( L ) ) != LUA_TTABLE ) {
return some error ind ...
}
// new 'pushs' goes on top of stack ...
lua_pushlstring( L, key, strlen( key ) ); // push the key
lua_pushlstring( L, value, strlen( value ) ); // push the value
lua_settable( L, -3 ); // 'bind' them to the object
// which is 3 sp 'slots'
// 'above'
lua_settop( L, sp );
return ok;
key or value may be numbers
so ...
lua_pushnumber( L, nkey );
or
lua_pushnumber( L, nvalue );
to wipe out an entry
lua_push...( L, key, ... )
lua_pushnil( L );
lua_settable( L, -3 ); // 'bind' them to the object
to wipe out the entire table
lua_pushlstring( L, tname, strlen( tname ) );
lua_pushnil( L );
lua_settable( L, LUA_GLOBALSINDEX );
lua_settable( L, -2 );
Attachment:
luapi-5.0a.tgz
Description: application/compressed