~ubuntu-branches/ubuntu/maverick/rrdtool/maverick

« back to all changes in this revision

Viewing changes to bindings/lua/compat-5.1r5/compat-5.1.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2010-07-22 08:07:01 UTC
  • mfrom: (1.2.8 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100722080701-k46mgdfz6euxwqsm
Tags: 1.4.3-1ubuntu1
* Merge from debian unstable, Remaining changes:
  - debian/control: Don't build against ruby1.9 as we don't want
    it in main.
* require libdbi >= 0.8.3 to prevent aborts when using dbi datasources

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** Compat-5.1
 
3
** Copyright Kepler Project 2004-2006 (http://www.keplerproject.org/compat)
 
4
** $Id: compat-5.1.c 1860 2009-06-10 19:05:56Z oetiker $
 
5
 
 
6
Copyright � 2004-2006 The Kepler Project.
 
7
 
 
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:
 
14
 
 
15
The above copyright notice and this permission notice shall be included in
 
16
all copies or substantial portions of the Software.
 
17
 
 
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
 
24
IN THE SOFTWARE.
 
25
 
 
26
*/
 
27
 
 
28
#include <stdio.h>
 
29
#include <string.h>
 
30
#include "lua.h"
 
31
#include "lauxlib.h"
 
32
#include "compat-5.1.h"
 
33
 
 
34
static void getfield(lua_State *L, int idx, const char *name) {
 
35
    const char *end = strchr(name, '.');
 
36
    lua_pushvalue(L, idx);
 
37
    while (end) {
 
38
        lua_pushlstring(L, name, end - name);
 
39
        lua_gettable(L, -2);
 
40
        lua_remove(L, -2);
 
41
        if (lua_isnil(L, -1)) return;
 
42
        name = end+1;
 
43
        end = strchr(name, '.');
 
44
    }
 
45
    lua_pushstring(L, name);
 
46
    lua_gettable(L, -2);
 
47
    lua_remove(L, -2);
 
48
}
 
49
 
 
50
static void setfield(lua_State *L, int idx, const char *name) {
 
51
    const char *end = strchr(name, '.');
 
52
    lua_pushvalue(L, idx);
 
53
    while (end) {
 
54
        lua_pushlstring(L, name, end - name);
 
55
        lua_gettable(L, -2);
 
56
        /* create table if not found */
 
57
        if (lua_isnil(L, -1)) {
 
58
            lua_pop(L, 1);
 
59
            lua_newtable(L);
 
60
            lua_pushlstring(L, name, end - name);
 
61
            lua_pushvalue(L, -2);
 
62
            lua_settable(L, -4);
 
63
        }
 
64
        lua_remove(L, -2);
 
65
        name = end+1;
 
66
        end = strchr(name, '.');
 
67
    }
 
68
    lua_pushstring(L, name);
 
69
    lua_pushvalue(L, -3);
 
70
    lua_settable(L, -3);
 
71
    lua_pop(L, 2);
 
72
}
 
73
 
 
74
LUALIB_API void luaL_module(lua_State *L, const char *libname,
 
75
                              const luaL_reg *l, int nup) {
 
76
  if (libname) {
 
77
    getfield(L, LUA_GLOBALSINDEX, libname);  /* check whether lib already exists */
 
78
    if (lua_isnil(L, -1)) { 
 
79
      int env, ns;
 
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 */
 
85
      env = lua_gettop(L);
 
86
 
 
87
      lua_newtable(L); /* create namespace for lib */
 
88
      ns = lua_gettop(L);
 
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 */
 
92
          lua_newtable(L);
 
93
          lua_pushvalue(L, -1);
 
94
          setfield(L, env, "package.loaded");
 
95
      }
 
96
      else if (!lua_istable(L, -1))
 
97
        luaL_error(L, "name conflict for library `%s'", libname);
 
98
      lua_pushstring(L, libname);
 
99
      lua_pushvalue(L, ns); 
 
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 */
 
105
    }
 
106
    lua_insert(L, -(nup+1));  /* move library table to below upvalues */
 
107
  }
 
108
  for (; l->name; l++) {
 
109
    int i;
 
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));
 
115
  }
 
116
  lua_pop(L, nup);  /* remove upvalues */
 
117
}
 
118