Debian Codegraph @

native/common/jp_inttype.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_inttype.h"
21
22 JPIntType::JPIntType()
23 : JPPrimitiveType("int")
24 {
25 }
26
27 JPIntType::~JPIntType()
28 = default;
29
30 JPClass* JPIntType::getBoxedClass(JPJavaFrame& frame) const
31 {
32 return frame.getContext()->_java_lang_Integer;
33 }
34
35 JPPyObject JPIntType::convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast)
36 {
37 JPPyObject tmp = JPPyObject::call(PyLong_FromLong(field(val)));
38 if (getHost() == nullptr)
39 return tmp;
40 JPPyObject out = JPPyObject::call(convertLong(getHost(), (PyLongObject*) tmp.get()));
41 PyJPValue_assignJavaSlot(frame, out.get(), JPValue(this, val));
42 return out;
43 }
44
45 JPValue JPIntType::getValueFromObject(JPJavaFrame& frame, const JPValue& obj)
46 {
47 jvalue v;
48 jobject jo = obj.getValue().l;
49 auto* jb = dynamic_cast<JPBoxedType*>( frame.findClassForObject(jo));
50 field(v) = (type_t) frame.CallIntMethodA(jo, jb->m_IntValueID, nullptr);
51 return JPValue(this, v);
52 }
53
54 JPConversionLong<JPIntType> intConversion;
55 JPConversionLongNumber<JPIntType> intNumberConversion;
56 JPConversionLongWiden<JPIntType> intWidenConversion;
57
58 class JPConversionJInt : public JPConversionJavaValue
59 {
60 public:
61
62 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
63 {
64 JPValue *value = match.getJavaSlot();
65 if (value == nullptr)
66 return JPMatch::_none;
67 match.type = JPMatch::_none;
68
69 // Implied conversion from boxed to primitive (JLS 5.1.8)
70 if (javaValueConversion->matches(cls, match)
71 || unboxConversion->matches(cls, match))
72 return match.type;
73
74 // Consider widening
75 JPClass *cls2 = value->getClass();
76 if (cls2->isPrimitive())
77 {
78 // https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2
79 auto *prim = dynamic_cast<JPPrimitiveType*>( cls2);
80 switch (prim->getTypeCode())
81 {
82 case 'C':
83 case 'S':
84 case 'B':
85 match.conversion = &intWidenConversion;
86 return match.type = JPMatch::_implicit;
87 default:
88 break;
89 }
90 }
91
92 // Unboxing must be to the from the exact boxed type (JLS 5.1.8)
93 return JPMatch::_implicit; //short cut further checks
94 }
95
96 void getInfo(JPClass *cls, JPConversionInfo &info) override
97 {
98 JPContext *context = JPContext_global;
99 PyList_Append(info.exact, (PyObject*) context->_int->getHost());
100 PyList_Append(info.implicit, (PyObject*) context->_byte->getHost());
101 PyList_Append(info.implicit, (PyObject*) context->_char->getHost());
102 PyList_Append(info.implicit, (PyObject*) context->_short->getHost());
103 unboxConversion->getInfo(cls, info);
104 }
105
106 } jintConversion;
107
108 JPMatch::Type JPIntType::findJavaConversion(JPMatch &match)
109 {
110 JP_TRACE_IN("JPIntType::findJavaConversion");
111
112 if (match.object == Py_None)
113 return match.type = JPMatch::_none;
114
115 if (jintConversion.matches(this, match)
116 || intConversion.matches(this, match)
117 || intNumberConversion.matches(this, match))
118 return match.type;
119
120 return match.type = JPMatch::_none;
121 JP_TRACE_OUT;
122 }
123
124 void JPIntType::getConversionInfo(JPConversionInfo &info)
125 {
126 JPJavaFrame frame = JPJavaFrame::outer();
127 jintConversion.getInfo(this, info);
128 intConversion.getInfo(this, info);
129 intNumberConversion.getInfo(this, info);
130 PyList_Append(info.ret, (PyObject*) JPContext_global->_int->getHost());
131 }
132
133 jarray JPIntType::newArrayOf(JPJavaFrame& frame, jsize sz)
134 {
135 return frame.NewIntArray(sz);
136 }
137
138 JPPyObject JPIntType::getStaticField(JPJavaFrame& frame, jclass c, jfieldID fid)
139 {
140 jvalue v;
141 field(v) = frame.GetStaticIntField(c, fid);
142 return convertToPythonObject(frame, v, false);
143 }
144
145 JPPyObject JPIntType::getField(JPJavaFrame& frame, jobject c, jfieldID fid)
146 {
147 jvalue v;
148 field(v) = frame.GetIntField(c, fid);
149 return convertToPythonObject(frame, v, false);
150 }
151
152 JPPyObject JPIntType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue* val)
153 {
154 jvalue v;
155 {
156 JPPyCallRelease call;
157 field(v) = frame.CallStaticIntMethodA(claz, mth, val);
158 }
159 return convertToPythonObject(frame, v, false);
160 }
161
162 JPPyObject JPIntType::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.CallIntMethodA(obj, mth, val);
169 else
170 field(v) = frame.CallNonvirtualIntMethodA(obj, clazz, mth, val);
171 }
172 return convertToPythonObject(frame, v, false);
173 }
174
175 void JPIntType::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.SetStaticIntField(c, fid, val);
182 }
183
184 void JPIntType::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.SetIntField(c, fid, val);
191 }
192
193 void JPIntType::setArrayRange(JPJavaFrame& frame, jarray a,
194 jsize start, jsize length, jsize step,
195 PyObject* sequence)
196 {
197 JP_TRACE_IN("JPIntType::setArrayRange");
198 JPPrimitiveArrayAccessor<array_t, type_t*> accessor(frame, a,
199 &JPJavaFrame::GetIntArrayElements, &JPJavaFrame::ReleaseIntArrayElements);
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, "i");
221 for (Py_ssize_t i = 0; i < length; ++i, index += step)
222 {
223 jvalue r = conv(memory);
224 val[index] = r.i;
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 int", 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) assertRange(v);
250 }
251 accessor.commit();
252 JP_TRACE_OUT;
253 }
254
255 JPPyObject JPIntType::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx)
256 {
257 auto array = (array_t) a;
258 type_t val;
259 frame.GetIntArrayRegion(array, ndx, 1, &val);
260 jvalue v;
261 field(v) = val;
262 return convertToPythonObject(frame, v, false);
263 }
264
265 void JPIntType::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.SetIntArrayRegion((array_t) a, ndx, 1, &val);
272 }
273
274 void JPIntType::getView(JPArrayView& view)
275 {
276 JPJavaFrame frame = JPJavaFrame::outer();
277 view.m_IsCopy = false;
278 view.m_Memory = (void*) frame.GetIntArrayElements(
279 (jintArray) view.m_Array->getJava(), &view.m_IsCopy);
280 view.m_Buffer.format = "=i";
281 view.m_Buffer.itemsize = sizeof (jint);
282 }
283
284 void JPIntType::releaseView(JPArrayView& view)
285 {
286 try
287 {
288 JPJavaFrame frame = JPJavaFrame::outer();
289 frame.ReleaseIntArrayElements((jintArray) view.m_Array->getJava(),
290 (jint*) view.m_Memory, view.m_Buffer.readonly ? JNI_ABORT : 0);
291 } catch (JPypeException&)
292 {
293 // This is called as part of the cleanup routine and exceptions
294 // are not permitted
295 }
296 }
297
298 const char* JPIntType::getBufferFormat()
299 {
300 return "=i";
301 }
302
303 Py_ssize_t JPIntType::getItemSize()
304 {
305 return sizeof (jfloat);
306 }
307
308 void JPIntType::copyElements(JPJavaFrame &frame, jarray a, jsize start, jsize len,
309 void* memory, int offset)
310 {
311 jint* b = (jint*) ((char*) memory + offset);
312 frame.GetIntArrayRegion((jintArray) a, start, len, b);
313 }
314
315 static void pack(jint* d, jvalue v)
316 {
317 *d = v.i;
318 }
319
320 PyObject *JPIntType::newMultiArray(JPJavaFrame &frame, JPPyBuffer &buffer, int subs, int base, jobject dims)
321 {
322 JP_TRACE_IN("JPIntType::newMultiArray");
323 return convertMultiArray<type_t>(
324 frame, this, &pack, "i",
325 buffer, subs, base, dims);
326 JP_TRACE_OUT;
327 }