Skip to content

Commit 4af0dfa

Browse files
committed
Fix: force-inline of 'native_to_java' did not work.
1 parent 8bd62c1 commit 4af0dfa

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ uint64_t (*PY_TRUFFLE_CEXT_LANDING_L)(void* name, ...);
5353
double (*PY_TRUFFLE_CEXT_LANDING_D)(void* name, ...);
5454
void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
5555

56-
typedef void* (*cache_t)(uint64_t);
57-
static cache_t cache;
58-
59-
#define resolve_handle(__cache__, __addr__) (__cache__)(__addr__)
56+
cache_t cache;
6057

6158
__attribute__((constructor (__COUNTER__)))
6259
static void initialize_upcall_functions() {
@@ -198,28 +195,6 @@ static void initialize_capi() {
198195
initialize_bufferprocs();
199196
}
200197

201-
// Heuristic to test if some value is a pointer object
202-
// TODO we need a reliable solution for that
203-
#define IS_POINTER(__val__) (polyglot_is_value(__val__) && !polyglot_fits_in_i64(__val__))
204-
205-
MUST_INLINE
206-
void* native_to_java(PyObject* obj) {
207-
if (obj == NULL) {
208-
return Py_NoValue;
209-
} else if (obj == Py_None) {
210-
return Py_None;
211-
} else if (polyglot_is_string(obj)) {
212-
return obj;
213-
} else if (truffle_is_handle_to_managed(obj)) {
214-
return resolve_handle(cache, (uint64_t)obj);
215-
} else if (truffle_is_handle_to_managed(obj->ob_refcnt)) {
216-
return truffle_managed_from_handle(obj->ob_refcnt);
217-
} else if (IS_POINTER(obj->ob_refcnt)) {
218-
return obj->ob_refcnt;
219-
}
220-
return obj;
221-
}
222-
223198
void* native_to_java_exported(PyObject* obj) {
224199
return native_to_java(obj);
225200
}
@@ -273,15 +248,15 @@ uint64_t PyTruffle_Wchar_Size() {
273248
}
274249

275250
void* PyObjectHandle_ForJavaObject(void* cobj, unsigned long flags) {
276-
if (!truffle_is_handle_to_managed(cobj)) {
251+
if (truffle_is_handle_to_managed(cobj)) {
277252
return truffle_deref_handle_for_managed(cobj);
278253
}
279254
return cobj;
280255
}
281256

282257
/** to be used from Java code only; only creates the deref handle */
283258
void* PyObjectHandle_ForJavaType(void* ptype) {
284-
if (!truffle_is_handle_to_managed(ptype)) {
259+
if (truffle_is_handle_to_managed(ptype)) {
285260
return truffle_deref_handle_for_managed(ptype);
286261
}
287262
return ptype;

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,36 @@ extern void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
149149
#define as_double(obj) polyglot_as_double(polyglot_invoke(PY_TRUFFLE_CEXT, "to_double", to_java(obj)))
150150
#define as_float(obj) ((float)as_double(obj))
151151

152+
typedef void* (*cache_t)(uint64_t);
153+
extern cache_t cache;
154+
155+
// Heuristic to test if some value is a pointer object
156+
// TODO we need a reliable solution for that
157+
#define IS_POINTER(__val__) (polyglot_is_value(__val__) && !polyglot_fits_in_i64(__val__))
158+
159+
#define resolve_handle(__cache__, __addr__) (__cache__)(__addr__)
160+
161+
__attribute__((always_inline))
162+
inline void* native_to_java(PyObject* obj) {
163+
if (obj == NULL) {
164+
return Py_NoValue;
165+
} else if (obj == Py_None) {
166+
return Py_None;
167+
} else if (polyglot_is_string(obj)) {
168+
return obj;
169+
} else if (truffle_is_handle_to_managed(obj)) {
170+
return resolve_handle(cache, (uint64_t)obj);
171+
} else {
172+
void* refcnt = obj->ob_refcnt;
173+
if (truffle_is_handle_to_managed(refcnt)) {
174+
return resolve_handle(cache, refcnt);
175+
} else if (IS_POINTER(refcnt)) {
176+
return refcnt;
177+
}
178+
return obj;
179+
}
180+
}
152181

153-
void* native_to_java(PyObject* obj);
154182
extern void* to_java(PyObject* obj);
155183
extern void* to_java_type(PyTypeObject* cls);
156184
extern PyObject* to_sulong(void *o);

0 commit comments

Comments
 (0)