Merge pull request #330 from c4games/lua54-compat

Lua 5.4 compatible
This commit is contained in:
HALX99 2021-03-13 18:46:13 +08:00 committed by GitHub
commit 6a868299f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 1 deletions

View File

@ -22,6 +22,12 @@ THE SOFTWARE.
]]
if (math.pow == nil) then
math.pow = function (x,y)
return x ^ y
end
end
require "cocos.cocos2d.Cocos2d"
require "cocos.cocos2d.Cocos2dConstants"
require "cocos.cocos2d.functions"

View File

@ -47,7 +47,7 @@ typedef int lua_Object;
#include "lua.h"
#include "lauxlib.h"
#if LUA_VERSION_NUM >= 502
#define lua_setfenv lua_setuservalue
#define lua_getfenv lua_getuservalue
@ -57,6 +57,16 @@ typedef int lua_Object;
#endif
#endif
#if LUA_VERSION_NUM >= 504
TOLUA_API const char* luaL_findtable(lua_State* L, int idx,
const char* fname, int szhint);
TOLUA_API void luaL_pushmodule(lua_State* L, const char* modname,
int sizehint);
TOLUA_API void luaL_openlib(lua_State* L, const char* libname,
const luaL_Reg* l, int nup);
#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0))
#endif
struct tolua_Error
{
int index;

View File

@ -17,6 +17,67 @@
#include <string.h>
#include <stdlib.h>
#if LUA_VERSION_NUM >= 504
static int libsize(const luaL_Reg* l) {
int size = 0;
for (; l && l->name; l++) size++;
return size;
}
TOLUA_API const char* luaL_findtable(lua_State* L, int idx,
const char* fname, int szhint) {
const char* e;
if (idx) lua_pushvalue(L, idx);
do {
e = strchr(fname, '.');
if (e == NULL) e = fname + strlen(fname);
lua_pushlstring(L, fname, e - fname);
if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */
lua_pop(L, 1); /* remove this nil */
lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
lua_pushlstring(L, fname, e - fname);
lua_pushvalue(L, -2);
lua_settable(L, -4); /* set new table into field */
}
else if (!lua_istable(L, -1)) { /* field has a non-table value? */
lua_pop(L, 2); /* remove table and value */
return fname; /* return problematic part of the name */
}
lua_remove(L, -2); /* remove previous table */
fname = e + 1;
} while (*e == '.');
return NULL;
}
TOLUA_API void luaL_pushmodule(lua_State* L, const char* modname,
int sizehint) {
luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1);
if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */
lua_pop(L, 1); /* remove previous result */
/* try global variable (and create one if it does not exist) */
lua_pushglobaltable(L);
if (luaL_findtable(L, 0, modname, sizehint) != NULL)
luaL_error(L, "name conflict for module '%s'", modname);
lua_pushvalue(L, -1);
lua_setfield(L, -3, modname); /* LOADED[modname] = new table */
}
lua_remove(L, -2); /* remove LOADED table */
}
TOLUA_API void luaL_openlib(lua_State* L, const char* libname,
const luaL_Reg* l, int nup) {
luaL_checkversion(L);
if (libname) {
luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
}
if (l)
luaL_setfuncs(L, l, nup);
else
lua_pop(L, nup); /* remove upvalues */
}
#endif
TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def)
{
return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);