diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-07-25 13:42:39 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-11-01 09:57:23 (GMT) |
commit | 145d388c1be2d6f2cd0c68c51448c339ed96371b (patch) | |
tree | 466cd7c3d42f57de2faab219f341bf50c0de0428 /src/FbTk | |
parent | d91899b389c42e6ce422dcdb3b45963e68be8bc5 (diff) | |
download | fluxbox_paul-145d388c1be2d6f2cd0c68c51448c339ed96371b.zip fluxbox_paul-145d388c1be2d6f2cd0c68c51448c339ed96371b.tar.bz2 |
Add checkudata and checkargno functions to lua::state
These can be used by functions to check the saneness of arguments. They throw an exception on
error. In the future, I might add more informative error messages.
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/Luamm.cc | 31 | ||||
-rw-r--r-- | src/FbTk/Luamm.hh | 12 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc index 1ef751f..80d0926 100644 --- a/src/FbTk/Luamm.cc +++ b/src/FbTk/Luamm.cc | |||
@@ -24,6 +24,8 @@ | |||
24 | 24 | ||
25 | #include "Luamm.hh" | 25 | #include "Luamm.hh" |
26 | 26 | ||
27 | #include <sstream> | ||
28 | |||
27 | namespace lua { | 29 | namespace lua { |
28 | namespace { | 30 | namespace { |
29 | // keys for storing values in lua registry | 31 | // keys for storing values in lua registry |
@@ -274,6 +276,35 @@ namespace lua { | |||
274 | throw lua::exception(this); | 276 | throw lua::exception(this); |
275 | } | 277 | } |
276 | 278 | ||
279 | void state::checkargno(int argno) throw(lua::check_error) | ||
280 | { | ||
281 | if(gettop() != argno) { | ||
282 | std::ostringstream str; | ||
283 | str << "Wrong number of arguments: expected " << argno << ", got " << gettop(); | ||
284 | throw lua::check_error(str.str()); | ||
285 | } | ||
286 | } | ||
287 | |||
288 | void *state::checkudata(int narg, const char *tname) throw(lua::check_error, std::bad_alloc) | ||
289 | { | ||
290 | checkstack(2); | ||
291 | stack_sentry s(*this); | ||
292 | |||
293 | void *p = touserdata(narg); | ||
294 | if(p != NULL) { | ||
295 | if(getmetatable(narg)) { | ||
296 | rawgetfield(REGISTRYINDEX, tname); | ||
297 | if(rawequal(-1, -2)) | ||
298 | return p; | ||
299 | pop(2); | ||
300 | } | ||
301 | } | ||
302 | std::ostringstream str; | ||
303 | str << "Invalid argument #" << narg << ": expected " << type_name(TUSERDATA) | ||
304 | << ", got " << type_name(type(narg)); | ||
305 | throw lua::check_error(str.str()); | ||
306 | } | ||
307 | |||
277 | void state::checkstack(int extra) throw(std::bad_alloc) | 308 | void state::checkstack(int extra) throw(std::bad_alloc) |
278 | { | 309 | { |
279 | if(not lua_checkstack(cobj, extra)) | 310 | if(not lua_checkstack(cobj, extra)) |
diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index 110ac97..b75fb12 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh | |||
@@ -131,6 +131,14 @@ namespace lua { | |||
131 | {} | 131 | {} |
132 | }; | 132 | }; |
133 | 133 | ||
134 | // thrown by check* functions when they detect an invalid argument | ||
135 | class check_error: public std::runtime_error { | ||
136 | public: | ||
137 | check_error(const std::string &msg) | ||
138 | : std::runtime_error(msg) | ||
139 | {} | ||
140 | }; | ||
141 | |||
134 | // a fancy wrapper around lua_State | 142 | // a fancy wrapper around lua_State |
135 | class state { | 143 | class state { |
136 | lua_State *cobj; | 144 | lua_State *cobj; |
@@ -264,6 +272,10 @@ namespace lua { | |||
264 | // type c, throw everything but the kitchen sink | 272 | // type c, throw everything but the kitchen sink |
265 | // call() is a protected mode call, we don't allow unprotected calls | 273 | // call() is a protected mode call, we don't allow unprotected calls |
266 | void call(int nargs, int nresults, int errfunc = 0); | 274 | void call(int nargs, int nresults, int errfunc = 0); |
275 | void checkargno(int argno) throw(lua::check_error); | ||
276 | void *checkudata(int narg, const char *tname) throw(lua::check_error, std::bad_alloc); | ||
277 | template<typename T> | ||
278 | T *checkudata(int narg, const char *tname) throw(lua::check_error, std::bad_alloc) { return static_cast<T *>(checkudata(narg, tname)); } | ||
267 | void concat(int n); | 279 | void concat(int n); |
268 | bool equal(int index1, int index2); | 280 | bool equal(int index1, int index2); |
269 | int gc(int what, int data); | 281 | int gc(int what, int data); |