summaryrefslogtreecommitdiff
path: root/lib/liblua_wrapper.cc
blob: 80a05d6a576552c54430619d19eeb558f265fb9a (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            liblua_wrapper.cc
 *
 *  Mon Aug 14 17:36:03 CEST 2006
 *  Copyright  2006 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of MIaV.
 *
 *  MIaV 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.
 *
 *  MIaV 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 MIaV; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "liblua_wrapper.h"

#include <stdexcept>

LibLUAWrapper::LibLUAWrapper()
{ 
  L = lua_open();
  //  luaL_openlibs(L);
}

LibLUAWrapper::~LibLUAWrapper()
{
  lua_close(L);
}

int LibLUAWrapper::loadFile(const char *fname)
{
  int s;

  s = luaL_loadfile(L, fname);

  switch(s) {
  case 0: //no errors;
    break;
  case LUA_ERRSYNTAX: //syntax error during pre-compilation;
  case LUA_ERRMEM: //memory allocation error.
  case LUA_ERRFILE: //cannot open/read the file.
    strerr = std::string(lua_tostring(L, lua_gettop(L)));
    return 1;
  default:
    strerr = std::string("Unknown return value of luaL_loadfile.");
    return 1;
  }

  s = lua_pcall(L, 0, LUA_MULTRET, 0);

  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.
    strerr = std::string(lua_tostring(L, lua_gettop(L)));
    return 1;
  default:
    strerr = std::string("Unknown return value of lua_pcall.");
    return 1;
  }

  return 0;
}

int LibLUAWrapper::loadBuffer(char *buffer)
{
  int s;

  s = luaL_loadstring(L, buffer);

  switch(s) {
  case 0: //no errors;
    break;
  case LUA_ERRSYNTAX: //syntax error during pre-compilation;
  case LUA_ERRMEM: //memory allocation error.
    strerr = std::string(lua_tostring(L, lua_gettop(L)));
    return 1;
  default:
    strerr = std::string("Unknown return value of luaL_loadstring.");
    return 1;
  }

  s = lua_pcall(L, 0, LUA_MULTRET, 0);

  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.
    strerr = std::string(lua_tostring(L, lua_gettop(L)));
    return 1;
  default:
    strerr = std::string("Unknown return value of lua_pcall.");
    return 1;
  }

  return 0;
}

int LibLUAWrapper::getInteger(const char *name)
{
  lua_getfield(L, LUA_GLOBALSINDEX, name); 
  
  int top = lua_gettop(L);

  int val = lua_tointeger(L, top);

  lua_remove(L, top);

  return val;
}

double LibLUAWrapper::getReal(const char *name)
{
  lua_getfield(L, LUA_GLOBALSINDEX, name); 
  
  int top = lua_gettop(L);

  double val = lua_tonumber(L, top);

  lua_remove(L, top);

  return val;
}

bool LibLUAWrapper::getBoolean(const char *name)
{
  lua_getfield(L, LUA_GLOBALSINDEX, name); 
  
  int top = lua_gettop(L);

  bool val = lua_toboolean(L, top);

  lua_remove(L, top);

  return val;
}

std::string LibLUAWrapper::getString(const char *name)
{
  lua_getfield(L, LUA_GLOBALSINDEX, name); 
  
  int top = lua_gettop(L);

  std::string val = std::string(lua_tostring(L, top));

  lua_remove(L, top);

  return val;
}

std::string LibLUAWrapper::error()
{
  return strerr;
}

#ifdef LUA_TEST
// Compile with:
// g++ liblua_wrapper.cc -DLUA_TEST -I /home/deva/lib/lua-5.1.1/include/ -L /home/deva/lib/lua-5.1.1/lib -llua

const char preload[] =
"a = 42.0\n"
"b = 42\n"
"c = 42\n"
"d = \"42\"\n";

int main()
{
  LibLUAWrapper lua;

  if(lua.loadBuffer((char*)preload)) {
    fprintf(stderr, "LUA buffer error: %s\n", lua.error().c_str());
    return 1;
  }

  if(lua.loadFile("test.lua")) {
    fprintf(stderr, "LUA load error: %s\n", lua.error().c_str());
    return 1;
  }

  printf("a: %f\n", lua.getReal("a"));
  printf("b: %d\n", lua.getInteger("b"));
  printf("c: %d\n", lua.getBoolean("c"));
  printf("d: %s\n", lua.getString("d").c_str());

  return 0;
}

#endif