From 619be354c8e38b53d36930dc6f7f1242cd2d853f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Apr 2007 10:19:53 -0300 Subject: [PATCH] lua_pushstring/pushlstring return string --- lapi.c | 17 +++++++++++------ ltests.c | 5 +++-- lua.h | 12 ++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lapi.c b/lapi.c index 5bc80d12..927eccca 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.58 2006/10/17 20:00:07 roberto Exp roberto $ +** $Id: lapi.c,v 2.59 2007/02/07 14:28:00 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -415,20 +415,25 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { } -LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { +LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { + TString *ts; lua_lock(L); luaC_checkGC(L); - setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); + ts = luaS_newlstr(L, s, len); + setsvalue2s(L, L->top, ts); api_incr_top(L); lua_unlock(L); + return getstr(ts); } -LUA_API void lua_pushstring (lua_State *L, const char *s) { - if (s == NULL) +LUA_API const char *lua_pushstring (lua_State *L, const char *s) { + if (s == NULL) { lua_pushnil(L); + return NULL; + } else - lua_pushlstring(L, s, strlen(s)); + return lua_pushlstring(L, s, strlen(s)); } diff --git a/ltests.c b/ltests.c index cbafa686..08141411 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.40 2006/10/10 17:40:17 roberto Exp roberto $ +** $Id: ltests.c,v 2.41 2007/04/10 12:17:52 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -892,7 +892,8 @@ static int testC (lua_State *L) { } else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); - lua_pushstring(L1, s); + const char *s1 = lua_pushstring(L1, s); + lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0); } else if EQ("objsize") { lua_pushinteger(L1, lua_objlen(L1, getindex)); diff --git a/lua.h b/lua.h index 89759d50..1c3341b6 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.223 2006/11/30 11:25:40 roberto Exp roberto $ +** $Id: lua.h,v 1.224 2007/02/07 17:54:52 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -158,11 +158,11 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx); /* ** push functions (C -> stack) */ -LUA_API void (lua_pushnil) (lua_State *L); -LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); -LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); -LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); -LUA_API void (lua_pushstring) (lua_State *L, const char *s); +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, va_list argp); LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);