diff options
| author | mpeterv <mpeterval@gmail.com> | 2015-03-19 12:22:02 +0300 |
|---|---|---|
| committer | mpeterv <mpeterval@gmail.com> | 2015-03-19 13:31:48 +0300 |
| commit | 3c7c4722a5ab6aa32f62622643a5b68fa1e5a7d2 (patch) | |
| tree | 5ae86d59eb90a64b3fe98a99fde24c76ad121d20 /src | |
| parent | 603b0ea346d050bef2ced801ec8818773aae0fa0 (diff) | |
| download | luarocks-3c7c4722a5ab6aa32f62622643a5b68fa1e5a7d2.tar.gz luarocks-3c7c4722a5ab6aa32f62622643a5b68fa1e5a7d2.tar.bz2 luarocks-3c7c4722a5ab6aa32f62622643a5b68fa1e5a7d2.zip | |
Refactor persist.load_into_table
* Add docstring for run_file helper function;
* Do not read file twice on Lua > 5.1, use load instead of loadfile;
* Update docstring for persist.load_into_table.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/persist.lua | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/luarocks/persist.lua b/src/luarocks/persist.lua index 9a40785c..8162c662 100644 --- a/src/luarocks/persist.lua +++ b/src/luarocks/persist.lua | |||
| @@ -9,8 +9,14 @@ package.loaded["luarocks.persist"] = persist | |||
| 9 | 9 | ||
| 10 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
| 11 | 11 | ||
| 12 | --- Load and run a Lua file in an environment. | ||
| 13 | -- @param filename string: the name of the file. | ||
| 14 | -- @param env table: the environment table. | ||
| 15 | -- @return (true, any) or (nil, string, string): true and the return value | ||
| 16 | -- of the file, or nil, an error message and an error code ("open", "load" | ||
| 17 | -- or "run") in case of errors. | ||
| 12 | local function run_file(filename, env) | 18 | local function run_file(filename, env) |
| 13 | local fd, err, errno = io.open(filename) | 19 | local fd, err = io.open(filename) |
| 14 | if not fd then | 20 | if not fd then |
| 15 | return nil, err, "open" | 21 | return nil, err, "open" |
| 16 | end | 22 | end |
| @@ -28,7 +34,7 @@ local function run_file(filename, env) | |||
| 28 | ran, err = pcall(chunk) | 34 | ran, err = pcall(chunk) |
| 29 | end | 35 | end |
| 30 | else -- Lua 5.2 | 36 | else -- Lua 5.2 |
| 31 | chunk, err = loadfile(filename, "t", env) | 37 | chunk, err = load(str, filename, "t", env) |
| 32 | if chunk then | 38 | if chunk then |
| 33 | ran, err = pcall(chunk) | 39 | ran, err = pcall(chunk) |
| 34 | end | 40 | end |
| @@ -47,9 +53,10 @@ end | |||
| 47 | -- @param filename string: the name of the file. | 53 | -- @param filename string: the name of the file. |
| 48 | -- @param tbl table or nil: if given, this table is used to store | 54 | -- @param tbl table or nil: if given, this table is used to store |
| 49 | -- loaded values. | 55 | -- loaded values. |
| 50 | -- @return table or (nil, string, string): a table with the file's assignments | 56 | -- @return (table, table) or (nil, string, string): a table with the file's |
| 51 | -- as fields, or nil, an error message and an error code ("load" or "run") | 57 | -- assignments as fields and set of undefined globals accessed in file, |
| 52 | -- in case of errors. | 58 | -- or nil, an error message and an error code ("open", "load" or "run") in |
| 59 | -- case of errors. | ||
| 53 | function persist.load_into_table(filename, tbl) | 60 | function persist.load_into_table(filename, tbl) |
| 54 | assert(type(filename) == "string") | 61 | assert(type(filename) == "string") |
| 55 | assert(type(tbl) == "table" or not tbl) | 62 | assert(type(tbl) == "table" or not tbl) |
| @@ -57,9 +64,8 @@ function persist.load_into_table(filename, tbl) | |||
| 57 | local result = tbl or {} | 64 | local result = tbl or {} |
| 58 | local globals = {} | 65 | local globals = {} |
| 59 | local globals_mt = { | 66 | local globals_mt = { |
| 60 | __index = function(t, n) | 67 | __index = function(t, k) |
| 61 | globals[n] = true | 68 | globals[k] = true |
| 62 | return rawget(t, n) | ||
| 63 | end | 69 | end |
| 64 | } | 70 | } |
| 65 | local save_mt = getmetatable(result) | 71 | local save_mt = getmetatable(result) |
