lua-users home
lua-l archive

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


在 2016/7/3 23:31, Viacheslav Usov 写道:
Is there any profound reason why luaL_tolstring can convert numbers, Boolean and nil values (and other things), while the other one can only convert numbers?

Cheers,
V.


in other scripting languages like Python, your host program use PyObject (and subtypes such as PyStringObject) directly.

but in Lua you are not supposed to use TValue or TString directly: they are internal to Lua. you must use the Lua API

to convert between various Lua types and the according C types via the stack. the function `lua_tolstring` is of the family

to convert value from Lua to C, others of the same family includes `lua_tonumber`, `lua_toboolean`, `lua_touserdata`.


the fact that `lua_tolstring` also accept a number value is just a side effect of the way Lua coerce number and string.


while the `luaL_tolstring` is a library function (instead of the core API) and is really meant to be a _converter_ which

convert various Lua values to its string representation. it's the C counterpart of the Lua side function tostring.


this is how I see the difference between the two functions. correct me if I am wrong.



--

the nerdy Peng / 书呆彭 / Sent from Thunderbird