Software /
code /
prosody
Annotate
util/require.lua @ 2005:478ba7e85862
MUC: Rewrote code for mediated invites to be more robust, and to support legacy clients.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 18 Oct 2009 15:28:00 +0500 |
parent | 1965:f641e7ee171c |
rev | line source |
---|---|
1965
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 -- Based on Kepler Compat-5.1 code |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 -- Copyright Kepler Project 2004-2006 (http://www.keplerproject.org/compat) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 -- $Id: compat-5.1.lua,v 1.22 2006/02/20 21:12:47 carregal Exp $ |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local LUA_DIRSEP = '/' |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local LUA_OFSEP = '_' |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local OLD_LUA_OFSEP = '' |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local POF = 'luaopen_' |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local LUA_PATH_MARK = '?' |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local LUA_IGMARK = ':' |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type = assert, error, getfenv, ipairs, loadfile, loadlib, pairs, setfenv, setmetatable, type |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local find, format, gfind, gsub, sub = string.find, string.format, string.gfind, string.gsub, string.sub |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 if package.nonglobal_module then return; end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 package.nonglobal_module = true; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local _PACKAGE = package |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local _LOADED = package.loaded |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local _PRELOAD = package.preload |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 -- looks for a file `name' in given path |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 local function findfile (name, pname) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 name = gsub (name, "%.", LUA_DIRSEP) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local path = _PACKAGE[pname] |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 assert (type(path) == "string", format ("package.%s must be a string", pname)) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 for c in gfind (path, "[^;]+") do |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 c = gsub (c, "%"..LUA_PATH_MARK, name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local f = io.open (c) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 if f then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 f:close () |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 return c |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return nil -- not found |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 -- check whether library is already loaded |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 local function loader_preload (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 assert (type(name) == "string", format ( |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 "bad argument #1 to `require' (string expected, got %s)", type(name))) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 assert (type(_PRELOAD) == "table", "`package.preload' must be a table") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 return _PRELOAD[name] |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 -- Lua library loader |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 local function loader_Lua (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 assert (type(name) == "string", format ( |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 "bad argument #1 to `require' (string expected, got %s)", type(name))) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 local filename = findfile (name, "path") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 if not filename then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 return false |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 local f, err = loadfile (filename) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if not f then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 error (format ("error loading module `%s' (%s)", name, err)) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 return f |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 local function mkfuncname (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 name = gsub (name, "^.*%"..LUA_IGMARK, "") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 name = gsub (name, "%.", LUA_OFSEP) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 return POF..name |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 local function old_mkfuncname (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 --name = gsub (name, "^.*%"..LUA_IGMARK, "") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 name = gsub (name, "%.", OLD_LUA_OFSEP) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 return POF..name |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 -- C library loader |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 local function loader_C (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 assert (type(name) == "string", format ( |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 "bad argument #1 to `require' (string expected, got %s)", type(name))) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 local filename = findfile (name, "cpath") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 if not filename then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 return false |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 local funcname = mkfuncname (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 local f, err = loadlib (filename, funcname) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 if not f then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 funcname = old_mkfuncname (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 f, err = loadlib (filename, funcname) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 if not f then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 error (format ("error loading module `%s' (%s)", name, err)) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 return f |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 local function loader_Croot (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 local p = gsub (name, "^([^.]*).-$", "%1") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 if p == "" then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 return |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 local filename = findfile (p, "cpath") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 if not filename then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 return |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local funcname = mkfuncname (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 local f, err, where = loadlib (filename, funcname) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 if f then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 return f |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 elseif where ~= "init" then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 error (format ("error loading module `%s' (%s)", name, err)) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 -- create `loaders' table |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 package.loaders = package.loaders or { loader_preload, loader_Lua, loader_C, loader_Croot, } |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 local _LOADERS = package.loaders |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 -- iterate over available loaders |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 local function load (name, loaders) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 -- iterate over available loaders |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 assert (type (loaders) == "table", "`package.loaders' must be a table") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 for i, loader in ipairs (loaders) do |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 local f = loader (name) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 if f then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 return f |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 error (format ("module `%s' not found", name)) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 -- sentinel |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 local sentinel = function () end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 local old_require = _G.require; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 local dep_path = {}; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 local current_env = nil; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 function _G.require(modname) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 --table.insert(dep_path, modname); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 --if getfenv(2) == getfenv(0) --[[and rawget(_G, "__locked")]] then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 -- print("**** Uh-oh, require called from locked global env at "..table.concat(dep_path, "->"), debug.traceback()); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 --end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 if not current_env and rawget(_G, "__locked") then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 _G.prosody.unlock_globals(); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 _G.__locked = false; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 local old_current_env; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 old_current_env, current_env = current_env, getfenv(2); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 local ok, ret = pcall(old_require, modname); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 current_env = old_current_env; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 if not current_env and rawget(_G, "__locked") == false then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 _G.prosody.lock_globals(); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 --table.remove(dep_path); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 if not ok then error(ret, 0); end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 return ret; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 -- findtable |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 local function findtable (t, f) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 assert (type(f)=="string", "not a valid field name ("..tostring(f)..")") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 local ff = f.."." |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 local ok, e, w = find (ff, '(.-)%.', 1) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 while ok do |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 local nt = rawget (t, w) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 if not nt then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 nt = {} |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 t[w] = nt |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 elseif type(t) ~= "table" then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 return sub (f, e+1) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 t = nt |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 ok, e, w = find (ff, '(.-)%.', e+1) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 return t |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 -- new package.seeall function |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 function _PACKAGE.seeall (module) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 local t = type(module) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 assert (t == "table", "bad argument #1 to package.seeall (table expected, got "..t..")") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 local meta = getmetatable (module) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 if not meta then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 meta = {} |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 setmetatable (module, meta) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 meta.__index = _G |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 -- new module function |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 -- |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 function _G.module (modname, ...) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 local ns = _LOADED[modname]; |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 if type(ns) ~= "table" then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 --if not current_env then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 -- print("module outside require for "..modname.." at "..debug.traceback()); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 --end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 ns = findtable (current_env or getfenv(2), modname); |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 if not ns then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 error (string.format ("name conflict for module '%s'", modname)) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 _LOADED[modname] = ns |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 if not ns._NAME then |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 ns._NAME = modname |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 ns._M = ns |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 ns._PACKAGE = gsub (modname, "[^.]*$", "") |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 setfenv (2, ns) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 for i, f in ipairs (arg) do |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 f (ns) |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 end |
f641e7ee171c
util.require: A replacement for Lua's require/module that doesn't load into the global environment by default (C modules still get past this)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 end |