tests/modules/performance/byte-compile-lua: improve bytecode detection

Previously, to determine if a file is byte-compiled, a simple binary
file detection was used, specifically checking if it contained any null
bytes. This commit updates the check to read the file header and compare
it with a known LuaJIT header.
This commit is contained in:
Stanislav Asunkin 2025-04-29 17:22:26 +03:00
parent ddddd780e0
commit 5b47c65705
2 changed files with 26 additions and 24 deletions

View file

@ -57,17 +57,19 @@ let
}).config.home-files; }).config.home-files;
in in
pkgs.runCommand "home-manager-extra-files-byte-compiling" { } '' pkgs.runCommand "home-manager-extra-files-byte-compiling" { } ''
is_binary() { is_byte_compiled() {
! grep -qI . "$1" # LuaJIT bytecode header is: ESC L J version
# https://github.com/LuaJIT/LuaJIT/blob/v2.1/src/lj_bcdump.h
[[ $(head -c3 "$1") = $'\x1bLJ' ]]
} }
test_byte_compiled() { test_byte_compiled() {
if ! is_binary "$home_files/.config/nvim/$1"; then if ! is_byte_compiled "$home_files/.config/nvim/$1"; then
echo "File $1 is expected to be byte compiled, but it's not" echo "File $1 is expected to be byte compiled, but it's not"
exit 1 exit 1
fi fi
} }
test_not_byte_compiled() { test_not_byte_compiled() {
if is_binary "$home_files/.config/nvim/$1"; then if is_byte_compiled "$home_files/.config/nvim/$1"; then
echo "File $1 is not expected to be byte compiled, but it is" echo "File $1 is not expected to be byte compiled, but it is"
exit 1 exit 1
fi fi

View file

@ -1,27 +1,27 @@
{ pkgs, ... }: { pkgs, ... }:
let let
isByteCompiledFun = '' isByteCompiledFun = # lua
local function is_byte_compiled(filename) ''
local f = assert(io.open(filename, "rb")) -- LuaJIT bytecode header is: ESC L J version
local data = assert(f:read("*a")) -- https://github.com/LuaJIT/LuaJIT/blob/v2.1/src/lj_bcdump.h
-- Assume that file is binary if it contains null bytes -- We are comparing the first three bytes of the file (until version)
for i = 1, #data do local expected_header = string.char(0x1b, 0x4c, 0x4a)
if data:byte(i) == 0 then local function is_byte_compiled(filename)
return true local f = assert(io.open(filename, "rb"))
local data = assert(f:read(3))
f:close()
return data == expected_header
end
local function test_rtp_file(name, is_compiled)
local file = assert(vim.api.nvim_get_runtime_file(name, false)[1], "file " .. name .. " not found in runtime")
if is_compiled then
assert(is_byte_compiled(file), name .. " is expected to be byte compiled, but it's not")
else
assert(not is_byte_compiled(file), name .. " is not expected to be byte compiled, but it is")
end end
end end
return false '';
end
local function test_rtp_file(name, is_compiled)
local file = assert(vim.api.nvim_get_runtime_file(name, false)[1], "file " .. name .. " not found in runtime")
if is_compiled then
assert(is_byte_compiled(file), name .. " is expected to be byte compiled, but it's not")
else
assert(not is_byte_compiled(file), name .. " is not expected to be byte compiled, but it is")
end
end
'';
in in
{ {
default = default =