Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/lualib/package.lua

raw symbols SCIP
1 --[[
2 -- A package library similar to the one that comes with Lua 5.1, but without
3 -- the local filesystem access. Based on Compat-5.1 which comes with the
4 -- following license notice:
5 --
6 -- Copyright © 2004-2006 The Kepler Project.
7 --
8 -- Permission is hereby granted, free of charge, to any person obtaining a copy of
9 -- this software and associated documentation files (the "Software"), to deal in
10 -- the Software without restriction, including without limitation the rights to
11 -- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12 -- the Software, and to permit persons to whom the Software is furnished to do so,
13 -- subject to the following conditions:
14 --
15 -- The above copyright notice and this permission notice shall be included in all
16 -- copies or substantial portions of the Software.
17 --
18 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20 -- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21 -- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22 -- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 --]]
25
26
27 local assert, error, ipairs, setmetatable, type = assert, error, ipairs, setmetatable, type
28 local format = string.format
29
30 --
31 -- avoid overwriting the package table if it's already there
32 --
33 package = package or {}
34 local _PACKAGE = package
35
36 package.loaded = package.loaded or {}
37 package.loaded.debug = debug
38 package.loaded.string = string
39 package.loaded.math = math
40 package.loaded.io = io
41 package.loaded.os = os
42 package.loaded.table = table
43 package.loaded._G = _G
44 package.loaded.coroutine = coroutine
45 package.loaded.package = package
46 local _LOADED = package.loaded
47
48 --
49 -- avoid overwriting the package.preload table if it's already there
50 --
51 package.preload = package.preload or {}
52 local _PRELOAD = package.preload
53
54 --
55 -- check whether library is already loaded
56 --
57 local function loader_preload (name)
58 assert (type(name) == "string", format (
59 "bad argument #1 to 'require' (string expected, got %s)", type(name)))
60 assert (type(_PRELOAD) == "table", "'package.preload' must be a table")
61 return _PRELOAD[name]
62 end
63
64 -- create 'loaders' table
65 package.loaders = package.loaders or { loader_preload }
66 local _LOADERS = package.loaders
67
68 --
69 -- iterate over available loaders
70 --
71 local function load (name, loaders)
72 -- iterate over available loaders
73 assert (type (loaders) == "table", "'package.loaders' must be a table")
74 for i, loader in ipairs (loaders) do
75 local f = loader (name)
76 if f then
77 return f
78 end
79 end
80 error (format ("module '%s' not found", name))
81 end
82
83 -- sentinel
84 local sentinel = function () end
85
86 --
87 -- require
88 --
89 function _G.require (modname)
90 assert (type(modname) == "string", format (
91 "bad argument #1 to 'require' (string expected, got %s)", type(modname)))
92 local p = _LOADED[modname]
93 if p ~= nil then -- is it there?
94 if p == sentinel then
95 error (format ("loop or previous error loading module '%s'", modname))
96 end
97 return p -- package is already loaded
98 end
99 local init = load (modname, _LOADERS)
100 _LOADED[modname] = sentinel
101 local actual_arg = _G.arg
102 _G.arg = { modname }
103 local res = init (modname)
104 if res ~= nil then
105 _LOADED[modname] = res
106 end
107 _G.arg = actual_arg
108 if _LOADED[modname] == sentinel then
109 _LOADED[modname] = true
110 end
111 return _LOADED[modname]
112 end
113
114
115 --
116 -- package.seeall function
117 --
118 function _PACKAGE.seeall (module)
119 local t = type(module)
120 assert (t == "table", "bad argument #1 to package.seeall (table expected, got "..t..")")
121 local meta = getmetatable (module)
122 if not meta then
123 meta = {}
124 setmetatable (module, meta)
125 end
126 meta.__index = _G
127 end