diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-02-11 15:39:15 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-02-11 15:39:15 -0200 |
| commit | 4a1ed6e6e53f8b208fd21d6ac8b0a34331c43ede (patch) | |
| tree | 604f0b29a1077dc25de66d95ab4276f50119da0e | |
| parent | 6b307744691a70778da77f4c7d5589ed7f4fcabf (diff) | |
| download | lua-4a1ed6e6e53f8b208fd21d6ac8b0a34331c43ede.tar.gz lua-4a1ed6e6e53f8b208fd21d6ac8b0a34331c43ede.tar.bz2 lua-4a1ed6e6e53f8b208fd21d6ac8b0a34331c43ede.zip | |
new field '__name' in metatables to help better error messages
| -rw-r--r-- | lauxlib.c | 14 |
1 files changed, 11 insertions, 3 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.256 2014/01/05 14:04:46 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.257 2014/02/05 19:14:53 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -169,8 +169,14 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { | |||
| 169 | 169 | ||
| 170 | 170 | ||
| 171 | static int typeerror (lua_State *L, int arg, const char *tname) { | 171 | static int typeerror (lua_State *L, int arg, const char *tname) { |
| 172 | const char *msg = lua_pushfstring(L, "%s expected, got %s", | 172 | const char *msg; |
| 173 | tname, luaL_typename(L, arg)); | 173 | const char *typearg = luaL_typename(L, arg); |
| 174 | if (lua_getmetatable(L, arg)) { | ||
| 175 | lua_getfield(L, -1, "__name"); | ||
| 176 | if (lua_isstring(L, -1)) | ||
| 177 | typearg = lua_tostring(L, -1); | ||
| 178 | } | ||
| 179 | msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); | ||
| 174 | return luaL_argerror(L, arg, msg); | 180 | return luaL_argerror(L, arg, msg); |
| 175 | } | 181 | } |
| 176 | 182 | ||
| @@ -275,6 +281,8 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { | |||
| 275 | return 0; /* leave previous value on top, but return 0 */ | 281 | return 0; /* leave previous value on top, but return 0 */ |
| 276 | lua_pop(L, 1); | 282 | lua_pop(L, 1); |
| 277 | lua_newtable(L); /* create metatable */ | 283 | lua_newtable(L); /* create metatable */ |
| 284 | lua_pushstring(L, tname); | ||
| 285 | lua_setfield(L, -2, "__name"); /* metatable.__name = tname */ | ||
| 278 | lua_pushvalue(L, -1); | 286 | lua_pushvalue(L, -1); |
| 279 | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ | 287 | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ |
| 280 | return 1; | 288 | return 1; |
