Browse Source

Keep math.mod and string.gfind when LuaJIT 2.1 is used

Code written assuming LuaJIT 2.0 is being used might rely on them, even though they're deprecated (and removed in LuaJIT 2.1).
Alex Szpakowski 4 years ago
parent
commit
f3afdee70a
1 changed files with 24 additions and 0 deletions
  1. 24 0
      src/modules/love/love.cpp

+ 24 - 0
src/modules/love/love.cpp

@@ -362,6 +362,23 @@ static int w_deprecation__gc(lua_State *)
 	return 0;
 }
 
+static void luax_addcompatibilityalias(lua_State *L, const char *module, const char *name, const char *alias)
+{
+	lua_getglobal(L, module);
+	if (lua_istable(L, -1))
+	{
+		lua_getfield(L, -1, alias);
+		bool hasalias = !lua_isnoneornil(L, -1);
+		lua_pop(L, 1);
+		if (!hasalias)
+		{
+			lua_getfield(L, -1, name);
+			lua_setfield(L, -2, alias);
+		}
+	}
+	lua_pop(L, 1);
+}
+
 int luaopen_love(lua_State *L)
 {
 	love::luax_insistpinnedthread(L);
@@ -469,6 +486,13 @@ int luaopen_love(lua_State *L)
 	love::luax_require(L, "love.data");
 	lua_pop(L, 1);
 
+#if LUA_VERSION_NUM <= 501
+	// These are deprecated in Lua 5.1. LuaJIT 2.1 removes them, but code
+	// written assuming LuaJIT 2.0 or Lua 5.1 is used might still rely on them.
+	luax_addcompatibilityalias(L, "math", "fmod", "mod");
+	luax_addcompatibilityalias(L, "string", "gmatch", "gfind");
+#endif
+
 #ifdef LOVE_ENABLE_LUASOCKET
 	love::luasocket::__open(L);
 #endif