Debian Codegraph @

native/common/jp_booleantype.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_array.h"
19 #include "jp_primitive_accessor.h"
20 #include "jp_booleantype.h"
21 #include "jp_boxedtype.h"
22
23 JPBooleanType::JPBooleanType()
24 : JPPrimitiveType("boolean")
25 {
26 }
27
28 JPBooleanType::~JPBooleanType()
29 = default;
30
31 JPClass* JPBooleanType::getBoxedClass(JPJavaFrame& frame) const
32 {
33 return frame.getContext()->_java_lang_Boolean;
34 }
35
36 JPPyObject JPBooleanType::convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast)
37 {
38 return JPPyObject::call(PyBool_FromLong(val.z));
39 }
40
41 JPValue JPBooleanType::getValueFromObject(JPJavaFrame& frame, const JPValue& obj)
42 {
43 jvalue v;
44 field(v) = frame.CallBooleanMethodA(obj.getValue().l, frame.getContext()->_java_lang_Boolean->m_BooleanValueID, nullptr) != 0;
45 return JPValue(this, v);
46 }
47
48 class JPConversionAsBoolean : public JPConversion
49 {
50 public:
51
52 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
53 {
54 if (!PyBool_Check(match.object))
55 return match.type = JPMatch::_none;
56 match.conversion = this;
57 return match.type = JPMatch::_exact;
58 }
59
60 void getInfo(JPClass * cls, JPConversionInfo &info) override
61 {
62 PyList_Append(info.exact, (PyObject*) & PyBool_Type);
63 }
64
65 jvalue convert(JPMatch &match) override
66 {
67 jvalue res;
68 jlong v = PyObject_IsTrue(match.object);
69 if (v == -1)
70 JP_PY_CHECK();
71 res.z = v != 0;
72 return res;
73 }
74 } asBooleanExact;
75
76 class JPConversionAsBooleanJBool : public JPConversionJavaValue
77 {
78 public:
79
80 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
81 {
82
83 JPValue *value = match.getJavaSlot();
84 if (value == nullptr)
85 return match.type = JPMatch::_none;
86 match.type = JPMatch::_none;
87 // Implied conversion from boxed to primitive (JLS 5.1.8)
88 if (javaValueConversion->matches(cls, match)
89 || unboxConversion->matches(cls, match))
90 return match.type;
91
92 // Unboxing must be to the from the exact boxed type (JLS 5.1.8)
93 return JPMatch::_implicit; // search no further.
94 }
95
96 void getInfo(JPClass *cls, JPConversionInfo &info) override
97 {
98 JPContext *context = JPContext_global;
99 PyList_Append(info.exact, (PyObject*) context->_boolean->getHost());
100 unboxConversion->getInfo(cls, info);
101 }
102
103 } asBooleanJBool;
104
105 class JPConversionAsBooleanLong : public JPConversionAsBoolean
106 {
107 public:
108
109 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
110 {
111 if (!PyLong_CheckExact(match.object)
112 && !PyIndex_Check(match.object))
113 return match.type = JPMatch::_none;
114 match.conversion = this;
115 return match.type = JPMatch::_implicit;
116 }
117
118 void getInfo(JPClass *cls, JPConversionInfo &info) override
119 {
120 PyObject *typing = PyImport_AddModule("jpype.protocol");
121 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "SupportsIndex"));
122 PyList_Append(info.expl, proto.get());
123 }
124
125 } asBooleanLong;
126
127 class JPConversionAsBooleanNumber : public JPConversionAsBoolean
128 {
129 public:
130
131 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
132 {
133 if (!PyNumber_Check(match.object))
134 return match.type = JPMatch::_none;
135 match.conversion = this;
136 return match.type = JPMatch::_explicit;
137 }
138
139 void getInfo(JPClass * cls, JPConversionInfo &info) override
140 {
141 PyObject *typing = PyImport_AddModule("jpype.protocol");
142 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "SupportsFloat"));
143 PyList_Append(info.expl, proto.get());
144 }
145
146 } asBooleanNumber;
147
148 JPMatch::Type JPBooleanType::findJavaConversion(JPMatch &match)
149 {
150 JP_TRACE_IN("JPBooleanType::findJavaConversion", this);
151
152 if (match.object == Py_None)
153 return match.type = JPMatch::_none;
154
155 if (asBooleanExact.matches(this, match)
156 || asBooleanJBool.matches(this, match)
157 || asBooleanLong.matches(this, match)
158 || asBooleanNumber.matches(this, match)
159 )
160 return match.type;
161 return match.type = JPMatch::_none;
162 JP_TRACE_OUT;
163 }
164
165 void JPBooleanType::getConversionInfo(JPConversionInfo &info)
166 {
167 JPJavaFrame frame = JPJavaFrame::outer();
168 asBooleanExact.getInfo(this, info);
169 asBooleanJBool.getInfo(this, info);
170 asBooleanLong.getInfo(this, info);
171 asBooleanNumber.getInfo(this, info);
172 PyList_Append(info.ret, (PyObject*) & PyBool_Type);
173 }
174
175 jarray JPBooleanType::newArrayOf(JPJavaFrame& frame, jsize sz)
176 {
177 return frame.NewBooleanArray(sz);
178 }
179
180 JPPyObject JPBooleanType::getStaticField(JPJavaFrame& frame, jclass c, jfieldID fid)
181 {
182 jvalue v;
183 field(v) = frame.GetStaticBooleanField(c, fid);
184 return convertToPythonObject(frame, v, false);
185 }
186
187 JPPyObject JPBooleanType::getField(JPJavaFrame& frame, jobject c, jfieldID fid)
188 {
189 jvalue v;
190 field(v) = frame.GetBooleanField(c, fid);
191 return convertToPythonObject(frame, v, false);
192 }
193
194 JPPyObject JPBooleanType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue* val)
195 {
196 jvalue v;
197 {
198 JPPyCallRelease call;
199 field(v) = frame.CallStaticBooleanMethodA(claz, mth, val);
200 }
201 return convertToPythonObject(frame, v, false);
202 }
203
204 JPPyObject JPBooleanType::invoke(JPJavaFrame& frame, jobject obj, jclass clazz, jmethodID mth, jvalue* val)
205 {
206 jvalue v;
207 {
208 JPPyCallRelease call;
209 if (clazz == nullptr)
210 field(v) = frame.CallBooleanMethodA(obj, mth, val);
211 else
212 field(v) = frame.CallNonvirtualBooleanMethodA(obj, clazz, mth, val);
213 }
214 return convertToPythonObject(frame, v, false);
215 }
216
217 void JPBooleanType::setStaticField(JPJavaFrame& frame, jclass c, jfieldID fid, PyObject* obj)
218 {
219 JPMatch match(&frame, obj);
220 if (findJavaConversion(match) < JPMatch::_implicit)
221 JP_RAISE(PyExc_TypeError, "Unable to convert to Java boolean");
222 type_t val = field(match.convert());
223 frame.SetStaticBooleanField(c, fid, val);
224 }
225
226 void JPBooleanType::setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* obj)
227 {
228 JPMatch match(&frame, obj);
229 if (findJavaConversion(match) < JPMatch::_implicit)
230 JP_RAISE(PyExc_TypeError, "Unable to convert to Java boolean");
231 type_t val = field(match.convert());
232 frame.SetBooleanField(c, fid, val);
233 }
234
235 void JPBooleanType::setArrayRange(JPJavaFrame& frame, jarray a,
236 jsize start, jsize length, jsize step,
237 PyObject* sequence)
238 {
239 JP_TRACE_IN("JPBooleanType::setArrayRange");
240 JPPrimitiveArrayAccessor<array_t, type_t*> accessor(frame, a,
241 &JPJavaFrame::GetBooleanArrayElements, &JPJavaFrame::ReleaseBooleanArrayElements);
242
243 type_t* val = accessor.get();
244 // First check if assigning sequence supports buffer API
245 if (PyObject_CheckBuffer(sequence))
246 {
247 JPPyBuffer buffer(sequence, PyBUF_FULL_RO);
248 if (buffer.valid())
249 {
250 Py_buffer& view = buffer.getView();
251 if (view.ndim != 1)
252 JP_RAISE(PyExc_TypeError, "buffer dims incorrect");
253 Py_ssize_t vshape = view.shape[0];
254 Py_ssize_t vstep = view.strides[0];
255 if (vshape != length)
256 JP_RAISE(PyExc_ValueError, "mismatched size");
257
258 char* memory = (char*) view.buf;
259 if (view.suboffsets && view.suboffsets[0] >= 0)
260 memory = *((char**) memory) + view.suboffsets[0];
261 jsize index = start;
262 jconverter conv = getConverter(view.format, (int) view.itemsize, "z");
263 for (Py_ssize_t i = 0; i < length; ++i, index += step)
264 {
265 jvalue r = conv(memory);
266 val[index] = r.z;
267 memory += vstep;
268 }
269 accessor.commit();
270 return;
271 } else
272 {
273 PyErr_Clear();
274 }
275 }
276
277 // Use sequence API
278 JPPySequence seq = JPPySequence::use(sequence);
279 jsize index = start;
280 for (Py_ssize_t i = 0; i < length; ++i, index += step)
281 {
282 int v = PyObject_IsTrue(seq[i].get());
283 if (v == -1)
284 JP_PY_CHECK();
285 val[index] = v;
286 }
287 accessor.commit();
288 JP_TRACE_OUT;
289 }
290
291 JPPyObject JPBooleanType::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx)
292 {
293 auto array = (array_t) a;
294 type_t val;
295 frame.GetBooleanArrayRegion(array, ndx, 1, &val);
296 jvalue v;
297 field(v) = val;
298 return convertToPythonObject(frame, v, false);
299 }
300
301 void JPBooleanType::setArrayItem(JPJavaFrame& frame, jarray a, jsize ndx, PyObject* obj)
302 {
303 JPMatch match(&frame, obj);
304 if (findJavaConversion(match) < JPMatch::_implicit)
305 JP_RAISE(PyExc_TypeError, "Unable to convert to Java boolean");
306 type_t val = field(match.convert());
307 frame.SetBooleanArrayRegion((array_t) a, ndx, 1, &val);
308 }
309
310 void JPBooleanType::getView(JPArrayView& view)
311 {
312 JPJavaFrame frame = JPJavaFrame::outer();
313 view.m_Memory = (void*) frame.GetBooleanArrayElements(
314 (jbooleanArray) view.m_Array->getJava(), &view.m_IsCopy);
315 view.m_Buffer.format = "?";
316 view.m_Buffer.itemsize = sizeof (jboolean);
317 }
318
319 void JPBooleanType::releaseView(JPArrayView& view)
320 {
321 try
322 {
323 JPJavaFrame frame = JPJavaFrame::outer();
324 frame.ReleaseBooleanArrayElements((jbooleanArray) view.m_Array->getJava(),
325 (jboolean*) view.m_Memory, view.m_Buffer.readonly ? JNI_ABORT : 0);
326 } catch (JPypeException&)
327 {
328 // This is called as part of the cleanup routine and exceptions
329 // are not permitted
330 }
331 }
332
333 const char* JPBooleanType::getBufferFormat()
334 {
335 return "?";
336 }
337
338 Py_ssize_t JPBooleanType::getItemSize()
339 {
340 return sizeof (jboolean);
341 }
342
343 void JPBooleanType::copyElements(JPJavaFrame &frame, jarray a, jsize start, jsize len,
344 void* memory, int offset)
345 {
346 auto* b = (jboolean*) ((char*) memory + offset);
347 frame.GetBooleanArrayRegion((jbooleanArray) a, start, len, b);
348 }
349
350 static void pack(jboolean* d, jvalue v)
351 {
352 *d = v.z;
353 }
354
355 PyObject *JPBooleanType::newMultiArray(JPJavaFrame &frame, JPPyBuffer &buffer, int subs, int base, jobject dims)
356 {
357 JP_TRACE_IN("JPBooleanType::newMultiArray");
358 return convertMultiArray<type_t>(
359 frame, this, &pack, "z",
360 buffer, subs, base, dims);
361 JP_TRACE_OUT;
362 }