lua-users home
lua-l archive

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



Lua authors!!!:
 :)

Would it be possible to have a "lua_pushstring_fixed()" API function, to cover this issue. That is, any pushings of a string that _relies_ on the pointer not to change after exit back to Lua world, would use such function to make the point. Normal string pushing would be seen as temporary.

With current (5.1) version, the call could be simply a macro doing the normal push.

-asko


On Fri, 19 May 2006 13:21:29 +0200
 Mike Pall <mikelu-0605@mike.de> wrote:
Hi,

Daniel Collins wrote:
The strings are immutable but need to persist in C longer than the duration of the API function called from Lua. So the string parameter passed in my method one would need to be copied into another buffer.

Ok, a returned string pointer is guaranteed to be valid as long as the corresponding Lua string exists in the Lua stack (see the
description of lua_tolstring() in the Lua 5.1 manual).

But the current GC algorithm (as of Lua 5.0/5.1) does not move objects. So right now you can also rely on the fact that the string pointer is valid as long as there is _any_ reference to it (i.e. from the table you use to store them). Yes, the latter means relying on implementation internals. But nobody forces you to upgrade to a newer Lua version. The C API
usually changes over major versions, anyway.

Bye,
    Mike