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