Skip to content

Commit 4b8eb4a

Browse files
committed
Implement support for legacy slot type Py_tp_methods and Py_tp_members
1 parent d9232e5 commit 4b8eb4a

File tree

5 files changed

+375
-5
lines changed

5 files changed

+375
-5
lines changed

graalpython/com.oracle.graal.python.cext/hpy/hpy.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ POLYGLOT_DECLARE_TYPE(HPySlot);
6060
POLYGLOT_DECLARE_TYPE(HPyMeth);
6161
POLYGLOT_DECLARE_TYPE(HPyFunc_Signature);
6262
POLYGLOT_DECLARE_TYPE(cpy_PyMethodDef);
63+
POLYGLOT_DECLARE_TYPE(cpy_PyMemberDef);
64+
POLYGLOT_DECLARE_TYPE(cpy_PyTypeSlot);
65+
POLYGLOT_DECLARE_TYPE(cpy_PyGetSetDef);
6366
POLYGLOT_DECLARE_TYPE(HPyMember_FieldType);
6467
POLYGLOT_DECLARE_TYPE(HPyMember);
6568
POLYGLOT_DECLARE_TYPE(HPyGetSet);
@@ -158,6 +161,48 @@ HPySlot_Slot graal_hpy_slot_get_slot(HPySlot *slot) {
158161
return slot->slot;
159162
}
160163

164+
/* getters for PyType_Slot */
165+
166+
int graal_hpy_legacy_slot_get_slot(cpy_PyTypeSlot *slot) {
167+
return slot->slot;
168+
}
169+
170+
void* graal_hpy_legacy_slot_get_methods(cpy_PyTypeSlot *slot) {
171+
uint64_t len=0;
172+
cpy_PyMethodDef *legacy_methods = (cpy_PyMethodDef *) slot->pfunc;
173+
if (legacy_methods != NULL) {
174+
while ((legacy_methods[len]).ml_name != NULL) {
175+
len++;
176+
}
177+
return polyglot_from_cpy_PyMethodDef_array(legacy_methods, len);
178+
}
179+
return NULL;
180+
}
181+
182+
void* graal_hpy_legacy_slot_get_members(cpy_PyTypeSlot *slot) {
183+
uint64_t len=0;
184+
cpy_PyMemberDef *legacy_members = (cpy_PyMemberDef *) slot->pfunc;
185+
if (legacy_members != NULL) {
186+
while ((legacy_members[len]).name != NULL) {
187+
len++;
188+
}
189+
return polyglot_from_cpy_PyMemberDef_array(legacy_members, len);
190+
}
191+
return NULL;
192+
}
193+
194+
void* graal_hpy_legacy_slot_get_descrs(cpy_PyTypeSlot *slot) {
195+
uint64_t len=0;
196+
cpy_PyGetSetDef *legacy_getset = (cpy_PyGetSetDef *) slot->pfunc;
197+
if (legacy_getset != NULL) {
198+
while (legacy_getset[len].name != NULL) {
199+
len++;
200+
}
201+
return polyglot_from_cpy_PyGetSetDef_array(legacy_getset, len);
202+
}
203+
return NULL;
204+
}
205+
161206
/* getters for HPyDef */
162207

