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/module.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/module.lua')
| -rw-r--r-- | compat53/module.lua | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/compat53/module.lua b/compat53/module.lua index 67b2a4a..90b2f06 100644 --- a/compat53/module.lua +++ b/compat53/module.lua | |||
| @@ -13,7 +13,11 @@ if lua_version < "5.3" then | |||
| 13 | debug, io, math, package, string, table | 13 | debug, io, math, package, string, table |
| 14 | local io_lines = io.lines | 14 | local io_lines = io.lines |
| 15 | local io_read = io.read | 15 | local io_read = io.read |
| 16 | local io_open = io.open | ||
| 17 | local io_popen = io.popen | ||
| 18 | local io_tmpfile = io.tmpfile | ||
| 16 | local unpack = lua_version == "5.1" and unpack or table.unpack | 19 | local unpack = lua_version == "5.1" and unpack or table.unpack |
| 20 | local debug_setmetatable = type(debug) == "table" and debug.setmetatable | ||
| 17 | 21 | ||
| 18 | -- create module table | 22 | -- create module table |
| 19 | M = {} | 23 | M = {} |
| @@ -451,7 +455,6 @@ if lua_version < "5.3" then | |||
| 451 | if type(debug) == "table" then | 455 | if type(debug) == "table" then |
| 452 | local debug_setfenv = debug.setfenv | 456 | local debug_setfenv = debug.setfenv |
| 453 | local debug_getfenv = debug.getfenv | 457 | local debug_getfenv = debug.getfenv |
| 454 | local debug_setmetatable = debug.setmetatable | ||
| 455 | 458 | ||
| 456 | M.debug = setmetatable({}, { __index = debug }) | 459 | M.debug = setmetatable({}, { __index = debug }) |
| 457 | 460 | ||
| @@ -823,6 +826,67 @@ if lua_version < "5.3" then | |||
| 823 | end | 826 | end |
| 824 | end -- not luajit | 827 | end -- not luajit |
| 825 | 828 | ||
| 829 | if is_luajit then | ||
| 830 | local compat_file_meta = {} | ||
| 831 | local compat_file_meta_loaded = false | ||
| 832 | |||
| 833 | local function load_compat_file_meta(file_meta) | ||
| 834 | -- fill compat_file_meta with original entries | ||
| 835 | for k, v in pairs(file_meta) do | ||
| 836 | compat_file_meta[k] = v | ||
| 837 | end | ||
| 838 | compat_file_meta.__index = {} | ||
| 839 | for k, v in pairs(file_meta.__index) do | ||
| 840 | compat_file_meta.__index[k] = v | ||
| 841 | end | ||
| 842 | |||
| 843 | -- update it with compatibility functions | ||
| 844 | local file_mt = require("compat53.file_mt") | ||
| 845 | file_mt.update_file_meta(compat_file_meta, is_luajit52) | ||
| 846 | |||
| 847 | compat_file_meta_loaded = true | ||
| 848 | end | ||
| 849 | |||
| 850 | function M.io.open(...) | ||
| 851 | local fd, err = io_open(...) | ||
| 852 | if fd and debug_setmetatable then | ||
| 853 | if not compat_file_meta_loaded then | ||
| 854 | local file_meta = gmt(fd) | ||
| 855 | load_compat_file_meta(file_meta) | ||
| 856 | end | ||
| 857 | debug_setmetatable(fd, compat_file_meta) | ||
| 858 | end | ||
| 859 | |||
| 860 | return fd, err | ||
| 861 | end | ||
| 862 | |||
| 863 | function M.io.popen(...) | ||
| 864 | local fd, err = io_popen(...) | ||
| 865 | if fd and debug_setmetatable then | ||
| 866 | if not compat_file_meta_loaded then | ||
| 867 | local file_meta = gmt(fd) | ||
| 868 | load_compat_file_meta(file_meta) | ||
| 869 | end | ||
| 870 | debug_setmetatable(fd, compat_file_meta) | ||
| 871 | end | ||
| 872 | |||
| 873 | return fd, err | ||
| 874 | end | ||
| 875 | |||
| 876 | function M.io.tmpfile(...) | ||
| 877 | local fd, err = io_tmpfile(...) | ||
| 878 | if fd and debug_setmetatable then | ||
| 879 | if not compat_file_meta_loaded then | ||
| 880 | local file_meta = gmt(fd) | ||
| 881 | load_compat_file_meta(file_meta) | ||
| 882 | end | ||
| 883 | debug_setmetatable(fd, compat_file_meta) | ||
| 884 | end | ||
| 885 | |||
| 886 | return fd, err | ||
| 887 | end | ||
| 888 | end | ||
| 889 | |||
| 826 | end -- lua 5.1 | 890 | end -- lua 5.1 |
| 827 | 891 | ||
| 828 | -- further write should be forwarded to _G | 892 | -- further write should be forwarded to _G |
