aboutsummaryrefslogtreecommitdiffstats
path: root/util/envload.lua
diff options
context:
space:
mode:
authorFlorian Zeitz <florob@babelmonkeys.de>2012-06-07 23:27:26 +0200
committerFlorian Zeitz <florob@babelmonkeys.de>2012-06-07 23:27:26 +0200
commit217e6d011c8d8ae2c44034fc9138925d77c3c10b (patch)
tree9b7ea204ab65e50608bb2d926e87f536151a0b98 /util/envload.lua
parente6bf33e872084aafa5919a57cf81df6bd63e509c (diff)
downloadprosody-217e6d011c8d8ae2c44034fc9138925d77c3c10b.tar.gz
prosody-217e6d011c8d8ae2c44034fc9138925d77c3c10b.zip
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Diffstat (limited to 'util/envload.lua')
-rw-r--r--util/envload.lua34
1 files changed, 34 insertions, 0 deletions
diff --git a/util/envload.lua b/util/envload.lua
new file mode 100644
index 00000000..53e28348
--- /dev/null
+++ b/util/envload.lua
@@ -0,0 +1,34 @@
+-- Prosody IM
+-- Copyright (C) 2008-2011 Florian Zeitz
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local load, loadstring, loadfile, setfenv = load, loadstring, loadfile, setfenv;
+local envload;
+local envloadfile;
+
+if setfenv then
+ function envload(code, source, env)
+ local f, err = loadstring(code, source);
+ if f and env then setfenv(f, env); end
+ return f, err;
+ end
+
+ function envloadfile(file, env)
+ local f, err = loadfile(file);
+ if f and env then setfenv(f, env); end
+ return f, err;
+ end
+else
+ function envload(code, source, env)
+ return load(code, source, nil, env);
+ end
+
+ function envloadfile(file, env)
+ return loadfile(file, nil, env);
+ end
+end
+
+return { envload = envload, envloadfile = envloadfile };