Index: trunk/src/lua/lua_handle.cc
===================================================================
--- trunk/src/lua/lua_handle.cc	(revision 333)
+++ trunk/src/lua/lua_handle.cc	(revision 333)
@@ -0,0 +1,66 @@
+// Copyright (C) 2014 ChaosForge Ltd
+// http://chaosforge.org/
+//
+// This file is part of Nova libraries.
+// For conditions of distribution and use, see copyright notice in nv.hh
+
+#include "nv/lua/lua_handle.hh"
+
+#include "nv/lua/lua_raw.hh"
+
+// pseudoindex must be valid
+void nv::lua::detail::push_handle_impl( lua_State* L, int pseudoindex, uint32 index )
+{
+	int stack = lua_gettop(L);
+	lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
+	lua_rawgeti( L, -1, index );                      // table, entry
+	if ( lua_isnil( L, -1 ) )
+	{
+		NV_LOG( nv::LOG_ERROR, "NIL" );
+	}
+	lua_replace( L, stack + 1 );
+	lua_settop( L, stack + 1 );
+}
+
+nv::lua::detail::handle_struct nv::lua::detail::to_handle_impl( lua_State* L, int i, uint32 dindex, uint32 dcounter )
+{
+	handle_conversion hc;
+	hc.h.index   = dindex;
+	hc.h.counter = dcounter;
+	if ( lua_istable( L, i ) )
+	{
+		lua_rawgeti( L, i, 1 ); // handle as double
+		hc.d = lua_tonumber( L, -1 );
+		lua_pop( L, 1 );
+	}
+	return hc.h;
+}
+
+void nv::lua::detail::register_handle_impl( lua_State* L, int pseudoindex, uint32 index, uint32 counter, bool empty )
+{
+	if ( empty )
+	{
+		lua_newtable( L );
+	}
+	if ( !lua_istable( L, -1 ) ) return;
+
+	handle_conversion hc;
+	hc.h.index   = index;
+	hc.h.counter = counter;
+	lua_pushnumber( L, hc.d );
+	lua_rawseti( L, -2, 1 );
+
+	lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex );
+	lua_insert( L, -2 );
+	lua_rawseti( L, -2, index );
+	lua_pop( L, 1 );
+}
+
+void nv::lua::detail::unregister_handle_impl( lua_State* L, int pseudoindex, uint32 index )
+{
+	int stack = lua_gettop(L);
+	lua_rawgeti( L, LUA_REGISTRYINDEX, pseudoindex ); // table
+	lua_pushinteger( L, 0 );
+	lua_rawseti( L, -2, index );
+	lua_settop( L, stack );
+}
Index: trunk/src/lua/lua_state.cc
===================================================================
--- trunk/src/lua/lua_state.cc	(revision 332)
+++ trunk/src/lua/lua_state.cc	(revision 333)
@@ -261,4 +261,15 @@
 	lua_pushliteral(m_state, LUA_TABLIBNAME);
 	lua_call(m_state, 1, 0);
+
+	{
+		// Preregister 8 references for Handle registry
+		lua_newtable(m_state);
+		NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
+		for ( uint32 i = 1; i < 8; ++i )
+		{
+			lua_newtable(m_state);
+			luaL_ref( m_state, LUA_REGISTRYINDEX );
+		}
+	}
 
 	if ( load_libs )
