-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcallable.cpp
More file actions
130 lines (89 loc) · 3.08 KB
/
callable.cpp
File metadata and controls
130 lines (89 loc) · 3.08 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
#include "variant/callable.h"
#include "casting/cast_args.h"
namespace pygodot {
void func_to_callable(GDExtensionUninitializedTypePtr ptr, py::function func) {
if(py::isinstance<Callable>(func)) {
cast(ptr, variant_type_to_enum_value<Callable>, false) = func;
return;
}
static py::handle cast_to_callable = resolve_name("godot._internal.utils.cast_to_callable");
func = cast_to_callable(func);
if(py::isinstance<Callable>(func)) {
cast(ptr, variant_type_to_enum_value<Callable>, false) = func;
return;
}
GDExtensionCallableCustomInfo info = {
.callable_userdata = func.ptr(),
.token = extension_interface::token,
.call_func = [](void* userdata, const GDExtensionConstVariantPtr* args,
GDExtensionInt argument_count, GDExtensionVariantPtr res, GDExtensionCallError* error)
{
py::gil_scoped_acquire gil;
auto func = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata));
try {
cast(res) = func(*cast(args, argument_count)); // XXX: cast info
if(error) {
error->error = GDEXTENSION_CALL_OK; // XXX
}
return;
}
CATCH_EXCEPTIONS_AND_PRINT_ERRORS(
"While calling: " + get_fully_qualified_name(func))
if(error) {
error->error = GDEXTENSION_CALL_ERROR_INVALID_METHOD; // XXX
}
},
.is_valid_func = [](void* userdata) -> GDExtensionBool {
py::gil_scoped_acquire gil;
try {
auto func = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata));
return py::bool_(func);
}
CATCH_EXCEPTIONS_AND_PRINT_ERRORS()
return false;
},
.free_func = [](void* userdata) {
py::gil_scoped_acquire gil;
py::handle(static_cast<PyObject*>(userdata)).dec_ref();
},
.hash_func = [](void* userdata) -> uint32_t {
py::gil_scoped_acquire gil;
try {
auto func = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata));
return py::hash(func);
}
CATCH_EXCEPTIONS_AND_PRINT_ERRORS()
return 0;
},
.equal_func = [](void* userdata_a, void* userdata_b) -> GDExtensionBool {
py::gil_scoped_acquire gil;
try {
auto func_a = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata_a));
auto func_b = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata_b));
return func_a.equal(func_b);
}
CATCH_EXCEPTIONS_AND_PRINT_ERRORS()
return false;
},
/*.less_than_func = [](void* userdata_a, void* userdata_b) {
py::gil_scoped_acquire gil;
auto func_a = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata_a));
auto func_b = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata_b));
return 0;
},*/
.to_string_func = [](void *userdata, GDExtensionBool *is_valid, GDExtensionStringPtr out) {
py::gil_scoped_acquire gil;
auto func = py::reinterpret_borrow<py::function>(static_cast<PyObject*>(userdata));
try {
cast(out) = py::str(func); // XXX: cast info
*is_valid = true;
return;
}
CATCH_EXCEPTIONS_AND_PRINT_ERRORS()
*is_valid = false;
}
};
func.inc_ref();
extension_interface::callable_custom_create(ptr, &info);
}
} // namespace pygodot