util.encodings: Spell out all IDNA 2008 options ICU has
[prosody.git] / util / envload.lua
blob6182a1f9001d0100723c84e26991c77ce09587b9
1 -- Prosody IM
2 -- Copyright (C) 2008-2011 Florian Zeitz
3 --
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
6 --
7 -- luacheck: ignore 113/setfenv 113/loadstring
9 local load, loadstring, setfenv = load, loadstring, setfenv;
10 local io_open = io.open;
11 local envload;
12 local envloadfile;
14 if setfenv then
15 function envload(code, source, env)
16 local f, err = loadstring(code, source);
17 if f and env then setfenv(f, env); end
18 return f, err;
19 end
21 function envloadfile(file, env)
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);
25 fh:close();
26 if f and env then setfenv(f, env); end
27 return f, err;
28 end
29 else
30 function envload(code, source, env)
31 return load(code, source, nil, env);
32 end
34 function envloadfile(file, env)
35 local fh, err, errno = io_open(file);
36 if not fh then return fh, err, errno; end
37 local f, err = load(fh:lines(2048), "@"..file, nil, env);
38 fh:close();
39 return f, err;
40 end
41 end
43 return { envload = envload, envloadfile = envloadfile };