Mercurial > prosody-hg
comparison util/envload.lua @ 7924:8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 01 Mar 2017 16:42:02 +0100 |
| parents | da54ad64f6da |
| children | 5dec27760ecd |
comparison
equal
deleted
inserted
replaced
| 7922:2fd20f372cb1 | 7924:8487fe9fc335 |
|---|---|
| 4 -- This project is MIT/X11 licensed. Please see the | 4 -- This project is MIT/X11 licensed. Please see the |
| 5 -- COPYING file in the source package for more information. | 5 -- COPYING file in the source package for more information. |
| 6 -- | 6 -- |
| 7 -- luacheck: ignore 113/setfenv | 7 -- luacheck: ignore 113/setfenv |
| 8 | 8 |
| 9 local load, loadstring, loadfile, setfenv = load, loadstring, loadfile, setfenv; | 9 local load, loadstring, setfenv = load, loadstring, setfenv; |
| 10 local io_open = io.open; | |
| 10 local envload; | 11 local envload; |
| 11 local envloadfile; | 12 local envloadfile; |
| 12 | 13 |
| 13 if setfenv then | 14 if setfenv then |
| 14 function envload(code, source, env) | 15 function envload(code, source, env) |
| 16 if f and env then setfenv(f, env); end | 17 if f and env then setfenv(f, env); end |
| 17 return f, err; | 18 return f, err; |
| 18 end | 19 end |
| 19 | 20 |
| 20 function envloadfile(file, env) | 21 function envloadfile(file, env) |
| 21 local f, err = loadfile(file); | 22 local fh, err, errno = io_open(file); |
| 23 if not fh then return fh, err, errno; end | |
| 24 local f, err = load(function () return fh:read(2048); end, "@"..file); | |
| 22 if f and env then setfenv(f, env); end | 25 if f and env then setfenv(f, env); end |
| 23 return f, err; | 26 return f, err; |
| 24 end | 27 end |
| 25 else | 28 else |
| 26 function envload(code, source, env) | 29 function envload(code, source, env) |
| 27 return load(code, source, nil, env); | 30 return load(code, source, nil, env); |
| 28 end | 31 end |
| 29 | 32 |
| 30 function envloadfile(file, env) | 33 function envloadfile(file, env) |
| 31 return loadfile(file, nil, env); | 34 local fh, err, errno = io_open(file); |
| 35 if not fh then return fh, err, errno; end | |
| 36 return load(fh:lines(2048), "@"..file, nil, env); | |
| 32 end | 37 end |
| 33 end | 38 end |
| 34 | 39 |
| 35 return { envload = envload, envloadfile = envloadfile }; | 40 return { envload = envload, envloadfile = envloadfile }; |
