lua-users home
lua-l archive

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


The Lua generic for is basically a C style expression loop.

C:
void swap(int *a, int i, int j) {
  int tmp = a[i];
  a[i] = a[j]; a[j] = tmp;
}
for (i=0, j=n; i<j; a[i]>a[j]?swap(a,i,j):0, i++, j--);


Lua:
local i, j = 1,n;
for test in function(a) if i<j then
    if a[i]>a[j] then a[i],a[j] = a[j],a[i] end
    i,j = i+1,j-1;
    return true
  end end, a do
end

2018-01-31 3:25 GMT+02:00 Paige DePol <lual@serfnet.org>:
> Soni They/Them L. <fakedme@gmail.com> wrote:
>
>> It would not. Integer and float are treated separately in the VM, but
>> exposed as a single type.
>>
>> They're physically distinct TValues.
>
> The numeric for loop is designed to work on the Lua numeric data type, there
> isn't a Rational number data type, so how would you update the numeric for
> loop to use them... or am I missing something in relation to your example of
> "for i=r(1,10), r(10,10), r(1,10) do ... end"?
>
>
>> The VM already checks if ttisinteger. Floats and ints have the same
>> performance. My proposal would make floats slightly slower (one extra
>> check), but it'd also fix a major issue with the current implementation
>> (an integral part of a lot of exploits out there), while not affecting int
>> performance at all.
>
> Out of curiosity, what exploits are you referring to?
>
> Would having the C style expression for loop be helpful in Lua I wonder?
>
> ~Paige
>
>