summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-10-09 23:58:21 +0800
committerruki <[email protected]>2021-10-09 23:58:21 +0800
commit1af0820871a06217312fcee22b68e15954fc553a (patch)
tree5eb9e9b0f96089bebc18256ae3cc6644975d8112
parentf35f6e15e13a4e5a360b94148534623b6a9c5feb (diff)
fix some errors for lua5.4
-rw-r--r--core/src/lcurses/lcurses.c14
-rw-r--r--core/src/xmake/engine.c24
-rw-r--r--core/src/xmake/prefix.h16
-rw-r--r--core/src/xmake/sandbox/interactive.c5
-rw-r--r--xmake/core/_xmake_main.lua4
-rw-r--r--xmake/core/base/bytes.lua3
-rw-r--r--xmake/core/base/table.lua9
-rw-r--r--xmake/core/sandbox/modules/interpreter/print.lua5
-rw-r--r--xmake/core/sandbox/modules/interpreter/unpack.lua2
-rw-r--r--xmake/core/sandbox/modules/unpack.lua2
10 files changed, 62 insertions, 22 deletions
diff --git a/core/src/lcurses/lcurses.c b/core/src/lcurses/lcurses.c
index f3bdf7244..a55b86606 100644
--- a/core/src/lcurses/lcurses.c
+++ b/core/src/lcurses/lcurses.c
@@ -2431,8 +2431,11 @@ int xm_curses_register (lua_State *L)
lua_pushliteral(L, "__index");
lua_pushvalue(L, -2); /* push metatable */
lua_rawset(L, -3); /* metatable.__index = metatable */
+#if 0
luaL_openlib(L, NULL, windowlib, 0);
-
+#else
+ luaL_setfuncs(L, windowlib, 0);
+#endif
lua_pop(L, 1); /* remove metatable from stack */
/*
@@ -2442,8 +2445,11 @@ int xm_curses_register (lua_State *L)
lua_pushliteral(L, "__index");
lua_pushvalue(L, -2); /* push metatable */
lua_rawset(L, -3); /* metatable.__index = metatable */
+#if 0
luaL_openlib(L, NULL, chstrlib, 0);
-
+#else
+ luaL_setfuncs(L, chstrlib, 0);
+#endif
lua_pop(L, 1); /* remove metatable from stack */
@@ -2451,7 +2457,11 @@ int xm_curses_register (lua_State *L)
** create global table with curses methods/variables/constants
*/
lua_newtable(L);
+#if 0
luaL_register(L, NULL, curseslib);
+#else
+ luaL_setfuncs(L, curseslib, 0);
+#endif
lua_pushstring(L, "init");
lua_pushvalue(L, -2);
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index dbea858c1..0793f426e 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -845,41 +845,41 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
luaL_openlibs(engine->lua);
// bind os functions
- luaL_register(engine->lua, "os", g_os_functions);
+ xm_lua_register(engine->lua, "os", g_os_functions);
// bind io functions
- luaL_register(engine->lua, "io", g_io_functions);
+ xm_lua_register(engine->lua, "io", g_io_functions);
// bind path functions
- luaL_register(engine->lua, "path", g_path_functions);
+ xm_lua_register(engine->lua, "path", g_path_functions);
// bind hash functions
- luaL_register(engine->lua, "hash", g_hash_functions);
+ xm_lua_register(engine->lua, "hash", g_hash_functions);
// bind string functions
- luaL_register(engine->lua, "string", g_string_functions);
+ xm_lua_register(engine->lua, "string", g_string_functions);
// bind process functions
- luaL_register(engine->lua, "process", g_process_functions);
+ xm_lua_register(engine->lua, "process", g_process_functions);
// bind sandbox functions
- luaL_register(engine->lua, "sandbox", g_sandbox_functions);
+ xm_lua_register(engine->lua, "sandbox", g_sandbox_functions);
// bind windows functions
#ifdef TB_CONFIG_OS_WINDOWS
- luaL_register(engine->lua, "winos", g_winos_functions);
+ xm_lua_register(engine->lua, "winos", g_winos_functions);
#endif
#ifdef XM_CONFIG_API_HAVE_READLINE
// bind readline functions
- luaL_register(engine->lua, "readline", g_readline_functions);
+ xm_lua_register(engine->lua, "readline", g_readline_functions);
#endif
// bind semver functions
- luaL_register(engine->lua, "semver", g_semver_functions);
+ xm_lua_register(engine->lua, "semver", g_semver_functions);
// bind libc functions
- luaL_register(engine->lua, "libc", g_libc_functions);
+ xm_lua_register(engine->lua, "libc", g_libc_functions);
#ifdef XM_CONFIG_API_HAVE_CURSES
// bind curses
@@ -1059,7 +1059,7 @@ tb_void_t xm_engine_register(xm_engine_ref_t self, tb_char_t const* module, luaL
// do register
lua_pushstring(engine->lua, module);
lua_newtable(engine->lua);
- luaL_register(engine->lua, tb_null, funcs);
+ xm_lua_register(engine->lua, tb_null, funcs);
lua_rawset(engine->lua, -3);
}
tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv, xm_engine_lni_initalizer_cb_t lni_initalizer)
diff --git a/core/src/xmake/prefix.h b/core/src/xmake/prefix.h
index f78b61127..fabb775fb 100644
--- a/core/src/xmake/prefix.h
+++ b/core/src/xmake/prefix.h
@@ -114,6 +114,22 @@ static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx)
}
#endif
+static __tb_inline__ tb_void_t xm_lua_register(lua_State *lua, tb_char_t const* libname, luaL_Reg const* l)
+{
+#if LUA_VERSION_NUM >= 504
+ lua_getglobal(lua, libname);
+ if (lua_isnil(lua, -1))
+ {
+ lua_pop(lua, 1);
+ lua_newtable(lua);
+ }
+ luaL_setfuncs(lua, l, 0);
+ lua_setglobal(lua, libname);
+#else
+ luaL_register(lua, libname, l);
+#endif
+}
+
#endif
diff --git a/core/src/xmake/sandbox/interactive.c b/core/src/xmake/sandbox/interactive.c
index a62f6f77c..3b6ef3ff4 100644
--- a/core/src/xmake/sandbox/interactive.c
+++ b/core/src/xmake/sandbox/interactive.c
@@ -51,6 +51,11 @@
// buffer size for prompt
#define LUA_PROMPT_BUFSIZE 4096
+// for lua5.4
+#ifndef LUA_QL
+# define LUA_QL(x) "'" x "'"
+#endif
+
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua
index cf82a5a9c..b35768058 100644
--- a/xmake/core/_xmake_main.lua
+++ b/xmake/core/_xmake_main.lua
@@ -120,8 +120,8 @@ function loadfile(filepath, mode, opt)
return script, errors
end
--- init package path
-table.insert(package.loaders, 2, function(v)
+-- init package path, package.searchers for lua5.4
+table.insert(package.loaders or package.searchers, 2, function(v)
local filepath = xmake._PROGRAM_DIR .. "/core/" .. v .. ".lua"
local script, serr = _loadfile_impl(filepath)
if not script then
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua
index 9f7b6ca4f..bb65d0cfc 100644
--- a/xmake/core/base/bytes.lua
+++ b/xmake/core/base/bytes.lua
@@ -28,6 +28,7 @@ local os = require("base/os")
local utils = require("base/utils")
local todisplay = require("base/todisplay")
local libc = require("base/libc")
+local table = require("base/table")
-- new a bytes instance
--
@@ -42,7 +43,7 @@ local libc = require("base/libc")
--
function _instance.new(...)
local args = {...}
- local arg1, arg2, arg3 = unpack(args)
+ local arg1, arg2, arg3 = table.unpack(args)
local instance = table.inherit(_instance)
if type(arg1) == "number" then
local size = arg1
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index 1e93efd8d..372e5f975 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -53,6 +53,13 @@ if not table.getn then
end
end
+-- get array max length for lua5.4
+if not table.maxn then
+ function table.maxn(t)
+ return #t
+ end
+end
+
-- move values of table(a1) to table(a2)
--
-- disable the builtin implementation for android termux/arm64, it will crash when calling `table.move({1, 1}, 1, 2, 1, {})`
@@ -322,7 +329,7 @@ end
-- unpack table values
-- polyfill of lua 5.2, @see https://www.lua.org/manual/5.2/manual.html#pdf-table.unpack
-table.unpack = unpack
+table.unpack = table.unpack or unpack
-- get keys of a table
function table.keys(tab)
diff --git a/xmake/core/sandbox/modules/interpreter/print.lua b/xmake/core/sandbox/modules/interpreter/print.lua
index 0f906f5a4..dd42c8fe9 100644
--- a/xmake/core/sandbox/modules/interpreter/print.lua
+++ b/xmake/core/sandbox/modules/interpreter/print.lua
@@ -19,6 +19,7 @@
--
-- load modules
+local table = require("base/table")
local try = require("sandbox/modules/try")
local catch = require("sandbox/modules/catch")
@@ -33,13 +34,13 @@ function _print(format, ...)
{
function ()
-- attempt to print format string first
- io.write(string.format(format, unpack(args)) .. "\n")
+ io.write(string.format(format, table.unpack(args)) .. "\n")
end,
catch
{
function ()
-- print multi-variables with raw lua action
- print(format, unpack(args))
+ print(format, table.unpack(args))
end
}
}
diff --git a/xmake/core/sandbox/modules/interpreter/unpack.lua b/xmake/core/sandbox/modules/interpreter/unpack.lua
index 766e49f98..d05c3ce3d 100644
--- a/xmake/core/sandbox/modules/interpreter/unpack.lua
+++ b/xmake/core/sandbox/modules/interpreter/unpack.lua
@@ -19,5 +19,5 @@
--
-- load module
-return unpack
+return require("base/table").unpack
diff --git a/xmake/core/sandbox/modules/unpack.lua b/xmake/core/sandbox/modules/unpack.lua
index 766e49f98..d05c3ce3d 100644
--- a/xmake/core/sandbox/modules/unpack.lua
+++ b/xmake/core/sandbox/modules/unpack.lua
@@ -19,5 +19,5 @@
--
-- load module
-return unpack
+return require("base/table").unpack