summaryrefslogtreecommitdiff
path: root/Runtime/Lua/LuaBind/LuaBindLFunction.cpp
blob: 1e7b75f7e34d6de984868b0a99e942d697414f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "LuaBindLFunction.h"
#include "LuaBindHelper.h"

namespace LuaBind
{

	LuaFunction::LuaFunction(const char* func)
	{
        argc = 0;
		method = func;
	}

	void LuaFunction::operator = (const char* func)
	{
		method = func;
	}

	void LuaFunction::AddInt(State& state, int n)
	{
		state.Push(n);
		++argc;
	}

	void LuaFunction::AddFloat(State& state, float n)
	{
		state.Push(n);
		++argc;
	}

	void LuaFunction::AddNil(State& state)
	{
		state.PushNil();
		++argc;
	}

	void LuaFunction::AddBool(State& state, bool b)
	{
		state.Push(b);
		++argc;
	}

	void LuaFunction::AddString(State& state, const char* str)
	{
		state.Push(str);
		++argc;
	}

	void LuaFunction::AddTable(State& state, INativeTable& tb)
	{
		tb.CastToTable(state);
		++argc;
	}

	void LuaFunction::Invoke(State& state, int nReturns)
	{
		if (state.GetGlobalField(method))
		{
            if (state.IsType(-1, LUA_TFUNCTION)) {
                int funcIdx = -1 - argc;
                lua_insert(state, funcIdx);
                state.Call(argc, nReturns);
            }
		}
        argc = 0;
	}

}