From 7a82c38437075cb2beb2fe7c6453aa91e019d6b7 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 29 Aug 2024 17:16:49 -0300 Subject: adjust file metatables even in compat53.module mode (#67) * adjust file metatables even in compat53.module mode * apply tweaks only to LuaJIT; file:write() only to compat=none --- compat53/init.lua | 62 +++++++------------------------------------------------ 1 file changed, 7 insertions(+), 55 deletions(-) (limited to 'compat53/init.lua') diff --git a/compat53/init.lua b/compat53/init.lua index a7f0c80..b507571 100644 --- a/compat53/init.lua +++ b/compat53/init.lua @@ -17,57 +17,15 @@ if lua_version < "5.3" then local file_meta = gmt(io.stdout) - -- make '*' optional for file:read and file:lines - if type(file_meta) == "table" and type(file_meta.__index) == "table" then - - local function addasterisk(fmt) - if type(fmt) == "string" and fmt:sub(1, 1) ~= "*" then - return "*"..fmt - else - return fmt - end - end - - local file_lines = file_meta.__index.lines - file_meta.__index.lines = function(self, ...) - local n = select('#', ...) - for i = 1, n do - local a = select(i, ...) - local b = addasterisk(a) - -- as an optimization we only allocate a table for the - -- modified format arguments when we have a '*' somewhere - if a ~= b then - local args = { ... } - args[i] = b - for j = i+1, n do - args[j] = addasterisk(args[j]) - end - return file_lines(self, unpack(args, 1, n)) - end - end - return file_lines(self, ...) - end + -- 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 - local file_read = file_meta.__index.read - file_meta.__index.read = function(self, ...) - local n = select('#', ...) - for i = 1, n do - local a = select(i, ...) - local b = addasterisk(a) - -- as an optimization we only allocate a table for the - -- modified format arguments when we have a '*' somewhere - if a ~= b then - local args = { ... } - args[i] = b - for j = i+1, n do - args[j] = addasterisk(args[j]) - end - return file_read(self, unpack(args, 1, n)) - end - end - return file_read(self, ...) - end + 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 @@ -85,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, { -- cgit v1.2.3-55-g6feb