source: trunk/src/lua/lua_handle.cc @ 365

Last change on this file since 365 was 365, checked in by epyon, 10 years ago
  • overhaul of logging: no longer stream operated no longer using STL no memory allocation shorthand macros fast file and console I/O
File size: 1.8 KB
RevLine 
[333]1// Copyright (C) 2014 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/lua/lua_handle.hh"
8
[365]9#include "nv/core/logging.hh"
[335]10#include "nv/lua/lua_state.hh"
[333]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{
[335]16        NV_LUA_STACK_ASSERT( L, +1 );
[333]17        lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
[364]18        lua_rawgeti( L, -1, (int)index );                 // table, entry
[335]19        if ( !lua_istable( L, -1 ) )
[333]20        {
[365]21                NV_LOG_ERROR( "NIL" );
[335]22                lua_pop( L, 2 );
23                lua_pushnil( L );
24                return;
[333]25        }
[335]26        lua_replace( L, -2 );
[333]27}
28
29nv::lua::detail::handle_struct nv::lua::detail::to_handle_impl( lua_State* L, int i, uint32 dindex, uint32 dcounter )
30{
[335]31        NV_LUA_STACK_ASSERT( L, 0 );
[333]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 );
[364]60        lua_rawseti( L, -2, (int)index );
[333]61        lua_pop( L, 1 );
62}
63
64void nv::lua::detail::unregister_handle_impl( lua_State* L, int pseudoindex, uint32 index )
65{
[335]66        NV_LUA_STACK_ASSERT( L, 0 );
[333]67        lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
68        lua_pushinteger( L, 0 );
[364]69        lua_rawseti( L, -2, (int)index );
[335]70        lua_pop( L, 1 );
[333]71}
Note: See TracBrowser for help on using the repository browser.