Repository navigation
Expand file tree
/
Copy pathPythonFunction.cpp
More file actions
196 lines (175 loc) · 5.68 KB
/
Copy pathPythonFunction.cpp
File metadata and controls
196 lines (175 loc) · 5.68 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "PythonFunction.h"
#include "UEPyModule.h"
void UPythonFunction::SetPyCallable(PyObject *callable)
{
py_callable = callable;
Py_INCREF(py_callable);
}
#if ENGINE_MINOR_VERSION > 18
void UPythonFunction::CallPythonCallable(UObject *Context, FFrame& Stack, RESULT_DECL)
#else
void UPythonFunction::CallPythonCallable(FFrame& Stack, RESULT_DECL)
#endif
{
FScopePythonGIL gil;
#if ENGINE_MINOR_VERSION <= 18
UObject *Context = Stack.Object;
#endif
UPythonFunction *function = static_cast<UPythonFunction *>(Stack.CurrentNativeFunction);
bool on_error = false;
bool is_static = function->HasAnyFunctionFlags(FUNC_Static);
FOutParmRec *OutParms = nullptr;
// count the number of arguments
Py_ssize_t argn = (Context && !is_static) ? 1 : 0;
for (TFieldIterator<UProperty> IArgs(function); IArgs; ++IArgs)
{
if (!PROP_IS_OUT_PARAM(*IArgs))
argn++;
}
#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Warning, TEXT("Initializing %d parameters"), argn);
#endif
PyObject *py_args = PyTuple_New(argn);
argn = 0;
if (Context && !is_static) {
PyObject *py_obj = (PyObject *)ue_get_python_uobject(Context);
if (!py_obj) {
unreal_engine_py_log_error();
on_error = true;
}
else {
Py_INCREF(py_obj);
PyTuple_SetItem(py_args, argn++, py_obj);
}
}
uint8 *frame = Stack.Locals;
if (*Stack.Code == EX_EndFunctionParms)
{ // native call
for (UProperty *prop = (UProperty *)function->Children; prop; prop = (UProperty *)prop->Next) {
if (PROP_IS_OUT_PARAM(prop))
continue;
if (!on_error) {
PyObject *arg = ue_py_convert_property(prop, (uint8 *)Stack.Locals, 0);
if (!arg) {
unreal_engine_py_log_error();
on_error = true;
}
else {
PyTuple_SetItem(py_args, argn++, arg);
}
}
}
}
else
{ // blueprint call
// largely copied from ScriptCore.cpp::CallFunction - for BP calls, we need to set up some of the FOutParmRec stuff ourselves
frame = (uint8 *)FMemory_Alloca(function->PropertiesSize);
FMemory::Memzero(frame, function->PropertiesSize);
for (UProperty *prop = (UProperty *)function->Children; *Stack.Code != EX_EndFunctionParms; prop = (UProperty *)prop->Next)
{
Stack.Step(Stack.Object, prop->ContainerPtrToValuePtr<uint8>(frame));
if (PROP_IS_OUT_PARAM(prop))
{
FOutParmRec *rec = (FOutParmRec*)FMemory_Alloca(sizeof(FOutParmRec));
rec->Property = prop;
rec->PropAddr = Stack.MostRecentPropertyAddress;
rec->NextOutParm = OutParms;
OutParms = rec;
continue;
}
if (!on_error) {
PyObject *arg = ue_py_convert_property(prop, frame, 0);
if (!arg) {
unreal_engine_py_log_error();
on_error = true;
}
else {
PyTuple_SetItem(py_args, argn++, arg);
}
}
}
}
Stack.Code++;
if (on_error || !function->py_callable) {
Py_DECREF(py_args);
return;
}
PyObject *ret = PyObject_CallObject(function->py_callable, py_args);
Py_DECREF(py_args);
if (!ret) {
unreal_engine_py_log_error();
return;
}
// get return value and/or any out params - for convenience, if a single item is returned, wrap it in a tuple so that we can process
// multi-out params and single out params with one block of code
bool wrapped_ret = false;
if (!PyTuple_Check(ret))
{
PyObject *wrapped = PyTuple_New(1);
PyTuple_SetItem(wrapped, 0, ret);
ret = wrapped;
}
int nret = PyTuple_Size(ret);
int tuple_index = 0;
for (TFieldIterator<UProperty> It(function); It && (It->PropertyFlags & CPF_Parm); ++It)
{
if (!PROP_IS_OUT_PARAM(*It))
continue;
if (tuple_index >= nret)
{
UE_LOG(LogPython, Error, TEXT("Python function %s didn't return enough values"), *function->GetFName().ToString());
break;
}
UProperty *prop = *It;
PyObject *py_obj = PyTuple_GetItem(ret, tuple_index);
if (prop->PropertyFlags & CPF_ReturnParm)
{ // handle the return value specially by having it write directly to the stack
if (!ue_py_convert_pyobject(py_obj, prop, (uint8*)RESULT_PARAM - prop->GetOffset_ForUFunction(), 0))
{
UE_LOG(LogPython, Error, TEXT("Invalid return value type for function %s"), *function->GetFName().ToString());
}
}
else
{ // Find the given FOutParmRec for this property - look in the stack first
uint8 *out_frame = nullptr;
for (FOutParmRec *rec = Stack.OutParms; rec != nullptr; rec = rec->NextOutParm)
{
if (rec->Property == prop)
{
out_frame = rec->PropAddr - prop->GetOffset_ForUFunction();
break;
}
}
if (!out_frame)
{ // look in our local out parms next
for (FOutParmRec *rec = OutParms; rec != nullptr; rec = rec->NextOutParm)
{
if (rec->Property == prop)
{
out_frame = rec->PropAddr - prop->GetOffset_ForUFunction();
break;
}
}
}
if (!out_frame)
{ // default to our current frame
out_frame = frame;
}
if (!ue_py_convert_pyobject(py_obj, prop, out_frame, 0))
{
UE_LOG(LogPython, Error, TEXT("Failed to convert output property for function %s"), *function->GetFName().ToString());
}
}
tuple_index++;
}
Py_DECREF(ret);
}
UPythonFunction::~UPythonFunction()
{
FScopePythonGIL gil;
Py_XDECREF(py_callable);
FUnrealEnginePythonHouseKeeper::Get()->UnregisterPyUObject(this);
#if defined(UEPY_MEMORY_DEBUG)
UE_LOG(LogPython, Warning, TEXT("PythonFunction callable %p XDECREF'ed"), this);
#endif
}