summaryrefslogtreecommitdiff
path: root/server/src/luautil.cc
blob: 116d21924fa6032d93167c4dddf0dabfda75887b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            luautil.cc
 *
 *  Fri Apr 13 14:38:53 CEST 2007
 *  Copyright 2007 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pracro is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "luautil.h"

#include "debug.h"

#include <string>

#include <vector>

std::vector< void * > pointers;

// for ntohl and htonl
#include <arpa/inet.h>

#define GLOBAL_PREFIX "magic_global_"

//
// Get the (somewhat hacky) global pointer to the parser object.
// It is set in the preload method.
//
void *Pracro::getGlobal(lua_State *L, std::string name)
{
  unsigned int top;
  unsigned int index;

  std::string var = std::string(GLOBAL_PREFIX) + name;

  lua_getfield(L, LUA_GLOBALSINDEX, var.c_str()); 
  top = lua_gettop(L);
  index = lua_tointeger(L, top);

  return pointers.at(index);
}

void Pracro::setGlobal(lua_State *L, std::string name, void *p)
{
  // Put the value of this in the globals
  char value_of_this[256];

  std::string val = std::string(GLOBAL_PREFIX) + name;

  /*
  unsigned int parser = (unsigned int)p;
  parser = htonl(parser);
  */

  pointers.push_back(p);
  unsigned int index = pointers.size() - 1;

  sprintf(value_of_this, "%s = %u\n", val.c_str(), index);
  int s = luaL_loadstring(L, value_of_this);
  switch(s) {
  case 0: //no errors;
    break;
  case LUA_ERRSYNTAX: //syntax error during pre-compilation;
  case LUA_ERRMEM: //memory allocation error.
    fprintf(stderr, "Error: %s\n", lua_tostring(L, lua_gettop(L)));
  default:
    fprintf(stderr, "Unknown return value of luaL_loadstring.\n");
  }

  // Run program (init)
  s = lua_pcall(L, 0, LUA_MULTRET, 0);
  // Check for errors
  switch(s) {
  case 0: // Success
    break;
  case LUA_ERRRUN:// a runtime error.
  case LUA_ERRMEM:// memory allocation error.
    // For such errors, Lua does not call the error handler function.
  case LUA_ERRERR:// error while running the error handler function.
    fprintf(stderr, "Error: %s\n", lua_tostring(L, lua_gettop(L)));
    break;
  default:
    fprintf(stderr, "Error: Unknown return value of lua_pcall.\n");
    break;
  }
}

void Pracro::call(lua_State *L, std::string function, int numargs)
{
  // Get function
  lua_getglobal(L, function.c_str());

  // Call it
  int s = lua_pcall(L, numargs, LUA_MULTRET, 0);

  // Check for errors
  switch(s) {
  case 0: // Success
    break;
  case LUA_ERRRUN:// a runtime error.
  case LUA_ERRMEM:// memory allocation error.
    // For such errors, Lua does not call the error handler function.
  case LUA_ERRERR:// error while running the error handler function.
    fprintf(stderr, "Error: %s\n", lua_tostring(L, lua_gettop(L)));
    break;
  default:
    fprintf(stderr, "Error: Unknown return value of lua_pcall.\n");
    break;
  }
}


/*
lua_isboolean
lua_iscfunction
lua_isfunction
lua_islightuserdata
lua_isnil
lua_isnone
lua_isnoneornil
lua_isnumber
lua_isstring
lua_istable
lua_isthread
lua_isuserdata
*/
int Pracro::checkParameters(lua_State *L, ...)
{
  va_list ap;
  va_start(ap, L);

  size_t nargs = lua_gettop(L); // number of arguments

  size_t size = 0;
  int t;
  while(1) { 
    t = va_arg(ap, int);
    if(t == T_END) break;

    switch(t) {
    case T_STRING:
    case T_NUMBER:
    case T_BOOLEAN:
      break;

    default:
      return luaL_error(L,"Unknown type specifier [%d] at position %d. "
                        "Missing TYPE_END?", t, size+1);
    }

    size++;
  }

  va_end(ap);

  if(nargs != size) {
    return luaL_error(L, "Number of args expected %d, got %d", size, nargs);
  }

  va_start(ap, L);

  size_t idx = 0;
  while(1) {
    t = va_arg(ap, int);
    if(t == T_END) break;

    switch(t) {
    case T_STRING:
      if(lua_isstring(L, lua_gettop(L)-(size-idx-1)) == 0) {
        va_end(ap);
        return luaL_error(L, "Parameter %d should be of type string.", idx+1);
      }
      break;

    case T_NUMBER:
      if(lua_isnumber(L, lua_gettop(L)-(size-idx-1)) == 0) {
        va_end(ap);
        return luaL_error(L, "Parameter %d should be of type number.", idx+1);
      }
      break;

    case T_BOOLEAN:
      if(lua_isboolean(L, lua_gettop(L)-(size-idx-1)) == 0) {
        va_end(ap);
        return luaL_error(L, "Parameter %d should be of type boolean.", idx+1);
      }
      break;

    default:
      va_end(ap);
      return luaL_error(L,"Unknown type specifier [%d] at position %d. "
                        "Missing TYPE_END?", t, idx+1);
    }

    idx++;
  }

  va_end(ap);

  return 0;
}

