forked from andrew-d/lua-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
35 lines (29 loc) · 881 Bytes
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--- Miscellaneous utility functions.
-- @module util
-- Our global environment.
local P = {}
-- Import section:
-- We declare everything this package needs from "outside" here.
local require = require
local pcall = pcall
-- No more external access after this point.
if platform.lua_version == '5.2' then
_ENV = P
else
setfenv(1, P)
end
-------------------------------------------------------------------------------
-- Requires a module, returning nil if it doesn't exist. Note that this
-- differs from the regular `require()` function, which will raise an error if
-- the given module does not exist.
-- @param ... The argument(s) to pass to `require()`
-- @return The module's table, or nil if it doesn't exist
function safe_require(...)
local err, ret = pcall(require, ...)
if err then
return ret
else
return nil
end
end
return P