diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2024-08-29 17:16:49 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-29 17:16:49 -0300 |
| commit | 7a82c38437075cb2beb2fe7c6453aa91e019d6b7 (patch) | |
| tree | 4b77c7e02eb1e6e5f2b875f7a5947b4fdf23c0f5 /compat53/init.lua | |
| parent | 1c679a282bf1255cddfbb6b3e8c5c72d9ebe57e8 (diff) | |
| download | lua-compat-5.3-7a82c38437075cb2beb2fe7c6453aa91e019d6b7.tar.gz lua-compat-5.3-7a82c38437075cb2beb2fe7c6453aa91e019d6b7.tar.bz2 lua-compat-5.3-7a82c38437075cb2beb2fe7c6453aa91e019d6b7.zip | |
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
Diffstat (limited to 'compat53/init.lua')
| -rw-r--r-- | compat53/init.lua | 62 |
1 files changed, 7 insertions, 55 deletions
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 | |||
| 17 | local file_meta = gmt(io.stdout) | 17 | local file_meta = gmt(io.stdout) |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | -- make '*' optional for file:read and file:lines | 20 | -- detect LuaJIT (including LUAJIT_ENABLE_LUA52COMPAT compilation flag) |
| 21 | if type(file_meta) == "table" and type(file_meta.__index) == "table" then | 21 | local is_luajit = (string.dump(function() end) or ""):sub(1, 3) == "\027LJ" |
| 22 | 22 | local is_luajit52 = is_luajit and | |
| 23 | local function addasterisk(fmt) | 23 | #setmetatable({}, { __len = function() return 1 end }) == 1 |
| 24 | if type(fmt) == "string" and fmt:sub(1, 1) ~= "*" then | ||
| 25 | return "*"..fmt | ||
| 26 | else | ||
| 27 | return fmt | ||
| 28 | end | ||
| 29 | end | ||
| 30 | |||
| 31 | local file_lines = file_meta.__index.lines | ||
| 32 | file_meta.__index.lines = function(self, ...) | ||
| 33 | local n = select('#', ...) | ||
| 34 | for i = 1, n do | ||
| 35 | local a = select(i, ...) | ||
| 36 | local b = addasterisk(a) | ||
| 37 | -- as an optimization we only allocate a table for the | ||
| 38 | -- modified format arguments when we have a '*' somewhere | ||
| 39 | if a ~= b then | ||
| 40 | local args = { ... } | ||
| 41 | args[i] = b | ||
| 42 | for j = i+1, n do | ||
| 43 | args[j] = addasterisk(args[j]) | ||
| 44 | end | ||
| 45 | return file_lines(self, unpack(args, 1, n)) | ||
| 46 | end | ||
| 47 | end | ||
| 48 | return file_lines(self, ...) | ||
| 49 | end | ||
| 50 | 24 | ||
| 51 | local file_read = file_meta.__index.read | ||
| 52 | file_meta.__index.read = function(self, ...) | ||
| 53 | local n = select('#', ...) | ||
| 54 | for i = 1, n do | ||
| 55 | local a = select(i, ...) | ||
| 56 | local b = addasterisk(a) | ||
| 57 | -- as an optimization we only allocate a table for the | ||
| 58 | -- modified format arguments when we have a '*' somewhere | ||
| 59 | if a ~= b then | ||
| 60 | local args = { ... } | ||
| 61 | args[i] = b | ||
| 62 | for j = i+1, n do | ||
| 63 | args[j] = addasterisk(args[j]) | ||
| 64 | end | ||
| 65 | return file_read(self, unpack(args, 1, n)) | ||
| 66 | end | ||
| 67 | end | ||
| 68 | return file_read(self, ...) | ||
| 69 | end | ||
| 70 | 25 | ||
| 26 | if type(file_meta) == "table" and type(file_meta.__index) == "table" then | ||
| 27 | local file_mt = require("compat53.file_mt") | ||
| 28 | file_mt.update_file_meta(file_meta, is_luajit52) | ||
| 71 | end -- got a valid metatable for file objects | 29 | end -- got a valid metatable for file objects |
| 72 | 30 | ||
| 73 | 31 | ||
| @@ -85,12 +43,6 @@ if lua_version < "5.3" then | |||
| 85 | local io_type = io.type | 43 | local io_type = io.type |
| 86 | 44 | ||
| 87 | 45 | ||
| 88 | -- detect LuaJIT (including LUAJIT_ENABLE_LUA52COMPAT compilation flag) | ||
| 89 | local is_luajit = (string.dump(function() end) or ""):sub(1, 3) == "\027LJ" | ||
| 90 | local is_luajit52 = is_luajit and | ||
| 91 | #setmetatable({}, { __len = function() return 1 end }) == 1 | ||
| 92 | |||
| 93 | |||
| 94 | -- make package.searchers available as an alias for package.loaders | 46 | -- make package.searchers available as an alias for package.loaders |
| 95 | local p_index = { searchers = package.loaders } | 47 | local p_index = { searchers = package.loaders } |
| 96 | setmetatable(package, { | 48 | setmetatable(package, { |
