Browse Source

Make and use a vector variant of luax_convobj

Which also prevents accessing the zeroeth element of an empty vector
Bart van Strien 6 years ago
parent
commit
7253118100
3 changed files with 20 additions and 6 deletions
  1. 14 2
      src/common/runtime.cpp
  2. 4 2
      src/common/runtime.h
  3. 2 2
      src/modules/graphics/wrap_Graphics.cpp

+ 14 - 2
src/common/runtime.cpp

@@ -623,7 +623,7 @@ int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn)
 	return 0;
 }
 
-int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
+int luax_convobj(lua_State *L, const int idxs[], int n, const char *mod, const char *fn)
 {
 	luax_getfunction(L, mod, fn);
 	for (int i = 0; i < n; i++)
@@ -638,6 +638,12 @@ int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *f
 	return 0;
 }
 
+int luax_convobj(lua_State *L, const std::vector<int>& idxs, const char *module, const char *function)
+{
+	const int *idxPtr = idxs.size() > 0 ? &idxs[0] : nullptr;
+	return luax_convobj(L, idxPtr, (int) idxs.size(), module, function);
+}
+
 int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn)
 {
 	// Convert string to a file.
@@ -649,7 +655,7 @@ int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn)
 	return ret;
 }
 
-int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
+int luax_pconvobj(lua_State *L, const int idxs[], int n, const char *mod, const char *fn)
 {
 	luax_getfunction(L, mod, fn);
 	for (int i = 0; i < n; i++)
@@ -662,6 +668,12 @@ int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *
 	return ret;
 }
 
+int luax_pconvobj(lua_State *L, const std::vector<int>& idxs, const char *module, const char *function)
+{
+	const int *idxPtr = idxs.size() > 0 ? &idxs[0] : nullptr;
+	return luax_pconvobj(L, idxPtr, (int) idxs.size(), module, function);
+}
+
 int luax_insist(lua_State *L, int idx, const char *k)
 {
 	// Convert to absolute index if necessary.

+ 4 - 2
src/common/runtime.h

@@ -389,11 +389,13 @@ int luax_convobj(lua_State *L, int idx, const char *module, const char *function
  * @param module The module in the love table.
  * @param function The function in the module.
  **/
-int luax_convobj(lua_State *L, int idxs[], int n, const char *module, const char *function);
+int luax_convobj(lua_State *L, const int idxs[], int n, const char *module, const char *function);
+int luax_convobj(lua_State *L, const std::vector<int>& idxs, const char *module, const char *function);
 
 // pcall versions of the above
 int luax_pconvobj(lua_State *L, int idx, const char *module, const char *function);
-int luax_pconvobj(lua_State *L, int idxs[], int n, const char *module, const char *function);
+int luax_pconvobj(lua_State *L, const int idxs[], int n, const char *module, const char *function);
+int luax_pconvobj(lua_State *L, const std::vector<int>& idxs, const char *module, const char *function);
 
 /**
  * 'Insist' that a table 'k' exists in the table at idx. Insistence involves that the

+ 2 - 2
src/modules/graphics/wrap_Graphics.cpp

@@ -1091,7 +1091,7 @@ int w_newFont(lua_State *L)
 		for (int i = 0; i < lua_gettop(L); i++)
 			idxs.push_back(i + 1);
 
-		luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newRasterizer");
+		luax_convobj(L, idxs, "font", "newRasterizer");
 	}
 
 	love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);
@@ -1122,7 +1122,7 @@ int w_newImageFont(lua_State *L)
 		for (int i = 0; i < lua_gettop(L); i++)
 			idxs.push_back(i + 1);
 
-		luax_convobj(L, &idxs[0], (int) idxs.size(), "font", "newImageRasterizer");
+		luax_convobj(L, idxs, "font", "newImageRasterizer");
 	}
 
 	love::font::Rasterizer *rasterizer = luax_checktype<love::font::Rasterizer>(L, 1);