lua-users home
lua-l archive

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

Mark Hamburg wrote:
> When using Lua for more or less data description tasks, I'm frequently
> finding myself wishing for a splice operator that would allow one to use
> all
> of the results of a function when building a table.


> Where the $ means that we take all of the results from the function rather
> than just the first. (And if it has no results, we also reflect that.) I
> would also be quite open to other syntaxes.
> Mark

Daniel Silverstone and I implemented this as part of the table
comprehensions patch (now part of Aranha, so I might be able to backport
it to Lua sometime). The syntax we chose, in order to avoid any new
lexemes, was to allow multiple returns (including no returns) if the
function call was followed by a ';' or at the end of the table constructor
-- in other words, a following ',' causes normalization to one value:

  { f(); g(), h() }


   f() and h() are fully expanded, g() is normalized to a single value

The implementation is quite straight-forward. The new table vm opcode is
modified to use two temporaries: the first is the table itself, and the
second is the current insertion index (so newtable itself always
initializes that slot to 1). The setlist opcode then updates this slot to
the next index.

This slightly simplifies the implementation of the setlist opcode; the
opcode itself does not need to store the starting index, only the number
of things to be added to the table, and there is no longer a need to
coerce an integer to an Instruction for large starting indices. The
benchmarking I did showed that the change was usually slightly faster on
Intel x86, with an additional speedup on a G4 Mac; apparently, the simpler
opcode decoding compensates for the cost of using an extra stack slot.

I can attempt to isolate this patch if anyone would like to see it.