lua-users home
lua-l archive

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


On Thu, Jan 30, 2014 at 6:09 PM, Daurnimator <quae@daurnimator.com> wrote:
> On 31 January 2014 11:54, Josh Haberman <jhaberman@gmail.com> wrote:
>> So I was just wondering if anyone had any out-of-the-box ideas about
>> mitigating this. One idea I had was to make the inner function a
>> string:
>>
>>   fast_iterate(iterator, "function (row) print(row.foo) end")
>>
>> This allows me to precisely control the function's environment, so I
>> can prevent references to "row" from escaping through the global
>> environment or upvalues. The downside is that then none of the
>> program's functions or variables will be visible, which limits the
>> usefulness of this approach pretty severely.
>
>
> You don't have to go as far as having users pass a function as a string.
> You can check if a function has upvalues via lua_getupvalue
> ==> If it does, then use the non-mutating iterator

That is an interesting idea that I like in concept, but can't globals
also let a value escape?

local captured = nil
-- Global function
function capture(x)
  captured = x
end

fast_iterate(
  -- Function has no upvalues, but row escapes.
  function(row)
    capture(row)
  end)