lua_pushstring/pushlstring return string

This commit is contained in:
Roberto Ierusalimschy 2007-04-17 10:19:53 -03:00
parent 94d40f3980
commit 619be354c8
3 changed files with 20 additions and 14 deletions

17
lapi.c
View File

@ -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 ** Lua API
** See Copyright Notice in lua.h ** 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); lua_lock(L);
luaC_checkGC(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); api_incr_top(L);
lua_unlock(L); lua_unlock(L);
return getstr(ts);
} }
LUA_API void lua_pushstring (lua_State *L, const char *s) { LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
if (s == NULL) if (s == NULL) {
lua_pushnil(L); lua_pushnil(L);
return NULL;
}
else else
lua_pushlstring(L, s, strlen(s)); return lua_pushlstring(L, s, strlen(s));
} }

View File

@ -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 ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -892,7 +892,8 @@ static int testC (lua_State *L) {
} }
else if EQ("tostring") { else if EQ("tostring") {
const char *s = lua_tostring(L1, getindex); 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") { else if EQ("objsize") {
lua_pushinteger(L1, lua_objlen(L1, getindex)); lua_pushinteger(L1, lua_objlen(L1, getindex));

12
lua.h
View File

@ -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 - An Extensible Extension Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** 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) ** push functions (C -> stack)
*/ */
LUA_API void (lua_pushnil) (lua_State *L); LUA_API void (lua_pushnil) (lua_State *L);
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); 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_pushinteger) (lua_State *L, lua_Integer n);
LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l);
LUA_API void (lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
va_list argp); va_list argp);
LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);