| 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_bytetype.h" |
| 21 | |
| 22 | JPByteType::JPByteType() |
| 23 | : JPPrimitiveType("byte") |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | JPByteType::~JPByteType() |
| 28 | = default; |
| 29 | |
| 30 | JPClass* JPByteType::getBoxedClass(JPJavaFrame& frame) const |
| 31 | { |
| 32 | return frame.getContext()->_java_lang_Byte; |
| 33 | } |
| 34 | |
| 35 | JPPyObject JPByteType::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 JPByteType::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<JPByteType> byteConversion; |
| 53 | JPConversionLongNumber<JPByteType> byteNumberConversion; |
| 54 | |
| 55 | class JPConversionJByte : public JPConversionJavaValue |
| 56 | { |
| 57 | public: |
| 58 | |
| 59 | JPMatch::Type matches(JPClass *cls, JPMatch &match) override |
| 60 | { |
| 61 | JPValue *value = match.getJavaSlot(); |
| 62 | if (value == nullptr) |
| 63 | return match.type = JPMatch::_none; |
| 64 | match.type = JPMatch::_none; |
| 65 | // Implied conversion from boxed to primitive (JLS 5.1.8) |
| 66 | if (javaValueConversion->matches(cls, match) |
| 67 | || unboxConversion->matches(cls, match)) |
| 68 | return match.type; |
| 69 | |
| 70 | // Unboxing must be to the from the exact boxed type (JLS 5.1.8) |
| 71 | return JPMatch::_implicit; // stop the search |
| 72 | } |
| 73 | |
| 74 | void getInfo(JPClass *cls, JPConversionInfo &info) override |
| 75 | { |
| 76 | JPContext *context = JPContext_global; |
| 77 | PyList_Append(info.exact, (PyObject*) context->_byte->getHost()); |
| 78 | unboxConversion->getInfo(cls, info); |
| 79 | } |
| 80 | |
| 81 | } jbyteConversion; |
| 82 | |
| 83 | JPMatch::Type JPByteType::findJavaConversion(JPMatch &match) |
| 84 | { |
| 85 | JP_TRACE_IN("JPByteType::findJavaConversion"); |
| 86 | |
| 87 | if (match.object == Py_None) |
| 88 | return match.type = JPMatch::_none; |
| 89 | |
| 90 | if (jbyteConversion.matches(this, match) |
| 91 | || byteConversion.matches(this, match) |
| 92 | || byteNumberConversion.matches(this, match)) |
| 93 | return match.type; |
| 94 | |
| 95 | return match.type = JPMatch::_none; |
| 96 | JP_TRACE_OUT; |
| 97 | } |
| 98 | |
| 99 | void JPByteType::getConversionInfo(JPConversionInfo &info) |
| 100 | { |
| 101 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 102 | jbyteConversion.getInfo(this, info); |
| 103 | byteConversion.getInfo(this, info); |
| 104 | byteNumberConversion.getInfo(this, info); |
| 105 | PyList_Append(info.ret, (PyObject*) JPContext_global->_int->getHost()); |
| 106 | } |
| 107 | |
| 108 | jarray JPByteType::newArrayOf(JPJavaFrame& frame, jsize sz) |
| 109 | { |
| 110 | return frame.NewByteArray(sz); |
| 111 | } |
| 112 | |
| 113 | JPPyObject JPByteType::getStaticField(JPJavaFrame& frame, jclass c, jfieldID fid) |
| 114 | { |
| 115 | jvalue v; |
| 116 | field(v) = frame.GetStaticByteField(c, fid); |
| 117 | return convertToPythonObject(frame, v, false); |
| 118 | } |
| 119 | |
| 120 | JPPyObject JPByteType::getField(JPJavaFrame& frame, jobject c, jfieldID fid) |
| 121 | { |
| 122 | jvalue v; |
| 123 | field(v) = frame.GetByteField(c, fid); |
| 124 | return convertToPythonObject(frame, v, false); |
| 125 | } |
| 126 | |
| 127 | JPPyObject JPByteType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue* val) |
| 128 | { |
| 129 | jvalue v; |
| 130 | { |
| 131 | JPPyCallRelease call; |
| 132 | field(v) = frame.CallStaticByteMethodA(claz, mth, val); |
| 133 | } |
| 134 | return convertToPythonObject(frame, v, false); |
| 135 | } |
| 136 | |
| 137 | JPPyObject JPByteType::invoke(JPJavaFrame& frame, jobject obj, jclass clazz, jmethodID mth, jvalue* val) |
| 138 | { |
| 139 | jvalue v; |
| 140 | { |
| 141 | JPPyCallRelease call; |
| 142 | if (clazz == nullptr) |
| 143 | field(v) = frame.CallByteMethodA(obj, mth, val); |
| 144 | else |
| 145 | field(v) = frame.CallNonvirtualByteMethodA(obj, clazz, mth, val); |
| 146 | } |
| 147 | return convertToPythonObject(frame, v, false); |
| 148 | } |
| 149 | |
| 150 | void JPByteType::setStaticField(JPJavaFrame& frame, jclass c, jfieldID fid, PyObject* obj) |
| 151 | { |
| 152 | JPMatch match(&frame, obj); |
| 153 | if (findJavaConversion(match) < JPMatch::_implicit) |
| 154 | JP_RAISE(PyExc_TypeError, "Unable to convert to Java byte"); |
| 155 | type_t val = field(match.convert()); |
| 156 | frame.SetStaticByteField(c, fid, val); |
| 157 | } |
| 158 | |
| 159 | void JPByteType::setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* obj) |
| 160 | { |
| 161 | JPMatch match(&frame, obj); |
| 162 | if (findJavaConversion(match) < JPMatch::_implicit) |
| 163 | JP_RAISE(PyExc_TypeError, "Unable to convert to Java byte"); |
| 164 | type_t val = field(match.convert()); |
| 165 | frame.SetByteField(c, fid, val); |
| 166 | } |
| 167 | |
| 168 | void JPByteType::setArrayRange(JPJavaFrame& frame, jarray a, |
| 169 | jsize start, jsize length, jsize step, PyObject* sequence) |
| 170 | { |
| 171 | JP_TRACE_IN("JPByteType::setArrayRange"); |
| 172 | JPPrimitiveArrayAccessor<array_t, type_t*> accessor(frame, a, |
| 173 | &JPJavaFrame::GetByteArrayElements, &JPJavaFrame::ReleaseByteArrayElements); |
| 174 | |
| 175 | type_t* val = accessor.get(); |
| 176 | // First check if assigning sequence supports buffer API |
| 177 | if (PyObject_CheckBuffer(sequence)) |
| 178 | { |
| 179 | JPPyBuffer buffer(sequence, PyBUF_FULL_RO); |
| 180 | if (buffer.valid()) |
| 181 | { |
| 182 | Py_buffer& view = buffer.getView(); |
| 183 | if (view.ndim != 1) |
| 184 | JP_RAISE(PyExc_TypeError, "buffer dims incorrect"); |
| 185 | Py_ssize_t vshape = view.shape[0]; |
| 186 | Py_ssize_t vstep = view.strides[0]; |
| 187 | if (vshape != length) |
| 188 | JP_RAISE(PyExc_ValueError, "mismatched size"); |
| 189 | |
| 190 | char* memory = (char*) view.buf; |
| 191 | if (view.suboffsets && view.suboffsets[0] >= 0) |
| 192 | memory = *((char**) memory) + view.suboffsets[0]; |
| 193 | jsize index = start; |
| 194 | jconverter conv = getConverter(view.format, (int) view.itemsize, "b"); |
| 195 | for (Py_ssize_t i = 0; i < length; ++i, index += step) |
| 196 | { |
| 197 | jvalue r = conv(memory); |
| 198 | val[index] = r.b; |
| 199 | memory += vstep; |
| 200 | } |
| 201 | accessor.commit(); |
| 202 | return; |
| 203 | } else |
| 204 | { |
| 205 | PyErr_Clear(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Use sequence API |
| 210 | JPPySequence seq = JPPySequence::use(sequence); |
| 211 | jsize index = start; |
| 212 | for (Py_ssize_t i = 0; i < length; ++i, index += step) |
| 213 | { |
| 214 | PyObject *item = seq[i].get(); |
| 215 | if (!PyIndex_Check(item)) |
| 216 | { |
| 217 | PyErr_Format(PyExc_TypeError, "Unable to implicitly convert '%s' to byte", Py_TYPE(item)->tp_name); |
| 218 | JP_RAISE_PYTHON(); |
| 219 | } |
| 220 | jlong v = PyLong_AsLongLong(item); |
| 221 | if (v == -1) |
| 222 | JP_PY_CHECK(); |
| 223 | val[index] = (type_t) assertRange(v); |
| 224 | } |
| 225 | accessor.commit(); |
| 226 | JP_TRACE_OUT; |
| 227 | } |
| 228 | |
| 229 | JPPyObject JPByteType::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx) |
| 230 | { |
| 231 | auto array = (array_t) a; |
| 232 | type_t val; |
| 233 | frame.GetByteArrayRegion(array, ndx, 1, &val); |
| 234 | jvalue v; |
| 235 | field(v) = val; |
| 236 | return convertToPythonObject(frame, v, false); |
| 237 | } |
| 238 | |
| 239 | void JPByteType::setArrayItem(JPJavaFrame& frame, jarray a, jsize ndx, PyObject* obj) |
| 240 | { |
| 241 | JPMatch match(&frame, obj); |
| 242 | if (findJavaConversion(match) < JPMatch::_implicit) |
| 243 | JP_RAISE(PyExc_TypeError, "Unable to convert to Java byte"); |
| 244 | type_t val = field(match.convert()); |
| 245 | frame.SetByteArrayRegion((array_t) a, ndx, 1, &val); |
| 246 | } |
| 247 | |
| 248 | void JPByteType::getView(JPArrayView& view) |
| 249 | { |
| 250 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 251 | view.m_Memory = (void*) frame.GetByteArrayElements( |
| 252 | (jbyteArray) view.m_Array->getJava(), &view.m_IsCopy); |
| 253 | view.m_Buffer.format = "b"; |
| 254 | view.m_Buffer.itemsize = sizeof (jbyte); |
| 255 | } |
| 256 | |
| 257 | void JPByteType::releaseView(JPArrayView& view) |
| 258 | { |
| 259 | try |
| 260 | { |
| 261 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 262 | frame.ReleaseByteArrayElements((jbyteArray) view.m_Array->getJava(), |
| 263 | (jbyte*) view.m_Memory, view.m_Buffer.readonly ? JNI_ABORT : 0); |
| 264 | } catch (JPypeException&) |
| 265 | { |
| 266 | // This is called as part of the cleanup routine and exceptions |
| 267 | // are not permitted |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | const char* JPByteType::getBufferFormat() |
| 272 | { |
| 273 | return "b"; |
| 274 | } |
| 275 | |
| 276 | Py_ssize_t JPByteType::getItemSize() |
| 277 | { |
| 278 | return sizeof (jbyte); |
| 279 | } |
| 280 | |
| 281 | void JPByteType::copyElements(JPJavaFrame &frame, jarray a, jsize start, jsize len, |
| 282 | void* memory, int offset) |
| 283 | { |
| 284 | auto* b = (jbyte*) ((char*) memory + offset); |
| 285 | frame.GetByteArrayRegion((jbyteArray) a, start, len, b); |
| 286 | } |
| 287 | |
| 288 | static void pack(jbyte* d, jvalue v) |
| 289 | { |
| 290 | *d = v.b; |
| 291 | } |
| 292 | |
| 293 | PyObject *JPByteType::newMultiArray(JPJavaFrame &frame, JPPyBuffer &buffer, int subs, int base, jobject dims) |
| 294 | { |
| 295 | JP_TRACE_IN("JPByteType::newMultiArray"); |
| 296 | return convertMultiArray<type_t>( |
| 297 | frame, this, &pack, "b", |
| 298 | buffer, subs, base, dims); |
| 299 | JP_TRACE_OUT; |
| 300 | } |