diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2025-01-23 10:24:49 +0100 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2025-01-23 10:24:49 +0100 |
commit | 4f445bfaf6d970d7df1479b960fa4f2bac63047c (patch) | |
tree | b5c6f66070d311b55f7071a191da34100d471461 | |
parent | 64ed183ab243cdc737203e835892b4e6cb446e46 (diff) | |
download | lua-compat-5.3-4f445bfaf6d970d7df1479b960fa4f2bac63047c.tar.gz lua-compat-5.3-4f445bfaf6d970d7df1479b960fa4f2bac63047c.tar.bz2 lua-compat-5.3-4f445bfaf6d970d7df1479b960fa4f2bac63047c.zip |
prevent loops when loading files within require
-rw-r--r-- | compat53/module.lua | 63 |
1 files changed, 22 insertions, 41 deletions
diff --git a/compat53/module.lua b/compat53/module.lua index d514413..52b1dd6 100644 --- a/compat53/module.lua +++ b/compat53/module.lua | |||
@@ -828,7 +828,7 @@ if lua_version < "5.3" then | |||
828 | 828 | ||
829 | if is_luajit then | 829 | if is_luajit then |
830 | local compat_file_meta = {} | 830 | local compat_file_meta = {} |
831 | local compat_file_meta_loaded = false | 831 | local compat_file_meta_loaded = 0 |
832 | 832 | ||
833 | local function load_compat_file_meta(file_meta) | 833 | local function load_compat_file_meta(file_meta) |
834 | -- fill compat_file_meta with original entries | 834 | -- fill compat_file_meta with original entries |
@@ -840,62 +840,43 @@ if lua_version < "5.3" then | |||
840 | compat_file_meta.__index[k] = v | 840 | compat_file_meta.__index[k] = v |
841 | end | 841 | end |
842 | 842 | ||
843 | compat_file_meta_loaded = 1 | ||
844 | |||
843 | -- update it with compatibility functions | 845 | -- update it with compatibility functions |
844 | local file_mt = require("compat53.file_mt") | 846 | local file_mt_ok, file_mt = pcall(require, "compat53.file_mt") |
845 | file_mt.update_file_meta(compat_file_meta, is_luajit52) | 847 | if file_mt_ok then |
848 | file_mt.update_file_meta(compat_file_meta, is_luajit52) | ||
846 | 849 | ||
847 | compat_file_meta_loaded = true | 850 | compat_file_meta_loaded = 2 |
851 | end | ||
848 | end | 852 | end |
849 | 853 | ||
850 | function M.io.open(...) | 854 | local function return_fd(fd, err, code) |
851 | local fd, err, code = io_open(...) | 855 | if not fd then |
856 | return fd, err, code | ||
857 | end | ||
852 | if fd and debug_setmetatable then | 858 | if fd and debug_setmetatable then |
853 | if not compat_file_meta_loaded then | 859 | if compat_file_meta_loaded == 0 then |
854 | local file_meta = gmt(fd) | 860 | local file_meta = gmt(fd) |
855 | load_compat_file_meta(file_meta) | 861 | load_compat_file_meta(file_meta) |
856 | end | 862 | end |
857 | debug_setmetatable(fd, compat_file_meta) | 863 | if compat_file_meta_loaded == 2 then |
864 | debug_setmetatable(fd, compat_file_meta) | ||
865 | end | ||
858 | end | 866 | end |
867 | return fd | ||
868 | end | ||
859 | 869 | ||
860 | if fd then | 870 | function M.io.open(...) |
861 | return fd | 871 | return return_fd(io_open(...)) |
862 | else | ||
863 | return fd, err, code | ||
864 | end | ||
865 | end | 872 | end |
866 | 873 | ||
867 | function M.io.popen(...) | 874 | function M.io.popen(...) |
868 | local fd, err, code = io_popen(...) | 875 | return return_fd(io_popen(...)) |
869 | if fd and debug_setmetatable then | ||
870 | if not compat_file_meta_loaded then | ||
871 | local file_meta = gmt(fd) | ||
872 | load_compat_file_meta(file_meta) | ||
873 | end | ||
874 | debug_setmetatable(fd, compat_file_meta) | ||
875 | end | ||
876 | |||
877 | if fd then | ||
878 | return fd | ||
879 | else | ||
880 | return fd, err, code | ||
881 | end | ||
882 | end | 876 | end |
883 | 877 | ||
884 | function M.io.tmpfile(...) | 878 | function M.io.tmpfile(...) |
885 | local fd, err, code = io_tmpfile(...) | 879 | return return_fd(io_tmpfile(...)) |
886 | if fd and debug_setmetatable then | ||
887 | if not compat_file_meta_loaded then | ||
888 | local file_meta = gmt(fd) | ||
889 | load_compat_file_meta(file_meta) | ||
890 | end | ||
891 | debug_setmetatable(fd, compat_file_meta) | ||
892 | end | ||
893 | |||
894 | if fd then | ||
895 | return fd | ||
896 | else | ||
897 | return fd, err, code | ||
898 | end | ||
899 | end | 880 | end |
900 | end | 881 | end |
901 | 882 | ||