lua-users home
lua-l archive

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


On Wed, Jun 13, 2012 at 5:15 PM, Coda Highland <chighland@gmail.com> wrote:
> On Wed, Jun 13, 2012 at 5:07 PM, Chris <coderight@gmail.com> wrote:
>> In LuaJIT there is no penalty for using zero-based indexing, it's even
>> recommended but the problem is that the Lua tables (non-FFI) are not
>> by default zero-based.  For example:
>> foo = { 0, 1, 2, 3 }
>>
>> You will get a one-based array.  Understandable to be compatible with
>> regular Lua code but annoying if you have no need for that. It's very
>> confusing for end users when you're using LuaJIT as a scripting
>> language and using FFI.  In fact it's confusing for me too, I can't
>> count how many times I use the wrong index.  Forcing the FFI types to
>> use one-based indexing doesn't work either when that data needs to go
>> to a native API (eg. OpenGL vertex data or something;  it can be faked
>> but you take a serious performance hit).  Off the top of my head I
>> can't think of any other of the API functions that are one-based by
>> default but those would need to be patched as well if there are any.
>>
>> Anybody have a patch to "fix" this?  I suppose something to make the
>> FFI arrays one-based would work just as well.  I would just like it to
>> be consistent across the board, whatever scheme. Compile option for
>> LuaJIT would be awesome, maybe it's already there.
>>
>> Thanks
>> CR
>>
>
> foo = { [0]=0, 1, 2, 3 }
>
> Problem solved.
>
> /s/ Adam

Aside: If you use this scheme, use numeric for instead of ipairs().
Not only will ipairs() miss the 0 index, but numeric for is faster
anyway (which is clearly important to you).

/s/ Adam