3 -- Copyright Kepler Project 2004-2006 (http://www.keplerproject.org/compat)
4 -- According to Lua 5.1
5 -- $Id: compat-5.1.lua,v 1.22 2006/02/20 21:12:47 carregal Exp $
7 -- Compat-5.1 is free software: it can be used for both academic and commercial
8 -- purposes at absolutely no cost. There are no royalties or GNU-like
9 -- "copyleft" restrictions. Compat-5.1 qualifies as Open Source software. Its
10 -- licenses are compatible with GPL. Compat-5.1 is not in the public domain and
11 -- the Kepler Project keep its copyright. The legal details are below.
13 -- The spirit of the license is that you are free to use Compat-5.1 for any
14 -- purpose at no cost without having to ask us. The only requirement is that if
15 -- you do use Compat-5.1, then you should give us credit by including the
16 -- appropriate copyright notice somewhere in your product or its documentation.
18 -- The Compat-5.1 library is designed and implemented by Roberto Ierusalimschy,
19 -- Diego Nehab, André Carregal and Tomás Guisasola. The implementation is not
20 -- derived from licensed software.
23 _COMPAT51 = "Compat-5.1 R5"
25 local LUA_DIRSEP = '/'
27 local OLD_LUA_OFSEP = ''
28 local POF = 'luaopen_'
29 local LUA_PATH_MARK = '?'
30 local LUA_IGMARK = ':'
32 local assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type = assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type
33 local find, format, gfind, gsub, sub = string.find, string.format, string.gfind, string.gsub, string.sub
36 -- avoid overwriting the package table if it's already there
38 package = package or {}
39 local _PACKAGE = package
41 package.path = LUA_PATH or os.getenv("LUA_PATH") or
43 "/usr/local/share/lua/5.0/?.lua;" ..
44 "/usr/local/share/lua/5.0/?/?.lua;" ..
45 "/usr/local/share/lua/5.0/?/init.lua" )
47 package.cpath = LUA_CPATH or os.getenv("LUA_CPATH") or
50 "/usr/local/lib/lua/5.0/?.so;" ..
51 "/usr/local/lib/lua/5.0/l?.so"
54 -- make sure require works with standard libraries
56 package.loaded = package.loaded or {}
57 package.loaded.debug = debug
58 package.loaded.string = string
59 package.loaded.math = math
60 package.loaded.io = io
61 package.loaded.os = os
62 package.loaded.table = table
63 package.loaded.base = _G
64 package.loaded.coroutine = coroutine
65 local _LOADED = package.loaded
68 -- avoid overwriting the package.preload table if it's already there
70 package.preload = package.preload or {}
71 local _PRELOAD = package.preload
75 -- looks for a file `name' in given path
77 local function findfile (name, pname)
78 name = gsub (name, "%.", LUA_DIRSEP)
79 local path = _PACKAGE[pname]
80 assert (type(path) == "string", format ("package.%s must be a string", pname))
81 for c in gfind (path, "[^;]+") do
82 c = gsub (c, "%"..LUA_PATH_MARK, name)
89 return nil -- not found
94 -- check whether library is already loaded
96 local function loader_preload (name)
97 assert (type(name) == "string", format (
98 "bad argument #1 to `require' (string expected, got %s)", type(name)))
99 assert (type(_PRELOAD) == "table", "`package.preload' must be a table")
100 return _PRELOAD[name]
105 -- Lua library loader
107 local function loader_Lua (name)
108 assert (type(name) == "string", format (
109 "bad argument #1 to `require' (string expected, got %s)", type(name)))
110 local filename = findfile (name, "path")
114 local f, err = loadfile (filename)
116 error (format ("error loading module `%s' (%s)", name, err))
122 local function mkfuncname (name)
123 name = gsub (name, "^.*%"..LUA_IGMARK, "")
124 name = gsub (name, "%.", LUA_OFSEP)
128 local function old_mkfuncname (name)
129 --name = gsub (name, "^.*%"..LUA_IGMARK, "")
130 name = gsub (name, "%.", OLD_LUA_OFSEP)
137 local function loader_C (name)
138 assert (type(name) == "string", format (
139 "bad argument #1 to `require' (string expected, got %s)", type(name)))
140 local filename = findfile (name, "cpath")
144 local funcname = mkfuncname (name)
145 local f, err = loadlib (filename, funcname)
147 funcname = old_mkfuncname (name)
148 f, err = loadlib (filename, funcname)
150 error (format ("error loading module `%s' (%s)", name, err))
157 local function loader_Croot (name)
158 local p = gsub (name, "^([^.]*).-$", "%1")
162 local filename = findfile (p, "cpath")
166 local funcname = mkfuncname (name)
167 local f, err, where = loadlib (filename, funcname)
170 elseif where ~= "init" then
171 error (format ("error loading module `%s' (%s)", name, err))
175 -- create `loaders' table
176 package.loaders = package.loaders or { loader_preload, loader_Lua, loader_C, loader_Croot, }
177 local _LOADERS = package.loaders
181 -- iterate over available loaders
183 local function load (name, loaders)
184 -- iterate over available loaders
185 assert (type (loaders) == "table", "`package.loaders' must be a table")
186 for i, loader in ipairs (loaders) do
187 local f = loader (name)
192 error (format ("module `%s' not found", name))
196 local sentinel = function () end
201 function _G.require (modname)
202 assert (type(modname) == "string", format (
203 "bad argument #1 to `require' (string expected, got %s)", type(name)))
204 local p = _LOADED[modname]
205 if p then -- is it there?
206 if p == sentinel then
207 error (format ("loop or previous error loading module '%s'", modname))
209 return p -- package is already loaded
211 local init = load (modname, _LOADERS)
212 _LOADED[modname] = sentinel
213 local actual_arg = _G.arg
215 local res = init (modname)
217 _LOADED[modname] = res
220 if _LOADED[modname] == sentinel then
221 _LOADED[modname] = true
223 return _LOADED[modname]
228 local function findtable (t, f)
229 assert (type(f)=="string", "not a valid field name ("..tostring(f)..")")
231 local ok, e, w = find (ff, '(.-)%.', 1)
233 local nt = rawget (t, w)
237 elseif type(t) ~= "table" then
241 ok, e, w = find (ff, '(.-)%.', e+1)
247 -- new package.seeall function
249 function _PACKAGE.seeall (module)
250 local t = type(module)
251 assert (t == "table", "bad argument #1 to package.seeall (table expected, got "..t..")")
252 local meta = getmetatable (module)
255 setmetatable (module, meta)
262 -- new module function
264 function _G.module (modname, ...)
265 local ns = _LOADED[modname]
266 if type(ns) ~= "table" then
267 ns = findtable (_G, modname)
269 error (string.format ("name conflict for module '%s'", modname))
271 _LOADED[modname] = ns
276 ns._PACKAGE = gsub (modname, "[^.]*$", "")
279 for i, f in ipairs (arg) do