Debian Codegraph @

native/common/jp_longtype.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_longtype.h"
21
22 JPLongType::JPLongType()
23 : JPPrimitiveType("long")
24 {
25 }
26
27 JPLongType::~JPLongType()
28 = default;
29
30 JPClass* JPLongType::getBoxedClass(JPJavaFrame& frame) const
31 {
32 return frame.getContext()->_java_lang_Long;
33 }
34
35 JPPyObject JPLongType::convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast)
36 {
37 JPPyObject tmp = JPPyObject::call(PyLong_FromLongLong(field(val)));
38 JPPyObject out = JPPyObject::call(convertLong(getHost(), (PyLongObject*) tmp.get()));
39 PyJPValue_assignJavaSlot(frame, out.get(), JPValue(this, val));
40 return out;
41 }
42
43 JPValue JPLongType::getValueFromObject(JPJavaFrame& frame, const JPValue& obj)
44 {
45 jvalue v;
46 jobject jo = obj.getValue().l;
47 auto* jb = dynamic_cast<JPBoxedType*>( frame.findClassForObject(jo));
48 field(v) = (type_t) frame.CallLongMethodA(jo, jb->m_LongValueID, nullptr);
49 return JPValue(this, v);
50 }
51
52 JPConversionLong<JPLongType> longConversion;
53 JPConversionLongNumber<JPLongType> longNumberConversion;
54 JPConversionLongWiden<JPLongType> longWidenConversion;
55
56 class JPConversionJLong : public JPConversionJavaValue
57 {
58 public:
59
60 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
61 {
62 JPValue* value = match.getJavaSlot();
63 if (value == nullptr)
64 return match.type = JPMatch::_none;
65
66 // Implied conversion from boxed to primitive (JLS 5.1.8)
67 if (javaValueConversion->matches(cls, match)
68 || unboxConversion->matches(cls, match))
69 return match.type;
70
71 // Consider widening
72 JPClass *cls2 = value->getClass();
73 if (cls2->isPrimitive())
74 {
75 // https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2
76 auto *prim = dynamic_cast<JPPrimitiveType*>( cls2);
77 switch (prim->getTypeCode())
78 {
79 case 'I':
80 case 'C':
81 case 'S':
82 case 'B':
83 match.conversion = &longWidenConversion;
84 return match.type = JPMatch::_implicit;
85 default:
86 break;
87 }
88 }
89
90 // Unboxing must be to the from the exact boxed type (JLS 5.1.8)
91 match.type = JPMatch::_none;
92 return JPMatch::_implicit;
93 }
94
95 void getInfo(JPClass *cls, JPConversionInfo &info) override
96 {
97 JPContext *context = JPContext_global;
98 PyList_Append(info.exact, (PyObject*) context->_long->getHost());
99 PyList_Append(info.implicit, (PyObject*) context->_byte->getHost());
100 PyList_Append(info.implicit, (PyObject*) context->_char->getHost());
101 PyList_Append(info.implicit, (PyObject*) context->_short->getHost());
102 PyList_Append(info.implicit, (PyObject*) context->_int->getHost());
103 unboxConversion->getInfo(cls, info);
104 }
105 } jlongConversion;
106
107 JPMatch::Type JPLongType::findJavaConversion(JPMatch &match)
108 {
109 JP_TRACE_IN("JPLongType::findJavaConversion");
110
111 if (match.object == Py_None)
112 return match.type = JPMatch::_none;
113
114
115 if (jlongConversion.matches(this, match)
116 || longConversion.matches(this, match)
117 || longNumberConversion.matches(this, match))
118 return match.type;
119
120 return match.type = JPMatch::_none;
121 JP_TRACE_OUT;
122 }
123
124 void JPLongType::getConversionInfo(JPConversionInfo &info)
125 {
126 JPJavaFrame frame = JPJavaFrame::outer();
127 jlongConversion.getInfo(this, info);
128 longConversion.getInfo(this, info);
129 longNumberConversion.getInfo(this, info);
130 PyList_Append(info.ret, (PyObject*) JPContext_global->_long->getHost());
131 }
132
133 jarray JPLongType::newArrayOf(JPJavaFrame& frame, jsize sz)
134 {
135 return frame.NewLongArray(sz);
136 }
137
138 JPPyObject JPLongType::getStaticField(JPJavaFrame& frame, jclass c, jfieldID fid)
139 {
140 jvalue v;
141 field(v) = frame.GetStaticLongField(c, fid);
142 return convertToPythonObject(frame, v, false);
143 }
144
145 JPPyObject JPLongType::getField(JPJavaFrame& frame, jobject c, jfieldID fid)
146 {
147 jvalue v;
148 field(v) = frame.GetLongField(c, fid);
149 return convertToPythonObject(frame, v, false);
150 }
151
152 JPPyObject JPLongType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue* val)
153 {
154 jvalue v;
155 {
156 JPPyCallRelease call;
157 field(v) = frame.CallStaticLongMethodA(claz, mth, val);
158 }
159 return convertToPythonObject(frame, v, false);
160 }
161
162 JPPyObject JPLongType::invoke(JPJavaFrame& frame, jobject obj, jclass clazz, jmethodID mth, jvalue* val)
163 {
164 jvalue v;
165 {
166 JPPyCallRelease call;
167 if (clazz == nullptr)
168 field(v) = frame.CallLongMethodA(obj, mth, val);
169 else
170 field(v) = frame.CallNonvirtualLongMethodA(obj, clazz, mth, val);
171 }
172 return convertToPythonObject(frame, v, false);
173 }
174
175 void JPLongType::setStaticField(JPJavaFrame& frame, jclass c, jfieldID fid, PyObject* obj)
176 {
177 JPMatch match(&frame, obj);
178 if (findJavaConversion(match) < JPMatch::_implicit)
179 JP_RAISE(PyExc_TypeError, "Unable to convert to Java int");
180 type_t val = field(match.convert());
181 frame.SetStaticLongField(c, fid, val);
182 }
183
184 void JPLongType::setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* obj)
185 {
186 JPMatch match(&frame, obj);
187 if (findJavaConversion(match) < JPMatch::_implicit)
188 JP_RAISE(PyExc_TypeError, "Unable to convert to Java int");
189 type_t val = field(match.convert());
190 frame.SetLongField(c, fid, val);
191 }
192
193 void JPLongType::setArrayRange(JPJavaFrame& frame, jarray a,
194 jsize start, jsize length, jsize step,
195 PyObject* sequence)
196 {
197 JP_TRACE_IN("JPLongType::setArrayRange");
198 JPPrimitiveArrayAccessor<array_t, type_t*> accessor(frame, a,
199 &JPJavaFrame::GetLongArrayElements, &JPJavaFrame::ReleaseLongArrayElements);
200
201 type_t* val = accessor.get();
202 // First check if assigning sequence supports buffer API
203 if (PyObject_CheckBuffer(sequence))
204 {
205 JPPyBuffer buffer(sequence, PyBUF_FULL_RO);
206 if (buffer.valid())
207 {
208 Py_buffer& view = buffer.getView();
209 if (view.ndim != 1)
210 JP_RAISE(PyExc_TypeError, "buffer dims incorrect");
211 Py_ssize_t vshape = view.shape[0];
212 Py_ssize_t vstep = view.strides[0];
213 if (vshape != length)
214 JP_RAISE(PyExc_ValueError, "mismatched size");
215
216 char* memory = (char*) view.buf;
217 if (view.suboffsets && view.suboffsets[0] >= 0)
218 memory = *((char**) memory) + view.suboffsets[0];
219 jsize index = start;
220 jconverter conv = getConverter(view.format, (int) view.itemsize, "j");
221 for (Py_ssize_t i = 0; i < length; ++i, index += step)
222 {
223 jvalue r = conv(memory);
224 val[index] = r.j;
225 memory += vstep;
226 }
227 accessor.commit();
228 return;
229 } else
230 {
231 PyErr_Clear();
232 }
233 }
234
235 // Use sequence API
236 JPPySequence seq = JPPySequence::use(sequence);
237 jsize index = start;
238 for (Py_ssize_t i = 0; i < length; ++i, index += step)
239 {
240 PyObject *item = seq[i].get();
241 if (!PyIndex_Check(item))
242 {
243 PyErr_Format(PyExc_TypeError, "Unable to implicitly convert '%s' to long", Py_TYPE(item)->tp_name);
244 JP_RAISE_PYTHON();
245 }
246 jlong v = PyLong_AsLongLong(item);
247 if (v == -1)
248 JP_PY_CHECK()
249 val[index] = (type_t) v;
250 }
251 accessor.commit();
252 JP_TRACE_OUT;
253 }
254
255 JPPyObject JPLongType::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx)
256 {
257 auto array = (array_t) a;
258 type_t val;
259 frame.GetLongArrayRegion(array, ndx, 1, &val);
260 jvalue v;
261 field(v) = val;
262 return convertToPythonObject(frame, v, false);
263 }
264
265 void JPLongType::setArrayItem(JPJavaFrame& frame, jarray a, jsize ndx, PyObject* obj)
266 {
267 JPMatch match(&frame, obj);
268 if (findJavaConversion(match) < JPMatch::_implicit)
269 JP_RAISE(PyExc_TypeError, "Unable to convert to Java int");
270 type_t val = field(match.convert());
271 frame.SetLongArrayRegion((array_t) a, ndx, 1, &val);
272 }
273
274 void JPLongType::getView(JPArrayView& view)
275 {
276 JPJavaFrame frame = JPJavaFrame::outer();
277 view.m_Memory = (void*) frame.GetLongArrayElements(
278 (jlongArray) view.m_Array->getJava(), &view.m_IsCopy);
279 view.m_Buffer.format = "=q";
280 view.m_Buffer.itemsize = sizeof (jlong);
281 }
282
283 void JPLongType::releaseView(JPArrayView& view)
284 {
285 try
286 {
287 JPJavaFrame frame = JPJavaFrame::outer();
288 frame.ReleaseLongArrayElements((jlongArray) view.m_Array->getJava(),
289 (jlong*) view.m_Memory, view.m_Buffer.readonly ? JNI_ABORT : 0);
290 } catch (JPypeException&)
291 {
292 // This is called as part of the cleanup routine and exceptions
293 // are not permitted
294 }
295 }
296
297 const char* JPLongType::getBufferFormat()
298 {
299 return "=q";
300 }
301
302 Py_ssize_t JPLongType::getItemSize()
303 {
304 return sizeof (jlong);
305 }
306
307 void JPLongType::copyElements(JPJavaFrame &frame, jarray a, jsize start, jsize len,
308 void* memory, int offset)
309 {
310 auto* b = (jlong*) ((char*) memory + offset);
311 frame.GetLongArrayRegion((jlongArray) a, start, len, b);
312 }
313
314 static void pack(jlong* d, jvalue v)
315 {
316 *d = v.j;
317 }
318
319 PyObject *JPLongType::newMultiArray(JPJavaFrame &frame, JPPyBuffer &buffer, int subs, int base, jobject dims)
320 {
321 JP_TRACE_IN("JPLongType::newMultiArray");
322 return convertMultiArray<type_t>(
323 frame, this, &pack, "j",
324 buffer, subs, base, dims);
325 JP_TRACE_OUT;
326 }