Skip to content

Commit

Permalink
Build LuaJIT w/ table.pack support (#1535)
Browse files Browse the repository at this point in the history
It's unfortunately behind the Lua 5.2 extensions ifdef in a standard build (possibly because it's so common that it's usually implemented in Lua and monkey-patched over the table metatable?).

But it's trivial and doesn't actually poses an API issue, and it's helpful for a number of our use-cases.
  • Loading branch information
NiLuJe committed Oct 12, 2022
1 parent 661e467 commit 4216c40
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ self = false

globals = {
"G_defaults",
"table.pack",
"table.unpack",
}

exclude_files = {
Expand Down
6 changes: 3 additions & 3 deletions ffi/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ function util.execute(...)
else
local pid = C.fork()
if pid == 0 then
local args = {...}
os.exit(C.execl(args[1], unpack(args, 1, #args+1)))
local args = table.pack(...)
os.exit(C.execl(args[1], unpack(args, 1, #args+1))) -- Last arg must be a NULL pointer
end
local status = ffi.new('int[1]')
C.waitpid(pid, status, 0)
Expand Down Expand Up @@ -722,7 +722,7 @@ This function was inspired by Qt:
<http://qt-project.org/doc/qt-4.8/internationalization.html#use-qstring-arg-for-dynamic-text>
--]]
function util.template(str, ...)
local params = {...}
local params = table.pack(...)
-- shortcut:
if #params == 0 then return str end
local result = string.gsub(str, "%%([1-9][0-9]?)",
Expand Down
8 changes: 5 additions & 3 deletions thirdparty/luajit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,22 @@ endif()

set(BUILD_CMD sh -c "${BUILD_CMD_STR}")

set(PATCH_CMD "${KO_PATCH} ${CMAKE_CURRENT_SOURCE_DIR}/koreader-luajit-makefile-tweaks.patch")
set(PATCH_CMD1 "${KO_PATCH} ${CMAKE_CURRENT_SOURCE_DIR}/koreader-luajit-makefile-tweaks.patch")
# Enable table.pack & table.unpack w/o the rest of the -DLUAJIT_ENABLE_LUA52COMPAT baggage...
set(PATCH_CMD2 "${KO_PATCH} ${CMAKE_CURRENT_SOURCE_DIR}/koreader-luajit-enable-table_pack.patch")

ko_write_gitclone_script(
GIT_CLONE_SCRIPT_FILENAME
https://github.com/LuaJIT/LuaJIT
dad04f1754723e76ba9dcf9f401f3134a0cd3972
6c4826f12c4d33b8b978004bc681eb1eef2be977
${SOURCE_DIR}
)

include(ExternalProject)
ExternalProject_Add(
${PROJECT_NAME}
DOWNLOAD_COMMAND ${CMAKE_COMMAND} -P ${GIT_CLONE_SCRIPT_FILENAME}
PATCH_COMMAND COMMAND ${PATCH_CMD}
PATCH_COMMAND COMMAND ${PATCH_CMD1} COMMAND ${PATCH_CMD2}
BUILD_IN_SOURCE 1
# skip configure
CONFIGURE_COMMAND ""
Expand Down
31 changes: 31 additions & 0 deletions thirdparty/luajit/koreader-luajit-enable-table_pack.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
diff --git a/src/lib_table.c b/src/lib_table.c
index a723326a..28af227f 100644
--- a/src/lib_table.c
+++ b/src/lib_table.c
@@ -267,7 +267,6 @@ LJLIB_CF(table_sort)
return 0;
}

-#if LJ_52
LJLIB_PUSH("n")
LJLIB_CF(table_pack)
{
@@ -283,7 +282,6 @@ LJLIB_CF(table_pack)
lj_gc_check(L);
return 1;
}
-#endif

LJLIB_NOREG LJLIB_CF(table_new) LJLIB_REC(.)
{
@@ -316,10 +314,8 @@ static int luaopen_table_clear(lua_State *L)
LUALIB_API int luaopen_table(lua_State *L)
{
LJ_LIB_REG(L, LUA_TABLIBNAME, table);
-#if LJ_52
lua_getglobal(L, "unpack");
lua_setfield(L, -2, "unpack");
-#endif
lj_lib_prereg(L, LUA_TABLIBNAME ".new", luaopen_table_new, tabV(L->top-1));
lj_lib_prereg(L, LUA_TABLIBNAME ".clear", luaopen_table_clear, tabV(L->top-1));
return 1;

0 comments on commit 4216c40

Please sign in to comment.