lua-users home
lua-l archive

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



On 29 Jun 2006, at 11:34, D Burgess wrote:

Carsten Fuchs wrote:
[...]
lua_tostring() from my code.

However, I'm wondering why lua_tostring() behaves in this (somewhat
surprising) manner? This makes it different from all the other lua_to*()
functions, and I wonder why this is so??

I guess that the conversion to a string means that the string has to be
allocated and stored somewhere. Numbers and booleans presumably
do not have the allocation issue.

Exactly. The string returned from lua_tostring is only valid whilst there is a corresponding Lua string on the stack. So lua_tostring must put a Lua string somewhere on the stack. And the current implementation overwrites the number with the string.

I'm sure lots of people have been bitten by this behaviour (I know I have). Maybe it would be better if lua_tostring took an additional argument which was the stack slot to overwrite with the Lua string. Then it would be more clear that lua_tostring was doing something a bit different, and if you didn't want to change your number into a string you could do that by specifying a different stack slot. There are other variations that come to mind too.

drj