lua-users home
lua-l archive

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

You can avoid the error problem by using a userdata and letting the GC collect it. That gets expensive relative to malloc/free if you do it every time since the GC is necessarily more expensive than the free. We could work around that, however, by doing the following:

1. Store the userdata pointer via a weak cache table entry and store the length in a static variable.

2. The code now checks the cached length and if it is long enough tries to fetch the cached userdata and put it on the stack. If that fails or if the length isn't long enough, it allocates a new userdata.

Nice solution, but an easier way would be to:
lua_cpcall a wrapper around lua_pushlstring (and apreq_quote if that can throw an error as well)
 return result from lua_cpcall, or throw error if ran out of memory.
(Another way - which is similar to your solution in that it reuses buffer space - would be to use lua's internal luaO_ buffer functions.)

Still not "neat" though. (given that fixing the error makes the code more unreadable then ignoring it, in the unlikely event that it occurs).

Funny though, this does demonstrate similar issues talked about in the "state of the Lua nation on resource cleanup".

- Alex