Skip to content

Compatibility with FILE* metatype broken on LuaJIT #70

@jgmdev

Description

@jgmdev

The following code causes an overwrite of the file metatype from FILE* to userdata which breaks compatibility with native C functions that expect a FILE* when performing something like luaL_checkudata(L, 1, LUA_FILEHANDLE);

if is_luajit then
local compat_file_meta = {}
local compat_file_meta_loaded = 0
local function load_compat_file_meta(file_meta)
-- fill compat_file_meta with original entries
for k, v in pairs(file_meta) do
compat_file_meta[k] = v
end
compat_file_meta.__index = {}
for k, v in pairs(file_meta.__index) do
compat_file_meta.__index[k] = v
end
compat_file_meta_loaded = 1
-- update it with compatibility functions
local file_mt_ok, file_mt = pcall(require, "compat53.file_mt")
if file_mt_ok then
file_mt.update_file_meta(compat_file_meta, is_luajit52)
compat_file_meta_loaded = 2
end
end
local function return_fd(fd, err, code)
if not fd then
return fd, err, code
end
if fd and debug_setmetatable then
if compat_file_meta_loaded == 0 then
local file_meta = gmt(fd)
load_compat_file_meta(file_meta)
end
if compat_file_meta_loaded == 2 then
debug_setmetatable(fd, compat_file_meta)
end
end
return fd
end
function M.io.open(...)
return return_fd(io_open(...))
end
function M.io.popen(...)
return return_fd(io_popen(...))
end
function M.io.tmpfile(...)
return return_fd(io_tmpfile(...))
end
end

I don't know what is the motive for this code but from my quick testing this change doesn't seems needed since the lines() and read() methods are properly overwritten from here for all subsequently created file objects:

if type(file_meta) == "table" and type(file_meta.__index) == "table" then
local file_mt = require("compat53.file_mt")
file_mt.update_file_meta(file_meta, is_luajit52)
end -- got a valid metatable for file objects

For reference: pragtical/pragtical#228

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions