aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2017-08-27 15:17:58 +0200
committerPhilipp Janda <siffiejoe@gmx.net>2017-08-27 15:17:58 +0200
commitaeb072066f799c87527c81b19919dfd3b55bc373 (patch)
tree5598c50644aa6b5b1a6f38ae77f6dc430512c22e
parent30144ee397495503c42fec822957b396eb3ebcd9 (diff)
downloadlua-compat-5.3-aeb072066f799c87527c81b19919dfd3b55bc373.tar.gz
lua-compat-5.3-aeb072066f799c87527c81b19919dfd3b55bc373.tar.bz2
lua-compat-5.3-aeb072066f799c87527c81b19919dfd3b55bc373.zip
Provide fixed luaL_tolstring for Lua 5.2 as well.
-rw-r--r--c-api/compat-5.3.c68
-rw-r--r--c-api/compat-5.3.h6
2 files changed, 37 insertions, 37 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c
index 96b38cc..883efb8 100644
--- a/c-api/compat-5.3.c
+++ b/c-api/compat-5.3.c
@@ -236,40 +236,6 @@ COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname) {
236} 236}
237 237
238 238
239COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
240 if (!luaL_callmeta(L, idx, "__tostring")) {
241 int t = lua_type(L, idx), tt = 0;
242 char const* name = NULL;
243 switch (t) {
244 case LUA_TNIL:
245 lua_pushliteral(L, "nil");
246 break;
247 case LUA_TSTRING:
248 case LUA_TNUMBER:
249 lua_pushvalue(L, idx);
250 break;
251 case LUA_TBOOLEAN:
252 if (lua_toboolean(L, idx))
253 lua_pushliteral(L, "true");
254 else
255 lua_pushliteral(L, "false");
256 break;
257 default:
258 tt = luaL_getmetafield(L, idx, "__name");
259 name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
260 lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
261 if (tt != LUA_TNIL)
262 lua_replace(L, -2);
263 break;
264 }
265 } else {
266 if (!lua_isstring(L, -1))
267 luaL_error(L, "'__tostring' must return a string");
268 }
269 return lua_tolstring(L, -1, len);
270}
271
272
273static int compat53_countlevels (lua_State *L) { 239static int compat53_countlevels (lua_State *L) {
274 lua_Debug ar; 240 lua_Debug ar;
275 int li = 1, le = 1; 241 int li = 1, le = 1;
@@ -564,6 +530,40 @@ COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s) {
564} 530}
565 531
566 532
533COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
534 if (!luaL_callmeta(L, idx, "__tostring")) {
535 int t = lua_type(L, idx), tt = 0;
536 char const* name = NULL;
537 switch (t) {
538 case LUA_TNIL:
539 lua_pushliteral(L, "nil");
540 break;
541 case LUA_TSTRING:
542 case LUA_TNUMBER:
543 lua_pushvalue(L, idx);
544 break;
545 case LUA_TBOOLEAN:
546 if (lua_toboolean(L, idx))
547 lua_pushliteral(L, "true");
548 else
549 lua_pushliteral(L, "false");
550 break;
551 default:
552 tt = luaL_getmetafield(L, idx, "__name");
553 name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
554 lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
555 if (tt != LUA_TNIL)
556 lua_replace(L, -2);
557 break;
558 }
559 } else {
560 if (!lua_isstring(L, -1))
561 luaL_error(L, "'__tostring' must return a string");
562 }
563 return lua_tolstring(L, -1, len);
564}
565
566
567COMPAT53_API void luaL_requiref (lua_State *L, const char *modname, 567COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
568 lua_CFunction openf, int glb) { 568 lua_CFunction openf, int glb) {
569 luaL_checkstack(L, 3, "not enough stack slots available"); 569 luaL_checkstack(L, 3, "not enough stack slots available");
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h
index ffebcd7..7bd8a56 100644
--- a/c-api/compat-5.3.h
+++ b/c-api/compat-5.3.h
@@ -172,9 +172,6 @@ COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname);
172#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata) 172#define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
173COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname); 173COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname);
174 174
175#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
176COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
177
178#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback) 175#define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
179COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level); 176COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level);
180 177
@@ -286,6 +283,9 @@ COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i);
286#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber) 283#define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
287COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s); 284COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s);
288 285
286#define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
287COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len);
288
289#define luaL_getmetafield(L, o, e) \ 289#define luaL_getmetafield(L, o, e) \
290 (luaL_getmetafield(L, (o), (e)) ? lua_type(L, -1) : LUA_TNIL) 290 (luaL_getmetafield(L, (o), (e)) ? lua_type(L, -1) : LUA_TNIL)
291 291