- Lua module build and install process rewritten using automake+libtool
[rrdtool.git] / bindings / lua / compat-5.1r5 / compat-5.1.c
1 /*
2 ** Compat-5.1
3 ** Copyright Kepler Project 2004-2006 (http://www.keplerproject.org/compat)
4 ** $Id$
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include "lua.h"
10 #include "lauxlib.h"
11 #include "compat-5.1.h"
12
13 static void getfield(lua_State *L, int idx, const char *name) {
14     const char *end = strchr(name, '.');
15     lua_pushvalue(L, idx);
16     while (end) {
17         lua_pushlstring(L, name, end - name);
18         lua_gettable(L, -2);
19         lua_remove(L, -2);
20         if (lua_isnil(L, -1)) return;
21         name = end+1;
22         end = strchr(name, '.');
23     }
24     lua_pushstring(L, name);
25     lua_gettable(L, -2);
26     lua_remove(L, -2);
27 }
28
29 static void setfield(lua_State *L, int idx, const char *name) {
30     const char *end = strchr(name, '.');
31     lua_pushvalue(L, idx);
32     while (end) {
33         lua_pushlstring(L, name, end - name);
34         lua_gettable(L, -2);
35         /* create table if not found */
36         if (lua_isnil(L, -1)) {
37             lua_pop(L, 1);
38             lua_newtable(L);
39             lua_pushlstring(L, name, end - name);
40             lua_pushvalue(L, -2);
41             lua_settable(L, -4);
42         }
43         lua_remove(L, -2);
44         name = end+1;
45         end = strchr(name, '.');
46     }
47     lua_pushstring(L, name);
48     lua_pushvalue(L, -3);
49     lua_settable(L, -3);
50     lua_pop(L, 2);
51 }
52
53 LUALIB_API void luaL_module(lua_State *L, const char *libname,
54                               const luaL_reg *l, int nup) {
55   if (libname) {
56     getfield(L, LUA_GLOBALSINDEX, libname);  /* check whether lib already exists */
57     if (lua_isnil(L, -1)) { 
58       int env, ns;
59       lua_pop(L, 1); /* get rid of nil */
60       lua_pushliteral(L, "require");
61       lua_gettable(L, LUA_GLOBALSINDEX); /* look for require */
62       lua_getfenv(L, -1); /* getfenv(require) */
63       lua_remove(L, -2); /* remove function require */
64       env = lua_gettop(L);
65
66       lua_newtable(L); /* create namespace for lib */
67       ns = lua_gettop(L);
68       getfield(L, env, "package.loaded"); /* get package.loaded table */
69       if (lua_isnil(L, -1)) { /* create package.loaded table */
70           lua_pop(L, 1); /* remove previous result */
71           lua_newtable(L);
72           lua_pushvalue(L, -1);
73           setfield(L, env, "package.loaded");
74       }
75       else if (!lua_istable(L, -1))
76         luaL_error(L, "name conflict for library `%s'", libname);
77       lua_pushstring(L, libname);
78       lua_pushvalue(L, ns); 
79       lua_settable(L, -3); /* package.loaded[libname] = ns */
80       lua_pop(L, 1); /* get rid of package.loaded table */
81       lua_pushvalue(L, ns); /* copy namespace */
82       setfield(L, LUA_GLOBALSINDEX, libname);
83       lua_remove (L, env); /* remove env */
84     }
85     lua_insert(L, -(nup+1));  /* move library table to below upvalues */
86   }
87   for (; l->name; l++) {
88     int i;
89     lua_pushstring(L, l->name);
90     for (i=0; i<nup; i++)  /* copy upvalues to the top */
91       lua_pushvalue(L, -(nup+1));
92     lua_pushcclosure(L, l->func, nup);
93     lua_settable(L, -(nup+3));
94   }
95   lua_pop(L, nup);  /* remove upvalues */
96 }
97