-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaFunctionBinding.cpp
More file actions
154 lines (122 loc) · 5.26 KB
/
LuaFunctionBinding.cpp
File metadata and controls
154 lines (122 loc) · 5.26 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2006-12 HumaNature Studios Inc.
#include "CorePch.h"
#include "LuaFunctionBinding.h"
namespace core {
// specializations
template<> int LuaFunctionBinding::pushReturnValue(lua_State* L, const bool& returnValue) { lua_pushboolean(L, returnValue); return 1; }
template<> int LuaFunctionBinding::pushReturnValue(lua_State* L, const int& returnValue) { lua_pushinteger(L, returnValue); return 1; }
template<> int LuaFunctionBinding::pushReturnValue(lua_State* L, const unsigned int& returnValue) { lua_pushinteger(L, returnValue); return 1; }
template<> int LuaFunctionBinding::pushReturnValue(lua_State* L, const float& returnValue) { lua_pushnumber(L, returnValue); return 1; }
template<> int LuaFunctionBinding::pushReturnValue(lua_State* L, const std::string& returnValue) { lua_pushstring(L, returnValue.c_str()); return 1; }
template<> int LuaFunctionBinding::pushReturnValue(lua_State* L, const char* const& returnValue) { lua_pushstring(L, returnValue); return 1; }
// dummys for complex return types.... not sure the ideal way to set this up, as VS & SNC fight
class SceneObject;
template<> int LuaFunctionBinding::pushReturnValue(lua_State*, SceneObject* const&) { return 1; }
LuaFunctionBinding::~LuaFunctionBinding()
{
}
void LuaFunctionBinding::call(lua_State* L)
{
if(L)
{
lua_State* parameterContext = L;
if(lua_isthread(L, -1))
{
// first parameter is thread, use it as the context instead
parameterContext = lua_tothread(L, -1);
}
interpretLuaParametersAndCall(parameterContext);
}
}
template<> int LuaFunctionBinding::interpretLuaParameter(const char* forFunction, lua_State* L, int parameterIndex)
{
int i = 0;
if(!lua_isnumber(L, parameterIndex + 1))
{
logWarn("Lua Binding", "Could not convert %s's parameter '%d' to int(it's a [%s]), %d", forFunction, parameterIndex, TypeOf(L, parameterIndex + 1), i);
}
else
{
i = luaL_checkint(L, parameterIndex + 1);
}
return i;
}
template<> float LuaFunctionBinding::interpretLuaParameter(const char* forFunction, lua_State* L, int parameterIndex)
{
float f = 0.0f;
if(!lua_isnumber(L, parameterIndex + 1))
{
logWarn("Lua Binding", "Could not convert %s's parameter '%d' to int(it's a [%s]), %f", forFunction, parameterIndex, TypeOf(L, parameterIndex + 1), f);
}
else
{
f = luaL_checknumber(L, parameterIndex + 1);
}
return f;
}
template<> bool LuaFunctionBinding::interpretLuaParameter(const char* forFunction, lua_State* L, int parameterIndex)
{
bool b = false;
if(!lua_isboolean(L, parameterIndex + 1))
{
logWarn("Lua Binding", "Could not convert %s's parameter '%d' to bool(it's a [%s]), %s", forFunction, parameterIndex, TypeOf(L, parameterIndex + 1), b ? "true" : "false");
}
else
{
b = lua_toboolean(L, parameterIndex + 1) != 0;
}
return b;
}
template<> const char* LuaFunctionBinding::interpretLuaParameter(const char* forFunction, lua_State* L, int parameterIndex)
{
const char* s = nullptr;
if(!lua_isstring(L, parameterIndex + 1))
{
logWarn("Lua Binding", "Could not convert %s's parameter '%d' to string(it's a [%s]), nullptr", forFunction, parameterIndex, TypeOf(L, parameterIndex + 1));
}
else
{
s = lua_tostring(L, parameterIndex + 1);
}
return s;
}
template<> std::string LuaFunctionBinding::interpretLuaParameter(const char* forFunction, lua_State* L, int parameterIndex)
{
const char* s = "";
if(!lua_isstring(L, parameterIndex + 1))
{
logWarn("Lua Binding", "Could not convert %s's parameter '%d' to std::string(it's a [%s]), \"\"", forFunction, parameterIndex, TypeOf(L, parameterIndex + 1));
}
else
{
s = lua_tostring(L, parameterIndex + 1);
}
return s;
}
template<> CompactStringDebug LuaFunctionBinding::interpretLuaParameter(const char* forFunction, lua_State* L, int parameterIndex)
{
const char* s = "";
if(!lua_isstring(L, parameterIndex + 1))
{
logWarn("Lua Binding", "Could not convert %s's parameter '%d' to CompactString(it's a [%s]), \"\"", forFunction, parameterIndex, TypeOf(L, parameterIndex + 1));
}
else
{
s = lua_tostring(L, parameterIndex + 1);
}
return s;
}
template<> CompactStringRelease LuaFunctionBinding::interpretLuaParameter(const char* forFunction, lua_State* L, int parameterIndex)
{
const char* s = "";
if(!lua_isstring(L, parameterIndex + 1))
{
logWarn("Lua Binding", "Could not convert %s's parameter '%d' to CompactString(it's a [%s]), \"\"", forFunction, parameterIndex, TypeOf(L, parameterIndex + 1));
}
else
{
s = lua_tostring(L, parameterIndex + 1);
}
return s;
}
} // namespace core