// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz // http://chaosforge.org/ // // This file is part of NV Libraries. // For conditions of distribution and use, see copyright notice in nv.hh #include #include using namespace nv; lua::function_base::function_base( lua_State* a_L, const path& a_path, bool a_global /*= true*/ ) : L(a_L) { if ( !a_path.resolve( L, a_global ) ) { lua_pop( L, 1 ); throw std::runtime_error("not a valid path - " + a_path.to_string() ); } if ( !lua_isfunction( L, -1 ) ) { lua_pop( L, 1 ); throw std::runtime_error("not a valid function - " + a_path.to_string() ); } m_ref = luaL_ref( L, LUA_REGISTRYINDEX ); } lua::function_base::function_base( const function_base& func ) : L(func.L) { lua_rawgeti( L, LUA_REGISTRYINDEX, func.m_ref ); m_ref = luaL_ref( L, LUA_REGISTRYINDEX ); } lua::function_base::~function_base() { luaL_unref( L, LUA_REGISTRYINDEX, m_ref ); } lua::function_base& lua::function_base::operator=(const function_base& func) { if ( this != &func ) { L = func.L; lua_rawgeti( L, LUA_REGISTRYINDEX, func.m_ref ); m_ref = luaL_ref( L, LUA_REGISTRYINDEX ); } return *this; } void lua::function_base::retrieve() { lua_rawgeti( L, LUA_REGISTRYINDEX, m_ref ); } void lua::function_base::call( int args, int results ) { int status = lua_pcall( L, args, results, 0 ); if ( status != 0 ) { std::string error = lua_tostring( L, -1 ); lua_pop( L, 1 ); throw std::runtime_error(error.c_str()); } }