163208
int graal_hpy_def_get_kind(HPyDef *def) {
@@ -233,6 +278,18 @@ void* graal_hpy_type_spec_get_defines(HPyType_Spec *type_spec) {
233278
return NULL;
234279
}
235280

281+
void* graal_hpy_type_spec_get_legacy_slots(HPyType_Spec *type_spec) {
282+
uint64_t len=0;
283+
cpy_PyTypeSlot *legacy_slots = (cpy_PyTypeSlot *) type_spec->legacy_slots;
284+
if (legacy_slots) {
285+
while (legacy_slots[len].slot != 0) {
286+
len++;
287+
}
288+
return polyglot_from_cpy_PyTypeSlot_array(legacy_slots, len);
289+
}
290+
return NULL;
291+
}
292+
236293

237294
/*
238295
* Casts a 'wchar_t*' array to an 'int8_t*' array and also associates the proper length.

graalpython/com.oracle.graal.python.cext/include/common/cpy_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
typedef struct _object cpy_PyObject;
1212
typedef cpy_PyObject *(*cpy_PyCFunction)(cpy_PyObject *, cpy_PyObject *);
1313
typedef struct PyMethodDef cpy_PyMethodDef;
14+
typedef PyType_Slot cpy_PyTypeSlot;
15+
typedef PyGetSetDef cpy_PyGetSetDef;
16+
typedef PyMemberDef cpy_PyMemberDef;
1417

1518

1619
#endif /* HPY_UNIVERSAL_CPY_TYPES_H */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.oracle.graal.python.builtins.objects.cext.hpy;
2+
3+
import java.util.Arrays;
4+
5+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
6+
import com.oracle.truffle.api.nodes.ExplodeLoop;
7+
8+
/**
9+
* Definitions for legacy slots.
10+
*/
11+
public abstract class GraalHPyLegacyDef {
12+
public static final int MEMBER_FLAG_READONLY = 1;
13+
14+
/**
15+
* Values for field {@code slot} of structure {@code PyType_Slot}.
16+
*/
17+
enum HPyLegacySlot {
18+
Py_mp_ass_subscript(3),
19+
Py_mp_length(4),
20+
Py_mp_subscript(5),
21+
Py_nb_absolute(6),
22+
Py_nb_add(7),
23+
Py_nb_and(8),
24+
Py_nb_bool(9),
25+
Py_nb_divmod(10),
26+
Py_nb_float(11),
27+
Py_nb_floor_divide(12),
28+
Py_nb_index(13),
29+
Py_nb_inplace_add(14),
30+
Py_nb_inplace_and(15),
31+
Py_nb_inplace_floor_divide(16),
32+
Py_nb_inplace_lshift(17),
33+
Py_nb_inplace_multiply(18),
34+
Py_nb_inplace_or(19),
35+
Py_nb_inplace_power(20),
36+
Py_nb_inplace_remainder(21),
37+
Py_nb_inplace_rshift(22),
38+
Py_nb_inplace_subtract(23),
39+
Py_nb_inplace_true_divide(24),
40+
Py_nb_inplace_xor(25),
41+
Py_nb_int(26),
42+
Py_nb_invert(27),
43+
Py_nb_lshift(28),
44+
Py_nb_multiply(29),
45+
Py_nb_negative(30),
46+
Py_nb_or(31),
47+
Py_nb_positive(32),
48+
Py_nb_power(33),
49+
Py_nb_remainder(34),
50+
Py_nb_rshift(35),
51+
Py_nb_subtract(36),
52+
Py_nb_true_divide(37),
53+
Py_nb_xor(38),
54+
Py_sq_ass_item(39),
55+
Py_sq_concat(40),
56+
Py_sq_contains(41),
57+
Py_sq_inplace_concat(42),
58+
Py_sq_inplace_repeat(43),
59+
Py_sq_item(44),
60+
Py_sq_length(45),
61+
Py_sq_repeat(46),
62+
Py_tp_alloc(47),
63+
Py_tp_base(48),
64+
Py_tp_bases(49),
65+
Py_tp_call(50),
66+
Py_tp_clear(51),
67+
Py_tp_dealloc(52),
68+
Py_tp_del(53),
69+
Py_tp_descr_get(54),
70+
Py_tp_descr_set(55),
71+
Py_tp_doc(56),
72+
Py_tp_getattr(57),
73+
Py_tp_getattro(58),
74+
Py_tp_hash(59),
75+
Py_tp_init(60),
76+
Py_tp_is_gc(61),
77+
Py_tp_iter(62),
78+
Py_tp_iternext(63),
79+
Py_tp_methods(64),
80+
Py_tp_new(65),
81+
Py_tp_repr(66),
82+
Py_tp_richcompare(67),
83+
Py_tp_setattr(68),
84+
Py_tp_setattro(69),
85+
Py_tp_str(70),
86+
Py_tp_traverse(71),
87+
Py_tp_members(72),
88+
Py_tp_getset(73),
89+
Py_tp_free(74),
90+
Py_nb_matrix_multiply(75),
91+
Py_nb_inplace_matrix_multiply(76),
92+
Py_am_await(77),
93+
Py_am_aiter(78),
94+
Py_am_anext(79);
95+
96+
/** The corresponding C enum value. */
97+
private final int value;
98+
99+
HPyLegacySlot(int value) {
100+
this.value = value;
101+
}
102+
103+
int getValue() {
104+
return value;
105+
}
106+
107+
@CompilationFinal(dimensions = 1) private static final HPyLegacySlot[] VALUES = Arrays.copyOf(values(), values().length);
108+
109+
@ExplodeLoop
110+
static HPyLegacySlot fromValue(int value) {
111+
for (int i = 0; i < VALUES.length; i++) {
112+
if (VALUES[i].value == value) {
113+
return VALUES[i];
114+
}
115+
}
116+
return null;
117+
}
118+
}
119+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyNativeSymbols.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ public abstract class GraalHPyNativeSymbols {
6969
public static final String GRAAL_HPY_MODULE_GET_LEGACY_METHODS = "graal_hpy_module_get_legacy_methods";
7070
public static final String GRAAL_HPY_FROM_HPY_TYPE_SPEC = "graal_hpy_from_HPyType_Spec";
7171
public static final String GRAAL_HPY_TYPE_SPEC_GET_DEFINES = "graal_hpy_type_spec_get_defines";
72+
public static final String GRAAL_HPY_TYPE_SPEC_GET_LEGECY_SLOTS = "graal_hpy_type_spec_get_legacy_slots";
7273
public static final String GRAAL_HPY_FROM_HPY_TYPE_SPEC_PARAM_ARRAY = "graal_hpy_from_HPyType_SpecParam_array";
7374
public static final String GRAAL_HPY_TYPE_SPEC_PARAM_GET_OBJECT = "graal_hpy_HPyType_SpecParam_get_object";
7475
public static final String GRAAL_HPY_MEMBER_GET_TYPE = "graal_hpy_member_get_type";
7576
public static final String GRAAL_HPY_SLOT_GET_SLOT = "graal_hpy_slot_get_slot";
7677
public static final String GRAAL_HPY_CALLOC = "graal_hpy_calloc";
78+
public static final String GRAAL_HPY_LEGACY_SLOT_GET_SLOT = "graal_hpy_legacy_slot_get_slot";
79+
public static final String GRAAL_HPY_LEGACY_SLOT_GET_METHODS = "graal_hpy_legacy_slot_get_methods";
80+
public static final String GRAAL_HPY_LEGACY_SLOT_GET_MEMBERS = "graal_hpy_legacy_slot_get_members";
81+
public static final String GRAAL_HPY_LEGACY_SLOT_GET_DESCRS = "graal_hpy_legacy_slot_get_descrs";
7782

7883
/* C functions for reading native members by offset */
7984
public static final String GRAAL_HPY_READ_S = "graal_hpy_read_s";

0 commit comments

Comments
 (0)