Comparison

util/envload.lua @ 5020:ef1eb65acbba

util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 07 Jun 2012 23:27:26 +0200
child 7728:da54ad64f6da
comparison
equal deleted inserted replaced
4923:760a1f367f02 5020:ef1eb65acbba
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
8 local load, loadstring, loadfile, setfenv = load, loadstring, loadfile, setfenv;
9 local envload;
10 local envloadfile;
11
12 if setfenv then
13 function envload(code, source, env)
14 local f, err = loadstring(code, source);
15 if f and env then setfenv(f, env); end
16 return f, err;
17 end
18
19 function envloadfile(file, env)
20 local f, err = loadfile(file);
21 if f and env then setfenv(f, env); end
22 return f, err;
23 end
24 else
25 function envload(code, source, env)
26 return load(code, source, nil, env);
27 end
28
29 function envloadfile(file, env)
30 return loadfile(file, nil, env);
31 end
32 end
33
34 return { envload = envload, envloadfile = envloadfile };