[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A question about "yieldables"
- From: William Ahern <william@...>
- Date: Thu, 26 Feb 2015 12:54:11 -0800
On Sun, Feb 22, 2015 at 09:14:09PM -0300, Soni L. wrote:
> Lua 5.2 added the ability to yield thru C functions, but most functions
> aren't yieldable (e.g. string.gsub, load, table.sort). Does the manual
> require that those functions be non-yieldable? or could a conforming
> implementation have them be yieldable? (Also why are they not yieldable
> in the reference implementation?)
>
tostring isn't yieldable, either, which is problematic when trying to
implement lazy evaluation. Imagine passing an object around which is tied to
an HTTP POST request. Also imagine you're using a non-blocking I/O type
framework. You don't have to bother reading the POST body until somebody
calls tostring on the object, which they may never get around to because of
filters, access restrictions, etc. Lazy evaluation let's you do this without
requiring application code to understand the underlying API or semantics;
all they need to do is use tostring.
I added a yieldable tostring as part of an auxlib module
(cqueues.auxlib.tostring). I submitted a patch based on my code but never
heard back.
In Lua 5.3 tostring is implemented in a single line by calling
luaL_tolstring. The yielable version is about 15 lines (and thus 15x
longer!). Some of that is reimplementing some boilerplate code behind the
luaL_tolstring call. A better patch might refactor some of the Lua
internals, such as maybe adding a luaL_callmetak function.