X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FScript.hh;h=2fe275e161aae3a7bd0e14bd15d94dd7907692cb;hp=f954e583488a786e2740c7042a4ab89782697577;hb=23d8f7a5fbd1eca7f46f2342c20ac5e28ae0128a;hpb=ca0f7bdfba63140dca0bd20586d31980f3938eb2 diff --git a/src/Moof/Script.hh b/src/Moof/Script.hh index f954e58..2fe275e 100644 --- a/src/Moof/Script.hh +++ b/src/Moof/Script.hh @@ -126,6 +126,18 @@ struct Script index(i), state(s) {} + /** + * A copied value presently points to the same value, except the real + * index is used. That means that if a value that refers to a frame + * referenced from the top of the stack will have its normalized index + * copied into the new value object. + */ + + Value(const Value& copy) : + index(copy.getRealIndex()), + state(copy.state) {} + + // check the type of the value bool isBoolean() const { return (bool)lua_isboolean(state, index); } bool isFunction() const { return (bool)lua_isfunction(state, index); } @@ -140,6 +152,73 @@ struct Script bool isData() const { return (bool)lua_isuserdata(state, index); } bool isLightData() const { return (bool)lua_islightuserdata(state, index); } + /** + * Check the value and throw and error if its the wrong type. This + * method never returns because it does a long jump. Consequently, + * constructed C++ objects which exist on the stack between the current + * frame and some lua function will not be destructed. That's not a + * problem for objects that only exist on the stack, but any objects + * that allocate memory on the heap (such as containers or strings) will + * leak. Therefore, you should only call this method after cleaning up + * such objects. + */ + + void requireType(TYPE type) const + { + if (type != getType()) + { + luaL_typerror(state, index, lua_typename(state, type)); + } + } + + void throwError(const char* error) + { + luaL_argerror(state, index, error); + } + + + Value& requireBoolean() + { + if (!isBoolean()) luaL_typerror(state, index, "boolean"); + return *this; + } + Value& requireNumber() + { + if (!isNumber()) luaL_typerror(state, index, "number"); + return *this; + } + Value& requireString() + { + if (!isString()) luaL_typerror(state, index, "string"); + return *this; + } + Value& requireTable() + { + if (!isTable()) luaL_typerror(state, index, "table"); + return *this; + } + Value& requireFunction() + { + if (!isFunction()) luaL_typerror(state, index, "function"); + return *this; + } + Value& requireData() + { + if (!isData()) luaL_typerror(state, index, "data"); + return *this; + } + Value& requireNil() + { + if (!isNil()) luaL_typerror(state, index, "nil"); + return *this; + } + Value& requireThread() + { + if (!isThread()) luaL_typerror(state, index, "thread"); + return *this; + } + + /** * Get the type of the value. */ @@ -155,7 +234,7 @@ struct Script std::string getTypeName() const { - return std::string(lua_typename(state, (int)getType())); + return std::string(luaL_typename(state, index)); } @@ -465,6 +544,22 @@ struct Script } + /** + * Throw an error with the value at the top of the stack. This method never + * returns because it does a long jump. Consequently, constructed C++ + * objects which exist on the stack between the current frame and some lua + * function will not be destructed. That's not a problem for objects that + * only exist on the stack, but any objects that allocate memory on the heap + * (such as containers or strings) will leak. Therefore, you should only + * call this method after cleaning up such objects. + */ + + void throwError() + { + lua_error(state_); + } + + /** * Get significant values. */ @@ -561,6 +656,10 @@ struct Script { lua_pushlstring(state_, value.c_str(), value.length()); } + void push(const char* value) + { + lua_pushstring(state_, value); + } void push(const char* value, size_t length) { lua_pushlstring(state_, value, length);