Debian Codegraph @

native/common/jp_chartype.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_chartype.h"
21 #include "jp_boxedtype.h"
22
23 JPCharType::JPCharType()
24 : JPPrimitiveType("char")
25 {
26 }
27
28 JPCharType::~JPCharType()
29 = default;
30
31 JPClass* JPCharType::getBoxedClass(JPJavaFrame& frame) const
32 {
33 return frame.getContext()->_java_lang_Character;
34 }
35
36 JPValue JPCharType::newInstance(JPJavaFrame& frame, JPPyObjectVector& args)
37 {
38 // This is only callable from one location so error checking is minimal
39 if (args.size() != 1 || !PyIndex_Check(args[0]))
40 JP_RAISE(PyExc_TypeError, "bad args"); // GCOVR_EXCL_LINE
41 jvalue jv;
42
43 // This is a cast so we must not fail
44 int overflow;
45 jv.c = PyLong_AsLongAndOverflow(args[0], &overflow);
46 return JPValue(this, jv);
47 }
48
49 JPPyObject JPCharType::convertToPythonObject(JPJavaFrame& frame, jvalue val, bool cast)
50 {
51 // if (!cast)
52 // {
53 JPPyObject out = JPPyObject::call(PyJPChar_Create((PyTypeObject*) _JChar, val.c));
54 PyJPValue_assignJavaSlot(frame, out.get(), JPValue(this, val));
55 return out;
56 // }
57 // JPPyObject tmp = JPPyObject::call(PyLong_FromLong(field(val)));
58 // JPPyObject out = JPPyObject::call(convertLong(getHost(), (PyLongObject*) tmp.get()));
59 // return out;
60 }
61
62 JPValue JPCharType::getValueFromObject(JPJavaFrame& frame, const JPValue& obj)
63 {
64 jvalue v;
65 field(v) = frame.CallCharMethodA(obj.getValue().l, frame.getContext()->_java_lang_Character->m_CharValueID, nullptr);
66 return JPValue(this, v);
67 }
68
69 class JPConversionAsChar : public JPConversion
70 {
71 using base_t = JPCharType;
72 public:
73
74 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
75 {
76 JP_TRACE_IN("JPConversionAsChar::matches");
77 if (!JPPyString::checkCharUTF16(match.object))
78 return match.type = JPMatch::_none;
79 match.conversion = this;
80 return match.type = JPMatch::_implicit;
81 JP_TRACE_OUT; // GCOVR_EXCL_LINE
82 }
83
84 void getInfo(JPClass *cls, JPConversionInfo &info) override
85 {
86 PyList_Append(info.implicit, (PyObject*) & PyUnicode_Type);
87 }
88
89 jvalue convert(JPMatch &match) override
90 {
91 jvalue res;
92 res.c = JPPyString::asCharUTF16(match.object);
93 return res;
94 }
95 } asCharConversion;
96
97 class JPConversionAsJChar : public JPConversionJavaValue
98 {
99 public:
100
101 JPMatch::Type matches(JPClass *cls, JPMatch &match) override
102 {
103 JPValue *value = match.getJavaSlot();
104 if (value == nullptr)
105 return match.type = JPMatch::_none;
106 match.type = JPMatch::_none;
107
108 // Exact
109 // Implied conversion from boxed to primitive (JLS 5.1.8)
110 if (javaValueConversion->matches(cls, match)
111 || unboxConversion->matches(cls, match))
112 return match.type;
113
114 // Unboxing must be to the from the exact boxed type (JLS 5.1.8)
115 return JPMatch::_implicit; // stop the search
116 }
117
118 void getInfo(JPClass *cls, JPConversionInfo &info) override
119 {
120 JPContext *context = JPContext_global;
121 PyList_Append(info.exact, (PyObject*) context->_char->getHost());
122 unboxConversion->getInfo(cls, info);
123 }
124
125 } asJCharConversion;
126
127 JPMatch::Type JPCharType::findJavaConversion(JPMatch &match)
128 {
129 JP_TRACE_IN("JPCharType::findJavaConversion");
130
131 if (match.object == Py_None)
132 return match.type = JPMatch::_none;
133
134 if (asJCharConversion.matches(this, match)
135 || asCharConversion.matches(this, match))
136 return match.type;
137
138 return match.type = JPMatch::_none;
139 JP_TRACE_OUT;
140 }
141
142 void JPCharType::getConversionInfo(JPConversionInfo &info)
143 {
144 JPJavaFrame frame = JPJavaFrame::outer();
145 asJCharConversion.getInfo(this, info);
146 asCharConversion.getInfo(this, info);
147 PyList_Append(info.ret, (PyObject*) JPContext_global->_char->getHost());
148 }
149
150 jarray JPCharType::newArrayOf(JPJavaFrame& frame, jsize sz)
151 {
152 return frame.NewCharArray(sz);
153 }
154
155 JPPyObject JPCharType::getStaticField(JPJavaFrame& frame, jclass c, jfieldID fid)
156 {
157 jvalue v;
158 field(v) = frame.GetStaticCharField(c, fid);
159 return convertToPythonObject(frame, v, false);
160 }
161
162 JPPyObject JPCharType::getField(JPJavaFrame& frame, jobject c, jfieldID fid)
163 {
164 jvalue v;
165 field(v) = frame.GetCharField(c, fid);
166 return convertToPythonObject(frame, v, false);
167 }
168
169 JPPyObject JPCharType::invokeStatic(JPJavaFrame& frame, jclass claz, jmethodID mth, jvalue* val)
170 {
171 jvalue v;
172 {
173 JPPyCallRelease call;
174 field(v) = frame.CallStaticCharMethodA(claz, mth, val);
175 }
176 return convertToPythonObject(frame, v, false);
177 }
178
179 JPPyObject JPCharType::invoke(JPJavaFrame& frame, jobject obj, jclass clazz, jmethodID mth, jvalue* val)
180 {
181 jvalue v;
182 {
183 JPPyCallRelease call;
184 if (clazz == nullptr)
185 field(v) = frame.CallCharMethodA(obj, mth, val);
186 else
187 field(v) = frame.CallNonvirtualCharMethodA(obj, clazz, mth, val);
188 }
189 return convertToPythonObject(frame, v, false);
190 }
191
192 void JPCharType::setStaticField(JPJavaFrame& frame, jclass c, jfieldID fid, PyObject* obj)
193 {
194 JPMatch match(&frame, obj);
195 if (findJavaConversion(match) < JPMatch::_implicit)
196 JP_RAISE(PyExc_TypeError, "Unable to convert to Java char");
197 type_t val = field(match.convert());
198 frame.SetStaticCharField(c, fid, val);
199 }
200
201 void JPCharType::setField(JPJavaFrame& frame, jobject c, jfieldID fid, PyObject* obj)
202 {
203 JPMatch match(&frame, obj);
204 if (findJavaConversion(match) < JPMatch::_implicit)
205 JP_RAISE(PyExc_TypeError, "Unable to convert to Java char");
206 type_t val = field(match.convert());
207 frame.SetCharField(c, fid, val);
208 }
209
210 void JPCharType::setArrayRange(JPJavaFrame& frame, jarray a,
211 jsize start, jsize length, jsize step,
212 PyObject* sequence)
213 {
214 JP_TRACE_IN("JPCharType::setArrayRange");
215
216 JPPrimitiveArrayAccessor<array_t, type_t*> accessor(frame, a,
217 &JPJavaFrame::GetCharArrayElements, &JPJavaFrame::ReleaseCharArrayElements);
218
219 type_t* val = accessor.get();
220 JPPySequence seq = JPPySequence::use(sequence);
221 jsize index = start;
222 for (Py_ssize_t i = 0; i < length; ++i, index += step)
223 {
224 jchar v = JPPyString::asCharUTF16(seq[i].get());
225 JP_PY_CHECK();
226 val[index] = (type_t) v;
227 }
228 accessor.commit();
229 JP_TRACE_OUT;
230 }
231
232 JPPyObject JPCharType::getArrayItem(JPJavaFrame& frame, jarray a, jsize ndx)
233 {
234 auto array = (array_t) a;
235 type_t val;
236 frame.GetCharArrayRegion(array, ndx, 1, &val);
237 jvalue v;
238 field(v) = val;
239 return convertToPythonObject(frame, v, false);
240 }
241
242 void JPCharType::setArrayItem(JPJavaFrame& frame, jarray a, jsize ndx, PyObject* obj)
243 {
244 JPMatch match(&frame, obj);
245 if (findJavaConversion(match) < JPMatch::_implicit)
246 JP_RAISE(PyExc_TypeError, "Unable to convert to Java char");
247 type_t val = field(match.convert());
248 frame.SetCharArrayRegion((array_t) a, ndx, 1, &val);
249 }
250
251 void JPCharType::getView(JPArrayView& view)
252 {
253 JPJavaFrame frame = JPJavaFrame::outer();
254 view.m_Memory = (void*) frame.GetCharArrayElements(
255 (jcharArray) view.m_Array->getJava(), &view.m_IsCopy);
256 view.m_Buffer.format = "H";
257 view.m_Buffer.itemsize = sizeof (jchar);
258 }
259
260 void JPCharType::releaseView(JPArrayView& view)
261 {
262 try
263 {
264 JPJavaFrame frame = JPJavaFrame::outer();
265 frame.ReleaseCharArrayElements((jcharArray) view.m_Array->getJava(),
266 (jchar*) view.m_Memory, view.m_Buffer.readonly ? JNI_ABORT : 0);
267 } catch (JPypeException&)
268 {
269 // This is called as part of the cleanup routine and exceptions
270 // are not permitted
271 }
272 }
273
274 const char* JPCharType::getBufferFormat()
275 {
276 return "H";
277 }
278
279 Py_ssize_t JPCharType::getItemSize()
280 {
281 return sizeof (jchar);
282 }
283
284 void JPCharType::copyElements(JPJavaFrame &frame, jarray a, jsize start, jsize len,
285 void* memory, int offset)
286 {
287 auto* b = (jchar*) ((char*) memory + offset);
288 frame.GetCharArrayRegion((jcharArray) a, start, len, b);
289 }
290
291 static void pack(jchar* d, jvalue v)
292 {
293 *d = v.c;
294 }
295
296 PyObject *JPCharType::newMultiArray(JPJavaFrame &frame, JPPyBuffer &buffer, int subs, int base, jobject dims)
297 {
298 JP_TRACE_IN("JPCharType::newMultiArray");
299 return convertMultiArray<type_t>(
300 frame, this, &pack, "c",
301 buffer, subs, base, dims);
302 JP_TRACE_OUT;
303 }