diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-07-25 13:42:39 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-07-25 13:42:39 (GMT) |
commit | 93fe0b36b768a5e681bf5bf0943ba3e98aa12c1d (patch) | |
tree | 186580eef0a0944fec36c32c60a6d22aebc4975e /src/FbTk/Luamm.cc | |
parent | bffb54bb4c5864007e632475185e6b22e650074f (diff) | |
download | fluxbox_pavel-93fe0b36b768a5e681bf5bf0943ba3e98aa12c1d.zip fluxbox_pavel-93fe0b36b768a5e681bf5bf0943ba3e98aa12c1d.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/Luamm.cc')
-rw-r--r-- | src/FbTk/Luamm.cc | 31 |
1 files changed, 31 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)) |