[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How two C-closures can share an upvalue??
- From: slonik.az@...
- Date: Mon, 14 Aug 2006 07:41:41 -0400
Asko,
my responses are in between the lines of your message below.
On 8/14/06, Asko Kauppi <askok@dnainternet.net> wrote:
There is no memory management involved in David's code clip. There
shouldn't.
This is exactly my problem with David's solution. For instance, a
value that is shared could be a pointer to an open file or some other
resource that may require a finalizer (__gc metamethod). There should
be a way to involve GC if it is needed.
'static' variable is not memory managed, and all the light user data
are, are pointers to that memory location.
As I pointed out earlier, an upvalue can be anything not necessarily
"static" value as defined in C.
About your real question, I am still listening (don't know an answer
myself). Maybe it's a twisted design, from your side. What exactly do
you need the shared upvalues for? :)
There is no any twisted design here:-) To put it simply I am trying to
understand to what degree C-closures resemble Lua-closures. Lua
closures are very clean and consistent mechanism for doing all sorts
of privacy/sharing policies. I would like to achieve same behaviour
with C-closures or at least to understand what the differences and
limitations are. IMHO, comparing C-closures and Lua-closures is a good
entry for Lua FAQ.
Thanks,
--Leo--
slonik.az@gmail.com kirjoitti 14.8.2006 kello 3.03:
> Hi David,
> thanks a lot for your help and for your suggestion!!
> Unfortunately, your solution does not quite solve the problem because
> functions myfunc1 and myfunc2 do not really share upvalues. Each one
> has its own upvalue that happens to contain a pointer that points to
> some common memory block (or Lua table).
> Firstly, it introduces one extra level of redirection. Secondly, how
> the memory will be cleaned? Lightuserdata is not being garbage
> collected and no one else is pointing to that memory block?
>
> Is there a way to share a given upvalue amongst several C-closures the
> same way it happens with Lua closures??
>
> --Leo--
>
> On 8/13/06, D Burgess <dburgess@gmail.com> wrote:
>> Case 1
>>
>> static int val;
>> lua_pushlightuserdata(L, &val);
>> lua_pushcclosurer(L, myfunc2, 1);
>> lua_pushlightuserdata(L, &val);
>> lua_pushcclosurer(L, myfunc1, 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--
>> >
>>