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 |
|
---|
9 | #include "nv/lua/lua_state.hh"
|
---|
10 | #include "nv/lua/lua_raw.hh"
|
---|
11 |
|
---|
12 | // pseudoindex must be valid
|
---|
13 | void nv::lua::detail::push_handle_impl( lua_State* L, int pseudoindex, uint32 index )
|
---|
14 | {
|
---|
15 | NV_LUA_STACK_ASSERT( L, +1 );
|
---|
16 | lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
|
---|
17 | lua_rawgeti( L, -1, index ); // table, entry
|
---|
18 | if ( !lua_istable( L, -1 ) )
|
---|
19 | {
|
---|
20 | NV_LOG( nv::LOG_ERROR, "NIL" );
|
---|
21 | lua_pop( L, 2 );
|
---|
22 | lua_pushnil( L );
|
---|
23 | return;
|
---|
24 | }
|
---|
25 | lua_replace( L, -2 );
|
---|
26 | }
|
---|
27 |
|
---|
28 | nv::lua::detail::handle_struct nv::lua::detail::to_handle_impl( lua_State* L, int i, uint32 dindex, uint32 dcounter )
|
---|
29 | {
|
---|
30 | NV_LUA_STACK_ASSERT( L, 0 );
|
---|
31 | handle_conversion hc;
|
---|
32 | hc.h.index = dindex;
|
---|
33 | hc.h.counter = dcounter;
|
---|
34 | if ( lua_istable( L, i ) )
|
---|
35 | {
|
---|
36 | lua_rawgeti( L, i, 1 ); // handle as double
|
---|
37 | hc.d = lua_tonumber( L, -1 );
|
---|
38 | lua_pop( L, 1 );
|
---|
39 | }
|
---|
40 | return hc.h;
|
---|
41 | }
|
---|
42 |
|
---|
43 | void nv::lua::detail::register_handle_impl( lua_State* L, int pseudoindex, uint32 index, uint32 counter, bool empty )
|
---|
44 | {
|
---|
45 | if ( empty )
|
---|
46 | {
|
---|
47 | lua_newtable( L );
|
---|
48 | }
|
---|
49 | if ( !lua_istable( L, -1 ) ) return;
|
---|
50 |
|
---|
51 | handle_conversion hc;
|
---|
52 | hc.h.index = index;
|
---|
53 | hc.h.counter = counter;
|
---|
54 | lua_pushnumber( L, hc.d );
|
---|
55 | lua_rawseti( L, -2, 1 );
|
---|
56 |
|
---|
57 | lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex );
|
---|
58 | lua_insert( L, -2 );
|
---|
59 | lua_rawseti( L, -2, index );
|
---|
60 | lua_pop( L, 1 );
|
---|
61 | }
|
---|
62 |
|
---|
63 | void nv::lua::detail::unregister_handle_impl( lua_State* L, int pseudoindex, uint32 index )
|
---|
64 | {
|
---|
65 | NV_LUA_STACK_ASSERT( L, 0 );
|
---|
66 | lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
|
---|
67 | lua_pushinteger( L, 0 );
|
---|
68 | lua_rawseti( L, -2, index );
|
---|
69 | lua_pop( L, 1 );
|
---|
70 | }
|
---|