source: trunk/src/lua/lua_values.cc @ 490

Last change on this file since 490 was 490, checked in by epyon, 9 years ago
  • temporary Lua 5.1 hardcode
File size: 4.2 KB
RevLine 
[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]11using nv::lua::linteger;
12using nv::lua::lnumber;
13using nv::lua::lunsigned;
14
[213]15int nv::lua::detail::upvalue_index( int i )
16{
17        return lua_upvalueindex(i);
18}
19
[208]20bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable )
[182]21{
[490]22        return nlua_testudata( L, index, metatable ) != 0;
[182]23}
24
[449]25void* nv::lua::detail::raw_check_userdata( lua_State *L, int index, const char* metatable )
[182]26{
[490]27        return nlua_testudata( L, index, metatable );
[182]28}
29
[449]30void* nv::lua::detail::raw_allocate_userdata( lua_State *L, size_t size, const char* metatable )
[182]31{
[208]32        void* result = lua_newuserdata(L, size);
[490]33        nlua_setmetatable( L, metatable );
[208]34        return result;
[182]35}
36
[208]37void nv::lua::detail::pop_and_discard( lua_State *L, int count )
[182]38{
[208]39        lua_pop( L, count );
[182]40}
41
[265]42void nv::lua::detail::push_nil( lua_State *L )
43{
44        lua_pushnil( L );
45}
46
[208]47void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v )
[182]48{
[490]49        nlua_pushunsigned( L, v );
[182]50}
51
[208]52void nv::lua::detail::push_integer ( lua_State *L, linteger v )
[182]53{
[208]54        lua_pushinteger( L, v );
55}
56
57void nv::lua::detail::push_number  ( lua_State *L, lnumber v )
58{
59        lua_pushnumber( L, v );
60}
61
62void nv::lua::detail::push_bool    ( lua_State *L, bool v )
63{
64        lua_pushboolean( L, v );
65}
66
[399]67void nv::lua::detail::push_string_view( lua_State *L, string_view s )
[358]68{
69        lua_pushlstring( L, s.data(), s.size() );
70}
71
[208]72void nv::lua::detail::push_pointer ( lua_State *L, void* p )
[182]73{
74        lua_pushlightuserdata( L, p );
75}
76
[265]77void nv::lua::detail::push_ref_object ( lua_State *L, ref object )
78{
79        lua_rawgeti( L, LUA_REGISTRYINDEX, object.get() );
80}
81
82
[208]83lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index )
[206]84{
[490]85        return nlua_tounsigned( L, index );
[206]86}
87
[208]88linteger    nv::lua::detail::to_integer ( lua_State *L, int index )
[182]89{
[208]90        return lua_tointeger( L, index );
[182]91}
92
[208]93lnumber     nv::lua::detail::to_number  ( lua_State *L, int index )
[182]94{
[208]95        return lua_tonumber( L, index );
[182]96}
97
[208]98bool        nv::lua::detail::to_bool    ( lua_State *L, int index )
[182]99{
[208]100        return lua_toboolean( L, index ) != 0;
[182]101}
102
[399]103nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index )
[358]104{
105        size_t length = 0;
106        const char* result = lua_tolstring( L, index, &length );
[399]107        return string_view( result, length );
[358]108}
109
[262]110void*       nv::lua::detail::to_pointer ( lua_State *L, int index )
[208]111{
[262]112        return lua_touserdata( L, index );
113}
114
115void* nv::lua::detail::to_ref_object  ( lua_State *L, int index )
116{
117        void* o = nullptr;
[208]118        if ( lua_istable( L , index ) )
[182]119        {
120                lua_pushstring( L, "__ptr" );
[208]121                lua_rawget( L, index );
[182]122                if ( lua_isuserdata( L, -1 ) )
123                {
[262]124                        o = lua_touserdata( L, -1 );
[182]125                }
126                lua_pop( L, 1 );
127        }
[208]128        return o;
[182]129}
130
[208]131lunsigned   nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def )
[207]132{
[490]133        return ( lua_type( L, index ) == LUA_TNUMBER ? nlua_tounsigned( L, index ) : def );
[207]134}
135
[208]136linteger    nv::lua::detail::to_integer ( lua_State *L, int index, linteger def )
[207]137{
[208]138        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def );
[207]139}
140
[208]141lnumber     nv::lua::detail::to_number  ( lua_State *L, int index, lnumber def )
[207]142{
[208]143        return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def );
[207]144}
145
[208]146bool        nv::lua::detail::to_bool    ( lua_State *L, int index, bool def )
[207]147{
[208]148        return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def );
[207]149}
150
[440]151nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index, string_view def )
[208]152{
[440]153        return ( lua_type( L, index ) == LUA_TSTRING ? nlua_tostringview( L, index ) : def );
[208]154}
155
[262]156void*       nv::lua::detail::to_pointer ( lua_State *L, int index, void* def )
[208]157{
[262]158        return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def );
159}
160
161void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def )
162{
163        void* o = def;
[208]164        if ( lua_istable( L , index ) )
165        {
[360]166                lua_pushliteral( L, "__ptr" );
[208]167                lua_rawget( L, index );
168                if ( lua_isuserdata( L, -1 ) )
169                {
[262]170                        o = lua_touserdata( L, -1 );
[208]171                }
172                lua_pop( L, 1 );
173        }
174        return o;
175}
Note: See TracBrowser for help on using the repository browser.