Debian Codegraph @

native/common/jp_functional.cpp

raw symbols SCIP
1 /*****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 *****************************************************************************/
16 #include "jpype.h"
17 #include "pyjp.h"
18 #include "jp_functional.h"
19 #include "jp_proxy.h"
20
21 JPFunctional::JPFunctional(JPJavaFrame& frame, jclass clss,
22 const string& name,
23 JPClass* super,
24 JPClassList& interfaces,
25 jint modifiers)
26 : JPClass(frame, clss, name, super, interfaces, modifiers)
27 {
28 m_Method = frame.getFunctional(clss);
29 }
30
31 JPFunctional::~JPFunctional()
32 = default;
33
34
35 class JPConversionFunctional : public JPConversion
36 {
37 public:
38
39 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
40 {
41 if (!PyCallable_Check(match.object))
42 return match.type = JPMatch::_none;
43
44 // def my_func(x, y=None) should be both a Function and a BiFunction
45 // i.e. the number of parameters accepted by the interface MUST
46 // 1. Be at most the maximum number of parameters accepted by the python function (parameter_count)
47 // (Unless the function accept a variable number of arguments, then this restriction does not
48 // apply).
49 // 2. Be at least the minumum number of parameters accepted by the python function
50 // (parameter_count - optional_parameter_count = number of required parameters).
51 // Notes:
52 // - keywords vargs does not remove restriction 1
53 // - keyword only arguments are not counted.
54 if (PyFunction_Check(match.object))
55 {
56 PyObject* func = match.object;
57 auto* code = (PyCodeObject*) PyFunction_GetCode(func); // borrowed
58 Py_ssize_t args = code->co_argcount;
59 bool is_varargs = ((code->co_flags&CO_VARARGS)==CO_VARARGS);
60 Py_ssize_t optional = 0;
61 JPPyObject defaults = JPPyObject::accept(PyObject_GetAttrString(func, "__defaults__"));
62 if (!defaults.isNull() && defaults.get() != Py_None)
63 optional = PyTuple_Size(defaults.get());
64 const int jargs = JPContext_global->getTypeManager()->interfaceParameterCount(cls);
65 // Too few arguments
66 if (!is_varargs && args < jargs)
67 return match.type = JPMatch::_none;
68 // Too many arguments
69 if (args - optional > jargs)
70 return match.type = JPMatch::_none;
71 }
72 else if (PyMethod_Check(match.object))
73 {
74 PyObject* func = PyMethod_Function(match.object); // borrowed
75 auto* code = (PyCodeObject*) PyFunction_GetCode(func); // borrowed
76 Py_ssize_t args = code->co_argcount;
77 bool is_varargs = ((code->co_flags&CO_VARARGS)==CO_VARARGS);
78 Py_ssize_t optional = 0;
79 JPPyObject defaults = JPPyObject::accept(PyObject_GetAttrString(func, "__defaults__"));
80 if (!defaults.isNull() && defaults.get() != Py_None)
81 optional = PyTuple_Size(defaults.get());
82 const int jargs = JPContext_global->getTypeManager()->interfaceParameterCount(cls);
83 // Bound self argument removes one argument
84 if ((PyMethod_Self(match.object))!=nullptr) // borrowed
85 args--;
86 // Too few arguments
87 if (!is_varargs && args < jargs)
88 return match.type = JPMatch::_none;
89 // Too many arguments
90 if (args - optional > jargs)
91 return match.type = JPMatch::_none;
92 }
93 match.conversion = this;
94 match.closure = cls;
95 return match.type = JPMatch::_implicit;
96 }
97
98 void getInfo(JPClass *cls, JPConversionInfo &info) override
99 {
100 PyObject *typing = PyImport_AddModule("jpype.protocol");
101 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "Callable"));
102 PyList_Append(info.implicit, proto.get());
103 }
104
105 jvalue convert(JPMatch &match) override
106 {
107 auto *cls = (JPFunctional*) match.closure;
108 JP_TRACE_IN("JPConversionFunctional::convert");
109 JPJavaFrame frame = JPJavaFrame::inner();
110 auto *self = (PyJPProxy*) PyJPProxy_Type->tp_alloc(PyJPProxy_Type, 0);
111 JP_PY_CHECK();
112 JPClassList cl;
113 cl.push_back(cls);
114 self->m_Proxy = new JPProxyFunctional(self, cl);
115 self->m_Target = match.object;
116 self->m_Dispatch = match.object;
117 self->m_Convert = true;
118 Py_INCREF(self->m_Target);
119 Py_INCREF(self->m_Dispatch);
120 jvalue v = self->m_Proxy->getProxy();
121 v.l = frame.keep(v.l);
122 Py_DECREF(self);
123 return v;
124 JP_TRACE_OUT; // GCOVR_EXCL_LINE
125 }
126 } functional_conversion;
127
128 JPMatch::Type JPFunctional::findJavaConversion(JPMatch &match)
129 {
130 JP_TRACE_IN("JPJPFunctional::findJavaConversiocdn");
131 JPClass::findJavaConversion(match);
132 if (match.type != JPMatch::_none)
133 return match.type;
134 if (functional_conversion.matches(this, match))
135 return match.type;
136 return match.type = JPMatch::_none;
137 JP_TRACE_OUT; // GCOVR_EXCL_LINE
138 }
139
140 void JPFunctional::getConversionInfo(JPConversionInfo &info)
141 {
142 JP_TRACE_IN("JPJPFunctional::getConversionInfo");
143 JPClass::getConversionInfo(info);
144 functional_conversion.getInfo(this, info);
145 JP_TRACE_OUT; // GCOVR_EXCL_LINE
146 }