| 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_arrayclass.h" |
| 20 | #include "jp_primitive_accessor.h" |
| 21 | |
| 22 | // Note: java represents arrays of zero length as null, thus we |
| 23 | // need to be careful to handle these properly. We need to |
| 24 | // carry them around so that we can match types. |
| 25 | |
| 26 | JPArray::JPArray(const JPValue &value) |
| 27 | : m_Object((jarray) value.getValue().l) |
| 28 | { |
| 29 | m_Class = dynamic_cast<JPArrayClass*>( value.getClass()); |
| 30 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 31 | JP_TRACE_IN("JPArray::JPArray"); |
| 32 | ASSERT_NOT_NULL(m_Class); |
| 33 | JP_TRACE(m_Class->toString()); |
| 34 | |
| 35 | // We will use this during range checks, so cache it |
| 36 | if (m_Object.get() == nullptr) |
| 37 | m_Length = 0; // GCOVR_EXCL_LINE |
| 38 | else |
| 39 | m_Length = frame.GetArrayLength(m_Object.get()); |
| 40 | |
| 41 | m_Step = 1; |
| 42 | m_Start = 0; |
| 43 | m_Slice = false; |
| 44 | |
| 45 | JP_TRACE_OUT; |
| 46 | } |
| 47 | |
| 48 | JPArray::JPArray(JPArray* instance, jsize start, jsize stop, jsize step) |
| 49 | : m_Object((jarray) instance->getJava()) |
| 50 | { |
| 51 | JP_TRACE_IN("JPArray::JPArraySlice"); |
| 52 | m_Class = instance->m_Class; |
| 53 | m_Step = step * instance->m_Step; |
| 54 | m_Start = instance->m_Start + instance->m_Step*start; |
| 55 | if (step > 0) |
| 56 | m_Length = (stop - start - 1 + step) / step; |
| 57 | else |
| 58 | m_Length = (stop - start + 1 + step) / step; |
| 59 | if (m_Length < 0) |
| 60 | m_Length = 0; // GCOVR_EXCL_LINE |
| 61 | m_Slice = true; |
| 62 | JP_TRACE_OUT; |
| 63 | } |
| 64 | |
| 65 | JPArray::~JPArray() |
| 66 | = default; |
| 67 | |
| 68 | jsize JPArray::getLength() const |
| 69 | { |
| 70 | return m_Length; |
| 71 | } |
| 72 | |
| 73 | void JPArray::setRange(jsize start, jsize length, jsize step, PyObject* val) |
| 74 | { |
| 75 | JP_TRACE_IN("JPArray::setRange"); |
| 76 | |
| 77 | // Make sure it is an iterable before we start |
| 78 | if (!PySequence_Check(val)) |
| 79 | JP_RAISE(PyExc_TypeError, "can only assign a sequence"); |
| 80 | |
| 81 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 82 | JPClass* compType = m_Class->getComponentType(); |
| 83 | JPPySequence seq = JPPySequence::use(val); |
| 84 | long plength = (long) seq.size(); |
| 85 | |
| 86 | JP_TRACE("Verify lengths", length, plength); |
| 87 | if ((long) length != plength) |
| 88 | { |
| 89 | // Python would allow mismatching size by growing or shrinking |
| 90 | // the length of the array. But java arrays are immutable in length. |
| 91 | std::stringstream out; |
| 92 | out << "Slice assignment must be of equal lengths : " << length << " != " << plength; |
| 93 | JP_RAISE(PyExc_ValueError, out.str()); |
| 94 | } |
| 95 | |
| 96 | JP_TRACE("Call component set range"); |
| 97 | jsize i0 = m_Start + m_Step*start; |
| 98 | compType->setArrayRange(frame, m_Object.get(), i0, length, m_Step*step, val); |
| 99 | JP_TRACE_OUT; |
| 100 | } |
| 101 | |
| 102 | void JPArray::setItem(jsize ndx, PyObject* val) |
| 103 | { |
| 104 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 105 | JPClass* compType = m_Class->getComponentType(); |
| 106 | |
| 107 | if (ndx < 0) |
| 108 | ndx += m_Length; |
| 109 | |
| 110 | if (ndx >= m_Length || ndx < 0) |
| 111 | JP_RAISE(PyExc_IndexError, "java array assignment out of bounds"); |
| 112 | |
| 113 | compType->setArrayItem(frame, m_Object.get(), m_Start + ndx*m_Step, val); |
| 114 | } |
| 115 | |
| 116 | JPPyObject JPArray::getItem(jsize ndx) |
| 117 | { |
| 118 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 119 | JPClass* compType = m_Class->getComponentType(); |
| 120 | |
| 121 | if (ndx < 0) |
| 122 | ndx += m_Length; |
| 123 | |
| 124 | if (ndx >= m_Length || ndx < 0) |
| 125 | { |
| 126 | JP_RAISE(PyExc_IndexError, "array index out of bounds"); |
| 127 | } |
| 128 | |
| 129 | return compType->getArrayItem(frame, m_Object.get(), m_Start + ndx * m_Step); |
| 130 | } |
| 131 | |
| 132 | jarray JPArray::clone(JPJavaFrame& frame, PyObject* obj) |
| 133 | { |
| 134 | JPValue value = m_Class->newArray(frame, m_Length); |
| 135 | JPClass* compType = m_Class->getComponentType(); |
| 136 | auto out = (jarray) value.getValue().l; |
| 137 | compType->setArrayRange(frame, out, 0, m_Length, 1, obj); |
| 138 | return out; |
| 139 | } |
| 140 | |
| 141 | JPArrayView::JPArrayView(JPArray* array) |
| 142 | { |
| 143 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 144 | m_Array = array; |
| 145 | m_RefCount = 0; |
| 146 | m_Buffer.obj = nullptr; |
| 147 | m_Buffer.ndim = 1; |
| 148 | m_Buffer.suboffsets = nullptr; |
| 149 | auto *type = dynamic_cast<JPPrimitiveType*>( array->getClass()->getComponentType()); |
| 150 | type->getView(*this); |
| 151 | m_Strides[0] = m_Buffer.itemsize * array->m_Step; |
| 152 | m_Shape[0] = array->m_Length; |
| 153 | m_Buffer.buf = (char*) m_Memory + m_Buffer.itemsize * array->m_Start; |
| 154 | m_Buffer.len = array->m_Length * m_Buffer.itemsize; |
| 155 | m_Buffer.shape = m_Shape; |
| 156 | m_Buffer.strides = m_Strides; |
| 157 | m_Buffer.readonly = 1; |
| 158 | m_Owned = false; |
| 159 | } |
| 160 | |
| 161 | JPArrayView::JPArrayView(JPArray* array, jobject collection) |
| 162 | { |
| 163 | JP_TRACE_IN("JPArrayView::JPArrayView"); |
| 164 | // All of the work has already been done by org.jpype.Utilities |
| 165 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 166 | m_Array = array; |
| 167 | |
| 168 | jint len = frame.GetArrayLength((jarray) collection); |
| 169 | jobject item0 = frame.GetObjectArrayElement((jobjectArray) collection, 0); |
| 170 | jobject item1 = frame.GetObjectArrayElement((jobjectArray) collection, 1); |
| 171 | |
| 172 | // First element is the primitive type that we are packing the array from |
| 173 | auto *componentType = dynamic_cast<JPPrimitiveType*>( |
| 174 | frame.findClass((jclass) item0)); |
| 175 | |
| 176 | // Second element is the shape of the array from which we compute the |
| 177 | // memory size, the shape, and strides |
| 178 | int dims; |
| 179 | Py_ssize_t itemsize; |
| 180 | Py_ssize_t sz; |
| 181 | { |
| 182 | JPPrimitiveArrayAccessor<jintArray, jint*> accessor(frame, (jintArray) item1, |
| 183 | &JPJavaFrame::GetIntArrayElements, &JPJavaFrame::ReleaseIntArrayElements); |
| 184 | jint* shape2 = accessor.get(); |
| 185 | dims = frame.GetArrayLength((jarray) item1); |
| 186 | itemsize = componentType->getItemSize(); |
| 187 | sz = itemsize; |
| 188 | for (int i = 0; i < dims; ++i) |
| 189 | { |
| 190 | m_Shape[i] = shape2[i]; |
| 191 | sz *= m_Shape[i]; |
| 192 | } |
| 193 | accessor.abort(); |
| 194 | } |
| 195 | Py_ssize_t stride = itemsize; |
| 196 | for (int i = 0; i < dims; ++i) |
| 197 | { |
| 198 | int n = dims - 1 - i; |
| 199 | m_Strides[n] = stride; |
| 200 | stride *= m_Shape[n]; |
| 201 | } |
| 202 | |
| 203 | m_RefCount = 0; |
| 204 | m_Memory = new char[sz]; |
| 205 | m_Owned = true; |
| 206 | |
| 207 | // All remaining elements are primitive arrays to be unpacked |
| 208 | int offset = 0; |
| 209 | Py_ssize_t last = m_Shape[dims - 1]; |
| 210 | for (Py_ssize_t i = 0; i < len - 2; i++) |
| 211 | { |
| 212 | auto a1 = (jarray) frame.GetObjectArrayElement((jobjectArray) collection, (jsize) i + 2); |
| 213 | componentType->copyElements(frame, a1, 0, (jsize) last, m_Memory, offset); |
| 214 | offset += (int) (itemsize * last); |
| 215 | frame.DeleteLocalRef(a1); |
| 216 | } |
| 217 | |
| 218 | // Copy values into Python buffer for consumption |
| 219 | m_Buffer.obj = nullptr; |
| 220 | m_Buffer.ndim = dims; |
| 221 | m_Buffer.suboffsets = nullptr; |
| 222 | m_Buffer.itemsize = itemsize; |
| 223 | m_Buffer.format = const_cast<char*> (componentType->getBufferFormat()); |
| 224 | m_Buffer.buf = (char*) m_Memory + m_Buffer.itemsize * array->m_Start; |
| 225 | m_Buffer.len = sz; |
| 226 | m_Buffer.shape = m_Shape; |
| 227 | m_Buffer.strides = m_Strides; |
| 228 | m_Buffer.readonly = 1; |
| 229 | JP_TRACE_OUT; // GCOVR_EXCL_LINE |
| 230 | } |
| 231 | |
| 232 | JPArrayView::~JPArrayView() |
| 233 | { |
| 234 | if (m_Owned) |
| 235 | delete [] (char*) m_Memory; |
| 236 | } |
| 237 | |
| 238 | void JPArrayView::reference() |
| 239 | { |
| 240 | m_RefCount++; |
| 241 | } |
| 242 | |
| 243 | bool JPArrayView::unreference() |
| 244 | { |
| 245 | m_RefCount--; |
| 246 | auto *type = dynamic_cast<JPPrimitiveType*>( m_Array->getClass()->getComponentType()); |
| 247 | if (m_RefCount == 0 && !m_Owned) |
| 248 | type->releaseView(*this); |
| 249 | return m_RefCount == 0; |
| 250 | } |