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