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