lua-users home
lua-l archive

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


you already have lua_tolstring

2015年7月28日 下午6:54于 "Dirk Laurie" <dirk.laurie@gmail.com>写道:
2015-07-28 5:45 GMT+02:00 Daurnimator <quae@daurnimator.com>:

>   - __tostring metamethods that have previously needed a `rawtostring`

I thought implementing `rawtostring` would be trivial. However, there
is no `lua_rawtostring` or `luaL_rawtostring`.

`LuaB_tostring` is very short, though:

static int luaB_tostring (lua_State *L) {
  luaL_checkany(L, 1);
  luaL_tolstring(L, 1, NULL);
  return 1;
}

The manual says this of `luaL_tolstring`:

If the value has a metatable with a "__tostring" field, then
luaL_tolstring calls the corresponding metamethod with the value as
argument, and uses the result of the call as its result.

It says nothing of the kind at `lua_tostring`. Instead, it says:

The Lua value must be a string or a number; otherwise, the function
returns NULL.

Therefore the functionality that represents tables, etc sits inside
luaL_tolstring, bundled in with the metatable test.

It would be more flexible to unbundle that functionality, i.e. to have
a function luaL_rawtostring that would be called if there is no
metamethod, but could also be called by itself.