Software /
code /
prosody
Annotate
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 |
parent | 7728:da54ad64f6da |
child | 7930:5dec27760ecd |
rev | line source |
---|---|
5020
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
1 -- Prosody IM |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2011 Florian Zeitz |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
3 -- |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
6 -- |
7728
da54ad64f6da
util.envload: Ignore use of Lua 5.1-only setfenv function [luacheck]
Kim Alvefur <zash@zash.se>
parents:
5020
diff
changeset
|
7 -- luacheck: ignore 113/setfenv |
5020
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
8 |
7924
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
9 local load, loadstring, setfenv = load, loadstring, setfenv; |
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
10 local io_open = io.open; |
5020
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
11 local envload; |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
12 local envloadfile; |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
13 |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
14 if setfenv then |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
15 function envload(code, source, env) |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
16 local f, err = loadstring(code, source); |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
17 if f and env then setfenv(f, env); end |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
18 return f, err; |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
19 end |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
20 |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
21 function envloadfile(file, env) |
7924
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
22 local fh, err, errno = io_open(file); |
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
23 if not fh then return fh, err, errno; end |
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
24 local f, err = load(function () return fh:read(2048); end, "@"..file); |
5020
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
25 if f and env then setfenv(f, env); end |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
26 return f, err; |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
27 end |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
28 else |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
29 function envload(code, source, env) |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
30 return load(code, source, nil, env); |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
31 end |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
32 |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
33 function envloadfile(file, env) |
7924
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
34 local fh, err, errno = io_open(file); |
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
35 if not fh then return fh, err, errno; end |
8487fe9fc335
util.envload: Open file here instead of letting loadfile do it so that all return values from io.open can be collected
Kim Alvefur <zash@zash.se>
parents:
7728
diff
changeset
|
36 return load(fh:lines(2048), "@"..file, nil, env); |
5020
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
37 end |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
38 end |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
39 |
ef1eb65acbba
util.envload: New module to abstract Lua 5.1's setfenv and Lua 5.2's load
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
40 return { envload = envload, envloadfile = envloadfile }; |