diff options
author | Robert Vollmert <rvollmert@gmx.net> | 2009-11-24 09:40:45 +0100 |
---|---|---|
committer | Robert Vollmert <rvollmert@gmx.net> | 2009-11-24 11:05:13 +0100 |
commit | 6728050e3e412aa0c335a3f7439657288c79d4c5 (patch) | |
tree | d1dd7a7a816a9898c1b378a0ba661b8d3647cf52 | |
parent | 5648dce61108e9c5059f390a1c9b28a68ce0b439 (diff) | |
download | crawl-ref-6728050e3e412aa0c335a3f7439657288c79d4c5.tar.gz crawl-ref-6728050e3e412aa0c335a3f7439657288c79d4c5.zip |
Add "maybe_bool CLua::callmbooleanfn".
This allows Lua hooks to say they don't have an opinion.
-rw-r--r-- | crawl-ref/source/clua.cc | 30 | ||||
-rw-r--r-- | crawl-ref/source/clua.h | 3 |
2 files changed, 25 insertions, 8 deletions
diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc index d750fc9f09..8506d5d143 100644 --- a/crawl-ref/source/clua.cc +++ b/crawl-ref/source/clua.cc @@ -467,12 +467,13 @@ bool CLua::calltopfn(lua_State *ls, const char *params, va_list args, return (!err); } -bool CLua::callbooleanfn(bool def, const char *fn, const char *params, ...) +maybe_bool CLua::callmbooleanfn(const char *fn, const char *params, + const va_list *args) { error.clear(); lua_State *ls = state(); if (!ls) - return (def); + return (B_MAYBE); int stacktop = lua_gettop(ls); @@ -480,17 +481,30 @@ bool CLua::callbooleanfn(bool def, const char *fn, const char *params, ...) if (!lua_isfunction(ls, -1)) { lua_pop(ls, 1); - CL_RESETSTACK_RETURN(ls, stacktop, def); + CL_RESETSTACK_RETURN(ls, stacktop, B_MAYBE); } + bool ret = calltopfn(ls, params, *args, 1); + if (!ret) + CL_RESETSTACK_RETURN(ls, stacktop, B_MAYBE); + + maybe_bool r = frombool(lua_toboolean(ls, -1)); + CL_RESETSTACK_RETURN(ls, stacktop, r); +} + +maybe_bool CLua::callmbooleanfn(const char *fn, const char *params, ...) +{ va_list args; va_start(args, params); - bool ret = calltopfn(ls, params, args, 1); - if (!ret) - CL_RESETSTACK_RETURN(ls, stacktop, def); + return (callmbooleanfn(fn, params, &args)); +} - def = lua_toboolean(ls, -1); - CL_RESETSTACK_RETURN(ls, stacktop, def); +bool CLua::callbooleanfn(bool def, const char *fn, const char *params, ...) +{ + va_list args; + va_start(args, params); + maybe_bool r = callmbooleanfn(fn, params, &args); + return (tobool(r, def)); } bool CLua::proc_returns(const char *par) const diff --git a/crawl-ref/source/clua.h b/crawl-ref/source/clua.h index 1bbf70c720..6877c47a18 100644 --- a/crawl-ref/source/clua.h +++ b/crawl-ref/source/clua.h @@ -128,6 +128,7 @@ public: void pushglobal(const std::string &name); + maybe_bool callmbooleanfn(const char *fn, const char *params, ...); bool callbooleanfn(bool defval, const char *fn, const char *params, ...); bool callfn(const char *fn, int nargs, int nret = 1); bool callfn(const char *fn, const char *params, ...); @@ -189,6 +190,8 @@ private: bool calltopfn(lua_State *ls, const char *format, va_list args, int retc = -1, va_list *fnr = NULL); + maybe_bool callmbooleanfn(const char *fn, const char *params, + const va_list *args); int push_args(lua_State *ls, const char *format, va_list args, va_list *cpto = NULL); |