fix: Lua 5.1 兼容性 + CI 稳定性
Some checks failed
CI / luacheck (push) Has been cancelled
CI / Test (Lua 5.3) (push) Has been cancelled
CI / Test (LuaJIT) (push) Has been cancelled
CI / Test (Lua 5.1) (push) Has been cancelled

- 修复 rime_context_filter.lua: load() 在 Lua 5.1 上用 loadstring
- 修复 test: load(ser) 在 Lua 5.1 上改用 loadstring
- 修复 CI: luarocks install 添加 continue-on-error
This commit is contained in:
sakuradairong
2026-06-10 16:36:40 +08:00
parent 0196b203ef
commit c67747c231
3 changed files with 34 additions and 11 deletions

View File

@@ -12,11 +12,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Lua & luacheck
- name: Install Lua & luarocks
run: |
sudo apt-get update -qq
sudo apt-get install -y -qq lua5.3 luarocks 2>/dev/null
sudo luarocks install luacheck 2>/dev/null
- name: Install luacheck
continue-on-error: true
run: sudo luarocks install luacheck 2>&1
- name: Lint
run: luacheck rime_context_filter.lua --std luacheck --no-max-line-length || true

View File

@@ -97,18 +97,29 @@ local function load(data_file)
if not content or #content == 0 then return {}, 0 end
local safe_env = {}
-- Lua 5.3+: load(chunk, name, mode, env)
-- Lua 5.1/LuaJIT: load(chunk, name) only — no sandbox parameter
local loader, err = load(content, "@" .. data_file, "t", safe_env)
if not loader then
-- Lua 5.1/LuaJIT 降级(无沙箱参数)
loader, err = load(content, "@" .. data_file)
local loader, err
if _VERSION == "Lua 5.1" then
-- Lua 5.1: load() accepts function only; loadstring for string chunks
loader, err = loadstring(content, "@" .. data_file)
if loader then
io.stderr:write("[rime-context-filter] WARNING: " ..
"Sandbox unavailable on Lua 5.1/LuaJIT. " ..
"Sandbox unavailable on Lua 5.1. " ..
"Data file could access global environment.\n")
end
else
-- Lua 5.3+: load(chunk, name, mode, env) with sandbox
loader, err = load(content, "@" .. data_file, "t", safe_env)
if not loader then
-- Fallback for embedders without 4-arg load
loader, err = load(content, "@" .. data_file)
if loader then
io.stderr:write("[rime-context-filter] WARNING: " ..
"Sandbox unavailable, data file could access global environment.\n")
end
end
end
if not loader then return {}, 0 end
local ok, data = pcall(loader)
if not ok or type(data) ~= "table" then return {}, 0 end

View File

@@ -93,7 +93,12 @@ check(ser:match("^return%s*{"), "starts with return {")
check(ser:match("}\n$"), "ends with }\\n")
-- 通过 Lua VM 验证语法正确性
local fn, err = load(ser)
local fn, err
if _VERSION == "Lua 5.1" then
fn, err = loadstring(ser)
else
fn, err = load(ser)
end
check(fn ~= nil, "serialized output is valid Lua (" .. tostring(err) .. ")")
if fn then
local ok2, loaded = pcall(fn)
@@ -109,7 +114,12 @@ end
local empty_ser = serialize({})
check(empty_ser:match("^return {"), "empty starts with return {")
check(empty_ser:match("}\n$"), "empty ends with }\\n")
local fn2, err2 = load(empty_ser)
local fn2, err2
if _VERSION == "Lua 5.1" then
fn2, err2 = loadstring(empty_ser)
else
fn2, err2 = load(empty_ser)
end
check(fn2 ~= nil, "empty output is valid Lua (" .. tostring(err2) .. ")")
if fn2 then
local ok3, empty_data = pcall(fn2)