[182] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | #include "nv/lua/lua_values.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/lua/lua_raw.hh"
|
---|
| 10 | #include "nv/object.hh"
|
---|
| 11 |
|
---|
[208] | 12 | using nv::lua::linteger;
|
---|
| 13 | using nv::lua::lnumber;
|
---|
| 14 | using nv::lua::lunsigned;
|
---|
| 15 |
|
---|
[213] | 16 | int nv::lua::detail::upvalue_index( int i )
|
---|
| 17 | {
|
---|
| 18 | return lua_upvalueindex(i);
|
---|
| 19 | }
|
---|
| 20 |
|
---|
[208] | 21 | bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
|
---|
[182] | 22 | {
|
---|
[208] | 23 | return luaL_testudata( L, index, metatable ) != 0;
|
---|
[182] | 24 | }
|
---|
| 25 |
|
---|
[208] | 26 | void* nv::lua::detail::check_userdata( lua_State *L, int index, const char* metatable )
|
---|
[182] | 27 | {
|
---|
[208] | 28 | return luaL_checkudata( L, index, metatable );
|
---|
[182] | 29 | }
|
---|
| 30 |
|
---|
[208] | 31 | void* nv::lua::detail::allocate_userdata( lua_State *L, size_t size, const char* metatable )
|
---|
[182] | 32 | {
|
---|
[208] | 33 | void* result = lua_newuserdata(L, size);
|
---|
| 34 | luaL_setmetatable( L, metatable );
|
---|
| 35 | return result;
|
---|
[182] | 36 | }
|
---|
| 37 |
|
---|
[208] | 38 | void nv::lua::detail::pop_and_discard( lua_State *L, int count )
|
---|
[182] | 39 | {
|
---|
[208] | 40 | lua_pop( L, count );
|
---|
[182] | 41 | }
|
---|
| 42 |
|
---|
[208] | 43 | void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
|
---|
[182] | 44 | {
|
---|
[208] | 45 | lua_pushunsigned( L, v );
|
---|
[182] | 46 | }
|
---|
| 47 |
|
---|
[208] | 48 | void nv::lua::detail::push_integer ( lua_State *L, linteger v )
|
---|
[182] | 49 | {
|
---|
[208] | 50 | lua_pushinteger( L, v );
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | void nv::lua::detail::push_number ( lua_State *L, lnumber v )
|
---|
| 54 | {
|
---|
| 55 | lua_pushnumber( L, v );
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | void nv::lua::detail::push_bool ( lua_State *L, bool v )
|
---|
| 59 | {
|
---|
| 60 | lua_pushboolean( L, v );
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | void nv::lua::detail::push_string ( lua_State *L, const std::string& s )
|
---|
| 64 | {
|
---|
| 65 | lua_pushlstring( L, s.c_str(), s.size() );
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | void nv::lua::detail::push_cstring ( lua_State *L, const char* s )
|
---|
| 69 | {
|
---|
| 70 | lua_pushstring( L, s );
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void nv::lua::detail::push_object ( lua_State *L, object* o )
|
---|
| 74 | {
|
---|
[182] | 75 | if ( o == nullptr )
|
---|
| 76 | {
|
---|
| 77 | lua_pushnil( L );
|
---|
| 78 | }
|
---|
| 79 | else
|
---|
| 80 | {
|
---|
| 81 | lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[208] | 85 | void nv::lua::detail::push_pointer ( lua_State *L, void* p )
|
---|
[182] | 86 | {
|
---|
| 87 | lua_pushlightuserdata( L, p );
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[208] | 90 | lunsigned nv::lua::detail::to_unsigned( lua_State *L, int index )
|
---|
[206] | 91 | {
|
---|
[208] | 92 | return lua_tounsigned( L, index );
|
---|
[206] | 93 | }
|
---|
| 94 |
|
---|
[208] | 95 | linteger nv::lua::detail::to_integer ( lua_State *L, int index )
|
---|
[182] | 96 | {
|
---|
[208] | 97 | return lua_tointeger( L, index );
|
---|
[182] | 98 | }
|
---|
| 99 |
|
---|
[208] | 100 | lnumber nv::lua::detail::to_number ( lua_State *L, int index )
|
---|
[182] | 101 | {
|
---|
[208] | 102 | return lua_tonumber( L, index );
|
---|
[182] | 103 | }
|
---|
| 104 |
|
---|
[208] | 105 | bool nv::lua::detail::to_bool ( lua_State *L, int index )
|
---|
[182] | 106 | {
|
---|
[208] | 107 | return lua_toboolean( L, index ) != 0;
|
---|
[182] | 108 | }
|
---|
| 109 |
|
---|
[208] | 110 | std::string nv::lua::detail::to_string ( lua_State *L, int index )
|
---|
[182] | 111 | {
|
---|
[208] | 112 | return lua_tostring( L, index );
|
---|
[182] | 113 | }
|
---|
| 114 |
|
---|
[208] | 115 | const char* nv::lua::detail::to_cstring ( lua_State *L, int index )
|
---|
[182] | 116 | {
|
---|
[208] | 117 | return lua_tostring( L, index );
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | nv::object* nv::lua::detail::to_object ( lua_State *L, int index )
|
---|
| 121 | {
|
---|
| 122 | object* o = nullptr;
|
---|
| 123 | if ( lua_istable( L , index ) )
|
---|
[182] | 124 | {
|
---|
| 125 | lua_pushstring( L, "__ptr" );
|
---|
[208] | 126 | lua_rawget( L, index );
|
---|
[182] | 127 | if ( lua_isuserdata( L, -1 ) )
|
---|
| 128 | {
|
---|
[208] | 129 | o = static_cast<object*>( lua_touserdata( L, -1 ) );
|
---|
[182] | 130 | }
|
---|
| 131 | lua_pop( L, 1 );
|
---|
| 132 | }
|
---|
[208] | 133 | return o;
|
---|
[182] | 134 | }
|
---|
| 135 |
|
---|
[208] | 136 | void* nv::lua::detail::to_pointer ( lua_State *L, int index )
|
---|
[182] | 137 | {
|
---|
[208] | 138 | return lua_touserdata( L, index );
|
---|
[182] | 139 | }
|
---|
| 140 |
|
---|
[208] | 141 | lunsigned nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
|
---|
[207] | 142 | {
|
---|
[208] | 143 | return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def );
|
---|
[207] | 144 | }
|
---|
| 145 |
|
---|
[208] | 146 | linteger nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
|
---|
[207] | 147 | {
|
---|
[208] | 148 | return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
|
---|
[207] | 149 | }
|
---|
| 150 |
|
---|
[208] | 151 | lnumber nv::lua::detail::to_number ( lua_State *L, int index, lnumber def )
|
---|
[207] | 152 | {
|
---|
[208] | 153 | return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
|
---|
[207] | 154 | }
|
---|
| 155 |
|
---|
[208] | 156 | bool nv::lua::detail::to_bool ( lua_State *L, int index, bool def )
|
---|
[207] | 157 | {
|
---|
[208] | 158 | return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
|
---|
[207] | 159 | }
|
---|
| 160 |
|
---|
[208] | 161 | std::string nv::lua::detail::to_string ( lua_State *L, int index, const std::string& def )
|
---|
| 162 | {
|
---|
| 163 | return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | const char* nv::lua::detail::to_cstring ( lua_State *L, int index, const char* def )
|
---|
| 167 | {
|
---|
| 168 | return ( lua_type( L, index ) == LUA_TSTRING ? lua_tostring( L, index ) : def );
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | nv::object* nv::lua::detail::to_object ( lua_State *L, int index, nv::object* def )
|
---|
| 172 | {
|
---|
| 173 | object* o = def;
|
---|
| 174 | if ( lua_istable( L , index ) )
|
---|
| 175 | {
|
---|
| 176 | lua_pushstring( L, "__ptr" );
|
---|
| 177 | lua_rawget( L, index );
|
---|
| 178 | if ( lua_isuserdata( L, -1 ) )
|
---|
| 179 | {
|
---|
| 180 | o = static_cast<object*>( lua_touserdata( L, -1 ) );
|
---|
| 181 | }
|
---|
| 182 | lua_pop( L, 1 );
|
---|
| 183 | }
|
---|
| 184 | return o;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
|
---|
| 188 | {
|
---|
| 189 | return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
|
---|
| 190 | } |
---|