source: trunk/src/lua/lua_function.cc @ 182

Last change on this file since 182 was 182, checked in by epyon, 12 years ago
  • lua/values - universal template-friendly any value push and retrieval with support for custom types
  • lua/function - powertool - std::function/boost::function compatible lua function storage with support for any return type and arbitrary parameters
File size: 1.6 KB
Line 
1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include <nv/lua/lua_function.hh>
8
9#include <nv/lua/lua_raw.hh>
10
11using namespace nv;
12
13lua::function_base::function_base( lua_State* L, const path& a_path, bool global /*= true*/ ) : L(L)
14{
15        if ( !a_path.resolve( L, global ) )
16        {
17                lua_pop( L, 1 );
18                throw std::runtime_error("not a valid path - " + a_path.to_string() );
19        }
20
21        if ( !lua_isfunction( L, -1 ) )
22        {
23                lua_pop( L, 1 );
24                throw std::runtime_error("not a valid function - " + a_path.to_string() );
25        }
26        m_ref = luaL_ref( L, LUA_REGISTRYINDEX );
27}
28
29lua::function_base::function_base( const function_base& func ) : L(L)
30{
31        lua_rawgeti( L, LUA_REGISTRYINDEX, func.m_ref );
32        m_ref = luaL_ref( L, LUA_REGISTRYINDEX );
33}
34
35lua::function_base::~function_base()
36{
37        luaL_unref( L, LUA_REGISTRYINDEX, m_ref );
38}
39
40lua::function_base& lua::function_base::operator=(const function_base& func)
41{
42        if ( this != &func )
43        {
44                L = func.L;
45                lua_rawgeti( L, LUA_REGISTRYINDEX, func.m_ref );
46                m_ref = luaL_ref( L, LUA_REGISTRYINDEX );
47        }
48        return *this;
49}
50
51void lua::function_base::retrieve()
52{
53        lua_rawgeti( L, LUA_REGISTRYINDEX, m_ref );
54}
55
56void lua::function_base::call( int args, int results )
57{
58        int status = lua_pcall( L, args, results, 0 );
59        if ( status != 0 )
60        {
61                std::string error = lua_tostring( L, -1 );
62                lua_pop( L, 1 );
63                throw std::runtime_error(error.c_str());
64        }
65}
66
Note: See TracBrowser for help on using the repository browser.