Browse Source

Add luax_checkstring/pushstring functions that handle inline \0 characters

Bart van Strien 13 years ago
parent
commit
2fafab9a0e
3 changed files with 41 additions and 24 deletions
  1. 12 0
      src/common/runtime.cpp
  2. 20 3
      src/common/runtime.h
  3. 9 21
      src/modules/thread/wrap_Thread.cpp

+ 12 - 0
src/common/runtime.cpp

@@ -117,6 +117,18 @@ namespace love
 			return (lua_toboolean(L, idx) == 1 ? true : false);
 		return b;
 	}
+	
+	std::string luax_checkstring(lua_State * L, int idx)
+	{
+		size_t len;
+		const char * str = luaL_checklstring(L, idx, &len);
+		return std::string(str, len);
+	}
+	
+	void luax_pushstring(lua_State * L, std::string str)
+	{
+		lua_pushlstring(L, str.data(), str.size());
+	}
 
 	int luax_assert_argc(lua_State * L, int min)
 	{

+ 20 - 3
src/common/runtime.h

@@ -109,7 +109,7 @@ namespace love
 	* Converts the value at idx to a bool. It follow the same rules
 	* as lua_toboolean, but returns a bool instead of an int.
 	* @param L The Lua state.
-	* @param idx The index on the Lua state.
+	* @param idx The index on the Lua stack.
 	* @return True if the value evaluates to true, false otherwise.
 	**/
 	bool luax_toboolean(lua_State * L, int idx);
@@ -118,19 +118,36 @@ namespace love
 	* Pushes a bool onto the stack. It's the same as lua_pushboolean,
 	* but with bool instead of int.
 	* @param L The Lua state.
-	* @paarm b The bool to push.
+	* @param b The bool to push.
 	**/
 	void luax_pushboolean(lua_State * L, bool b);
 
 	/**
 	* Converts the value at idx to a bool, or if not present, b is returned.
 	* @param L The Lua state.
-	* @param idx The index of the Lua state.
+	* @param idx The index of the Lua stack.
 	* @param b The value to return if no value exist at the specified index.
 	* @return True if the value evaluates to true, false otherwise.
 	**/
 	bool luax_optboolean(lua_State * L, int idx, bool b);
 
+	/**
+	* Converts the value at idx to a std::string. It takes care of the string
+	* size and possible embedded nulls.
+	* @param L The Lua state.
+	* @param idx The index on the Lua stack.
+	* @return Copy of the string at the specified index.
+	**/
+	std::string luax_checkstring(lua_State * L, int idx);
+
+	/**
+	* Pushes a std::string onto the stack. It uses the length of the string
+	* for lua_pushlstring's len argument.
+	* @param L The Lua state.
+	* @param str The string to push.
+	**/
+	void luax_pushstring(lua_State * L, std::string str);
+	
 	/**
 	* Require at least 'min' number of items on the stack.
 	* @param L The Lua state.

+ 9 - 21
src/modules/thread/wrap_Thread.cpp

@@ -20,18 +20,6 @@
 
 #include "wrap_Thread.h"
 
-// anonymous namespace for getting an std::string from the stack
-// that may contain embedded NULLs
-namespace
-{
-	std::string luax_checklstring(lua_State *L, int idx)
-	{
-		size_t len;
-		const char* str = luaL_checklstring(L, idx, &len);
-		return std::string(str, len);
-	}
-}
-
 namespace love
 {
 namespace thread
@@ -66,7 +54,7 @@ namespace thread
 	{
 		Thread *t = luax_checkthread(L, 1);
 		// allow names containing \0
-		lua_pushlstring(L, t->getName().c_str(), t->getName().length());
+		luax_pushstring(L, t->getName());
 		return 1;
 	}
 
@@ -115,7 +103,7 @@ namespace thread
 	int w_Thread_get(lua_State *L)
 	{
 		Thread *t = luax_checkthread(L, 1);
-		std::string name = luax_checklstring(L, 2);
+		std::string name = luax_checkstring(L, 2);
 		t->lock();
 		ThreadVariant *v = t->get(name);
 		t->clear(name);
@@ -139,7 +127,7 @@ namespace thread
 		for (std::vector<std::string>::iterator it = keys.begin(); it != keys.end(); it++)
 		{
 			lua_pushnumber(L, i++);
-			lua_pushlstring(L, it->c_str(), it->length());
+			luax_pushstring(L, *it);
 			lua_settable(L, -3);
 		}
 		return 1;
@@ -148,7 +136,7 @@ namespace thread
 	int w_Thread_demand(lua_State *L)
 	{
 		Thread *t = luax_checkthread(L, 1);
-		std::string name = luax_checklstring(L, 2);
+		std::string name = luax_checkstring(L, 2);
 		t->lock();
 		ThreadVariant *v = t->demand(name);
 		t->clear(name);
@@ -164,7 +152,7 @@ namespace thread
 	int w_Thread_peek(lua_State *L)
 	{
 		Thread *t = luax_checkthread(L, 1);
-		std::string name = luax_checklstring(L, 2);
+		std::string name = luax_checkstring(L, 2);
 		t->lock();
 		ThreadVariant *v = t->get(name);
 		t->unlock();
@@ -195,7 +183,7 @@ namespace thread
 	int w_Thread_set(lua_State *L)
 	{
 		Thread *t = luax_checkthread(L, 1);
-		std::string name = luax_checklstring(L, 2);
+		std::string name = luax_checkstring(L, 2);
 		ThreadVariant *v;
 		size_t len;
 		const char *str;
@@ -249,7 +237,7 @@ namespace thread
 
 	int w_newThread(lua_State *L)
 	{
-		std::string name = luax_checklstring(L, 1);
+		std::string name = luax_checkstring(L, 1);
 		love::Data *data;
 		if (lua_isstring(L, 2))
 			luax_convobj(L, 2, "filesystem", "newFile");
@@ -284,7 +272,7 @@ namespace thread
 		for (unsigned int i = 0; i<count; i++)
 		{
 			// allow names containing \0
-			lua_pushlstring(L, list[i]->getName().c_str(), list[i]->getName().length());
+			luax_pushstring(L, list[i]->getName());
 			luax_newtype(L, "Thread", THREAD_THREAD_T, (void*) list[i]);
 			list[i]->lock();
 			list[i]->retain();
@@ -303,7 +291,7 @@ namespace thread
 			lua_getfield(L, -1, "_curthread");
 			return 1;
 		}
-		std::string name = luax_checklstring(L, 1);
+		std::string name = luax_checkstring(L, 1);
 		Thread *t = instance->getThread(name);
 		if (t)
 		{