3 ** Copyright Kepler Project 2004-2006 (http://www.keplerproject.org/compat)
6 Copyright © 2004-2006 The Kepler Project.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to
10 deal in the Software without restriction, including without limitation the
11 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 sell copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
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,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 #include "compat-5.1.h"
34 static void getfield(lua_State *L, int idx, const char *name) {
35 const char *end = strchr(name, '.');
36 lua_pushvalue(L, idx);
38 lua_pushlstring(L, name, end - name);
41 if (lua_isnil(L, -1)) return;
43 end = strchr(name, '.');
45 lua_pushstring(L, name);
50 static void setfield(lua_State *L, int idx, const char *name) {
51 const char *end = strchr(name, '.');
52 lua_pushvalue(L, idx);
54 lua_pushlstring(L, name, end - name);
56 /* create table if not found */
57 if (lua_isnil(L, -1)) {
60 lua_pushlstring(L, name, end - name);
66 end = strchr(name, '.');
68 lua_pushstring(L, name);
74 LUALIB_API void luaL_module(lua_State *L, const char *libname,
75 const luaL_reg *l, int nup) {
77 getfield(L, LUA_GLOBALSINDEX, libname); /* check whether lib already exists */
78 if (lua_isnil(L, -1)) {
80 lua_pop(L, 1); /* get rid of nil */
81 lua_pushliteral(L, "require");
82 lua_gettable(L, LUA_GLOBALSINDEX); /* look for require */
83 lua_getfenv(L, -1); /* getfenv(require) */
84 lua_remove(L, -2); /* remove function require */
87 lua_newtable(L); /* create namespace for lib */
89 getfield(L, env, "package.loaded"); /* get package.loaded table */
90 if (lua_isnil(L, -1)) { /* create package.loaded table */
91 lua_pop(L, 1); /* remove previous result */
94 setfield(L, env, "package.loaded");
96 else if (!lua_istable(L, -1))
97 luaL_error(L, "name conflict for library `%s'", libname);
98 lua_pushstring(L, libname);
100 lua_settable(L, -3); /* package.loaded[libname] = ns */
101 lua_pop(L, 1); /* get rid of package.loaded table */
102 lua_pushvalue(L, ns); /* copy namespace */
103 setfield(L, LUA_GLOBALSINDEX, libname);
104 lua_remove (L, env); /* remove env */
106 lua_insert(L, -(nup+1)); /* move library table to below upvalues */
108 for (; l->name; l++) {
110 lua_pushstring(L, l->name);
111 for (i=0; i<nup; i++) /* copy upvalues to the top */
112 lua_pushvalue(L, -(nup+1));
113 lua_pushcclosure(L, l->func, nup);
114 lua_settable(L, -(nup+3));
116 lua_pop(L, nup); /* remove upvalues */