Changeset 206
- Timestamp:
- 08/19/13 06:37:47 (12 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/lua/lua_state.hh
r197 r206 31 31 typedef int reference; 32 32 33 class state_wrapper 34 { 35 public: 36 state_wrapper( lua_State* a_state, bool a_owner ) : m_state( a_state ), m_owner( a_owner ), m_global( true ) {} 37 virtual ~state_wrapper(); 38 39 template < typename R > 40 R call( const path& p ) 41 { 42 if ( push_function( p, m_global ) ) 43 { 44 if ( call_function( 0, detail::return_count< R >::value ) == 0 ) 45 { 46 return detail::pop_return_value<R>( m_state ); 47 } 48 } 49 return R(); 50 } 51 template < typename R, typename T1 > 52 R call( const path& p, const T1& p1 ) 53 { 54 if ( push_function( p, m_global ) ) 55 { 56 detail::push_value( m_state, p1 ); 57 if ( call_function( 1, detail::return_count< R >::value ) == 0 ) 58 { 59 return detail::pop_return_value<R>( m_state ); 60 } 61 } 62 return R(); 63 } 64 template < typename R, typename T1, typename T2 > 65 R call( const path& p, const T1& p1, const T2& p2 ) 66 { 67 if ( push_function( p, m_global ) ) 68 { 69 detail::push_values( m_state, p1, p2 ); 70 if ( call_function( 2, detail::return_count< R >::value ) == 0 ) 71 { 72 return detail::pop_return_value<R>( m_state ); 73 } 74 } 75 return R(); 76 } 77 template < typename R, typename T1, typename T2, typename T3 > 78 R call( const path& p, const T1& p1, const T2& p2, const T3& p3 ) 79 { 80 if ( push_function( p, m_global ) ) 81 { 82 detail::push_values( m_state, p1, p2, p3 ); 83 if ( call_function( 3, detail::return_count< R >::value ) == 0 ) 84 { 85 return detail::pop_return_value<R>( m_state ); 86 } 87 } 88 return R(); 89 } 90 template < typename R, typename T1, typename T2, typename T3, typename T4 > 91 R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 ) 92 { 93 if ( push_function( p, m_global ) ) 94 { 95 detail::push_value( m_state, p1, p2, p3, p4 ); 96 if ( call_function( 4, detail::return_count< R >::value ) == 0 ) 97 { 98 return detail::pop_return_value<R>( m_state ); 99 } 100 } 101 return R(); 102 } 103 104 template< typename R > 105 R get( const path& p ) 106 { 107 if ( p.resolve( m_state, m_global ) ) 108 { 109 return detail::pop_return_value<R>( m_state ); 110 } 111 return R(); 112 } 113 114 template< typename R, typename T > 115 R get( const T& key ) 116 { 117 if ( m_global ) push_global_table(); 118 detail::push_value( m_state, key ); 119 call_get(); 120 if ( m_global ) pop_global_table(); 121 return detail::pop_return_value<R>( m_state ); 122 } 123 124 template< typename R, typename T > 125 R raw_get( const T& key ) 126 { 127 if ( m_global ) push_global_table(); 128 detail::push_value( m_state, key ); 129 call_get_raw(); 130 if ( m_global ) pop_global_table(); 131 return detail::pop_return_value<R>( m_state ); 132 } 133 134 bool is_defined( const path& p ) { return is_defined( p, m_global ); } 135 protected: 136 bool is_defined( const path& p, bool global ); 137 bool push_function( const path& p, bool global ); 138 int call_function( int nargs, int nresults ); 139 private: 140 void push_global_table(); 141 void pop_global_table(); 142 void call_get(); 143 void call_get_raw(); 144 protected: 145 lua_State* m_state; 146 bool m_owner; 147 bool m_global; 148 }; 149 33 150 class stack_guard 34 151 { … … 43 160 }; 44 161 162 163 45 164 class table_guard; 46 class state 165 class state : public state_wrapper 47 166 { 48 167 friend class stack_guard; … … 56 175 int get_stack_size(); 57 176 void log_stack(); 58 bool is_defined( const path& p, bool global = true );59 177 lua_State* get_raw(); 60 178 reference register_object( object * o ); 61 179 reference register_object( object * o, const char* lua_name ); 180 void store_metadata( object* o, const std::string& metaname, void* pointer ); 62 181 void unregister_object( object * o ); 63 182 void register_enum( type_database* db, const std::string& name, const std::string& prefix = std::string() ); 64 operator lua_State*() { return L; } 65 ~state(); 66 67 template < typename R > 68 R call( const path& p ) 69 { 70 if ( push_function( p ) ) 71 { 72 if ( call_function( 0, detail::return_count< R >::value ) == 0 ) 73 { 74 return detail::pop_return_value<R>( L ); 75 } 76 } 77 return R(); 78 } 79 template < typename R, typename T1 > 80 R call( const path& p, const T1& p1 ) 81 { 82 if ( push_function( p ) ) 83 { 84 detail::push_value( L, p1 ); 85 if ( call_function( 1, detail::return_count< R >::value ) == 0 ) 86 { 87 return detail::pop_return_value<R>( L ); 88 } 89 } 90 return R(); 91 } 92 template < typename R, typename T1, typename T2 > 93 R call( const path& p, const T1& p1, const T2& p2 ) 94 { 95 if ( push_function( p ) ) 96 { 97 detail::push_values( L, p1, p2 ); 98 if ( call_function( 2, detail::return_count< R >::value ) == 0 ) 99 { 100 return detail::pop_return_value<R>( L ); 101 } 102 } 103 return R(); 104 } 105 template < typename R, typename T1, typename T2, typename T3 > 106 R call( const path& p, const T1& p1, const T2& p2, const T3& p3 ) 107 { 108 if ( push_function( p ) ) 109 { 110 detail::push_values( L, p1, p2, p3 ); 111 if ( call_function( 3, detail::return_count< R >::value ) == 0 ) 112 { 113 return detail::pop_return_value<R>( L ); 114 } 115 } 116 return R(); 117 } 118 template < typename R, typename T1, typename T2, typename T3, typename T4 > 119 R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 ) 120 { 121 if ( push_function( p ) ) 122 { 123 detail::push_value( L, p1, p2, p3, p4 ); 124 if ( call_function( 4, detail::return_count< R >::value ) == 0 ) 125 { 126 return detail::pop_return_value<R>( L ); 127 } 128 } 129 return R(); 130 } 131 132 private: 133 bool push_function( const path& p, bool global = true ); 134 int call_function( int nargs, int nresults ); 183 operator lua_State*() { return m_state; } 184 private: 135 185 int load_string( const std::string& code, const std::string& name ); 136 186 int load_stream( std::istream& stream, const std::string& name ); 137 187 int load_file( const std::string& filename ); 138 188 int do_current( const std::string& name, int rvalues = 0 ); 139 bool push( const std::string& path, bool global = true );140 189 void deep_pointer_copy( int index, void* obj ); 141 private: 142 bool m_owner; 143 lua_State* L; 144 }; 145 146 class table_guard 190 }; 191 192 class table_guard : public state_wrapper 147 193 { 148 194 public: 149 195 table_guard( state* lstate, const path& p, bool global = true ); 150 196 table_guard( const table_guard& parent, const path& p ); 197 virtual ~table_guard(); 151 198 size_t get_size(); 152 199 bool has_field( const std::string& element ); … … 157 204 double get_double( const std::string& element, double defval = 0.0 ); 158 205 bool get_boolean( const std::string& element, bool defval = false ); 159 bool is_defined( const path& p );160 161 template< typename R, typename T >162 R get( const T& key )163 {164 detail::push_value( L->L, key );165 call_get();166 return detail::pop_return_value<R>( L->L );167 }168 169 template< typename R, typename T >170 R raw_get( const T& key )171 {172 detail::push_value( L->L, key );173 call_get_raw();174 return detail::pop_return_value<R>( L->L );175 }176 206 177 207 template< uint32 SIZE, typename T > … … 189 219 } 190 220 191 template < typename R > 192 R call( const path& p ) 193 { 194 if ( L->push_function( p, false ) ) 195 { 196 if ( call_function( 0, detail::return_count< R >::value ) == 0 ) 197 { 198 return detail::pop_return_value<R>( L ); 199 } 200 } 201 return R(); 202 } 203 template < typename R, typename T1 > 204 R call( const path& p, const T1& p1 ) 205 { 206 if ( L->push_function( p, false ) ) 207 { 208 detail::push_value( L, p1 ); 209 if ( L->call_function( 1, detail::return_count< R >::value ) == 0 ) 210 { 211 return detail::pop_return_value<R>( L ); 212 } 213 } 214 return R(); 215 } 216 template < typename R, typename T1, typename T2 > 217 R call( const path& p, const T1& p1, const T2& p2 ) 218 { 219 if ( L->push_function( p, false ) ) 220 { 221 detail::push_values( L, p1, p2 ); 222 if ( L->call_function( 2, detail::return_count< R >::value ) == 0 ) 223 { 224 return detail::pop_return_value<R>( L ); 225 } 226 } 227 return R(); 228 } 229 template < typename R, typename T1, typename T2, typename T3 > 230 R call( const path& p, const T1& p1, const T2& p2, const T3& p3 ) 231 { 232 if ( L->push_function( p, false ) ) 233 { 234 detail::push_values( L, p1, p2, p3 ); 235 if ( L->call_function( 3, detail::return_count< R >::value ) == 0 ) 236 { 237 return detail::pop_return_value<R>( L ); 238 } 239 } 240 return R(); 241 } 242 template < typename R, typename T1, typename T2, typename T3, typename T4 > 243 R call( const path& p, const T1& p1, const T2& p2, const T3& p3, const T4& p4 ) 244 { 245 if ( L->push_function( p, false ) ) 246 { 247 detail::push_value( L, p1, p2, p3, p4 ); 248 if ( L->call_function( 4, detail::return_count< R >::value ) == 0 ) 249 { 250 return detail::pop_return_value<R>( L ); 251 } 252 } 253 return R(); 254 } 255 256 private: 257 bool push_function( const path& p ); 258 int call_function( int nargs, int nresults ); 259 void call_get(); 260 void call_get_raw(); 221 private: 261 222 void get_raw_flags( const std::string& element, uint8* data, uint32 count ); 262 223 263 state* L; 264 stack_guard m_guard; 224 int m_level; 265 225 }; 266 226 -
trunk/nv/lua/lua_values.hh
r197 r206 80 80 void push_values( lua_State *L, const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5 ) { push_value( L, p1 ); push_value( L, p2 ); push_value( L, p3 ); push_value( L, p4 ); push_value( L, p5 ); } 81 81 82 void pop_value( lua_State *L, int& n ); 82 83 void pop_value( lua_State *L, long& n ); 83 84 void pop_value( lua_State *L, double& n ); … … 95 96 { 96 97 T ret; 97 p ass_traits<T>::pop( L, ret );98 pop_value( L, ret ); 98 99 return ret; 99 100 } -
trunk/src/lua/lua_area.cc
r204 r206 150 150 static int nlua_area_coords( lua_State* L ) 151 151 { 152 nv::rectangle* a( nlua_to_parea( L, lua_upvalueindex(1)) );152 nv::rectangle* a( nlua_to_parea( L, 1 ) ); 153 153 nv::ivec2 c( a->ul ); 154 154 c.x--; … … 184 184 static int nlua_area_edges( lua_State* L ) 185 185 { 186 nv::rectangle* a( nlua_to_parea( L, lua_upvalueindex(1)) );186 nv::rectangle* a( nlua_to_parea( L, 1 ) ); 187 187 nv::ivec2 c( a->ul ); 188 188 c.x--; -
trunk/src/lua/lua_map_area.cc
r198 r206 9 9 #include "nv/lua/lua_area.hh" 10 10 #include "nv/lua/lua_glm.hh" 11 #include "nv/lua/lua_values.hh" 11 12 #include "nv/lua/lua_raw.hh" 12 13 … … 53 54 nv::map_area* nlua_to_map_area( lua_State* L, int index ) 54 55 { 55 return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE ); 56 if ( lua_type( L, index ) == LUA_TTABLE ) 57 { 58 nv::map_area* o = nullptr; 59 if ( lua_istable( L , index ) ) 60 { 61 lua_pushstring( L, "__map_area_ptr" ); 62 lua_rawget( L, index ); 63 if ( lua_isuserdata( L, -1 ) ) 64 { 65 o = static_cast<nv::map_area*>( lua_touserdata( L, -1 ) ); 66 } 67 lua_pop( L, 1 ); 68 } 69 return o; 70 } 71 else 72 { 73 return *(nv::map_area**)luaL_checkudata( L, index, NLUA_MAP_AREA_METATABLE ); 74 } 56 75 } 57 76 … … 83 102 { 84 103 nv::map_area* ma = nlua_to_map_area( L, 1 ); 85 nlua_push_area( L, ma->get_rectangle() ); 104 nv::rectangle r = ma->get_rectangle(); 105 r.lr.x -= 1; 106 r.lr.y -= 1; 107 nlua_push_area( L, r ); 86 108 return 1; 87 109 } -
trunk/src/lua/lua_state.cc
r204 r206 16 16 17 17 lua::stack_guard::stack_guard( lua::state* aL ) 18 : L(aL), m_level( lua_gettop(aL-> L) )18 : L(aL), m_level( lua_gettop(aL->m_state) ) 19 19 { 20 20 … … 22 22 23 23 lua::stack_guard::stack_guard( lua::state& aL ) 24 : L(&aL), m_level( lua_gettop(aL. L) )24 : L(&aL), m_level( lua_gettop(aL.m_state) ) 25 25 { 26 26 … … 29 29 lua::stack_guard::~stack_guard() 30 30 { 31 lua_settop( L->L, m_level ); 32 } 33 34 lua::state::state( bool load_libs /*= false*/ ) 31 lua_settop( L->m_state, m_level ); 32 } 33 34 lua::state::state( lua_State* state ) : state_wrapper( state, false ) 35 { 36 37 } 38 39 lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ) 35 40 { 36 41 load_lua_library(); 37 42 m_owner = true; 38 L= luaL_newstate( );39 40 lua_pushcfunction( L, luaopen_base);41 lua_pushliteral( L, LUA_TABLIBNAME);42 lua_call( L, 1, 0);43 m_state = luaL_newstate( ); 44 45 lua_pushcfunction(m_state, luaopen_base); 46 lua_pushliteral(m_state, LUA_TABLIBNAME); 47 lua_call(m_state, 1, 0); 43 48 44 49 if ( load_libs ) … … 55 60 for(; lib->func != NULL; lib++) 56 61 { 57 lib->func( L);62 lib->func( m_state ); 58 63 } 59 64 } … … 65 70 { 66 71 NV_LOG( nv::LOG_TRACE, "Loading Lua string '" << name << "'"); 67 return luaL_loadbuffer( L, code.c_str(), code.length(), name.c_str() );72 return luaL_loadbuffer( m_state, code.c_str(), code.length(), name.c_str() ); 68 73 } 69 74 … … 79 84 { 80 85 NV_LOG( nv::LOG_NOTICE, "Loading Lua file '" << filename << "'"); 81 return luaL_loadfile( L, filename.c_str() );86 return luaL_loadfile( m_state, filename.c_str() ); 82 87 } 83 88 … … 88 93 if (result) 89 94 { 90 NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring( L, -1));95 NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring(m_state, -1)); 91 96 return false; 92 97 } … … 100 105 if (result) 101 106 { 102 NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring( L, -1));107 NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring(m_state, -1)); 103 108 return false; 104 109 } … … 112 117 if (result) 113 118 { 114 NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring( L, -1));119 NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring(m_state, -1)); 115 120 return false; 116 121 } … … 120 125 int lua::state::do_current( const std::string& name, int rvalues ) 121 126 { 122 int result = lua_pcall( L, 0, rvalues, 0);127 int result = lua_pcall(m_state, 0, rvalues, 0); 123 128 if (result) 124 129 { 125 NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring( L, -1));126 lua_pop( L, 1 );127 } 128 return result; 129 } 130 131 lua::state ::~state()130 NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring(m_state, -1)); 131 lua_pop( m_state, 1 ); 132 } 133 return result; 134 } 135 136 lua::state_wrapper::~state_wrapper() 132 137 { 133 138 if (m_owner) 134 139 { 135 lua_close( L);140 lua_close( m_state ); 136 141 } 137 142 } … … 145 150 if (global) 146 151 { 147 lua_getglobal( L, path.c_str() );152 lua_getglobal( m_state, path.c_str() ); 148 153 } 149 154 else 150 155 { 151 lua_getfield( L, -1, path.c_str() );152 } 153 return !lua_isnil( L, -1 );156 lua_getfield( m_state, -1, path.c_str() ); 157 } 158 return !lua_isnil( m_state, -1 ); 154 159 } 155 160 … … 163 168 if (global) 164 169 { 165 lua_getglobal( L, path.substr(start,point-start).c_str() );170 lua_getglobal( m_state, path.substr(start,point-start).c_str() ); 166 171 } 167 172 else 168 173 { 169 lua_getfield( L, -1, path.substr(start,point-start).c_str() );174 lua_getfield( m_state, -1, path.substr(start,point-start).c_str() ); 170 175 } 171 176 } 172 177 else 173 178 { 174 if ( lua_istable( L, -1 ) )179 if ( lua_istable( m_state, -1 ) ) 175 180 { 176 lua_pushstring( L, path.substr(start,point-start).c_str() );177 lua_gettable( L, -2 );178 lua_insert( L, -2 );179 lua_pop( L, 1 );181 lua_pushstring( m_state, path.substr(start,point-start).c_str() ); 182 lua_gettable( m_state, -2 ); 183 lua_insert( m_state, -2 ); 184 lua_pop( m_state, 1 ); 180 185 } 181 186 else 182 187 { 183 lua_pop( L, 1);184 lua_pushnil( L);188 lua_pop(m_state, 1); 189 lua_pushnil(m_state); 185 190 return false; 186 191 } … … 195 200 int lua::state::get_stack_size() 196 201 { 197 return lua_gettop( L);202 return lua_gettop( m_state ); 198 203 } 199 204 200 205 lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global ) 201 : L(lstate), m_guard(lstate) 202 { 203 if ( !p.resolve( L->get_raw(), global ) ) 206 : state_wrapper( lstate->get_raw(), false ), m_level(0) 207 { 208 m_global = false; 209 m_level = lua_gettop( m_state ); 210 if ( !p.resolve( m_state, global ) ) 204 211 { 205 212 // TODO : error handling … … 208 215 209 216 lua::table_guard::table_guard( const table_guard& parent, const path& p ) 210 : L( parent.L ), m_guard( parent.L ) 211 { 212 if ( !p.resolve( L->get_raw(), false ) ) 217 : state_wrapper( parent.m_state, false ), m_level(0) 218 { 219 m_global = false; 220 m_level = lua_gettop( m_state ); 221 if ( !p.resolve( m_state, false ) ) 213 222 { 214 223 // TODO : error handling … … 218 227 size_t lua::table_guard::get_size() 219 228 { 220 return lua_rawlen( L->get_raw(), -1 );229 return lua_rawlen( m_state, -1 ); 221 230 } 222 231 … … 224 233 bool lua::table_guard::has_field( const string& element ) 225 234 { 226 lua_getfield( L->L, -1, element.c_str() );227 bool result = lua_isnil( L->L, -1 );228 lua_pop( L->L, 1 );235 lua_getfield( m_state, -1, element.c_str() ); 236 bool result = lua_isnil( m_state, -1 ); 237 lua_pop( m_state, 1 ); 229 238 return result; 230 239 } … … 232 241 string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ ) 233 242 { 234 lua_getfield( L->L, -1, element.c_str() );235 string result( ( lua_type( L->L, -1 ) == LUA_TSTRING ) ? lua_tostring( L->L, -1 ) : defval );236 lua_pop( L->L, 1 );243 lua_getfield( m_state, -1, element.c_str() ); 244 string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval ); 245 lua_pop( m_state, 1 ); 237 246 return result; 238 247 } … … 240 249 char lua::table_guard::get_char( const string& element, char defval /*= "" */ ) 241 250 { 242 lua_getfield( L->L, -1, element.c_str() );243 char result = ( lua_type( L->L, -1 ) == LUA_TSTRING && lua_rawlen( L->L, -1 ) > 0 ) ? lua_tostring( L->L, -1 )[0] : defval;244 lua_pop( L->L, 1 );251 lua_getfield( m_state, -1, element.c_str() ); 252 char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval; 253 lua_pop( m_state, 1 ); 245 254 return result; 246 255 } … … 248 257 int lua::table_guard::get_integer( const string& element, int defval /*= "" */ ) 249 258 { 250 lua_getfield( L->L, -1, element.c_str() );251 lua_Integer result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tointeger( L->L, -1 ) : defval;252 lua_pop( L->L, 1 );259 lua_getfield( m_state, -1, element.c_str() ); 260 lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval; 261 lua_pop( m_state, 1 ); 253 262 return static_cast< int >( result ); 254 263 } … … 256 265 unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ ) 257 266 { 258 lua_getfield( L->L, -1, element.c_str() );259 unsigned result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tounsigned( L->L, -1 ) : defval;260 lua_pop( L->L, 1 );267 lua_getfield( m_state, -1, element.c_str() ); 268 unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval; 269 lua_pop( m_state, 1 ); 261 270 return result; 262 271 } … … 264 273 double lua::table_guard::get_double( const string& element, double defval /*= "" */ ) 265 274 { 266 lua_getfield( L->L, -1, element.c_str() );267 double result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tonumber( L->L, -1 ) : defval;268 lua_pop( L->L, 1 );275 lua_getfield( m_state, -1, element.c_str() ); 276 double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval; 277 lua_pop( m_state, 1 ); 269 278 return result; 270 279 } … … 272 281 bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ ) 273 282 { 274 lua_getfield( L->L, -1, element.c_str() ); 275 bool result = lua_type( L->L, -1 ) == LUA_TBOOLEAN ? lua_toboolean( L->L, -1 ) != 0 : defval; 276 lua_pop( L->L, 1 ); 277 return result; 278 } 279 280 void lua::table_guard::call_get() 281 { 282 lua_gettable( L->L, -2 ); 283 } 284 285 void lua::table_guard::call_get_raw() 286 { 287 lua_rawget( L->L, -2 ); 288 } 289 290 bool nv::lua::table_guard::is_defined( const path& p ) 291 { 292 return L->is_defined( p, false ); 283 lua_getfield( m_state, -1, element.c_str() ); 284 bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval; 285 lua_pop( m_state, 1 ); 286 return result; 287 } 288 289 void nv::lua::state_wrapper::push_global_table() 290 { 291 lua_pushglobaltable( m_state ); 292 } 293 294 void nv::lua::state_wrapper::pop_global_table() 295 { 296 lua_replace( m_state, -2 ); 297 } 298 299 void lua::state_wrapper::call_get() 300 { 301 lua_gettable( m_state, -2 ); 302 } 303 304 void lua::state_wrapper::call_get_raw() 305 { 306 lua_rawget( m_state, -2 ); 293 307 } 294 308 295 309 void lua::table_guard::get_raw_flags( const std::string& element, uint8* data, uint32 count ) 296 310 { 297 lua_getfield( L->L, -1, element.c_str() );298 if ( lua_type( L->L, -1 ) != LUA_TTABLE )299 { 300 lua_pop( L->L, 1 );311 lua_getfield( m_state, -1, element.c_str() ); 312 if ( lua_type( m_state, -1 ) != LUA_TTABLE ) 313 { 314 lua_pop( m_state, 1 ); 301 315 return; 302 316 } 303 nlua_toflags( L->L, -1, data, count );304 lua_pop( L->L, 1 );317 nlua_toflags( m_state, -1, data, count ); 318 lua_pop( m_state, 1 ); 305 319 } 306 320 … … 308 322 void lua::state::log_stack() 309 323 { 310 int top = lua_gettop( L);324 int top = lua_gettop(m_state); 311 325 NV_LOG( LOG_DEBUG, "Stack dump (" << top << ")"); 312 326 for ( int i = 0; i < top; ++i ) 313 327 { 314 NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename( L, lua_type(L, i+1) ) << " = " << nlua_typecontent(L, i+1) );328 NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(m_state, lua_type(m_state, i+1) ) << " = " << nlua_typecontent(m_state, i+1) ); 315 329 } 316 330 } … … 318 332 lua_State* lua::state::get_raw() 319 333 { 320 return L;334 return m_state; 321 335 } 322 336 … … 325 339 if ( o == nullptr ) return ref_none; 326 340 stack_guard guard( this ); 327 lua_getglobal( L, lua_name );328 if ( lua_isnil( L, -1 ) )341 lua_getglobal( m_state, lua_name ); 342 if ( lua_isnil( m_state, -1 ) ) 329 343 { 330 344 NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" ); 331 345 } 332 346 deep_pointer_copy( -1, o ); 333 return luaL_ref( L, LUA_REGISTRYINDEX );347 return luaL_ref( m_state, LUA_REGISTRYINDEX ); 334 348 } 335 349 … … 348 362 if (!o) return; 349 363 stack_guard guard( this ); 350 lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );351 lua_pushstring( L, "__ptr" );352 lua_pushboolean( L, false );353 lua_rawset( L, -3 );354 lua_pop( L, 1 );355 luaL_unref( L, LUA_REGISTRYINDEX, o->get_lua_index() );364 lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() ); 365 lua_pushstring( m_state, "__ptr" ); 366 lua_pushboolean( m_state, false ); 367 lua_rawset( m_state, -3 ); 368 lua_pop( m_state, 1 ); 369 luaL_unref( m_state, LUA_REGISTRYINDEX, o->get_lua_index() ); 356 370 } 357 371 358 372 void lua::state::deep_pointer_copy( int index, void* obj ) 359 373 { 360 index = lua_absindex( L, index );361 lua_newtable( L);362 lua_pushnil( L);374 index = lua_absindex( m_state, index ); 375 lua_newtable( m_state ); 376 lua_pushnil( m_state ); 363 377 bool has_functions = false; 364 378 bool has_metatable = false; 365 379 366 while ( lua_next( L, index ) != 0 )367 { 368 if ( lua_isfunction( L, -1 ) )380 while ( lua_next( m_state, index ) != 0 ) 381 { 382 if ( lua_isfunction( m_state, -1 ) ) 369 383 has_functions = true; 370 else if ( lua_istable( L, -1 ) )384 else if ( lua_istable( m_state, -1 ) ) 371 385 { 372 386 deep_pointer_copy( -1, obj ); 373 lua_insert( L, -2 );374 lua_pop( L, 1 );375 } 376 lua_pushvalue( L, -2 );377 lua_insert( L, -2 );378 lua_settable( L, -4 );379 } 380 381 if ( lua_getmetatable( L, -2 ) )382 { 383 lua_setmetatable( L, -2 );387 lua_insert( m_state, -2 ); 388 lua_pop( m_state, 1 ); 389 } 390 lua_pushvalue( m_state, -2 ); 391 lua_insert( m_state, -2 ); 392 lua_settable( m_state, -4 ); 393 } 394 395 if ( lua_getmetatable( m_state, -2 ) ) 396 { 397 lua_setmetatable( m_state, -2 ); 384 398 has_metatable = true; 385 399 } … … 387 401 if ( has_functions || has_metatable ) 388 402 { 389 lua_pushstring( L, "__ptr" );390 lua_pushlightuserdata( L, obj );391 lua_rawset( L, -3 );403 lua_pushstring( m_state, "__ptr" ); 404 lua_pushlightuserdata( m_state, obj ); 405 lua_rawset( m_state, -3 ); 392 406 } 393 407 } … … 399 413 for ( const auto& entry : et->enum_list ) 400 414 { 401 lua_pushinteger( L, entry.value );415 lua_pushinteger( m_state, entry.value ); 402 416 if ( prefix.empty() ) 403 417 { 404 lua_setglobal( L, entry.name.c_str() );418 lua_setglobal( m_state, entry.name.c_str() ); 405 419 } 406 420 else 407 421 { 408 lua_setglobal( L, ( prefix + entry.name ).c_str() ); 409 } 410 } 411 } 412 413 414 bool nv::lua::state::is_defined( const path& p, bool global ) 415 { 416 if ( !p.resolve( L, global ) ) 417 { 418 return false; 419 } 420 bool result = !lua_isnil( L, -1 ); 421 lua_pop( L, 1 ); 422 return result; 423 } 424 425 int lua::state::call_function( int nargs, int nresults ) 426 { 427 int status = lua_pcall( L, nargs, nresults, 0 ); 422 lua_setglobal( m_state, ( prefix + entry.name ).c_str() ); 423 } 424 } 425 } 426 427 void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer ) 428 { 429 if (!o) return; 430 stack_guard guard( this ); 431 lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() ); 432 lua_pushstring( m_state, metaname.c_str() ); 433 lua_pushlightuserdata( m_state, pointer ); 434 lua_rawset( m_state, -3 ); 435 lua_pop( m_state, 1 ); 436 } 437 438 bool nv::lua::state_wrapper::is_defined( const path& p, bool global ) 439 { 440 if ( !p.resolve( m_state, global ) ) 441 { 442 return false; 443 } 444 bool result = !lua_isnil( m_state, -1 ); 445 lua_pop( m_state, 1 ); 446 return result; 447 } 448 449 bool nv::lua::state_wrapper::push_function( const path& p, bool global ) 450 { 451 if ( !p.resolve( m_state, global ) ) 452 { 453 NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() ); 454 return false; 455 } 456 457 if ( !lua_isfunction( m_state, -1 ) ) 458 { 459 lua_pop( m_state, 1 ); 460 NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() ); 461 return false; 462 } 463 return true; 464 } 465 466 int nv::lua::state_wrapper::call_function( int nargs, int nresults ) 467 { 468 int status = lua_pcall( m_state, nargs, nresults, 0 ); 428 469 if ( status != 0 ) 429 470 { 430 std::string error = lua_tostring( L, -1 );431 lua_pop( L, 1 );471 std::string error = lua_tostring( m_state, -1 ); 472 lua_pop( m_state, 1 ); 432 473 NV_LOG( LOG_ERROR, "Lua error : " << error ) 433 474 } … … 435 476 } 436 477 437 bool lua::state::push_function( const path& p, bool global ) 438 { 439 if ( !p.resolve( L, global ) ) 440 { 441 NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() ); 442 return false; 443 } 444 445 if ( !lua_isfunction( L, -1 ) ) 446 { 447 lua_pop( L, 1 ); 448 NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() ); 449 return false; 450 } 451 return true; 452 } 453 478 lua::table_guard::~table_guard() 479 { 480 lua_settop( m_state, m_level ); 481 } -
trunk/src/lua/lua_values.cc
r183 r206 52 52 } 53 53 54 void nv::lua::detail::pop_value( lua_State *L, int& n ) 55 { 56 n = lua_tointeger( L, -1 ); 57 lua_pop( L, 1 ); 58 } 59 54 60 void nv::lua::detail::pop_value( lua_State *L, long& n ) 55 61 {
Note: See TracChangeset
for help on using the changeset viewer.