Debian Codegraph @

native/common/jp_floattype.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_floattype.h"
21 #include "jp_boxedtype.h"
22
23 JPFloatType::JPFloatType()
24 : JPPrimitiveType("float")
25 {
26 }
27
28 JPFloatType::~JPFloatType()
29 = default;
30
31 JPClass* JPFloatType::getBoxedClass(JPJavaFrame& frame) const
32 {
33 return frame.getContext()->_java_lang_Float;
34 }
35
36 JPPyObject JPFloatType::convertToPythonObject(JPJavaFrame& frame, jvalue value, bool cast)
37 {
38 PyTypeObject * wrapper = getHost();
39 JPPyObject obj = JPPyObject::call(wrapper->tp_alloc(wrapper, 0));
40 ((PyFloatObject*) obj.get())->ob_fval = value.f;
41 PyJPValue_assignJavaSlot(frame, obj.get(), JPValue(this, value));
42 return obj;
43 }
44
45 JPValue JPFloatType::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.CallFloatMethodA(jo, jb->m_FloatValueID, nullptr);
51 return JPValue(this, v);
52 }
53
54 static JPConversionAsFloat<JPFloatType> asFloatConversion;
55 static JPConversionLongAsFloat<JPFloatType> asFloatLongConversion;
56 static JPConversionFloatWiden<JPFloatType> floatWidenConversion;
57
58 class JPConversionAsJFloat : 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 match.type = 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 'B':
83 case 'S':
84 case 'C':
85 case 'I':
86 case 'J':
87 match.conversion = &floatWidenConversion;
88 return match.type = JPMatch::_implicit;
89 default:
90 break;
91 }
92 }
93
94 // Unboxing must be to the from the exact boxed type (JLS 5.1.8)
95 return JPMatch::_implicit; // stop search
96 }
97
98 void getInfo(JPClass *cls, JPConversionInfo &info) override
99 {
100 JPContext *context = JPContext_global;
101 PyList_Append(info.exact, (PyObject*) context->_float->getHost());
102 PyList_Append(info.implicit, (PyObject*) context->_byte->getHost());
103 PyList_Append(info.implicit, (PyObject*) context->_char->getHost());
104 PyList_Append(info.implicit, (PyObject*) context->_short->getHost());
105 PyList_Append(info.implicit, (PyObject*) context->_int->getHost());
106 PyList_Append(info.implicit, (PyObject*) context->_long->getHost());
107 unboxConversion->getInfo(cls, info);
108 }
109
110 } asJFloatConversion;
111
112 JPMatch::Type JPFloatType::findJavaConversion(JPMatch &match)
113 {
114 JP_TRACE_IN("JPFloatType::findJavaConversion");
115
116 if (match.object == Py_None)
117 return match.type = JPMatch::_none;
118
119 if (asJFloatConversion.matches(this, match)
120 || asFloatLongConversion.matches(this, match)
121 || asFloatConversion.matches(this, match))
122 return match.type;
123
124 return match.type = JPMatch::_none;
125 JP_TRACE_OUT;
126 }
127
128 void JPFloatType::getConversionInfo(JPConversionInfo &info)
129 {
130 JPJavaFrame frame = JPJavaFrame::outer();
131 asJFloatConversion.getInfo(this, info);
132 asFloatLongConversion.getInfo(this, info);
133 asFloatConversion.getInfo(this, info);
134 PyList_Append(info.ret, (PyObject*) JPContext_global->_float->getHost());
135 }
136
137 jarray JPFloatType::newArrayOf(JPJavaFrame& frame, jsize sz)
138 {
139 return frame.NewFloatArray(sz);
140 }
141
142 JPPyObject JPFloatType::getStaticField(JPJavaFrame& frame, jclass c, jfieldID fid)
143 {
144 jvalue v;
145 field(v) = frame.GetStaticFloatField(c, fid);
146 return convertToPythonObject(frame, v, false);
147 }
148
149 JPPyObject JPFloatType::getField(JPJavaFrame& frame, jobject c, jfieldID fid)
150 {
151 jvalue v;
152 field(v) = frame.GetFloatField(c, fid);
153 return convertToPythonObject(frame, v, false);
154 }
155
156 JPPyObject JPFloatType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue *val)
157 {
158 jvalue v;
159 {
160 JPPyCallRelease call;
161 field(v) = frame.CallStaticFloatMethodA(claz, mth, val);
162 }
163 return convertToPythonObject(frame, v, false);
164 }
165
166 JPPyObject JPFloatType::invoke(JPJavaFrame& frame, jobject obj, jclass clazz, jmethodID mth, jvalue *val)
167 {
168 jvalue v;
169 {
170 JPPyCallRelease call;
171 if (clazz == nullptr)
172 field(v) = frame.CallFloatMethodA(obj, mth, val);
173 else
174 field(v) = frame.CallNonvirtualFloatMethodA(obj, clazz, mth, val);
175 }
176 return convertToPythonObject(frame, v, false);
177 }
178
179 void JPFloatType::setStaticField(JPJavaFrame& frame, jclass c, jfieldID fid, PyObject *obj)
180 {
181 JPMatch match(&frame, obj);
182 if (findJavaConversion(match) < JPMatch::_implicit)
183 JP_RAISE(PyExc_TypeError, "Unable to convert to Java float");
184 type_t val = field(match.convert());
185 frame.SetStaticFloatField(c, fid, val);
186 }
187
188 void JPFloatType::setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject *obj)
189 {
190 JPMatch match(&frame, obj);
191 if (findJavaConversion(match) < JPMatch::_implicit)
192 JP_RAISE(PyExc_TypeError, "Unable to convert to Java float");
193 type_t val = field(match.convert());
194 frame.SetFloatField(c, fid, val);
195 }
196
197 void JPFloatType::setArrayRange(JPJavaFrame& frame, jarray a,
198 jsize start, jsize length, jsize step,
199 PyObject* sequence)
200 {
201 JP_TRACE_IN("JPFloatType::setArrayRange");
202 JPPrimitiveArrayAccessor<array_t, type_t*> accessor(frame, a,
203 &JPJavaFrame::GetFloatArrayElements, &JPJavaFrame::ReleaseFloatArrayElements);
204
205 type_t* val = accessor.get();
206 // First check if assigning sequence supports buffer API
207 if (PyObject_CheckBuffer(sequence))
208 {
209 JPPyBuffer buffer(sequence, PyBUF_FULL_RO);
210 if (buffer.valid())
211 {
212 Py_buffer& view = buffer.getView();
213 if (view.ndim != 1)
214 JP_RAISE(PyExc_TypeError, "buffer dims incorrect");
215 Py_ssize_t vshape = view.shape[0];
216 Py_ssize_t vstep = view.strides[0];
217 if (vshape != length)
218 JP_RAISE(PyExc_ValueError, "mismatched size");
219
220 char* memory = (char*) view.buf;
221 if (view.suboffsets && view.suboffsets[0] >= 0)
222 memory = *((char**) memory) + view.suboffsets[0];
223 jsize index = start;
224 jconverter conv = getConverter(view.format, (int) view.itemsize, "f");
225 for (Py_ssize_t i = 0; i < length; ++i, index += step)
226 {
227 jvalue r = conv(memory);
228 val[index] = r.f;
229 memory += vstep;
230 }
231 accessor.commit();
232 return;
233 } else
234 {
235 PyErr_Clear();
236 }
237 }
238
239 // Use sequence API
240 JPPySequence seq = JPPySequence::use(sequence);
241 jsize index = start;
242 for (Py_ssize_t i = 0; i < length; ++i, index += step)
243 {
244 double v = PyFloat_AsDouble(seq[i].get());
245 if (v == -1.)
246 JP_PY_CHECK();
247 val[index] = (type_t) v;
248 }
249 accessor.commit();
250 JP_TRACE_OUT;
251 }
252
253 JPPyObject JPFloatType::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx)
254 {
255 auto array = (array_t) a;
256 type_t val;
257 frame.GetFloatArrayRegion(array, ndx, 1, &val);
258 jvalue v;
259 field(v) = val;
260 return convertToPythonObject(frame, v, false);
261 }
262
263 void JPFloatType::setArrayItem(JPJavaFrame& frame, jarray a, jsize ndx, PyObject* obj)
264 {
265 JPMatch match(&frame, obj);
266 if (findJavaConversion(match) < JPMatch::_implicit)
267 JP_RAISE(PyExc_TypeError, "Unable to convert to Java float");
268 type_t val = field(match.convert());
269 frame.SetFloatArrayRegion((array_t) a, ndx, 1, &val);
270 }
271
272 void JPFloatType::getView(JPArrayView& view)
273 {
274 JPJavaFrame frame = JPJavaFrame::outer();
275 view.m_Memory = (void*) frame.GetFloatArrayElements(
276 (jfloatArray) view.m_Array->getJava(), &view.m_IsCopy);
277 view.m_Buffer.format = "f";
278 view.m_Buffer.itemsize = sizeof (jfloat);
279 }
280
281 void JPFloatType::releaseView(JPArrayView& view)
282 {
283 try
284 {
285 JPJavaFrame frame = JPJavaFrame::outer();
286 frame.ReleaseFloatArrayElements((jfloatArray) view.m_Array->getJava(),
287 (jfloat*) view.m_Memory, view.m_Buffer.readonly ? JNI_ABORT : 0);
288 } catch (JPypeException&)
289 {
290 // This is called as part of the cleanup routine and exceptions
291 // are not permitted
292 }
293 }
294
295 const char* JPFloatType::getBufferFormat()
296 {
297 return "f";
298 }
299
300 Py_ssize_t JPFloatType::getItemSize()
301 {
302 return sizeof (jfloat);
303 }
304
305 void JPFloatType::copyElements(JPJavaFrame &frame, jarray a, jsize start, jsize len,
306 void* memory, int offset)
307 {
308 auto* b = (jfloat*) ((char*) memory + offset);
309 frame.GetFloatArrayRegion((jfloatArray) a, start, len, b);
310 }
311
312 static void pack(jfloat* d, jvalue v)
313 {
314 *d = v.f;
315 }
316
317 PyObject *JPFloatType::newMultiArray(JPJavaFrame &frame, JPPyBuffer &buffer, int subs, int base, jobject dims)
318 {
319 JP_TRACE_IN("JPFloatType::newMultiArray");
320 return convertMultiArray<type_t>(
321 frame, this, &pack, "f",
322 buffer, subs, base, dims);
323 JP_TRACE_OUT;
324 }