lua-users home
lua-l archive

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


Case 1

static int val;
lua_pushlightuserdata(L, &val);
lua_pushcclosurer(L,  myfunc2, 1);
lua_pushlightuserdata(L, &val);
lua_pushcclosurer(L,  myfunc2, 1);

Case 2
If its lua values you wish to share (rather than C) create the value
as a table entry and use the table for the upvalue.

David B

On 8/14/06, slonik.az@gmail.com <slonik.az@gmail.com> wrote:
Hi Lua Experts,
Lua closures can share their private state with each other by sharing
their "outer local" variables. In the following code snippet "local
cnt" is accessible by both cnt_step() and cnt_get() and no other
function in the world could touch it.

function cnt_init()
   local cnt= 0
   function cnt_step() cnt= cnt + 1 end
   function cnt_get()  return cnt; end
end
cnt_init();

I would like to achieve the same effect using C-closures. But I cannot
figure out how?
Roberto's book "Programming in Lua, 2-nd edition" that I recently
bought shows how a C-closure stores its individual state in an upvalue
(lua_pushcclosure). But here I need two functions to have an access to
the same upvalue.
Any help is greatly appreciated.

Thanks and Best Regards,
--Leo--