source: trunk/src/lua/lua_handle.cc

Last change on this file was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 1.9 KB
Line 
1// Copyright (C) 2014-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/lua/lua_handle.hh"
8
9#include "nv/core/logging.hh"
10#include "nv/lua/lua_state.hh"
11#include "nv/lua/lua_raw.hh"
12
13// pseudoindex must be valid
14void nv::lua::detail::push_handle_impl( lua_State* L, int pseudoindex, uint32 index )
15{
16        NV_LUA_STACK_ASSERT( L, +1 );
17        lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
18        lua_rawgeti( L, -1, int( index ) );                 // table, entry
19        if ( !lua_istable( L, -1 ) )
20        {
21                NV_LOG_ERROR( "NIL" );
22                lua_pop( L, 2 );
23                lua_pushnil( L );
24                return;
25        }
26        lua_replace( L, -2 );
27}
28
29nv::lua::detail::handle_struct nv::lua::detail::to_handle_impl( lua_State* L, int i, uint32 dindex, uint32 dcounter )
30{
31        NV_LUA_STACK_ASSERT( L, 0 );
32        handle_conversion hc;
33        hc.h.index   = dindex;
34        hc.h.counter = dcounter;
35        if ( lua_istable( L, i ) )
36        {
37                lua_rawgeti( L, i, 1 ); // handle as double
38                hc.d = lua_tonumber( L, -1 );
39                lua_pop( L, 1 );
40        }
41        return hc.h;
42}
43
44void nv::lua::detail::register_handle_impl( lua_State* L, int pseudoindex, uint32 index, uint32 counter, bool empty )
45{
46        if ( empty )
47        {
48                lua_newtable( L );
49        }
50        if ( !lua_istable( L, -1 ) ) return;
51
52        handle_conversion hc;
53        hc.h.index   = index;
54        hc.h.counter = counter;
55        lua_pushnumber( L, hc.d );
56        lua_rawseti( L, -2, 1 );
57
58        lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex );
59        lua_insert( L, -2 );
60        lua_rawseti( L, -2, int( index ) );
61        lua_pop( L, 1 );
62}
63
64void nv::lua::detail::unregister_handle_impl( lua_State* L, int pseudoindex, uint32 index )
65{
66        NV_LUA_STACK_ASSERT( L, 0 );
67        lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
68        lua_pushinteger( L, 0 );
69        lua_rawseti( L, -2, int( index ) );
70        lua_pop( L, 1 );
71}
Note: See TracBrowser for help on using the repository browser.