#ifdef TEST_LUAUTIL
//deps:
//cflags: $(LUA_CFLAGS) -I..
//libs: $(LUA_LIBS)
#include "test.h"

#define LUAPROG \
  "testfunc('a string', 42, true)"

#define LUAPROG_BAD1 \
  "testfunc('a string', 42)"

#define LUAPROG_BAD2 \
  "testfunc('a string', 42, true, 'another one')"

#define LUAPROG_BAD3 \
  "testfunc(false, 42, 'string')"

#define LUAPROG_MISSING_END \
  "testfunc_bad('a string', 42, true, 1)"

static int testfunc(lua_State *L)
{
  Pracro::checkParameters(L,
                          Pracro::T_STRING,
                          Pracro::T_NUMBER,
                          Pracro::T_BOOLEAN,
                          Pracro::T_END);
  return 0;
}

static int testfunc_bad(lua_State *L)
{
  Pracro::checkParameters(L,
                          Pracro::T_STRING,
                          Pracro::T_NUMBER,
                          Pracro::T_BOOLEAN,
                          Pracro::T_NUMBER);
  return 0;
}


TEST_BEGIN;

int a, b, c;

lua_State *L;
L = luaL_newstate();
if(L == NULL) TEST_FATAL("Could not allocate lua state.");
luaL_openlibs(L);

Pracro::setGlobal(L, "a", &a);
Pracro::setGlobal(L, "b", &b);
Pracro::setGlobal(L, "c", &c);

TEST_EQUAL(Pracro::getGlobal(L, "b"), &b, "Test get global");
TEST_EQUAL(Pracro::getGlobal(L, "c"), &c, "Test get global");
TEST_EQUAL(Pracro::getGlobal(L, "a"), &a, "Test get global");

lua_register(L, "testfunc", testfunc);

if(luaL_loadstring(L, LUAPROG)) {
  TEST_FATAL(lua_tostring(L, lua_gettop(L)))
}
TEST_EQUAL_INT(lua_pcall(L, 0, LUA_MULTRET, 0), 0, "Good one");

if(luaL_loadstring(L, LUAPROG_BAD1)) {
  TEST_FATAL(lua_tostring(L, lua_gettop(L)))
}
TEST_NOTEQUAL_INT(lua_pcall(L, 0, LUA_MULTRET, 0), 0, "Too short one");

if(luaL_loadstring(L, LUAPROG_BAD2)) {
  TEST_FATAL(lua_tostring(L, lua_gettop(L)))
}
TEST_NOTEQUAL_INT(lua_pcall(L, 0, LUA_MULTRET, 0), 0, "Too long one");

if(luaL_loadstring(L, LUAPROG_BAD3)) {
  TEST_FATAL(lua_tostring(L, lua_gettop(L)))
}
TEST_NOTEQUAL_INT(lua_pcall(L, 0, LUA_MULTRET, 0), 0, "Plain wrong");


lua_register(L, "testfunc_bad", testfunc_bad);
if(luaL_loadstring(L, LUAPROG_MISSING_END)) {
  TEST_FATAL(lua_tostring(L, lua_gettop(L)))
}
TEST_NOTEQUAL_INT(lua_pcall(L, 0, LUA_MULTRET, 0), 0, "Missing T_END");

lua_close(L);

TEST_END;

#endif/*TEST_LUAUTIL*/