X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FScript.hh;h=4db93d5edc68501e446010fad5697bdbf86adfa1;hp=f954e583488a786e2740c7042a4ab89782697577;hb=a31d65a998121df0651c57bfb68782e2a07d2e2f;hpb=ca0f7bdfba63140dca0bd20586d31980f3938eb2 diff --git a/src/Moof/Script.hh b/src/Moof/Script.hh index f954e58..4db93d5 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)); } @@ -389,15 +468,15 @@ struct Script Script() : - state_(luaL_newstate()) + mState(luaL_newstate()) { - lua_pushlightuserdata(state_, this); - lua_setfield(state_, LUA_REGISTRYINDEX, "_script_obj"); + lua_pushlightuserdata(mState, this); + lua_setfield(mState, LUA_REGISTRYINDEX, "_script_obj"); } ~Script() { - if (isMainThread_) lua_close(state_); + if (mIsMainThread) lua_close(mState); } @@ -409,24 +488,24 @@ struct Script void importStandardLibraries() { - luaL_openlibs(state_); + luaL_openlibs(mState); } void importFunction(const std::string& name, const Function& function) { push(function); - lua_setglobal(state_, name.c_str()); + lua_setglobal(mState, name.c_str()); } STATUS doString(const std::string& commands) { - return (STATUS)luaL_dostring(state_, commands.c_str()); + return (STATUS)luaL_dostring(mState, commands.c_str()); } STATUS doFile(const std::string& file) { - return (STATUS)luaL_dofile(state_, file.c_str()); + return (STATUS)luaL_dofile(mState, file.c_str()); } @@ -436,32 +515,48 @@ struct Script Script pushNewThread() { - return Script(state_); + return Script(mState); } void pushThread() { - lua_pushthread(state_); + lua_pushthread(mState); } STATUS resume(int nargs) { - return (STATUS)lua_resume(state_, nargs); + return (STATUS)lua_resume(mState, nargs); } STATUS getStatus() const { - return (STATUS)lua_status(state_); + return (STATUS)lua_status(mState); } int yield(int results) { - return lua_yield(state_, results); + return lua_yield(mState, results); } bool isMainThread() const { - return isMainThread_; + return mIsMainThread; + } + + + /** + * 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(mState); } @@ -471,22 +566,22 @@ struct Script Value getGlobalTable() const { - return Value(state_, GLOBALS); + return Value(mState, GLOBALS); } Value getRegistryTable() const { - return Value(state_, REGISTRY); + return Value(mState, REGISTRY); } Value getEnvironmentTable() const { - return Value(state_, ENVIRONMENT); + return Value(mState, ENVIRONMENT); } Value getTop() const { - return Value(state_, lua_gettop(state_)); + return Value(mState, lua_gettop(mState)); } /** @@ -495,12 +590,12 @@ struct Script int getSize() const { - return lua_gettop(state_); + return lua_gettop(mState); } void setSize(int size) { - lua_settop(state_, size); + lua_settop(mState, size); } void clear() @@ -519,7 +614,7 @@ struct Script bool checkStack(int extra) { - return (bool)lua_checkstack(state_, extra); + return (bool)lua_checkstack(mState, extra); } @@ -529,7 +624,7 @@ struct Script void concat(int n) { - lua_concat(state_, n); + lua_concat(mState, n); } @@ -540,73 +635,77 @@ struct Script template void push(T value) { - lua_pushinteger(state_, lua_Integer(value)); + lua_pushinteger(mState, lua_Integer(value)); } void push(bool value) { - lua_pushboolean(state_, int(value)); + lua_pushboolean(mState, int(value)); } void push(float value) { - lua_pushnumber(state_, (lua_Number)value); + lua_pushnumber(mState, (lua_Number)value); } void push(double value) { - lua_pushnumber(state_, (lua_Number)value); + lua_pushnumber(mState, (lua_Number)value); } void push(const std::string& value) { - lua_pushlstring(state_, value.c_str(), value.length()); + lua_pushlstring(mState, value.c_str(), value.length()); + } + void push(const char* value) + { + lua_pushstring(mState, value); } void push(const char* value, size_t length) { - lua_pushlstring(state_, value, length); + lua_pushlstring(mState, value, length); } void push(const Function& function) { - functions_.push_back(function); + mFunctions.push_back(function); - lua_pushlightuserdata(state_, (void*)&functions_.back()); - lua_pushcclosure(state_, dispatchCall, 1); + lua_pushlightuserdata(mState, (void*)&mFunctions.back()); + lua_pushcclosure(mState, dispatchCall, 1); } void push(void* data) { - lua_pushlightuserdata(state_, data); + lua_pushlightuserdata(mState, data); } void pushNil() { - lua_pushnil(state_); + lua_pushnil(mState); } void pushFromThread(Script& thread, int n) { - lua_xmove(thread.state_, state_, n); + lua_xmove(thread.mState, mState, n); } STATUS pushCode(const std::string& filename) { - return (STATUS)luaL_loadfile(state_, filename.c_str()); + return (STATUS)luaL_loadfile(mState, filename.c_str()); } STATUS pushCode(const std::string& name, const char* buffer, size_t size) { - return (STATUS)luaL_loadbuffer(state_, buffer, size, name.c_str()); + return (STATUS)luaL_loadbuffer(mState, buffer, size, name.c_str()); } void* pushNewData(size_t size) { - return lua_newuserdata(state_, size); + return lua_newuserdata(mState, size); } void pushNewTable() { - lua_newtable(state_); + lua_newtable(mState); } @@ -619,7 +718,7 @@ struct Script STATUS call(int nargs, int nresults = LUA_MULTRET) { - return (STATUS)lua_pcall(state_, nargs, nresults, 0); + return (STATUS)lua_pcall(mState, nargs, nresults, 0); } @@ -629,7 +728,7 @@ struct Script void pop(int n = 1) { - lua_pop(state_, n); + lua_pop(mState, n); } @@ -639,7 +738,7 @@ struct Script Value operator [] (int index) const { - return Value(state_, index); + return Value(mState, index); } @@ -649,12 +748,12 @@ struct Script void get(const std::string& field, int index = GLOBALS) const { - lua_getfield(state_, index, field.c_str()); + lua_getfield(mState, index, field.c_str()); } void set(const std::string& field, int index = GLOBALS) { - lua_setfield(state_, index, field.c_str()); + lua_setfield(mState, index, field.c_str()); } @@ -664,34 +763,34 @@ struct Script void collectAll() { - lua_gc(state_, LUA_GCCOLLECT, 0); + lua_gc(mState, LUA_GCCOLLECT, 0); } void stopCollector() { - lua_gc(state_, LUA_GCSTOP, 0); + lua_gc(mState, LUA_GCSTOP, 0); } void restartCollector() { - lua_gc(state_, LUA_GCRESTART, 0); + lua_gc(mState, LUA_GCRESTART, 0); } int getUsedMemory() const { // in kilobytes - return lua_gc(state_, LUA_GCCOUNT, 0); + return lua_gc(mState, LUA_GCCOUNT, 0); } void collectStep(int step) { - lua_gc(state_, LUA_GCSTEP, step); + lua_gc(mState, LUA_GCSTEP, step); } void tuneCollector(int pause, int step) { - lua_gc(state_, LUA_GCSETPAUSE, pause); - lua_gc(state_, LUA_GCSETSTEPMUL, step); + lua_gc(mState, LUA_GCSETPAUSE, pause); + lua_gc(mState, LUA_GCSETSTEPMUL, step); } @@ -711,8 +810,8 @@ struct Script private: Script(lua_State* state) : - state_(lua_newthread(state)), - isMainThread_(false) {} + mState(lua_newthread(state)), + mIsMainThread(false) {} static int dispatchCall(lua_State* state) { @@ -726,9 +825,9 @@ private: return (*function)(*script); } - lua_State* state_; - bool isMainThread_; - std::list functions_; + lua_State* mState; + bool mIsMainThread; + std::list mFunctions; };