浏览代码

lua_pushstring/pushlstring return string

Roberto Ierusalimschy 18 年之前
父节点
当前提交
619be354c8
共有 3 个文件被更改,包括 20 次插入14 次删除
  1. 11 6
      lapi.c
  2. 3 2
      ltests.c
  3. 6 6
      lua.h

+ 11 - 6
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));
 }
 
 

+ 3 - 2
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));

+ 6 - 6
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, ...);