Debian Codegraph @

native/common/include/jp_primitive_accessor.h

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 #ifndef JP_PRIMITIVE_ACCESSOR_H
17 #define JP_PRIMITIVE_ACCESSOR_H
18 #include <Python.h>
19 #include "jp_exception.h"
20 #include "jp_javaframe.h"
21 #include "jp_match.h"
22
23 template <typename array_t, typename ptr_t>
24 class JPPrimitiveArrayAccessor
25 {
26 using releaseFnc = void (JPJavaFrame::*)(array_t, ptr_t, jint);
27 using accessFnc = ptr_t (JPJavaFrame::*)(array_t, jboolean *);
28
29 JPJavaFrame& _frame;
30 array_t _array;
31 ptr_t _elem;
32 releaseFnc _release;
33 jboolean _iscopy;
34
35 public:
36
37 JPPrimitiveArrayAccessor(JPJavaFrame& frame, jarray array, accessFnc access, releaseFnc release)
38 : _frame(frame), _array((array_t) array), _release(release)
39 {
40 _elem = ((&_frame)->*access)(_array, &_iscopy);
41 }
42
43 ~JPPrimitiveArrayAccessor()
44 {
45 // This is fallback if commit or abort is not called.
46 // It should only occur in cases where a throw has
47 // already been issued.
48 try
49 {
50 if (_array)
51 ((&_frame)->*_release)(_array, _elem, JNI_ABORT);
52 } catch (JPypeException&) // GCOVR_EXCL_LINE
53 {
54 // We can't throw here because it would abort.
55 // But this is called on a non-op release, so
56 // we will just eat it
57 }
58 }
59
60 jsize size()
61 {
62 return _frame.GetArrayLength(_array);
63 }
64
65 ptr_t get()
66 {
67 return _elem;
68 }
69
70 void commit()
71 {
72 // Prevent the dtor from calling a second time
73 array_t a = _array;
74 _array = 0;
75 ((&_frame)->*_release)(a, _elem, 0);
76 }
77
78 void abort()
79 {
80 // Prevent the dtor from calling a second time
81 array_t a = _array;
82 _array = 0;
83 ((&_frame)->*_release)(a, _elem, JNI_ABORT);
84 }
85
86 } ;
87
88 template <class type_t> PyObject *convertMultiArray(
89 JPJavaFrame &frame,
90 JPPrimitiveType* cls,
91 void (*pack)(type_t*, jvalue),
92 const char* code,
93 JPPyBuffer &buffer,
94 int subs, int base, jobject dims)
95 {
96 JPContext *context = frame.getContext();
97 Py_buffer& view = buffer.getView();
98 jconverter converter = getConverter(view.format, (int) view.itemsize, code);
99 if (converter == nullptr)
100 {
101 PyErr_Format(PyExc_TypeError, "No type converter found");
102 return nullptr;
103 }
104
105 // Reserve space for array.
106 auto contents = (jobjectArray) context->_java_lang_Object->newArrayOf(frame, subs);
107 std::vector<Py_ssize_t> indices(view.ndim);
108 int u = view.ndim - 1;
109 int k = 0;
110 jarray a0 = cls->newArrayOf(frame, base);
111 frame.SetObjectArrayElement(contents, k++, a0);
112 jboolean isCopy;
113 void *mem = frame.getEnv()->GetPrimitiveArrayCritical(a0, &isCopy);
114 JP_TRACE_JAVA("GetPrimitiveArrayCritical", mem);
115 auto *dest = (type_t*) mem;
116
117 Py_ssize_t step;
118 if (view.strides == nullptr)
119 step = view.itemsize;
120 else
121 step = view.strides[u];
122
123 // Align with the first element in the array
124 char *src = buffer.getBufferPtr(indices);
125
126 // Traverse the array
127 while (true)
128 {
129 if (indices[u] == view.shape[u])
130 {
131 int j;
132 for (j = 0; j < u; ++j)
133 {
134 indices[u - j - 1]++;
135 if (indices[u - j - 1] < view.shape[u - j - 1])
136 break;
137 indices[u - j - 1] = 0;
138 }
139 // Commit the current section
140 indices[u] = 0;
141 JP_TRACE_JAVA("ReleasePrimitiveArrayCritical", mem);
142 frame.getEnv()->ReleasePrimitiveArrayCritical(a0, mem, JNI_COMMIT);
143 frame.DeleteLocalRef(a0);
144
145 // If we hit the shape of the uppermost we are done
146 if (j == u)
147 break;
148
149 a0 = cls->newArrayOf(frame, base);
150 frame.SetObjectArrayElement(contents, k++, a0);
151 mem = frame.getEnv()->GetPrimitiveArrayCritical(a0, &isCopy);
152 JP_TRACE_JAVA("GetPrimitiveArrayCritical", mem);
153 dest = (type_t*) mem;
154 src = buffer.getBufferPtr(indices);
155 }
156 pack(dest, converter(src));
157 src += step;
158 dest++;
159 indices[u]++;
160 }
161
162 // Assemble it into a multidimensional array
163 jobject out = frame.assemble(dims, contents);
164
165 // Convert it to Python
166 JPClass *type = context->_java_lang_Object;
167 if (out != nullptr)
168 type = frame.findClassForObject(out);
169 jvalue v;
170 v.l = out;
171 return type->convertToPythonObject(frame, v, false).keep();
172 }
173
174 template <typename base_t>
175 class JPConversionLong : public JPIndexConversion
176 {
177 public:
178
179 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
180 {
181 if (!PyLong_CheckExact(match.object) && !PyIndex_Check(match.object))
182 return match.type = JPMatch::_none;
183 match.conversion = this;
184 return match.type = JPMatch::_implicit;
185 }
186
187 jvalue convert(JPMatch &match) override
188 {
189 jvalue res;
190 if (match.type == JPMatch::_exact)
191 {
192 auto val = (jlong) PyLong_AsUnsignedLongLongMask(match.object);
193 if (val == -1)
194 JP_PY_CHECK(); // GCOVR_EXCL_LINE
195 base_t::field(res) = (typename base_t::type_t) val;
196 } else
197 {
198 auto val = (jlong) PyLong_AsLongLong(match.object);
199 if (val == -1)
200 JP_PY_CHECK(); // GCOVR_EXCL_LINE
201 base_t::field(res) = (typename base_t::type_t) base_t::assertRange(val);
202 }
203 return res;
204 }
205 } ;
206
207 template <typename base_t>
208 class JPConversionLongNumber : public JPConversionLong<base_t>
209 {
210 public:
211
212 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
213 {
214 if (!PyNumber_Check(match.object))
215 return match.type = JPMatch::_none;
216 match.conversion = this;
217 return match.type = JPMatch::_explicit;
218 }
219
220 void getInfo(JPClass *cls, JPConversionInfo &info) override
221 {
222 PyObject *typing = PyImport_AddModule("jpype.protocol");
223 JPPyObject proto = JPPyObject::call(PyObject_GetAttrString(typing, "SupportsFloat"));
224 PyList_Append(info.expl, proto.get());
225 }
226
227 jvalue convert(JPMatch &match) override
228 {
229 JPPyObject obj = JPPyObject::call(PyNumber_Long(match.object));
230 match.object = obj.get();
231 return JPConversionLong<base_t>::convert(match);
232 }
233 } ;
234
235 template <typename base_t>
236 class JPConversionLongWiden : public JPConversion
237 {
238 public:
239 // GCOVR_EXCL_START
240
241 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
242 {
243 return JPMatch::_none; // Not used
244 }
245
246 void getInfo(JPClass *cls, JPConversionInfo &info) override
247 {
248 // Not used
249 }
250 // GCOVR_EXCL_STOP
251
252 jvalue convert(JPMatch &match) override
253 {
254 JPValue *value = match.getJavaSlot();
255 jvalue ret;
256 base_t::field(ret) = (typename base_t::type_t) (dynamic_cast<JPPrimitiveType*>(
257 value->getClass()))->getAsLong(value->getValue());
258 return ret;
259 }
260 } ;
261
262 template <typename base_t>
263 class JPConversionAsFloat : public JPNumberConversion
264 {
265 public:
266
267 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
268 {
269 if (!PyNumber_Check(match.object))
270 return match.type = JPMatch::_none;
271 match.conversion = this;
272 return match.type = JPMatch::_implicit;
273 }
274
275 jvalue convert(JPMatch &match) override
276 {
277 jvalue res;
278 double val = PyFloat_AsDouble(match.object);
279 if (val == -1.0)
280 JP_PY_CHECK(); // GCOVR_EXCL_LINE
281 base_t::field(res) = (typename base_t::type_t) val;
282 return res;
283 }
284 } ;
285
286 template <typename base_t>
287 class JPConversionLongAsFloat : public JPConversion
288 {
289 public:
290
291 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
292 {
293 if (!PyLong_Check(match.object))
294 return match.type = JPMatch::_none;
295 match.conversion = this;
296 return match.type = JPMatch::_implicit;
297 }
298
299 void getInfo(JPClass *cls, JPConversionInfo &info) override
300 {
301 PyList_Append(info.implicit, (PyObject*) & PyLong_Type);
302 }
303
304 jvalue convert(JPMatch &match) override
305 {
306 jvalue res;
307 jdouble v = PyLong_AsDouble(match.object);
308 if (v == -1.0)
309 JP_PY_CHECK(); // GCOVR_EXCL_LINE
310 base_t::field(res) = (typename base_t::type_t) v;
311 return res;
312 }
313 } ;
314
315 template <typename base_t>
316 class JPConversionFloatWiden : public JPConversion
317 {
318 public:
319
320 // GCOVR_EXCL_START
321
322 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
323 {
324 return JPMatch::_none; // not used
325 }
326
327 void getInfo(JPClass *cls, JPConversionInfo &info) override
328 {
329 }
330 // GCOVR_EXCL_STOP
331
332 jvalue convert(JPMatch &match) override
333 {
334 JPValue *value = match.getJavaSlot();
335 jvalue ret;
336 base_t::field(ret) = (typename base_t::type_t) (dynamic_cast<JPPrimitiveType*>( value->getClass()))->getAsDouble(value->getValue());
337 return ret;
338 }
339 } ;
340
341 #endif /* JP_PRIMITIVE_ACCESSOR_H */