lua-users home
lua-l archive

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

On Fri, Jul 31, 2009 at 4:55 PM, TNHarris<> wrote:
> I don't believe the original patch was marking the coroutines
> properly. I redid the GC so finalizers are treated similarly to userdata
> __gc methods, the coroutine doesn't get collected until a second
> finalize pass is done.
> [lua514-finalizers0609.patch]
Awesome, the patch passes successfully.

An enhancement I'll probably make to this sometime soon is to see what
is necessary to make it non-throwing so I can use lua_finally in a
case like:

struct resource *x = get_resource();
lua_pushlightuserdata(L, x);
lua_pushclosure(L, cleanup_x, 1);
lua_finally(L, 1, 0, LUA_FINALWAYS);

.. though... lua_pushclosure may throw as well, couldn't it :-/
I suppose I could push the closure first with one empty slot, get the
resource, then populate that slot, then hook lua_finally in...

Any thoughts on this use case?

> At the same time I simplified how finalizers are stored, and also
> improves the speed. A call with finally is as fast as pcall and 43%
> slower than using neither. With the previous patch, finally was 8%
> slower than pcall and 59% slower than a regular call.
> That said, I'm not even sure if this patch is needed, as I also put
> together a rough framework of finalizers in pure lua.
> [final.lua]
While a lua-based finalizer's scheme is quite a useful item.  You
can't as easily throw a finalizer into 'C' ...

My guess is you do 'fcall(function,....)' to define a scope and
finally/finalize much like the C-based version.

Thomas Harning Jr.