Skip to content

Commit

Permalink
Apply tweaks only to LuaJIT; file:write() only to compat=none
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Aug 29, 2024
1 parent 9c0a31f commit 2cdfd42
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 84 deletions.
16 changes: 9 additions & 7 deletions compat53/file_mt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local function addasterisk(fmt)
end
end

function M.update_file_meta(file_meta)
function M.update_file_meta(file_meta, is_luajit52)

-- make '*' optional for file:read and file:lines

Expand Down Expand Up @@ -56,13 +56,15 @@ function M.update_file_meta(file_meta)
return file_read(self, ...)
end

local file_write = file_meta.__index.write
file_meta.__index.write = function(self, ...)
local ret, err = file_write(self, ...)
if ret then
return self
if not is_luajit52 then
local file_write = file_meta.__index.write
file_meta.__index.write = function(self, ...)
local ret, err = file_write(self, ...)
if ret then
return self
end
return ret, err
end
return ret, err
end
end

Expand Down
14 changes: 7 additions & 7 deletions compat53/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ if lua_version < "5.3" then
local file_meta = gmt(io.stdout)


-- detect LuaJIT (including LUAJIT_ENABLE_LUA52COMPAT compilation flag)
local is_luajit = (string.dump(function() end) or ""):sub(1, 3) == "\027LJ"
local is_luajit52 = is_luajit and
#setmetatable({}, { __len = function() return 1 end }) == 1


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)
file_mt.update_file_meta(file_meta, is_luajit52)
end -- got a valid metatable for file objects


Expand All @@ -37,12 +43,6 @@ if lua_version < "5.3" then
local io_type = io.type


-- detect LuaJIT (including LUAJIT_ENABLE_LUA52COMPAT compilation flag)
local is_luajit = (string.dump(function() end) or ""):sub(1, 3) == "\027LJ"
local is_luajit52 = is_luajit and
#setmetatable({}, { __len = function() return 1 end }) == 1


-- make package.searchers available as an alias for package.loaders
local p_index = { searchers = package.loaders }
setmetatable(package, {
Expand Down
120 changes: 61 additions & 59 deletions compat53/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,65 +205,6 @@ if lua_version < "5.3" then
end
end

local compat_file_meta = {}
local compat_file_meta_loaded = false

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

-- update it with compatibility functions
local file_mt = require("compat53.file_mt")
file_mt.update_file_meta(compat_file_meta)

compat_file_meta_loaded = true
end

function M.io.open(...)
local fd, err = io_open(...)
if fd and debug_setmetatable then
if not compat_file_meta_loaded then
local file_meta = gmt(fd)
load_compat_file_meta(file_meta)
end
debug_setmetatable(fd, compat_file_meta)
end

return fd, err
end

function M.io.popen(...)
local fd, err = io_popen(...)
if fd and debug_setmetatable then
if not compat_file_meta_loaded then
local file_meta = gmt(fd)
load_compat_file_meta(file_meta)
end
debug_setmetatable(fd, compat_file_meta)
end

return fd, err
end

function M.io.tmpfile(...)
local fd, err = io_tmpfile(...)
if fd and debug_setmetatable then
if not compat_file_meta_loaded then
local file_meta = gmt(fd)
load_compat_file_meta(file_meta)
end
debug_setmetatable(fd, compat_file_meta)
end

return fd, err
end

function M.io.read(...)
local n = select('#', ...)
for i = 1, n do
Expand Down Expand Up @@ -884,6 +825,67 @@ if lua_version < "5.3" then
end
end -- not luajit

if is_luajit then
local compat_file_meta = {}
local compat_file_meta_loaded = false

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

-- update it with compatibility functions
local file_mt = require("compat53.file_mt")
file_mt.update_file_meta(compat_file_meta, is_luajit52)

compat_file_meta_loaded = true
end

function M.io.open(...)
local fd, err = io_open(...)
if fd and debug_setmetatable then
if not compat_file_meta_loaded then
local file_meta = gmt(fd)
load_compat_file_meta(file_meta)
end
debug_setmetatable(fd, compat_file_meta)
end

return fd, err
end

function M.io.popen(...)
local fd, err = io_popen(...)
if fd and debug_setmetatable then
if not compat_file_meta_loaded then
local file_meta = gmt(fd)
load_compat_file_meta(file_meta)
end
debug_setmetatable(fd, compat_file_meta)
end

return fd, err
end

function M.io.tmpfile(...)
local fd, err = io_tmpfile(...)
if fd and debug_setmetatable then
if not compat_file_meta_loaded then
local file_meta = gmt(fd)
load_compat_file_meta(file_meta)
end
debug_setmetatable(fd, compat_file_meta)
end

return fd, err
end
end

end -- lua 5.1

-- further write should be forwarded to _G
Expand Down
26 changes: 15 additions & 11 deletions tests/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ end
local V = _VERSION:gsub("^.*(%d+)%.(%d+)$", "%1%2")
if jit then V = "jit" end

local is_puclua51 = (_VERSION == "Lua 5.1" and not jit)

local mode = "global"
if arg[1] == "module" then
mode = "module"
Expand Down Expand Up @@ -588,17 +590,19 @@ do
io.input("data.txt")
print("io.read()", io.read("n", "number", "l", "a"))
io.input(io.stdin)
local f = assert(io.open("data.txt", "r"))
print("file:read()", f:read("*n", "*number", "*l", "*a"))
f:close()
f = assert(io.open("data.txt", "r"))
print("file:read()", f:read("n", "number", "l", "a"))
f:close()
os.remove("data.txt")

local g = assert(io.open("data.txt", "w"))
print("io.open => file:write()", type(g:write("hello")))
g:close()
if not is_puclua51 then
local f = assert(io.open("data.txt", "r"))
print("file:read()", f:read("*n", "*number", "*l", "*a"))
f:close()
f = assert(io.open("data.txt", "r"))
print("file:read()", f:read("n", "number", "l", "a"))
f:close()
os.remove("data.txt")

local g = assert(io.open("data.txt", "w"))
print("io.open => file:write()", type(g:write("hello")))
g:close()
end
os.remove("data.txt")
end

Expand Down

0 comments on commit 2cdfd42

Please sign in to comment.