lua-users home
lua-l archive

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


Your example is incomplete, so here's a guess:

// C /////////////////////////////
// I assume you use these existing two functions
extern void* getdata();
extern void processdata(void* mem);

static int mylib_getdata(lua_State *L)
{
   // Get data from elsewhere and put it on Lua stack
   lua_puslightuserdata(L, getdata());
   // Tell Lua we gave it a userdata
   return 1;
}

static int mylib_process(lua_Stat *L)
{
   // Verify first parameter type
   luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
   // Take the first parameter
   void* mem = lua_touserdata(L, 1);
   // Do something with data
   processdata(mem);
   // Tell Lua we return nothing
   return 0;
}

static const struct luaL_Reg mylib [] = {
   {"getdata", mylib_getdata},
   {"process", mylib_process},
   {NULL, NULL}
};

extern int luaopen_mylib{lua_Stat *L)
{
     luaL_register(L, "mylib", mylib);
     return 1;
}
//////////////////////////////////

-- Lua ---------------------------
-- Open your lib
require("mylib")

-- Get your data
local mem = mylib.getdata()

-- Process it ad vitam aeternam
while true do
   mylib.process(mem)
end
----------------------------------

-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Wesley Smith
Envoyé : 3 octobre 2006 12:48
À : Lua list
Objet : light user data

I'm trying to figure out how to use lua_pushlightuserdata.  I have a function

put_data_in_lua(lua_State *L, void *mem)
{
     // want get the mem into Lua
     // how do I get it into a lua variable?
     lua_puslightuserdata(L, mem);   //is this right?  how do assign
it to a variable?
}


static int process_data(lua_Stat *L)
{
      //get user data and do something with it
}

static const struct luaL_Reg mylib [] = {
    {"process", process_data},
    {NULL, NULL}
};

extern int luaopen_mylib{lua_Stat *L)
{
     luaL_register(L, "mylib", mylib);
     return 1;
}

/***********************
Lua Script
*/

memory = nil

function setmem(mem)
    memory = mem
end

function process_memory
    memory.process()
end

thanks,
wes