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