summaryrefslogtreecommitdiffstats
path: root/src/tolua_base.h
blob: 4f1038c092d8bf3eb4d6f457955233512e87931a (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef TOLUA_BASE_H
#define TOLUA_BASE_H

#pragma warning(disable:4800) // This file is ONLY included by Bindings.cpp and it throws lots of C4800 warnings

#include "tolua++.h"





class ToluaBase {

	int lua_instance;

protected:

	lua_State* lua_state;

	void lua_stacktrace(lua_State* L) const
	{
		lua_Debug entry;
		int depth = 0;

		while (lua_getstack(L, depth, &entry))
		{
			lua_getinfo(L, "Sln", &entry);

			LOGERROR("%s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?");
			depth++;
		}
	}


	bool report_errors(int status) const
	{
		if ( status!=0 )
		{
			const char* s = lua_tostring(lua_state, -1);
			LOGERROR("-- %s", s );
			//lua_pop(lua_state, 1);
			LOGERROR("Stack:");
			lua_stacktrace( lua_state );
			return true;
		}
		return false;
	}

	bool push_method(const char* name, lua_CFunction f) const {

		if (!lua_state) return false;

		lua_getref(lua_state, lua_instance);
		lua_pushstring(lua_state, name);
		//LOGINFO("1. push_method() Stack size: %i", lua_gettop( lua_state ) );
		lua_gettable(lua_state, -2);
		//LOGINFO("2. push_method() Stack size: %i", lua_gettop( lua_state ) );

		if (lua_isnil(lua_state, -1)) {

			// pop the table
			lua_pop(lua_state, 2);
			return false;

		} else {

			if (f) {
				if (lua_iscfunction(lua_state, -1)) {
					lua_pop(lua_state, 2);
					return false;
				};
				/* // not for now
				lua_pushcfunction(lua_state, f);
				if (lua_rawequal(lua_state, -1, -2)) {

					// avoid recursion, pop both functions and the table
					lua_pop(lua_state, 3);
					return false;
				};

				// pop f
				lua_pop(lua_state, 1);
				*/
			};

			// swap table with function
			lua_insert(lua_state, -2);
		};

		return true;
	};

	void dbcall(lua_State* L, int nargs, int nresults) const {

		// using lua_call for now
		int s = lua_pcall(L, nargs, nresults, 0);
		report_errors( s );
	};
public:
	
	int GetInstance() { return lua_instance; }
	lua_State* GetLuaState() { return lua_state; }

	void tolua__set_instance(lua_State* L, lua_Object lo) {

		lua_state = L;

		lua_pushvalue(L, lo);
		lua_instance = lua_ref(lua_state, 1);
	};

	ToluaBase() {

		lua_state = NULL;
	};

	~ToluaBase() {

		if (lua_state) {

			lua_unref(lua_state, lua_instance);
		};
	};
};

#endif