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 $
8 _COMPAT51 = "Compat-5.1 R5"
10 local LUA_DIRSEP = '/'
12 local OLD_LUA_OFSEP = ''
13 local POF = 'luaopen_'
14 local LUA_PATH_MARK = '?'
15 local LUA_IGMARK = ':'
17 local assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type = assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type
18 local find, format, gfind, gsub, sub = string.find, string.format, string.gfind, string.gsub, string.sub
21 -- avoid overwriting the package table if it's already there
23 package = package or {}
24 local _PACKAGE = package
26 package.path = LUA_PATH or os.getenv("LUA_PATH") or
28 "/usr/local/share/lua/5.0/?.lua;" ..
29 "/usr/local/share/lua/5.0/?/?.lua;" ..
30 "/usr/local/share/lua/5.0/?/init.lua" )
32 package.cpath = LUA_CPATH or os.getenv("LUA_CPATH") or
35 "/usr/local/lib/lua/5.0/?.so;" ..
36 "/usr/local/lib/lua/5.0/l?.so"
39 -- make sure require works with standard libraries
41 package.loaded = package.loaded or {}
42 package.loaded.debug = debug
43 package.loaded.string = string
44 package.loaded.math = math
45 package.loaded.io = io
46 package.loaded.os = os
47 package.loaded.table = table
48 package.loaded.base = _G
49 package.loaded.coroutine = coroutine
50 local _LOADED = package.loaded
53 -- avoid overwriting the package.preload table if it's already there
55 package.preload = package.preload or {}
56 local _PRELOAD = package.preload
60 -- looks for a file `name' in given path
62 local function findfile (name, pname)
63 name = gsub (name, "%.", LUA_DIRSEP)
64 local path = _PACKAGE[pname]
65 assert (type(path) == "string", format ("package.%s must be a string", pname))
66 for c in gfind (path, "[^;]+") do
67 c = gsub (c, "%"..LUA_PATH_MARK, name)
74 return nil -- not found
79 -- check whether library is already loaded
81 local function loader_preload (name)
82 assert (type(name) == "string", format (
83 "bad argument #1 to `require' (string expected, got %s)", type(name)))
84 assert (type(_PRELOAD) == "table", "`package.preload' must be a table")
92 local function loader_Lua (name)
93 assert (type(name) == "string", format (
94 "bad argument #1 to `require' (string expected, got %s)", type(name)))
95 local filename = findfile (name, "path")
99 local f, err = loadfile (filename)
101 error (format ("error loading module `%s' (%s)", name, err))
107 local function mkfuncname (name)
108 name = gsub (name, "^.*%"..LUA_IGMARK, "")
109 name = gsub (name, "%.", LUA_OFSEP)
113 local function old_mkfuncname (name)
114 --name = gsub (name, "^.*%"..LUA_IGMARK, "")
115 name = gsub (name, "%.", OLD_LUA_OFSEP)
122 local function loader_C (name)
123 assert (type(name) == "string", format (
124 "bad argument #1 to `require' (string expected, got %s)", type(name)))
125 local filename = findfile (name, "cpath")
129 local funcname = mkfuncname (name)
130 local f, err = loadlib (filename, funcname)
132 funcname = old_mkfuncname (name)
133 f, err = loadlib (filename, funcname)
135 error (format ("error loading module `%s' (%s)", name, err))
142 local function loader_Croot (name)
143 local p = gsub (name, "^([^.]*).-$", "%1")
147 local filename = findfile (p, "cpath")
151 local funcname = mkfuncname (name)
152 local f, err, where = loadlib (filename, funcname)
155 elseif where ~= "init" then
156 error (format ("error loading module `%s' (%s)", name, err))
160 -- create `loaders' table
161 package.loaders = package.loaders or { loader_preload, loader_Lua, loader_C, loader_Croot, }
162 local _LOADERS = package.loaders
166 -- iterate over available loaders
168 local function load (name, loaders)
169 -- iterate over available loaders
170 assert (type (loaders) == "table", "`package.loaders' must be a table")
171 for i, loader in ipairs (loaders) do
172 local f = loader (name)
177 error (format ("module `%s' not found", name))
181 local sentinel = function () end
186 function _G.require (modname)
187 assert (type(modname) == "string", format (
188 "bad argument #1 to `require' (string expected, got %s)", type(name)))
189 local p = _LOADED[modname]
190 if p then -- is it there?
191 if p == sentinel then
192 error (format ("loop or previous error loading module '%s'", modname))
194 return p -- package is already loaded
196 local init = load (modname, _LOADERS)
197 _LOADED[modname] = sentinel
198 local actual_arg = _G.arg
200 local res = init (modname)
202 _LOADED[modname] = res
205 if _LOADED[modname] == sentinel then
206 _LOADED[modname] = true
208 return _LOADED[modname]
213 local function findtable (t, f)
214 assert (type(f)=="string", "not a valid field name ("..tostring(f)..")")
216 local ok, e, w = find (ff, '(.-)%.', 1)
218 local nt = rawget (t, w)
222 elseif type(t) ~= "table" then
226 ok, e, w = find (ff, '(.-)%.', e+1)
232 -- new package.seeall function
234 function _PACKAGE.seeall (module)
235 local t = type(module)
236 assert (t == "table", "bad argument #1 to package.seeall (table expected, got "..t..")")
237 local meta = getmetatable (module)
240 setmetatable (module, meta)
247 -- new module function
249 function _G.module (modname, ...)
250 local ns = _LOADED[modname]
251 if type(ns) ~= "table" then
252 ns = findtable (_G, modname)
254 error (string.format ("name conflict for module '%s'", modname))
256 _LOADED[modname] = ns
261 ns._PACKAGE = gsub (modname, "[^.]*$", "")
264 for i, f in ipairs (arg) do