Skip to content

Commit 8c063f5

Browse files
committed
Update C extension from deprecated Data API to TypedData API
The old Data_Wrap_Struct/Data_Get_Struct API has been deprecated for a long time. This updates all bindings to use TypedData_Wrap_Struct/TypedData_Get_Struct with proper rb_data_type_t definitions. Key fixes during the conversion: - Reader expand returns nodes wrapped with rxml_node_unmanaged_data_type (no mark, no free) since these transient nodes are freed on the next read - Removed stale Check_Type(*, T_DATA) guards that reject TypedData objects - Set parent type links so type hierarchy checks pass (e.g. Attr -> Node, AttrDecl -> Attr, HTMLParser::Context -> Parser::Context) - Added alloc function for HTMLParser::Context so it wraps with the correct type
1 parent 912ed4e commit 8c063f5

34 files changed

Lines changed: 533 additions & 307 deletions

ext/libxml/ruby_xml_attr.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,29 @@
3232

3333
VALUE cXMLAttr;
3434

35-
void rxml_attr_mark(xmlAttrPtr xattr)
35+
static void rxml_attr_mark(void *data)
3636
{
37+
xmlAttrPtr xattr = (xmlAttrPtr) data;
3738
/* This can happen if Ruby does a GC run after creating the
3839
new attribute but before initializing it. */
3940
if (xattr != NULL)
4041
rxml_node_mark((xmlNodePtr) xattr);
4142
}
4243

44+
const rb_data_type_t rxml_attr_type = {
45+
"LibXML::XML::Attr",
46+
{rxml_attr_mark, NULL, 0},
47+
&rxml_node_data_type, 0, RUBY_TYPED_FREE_IMMEDIATELY
48+
};
49+
4350
VALUE rxml_attr_wrap(xmlAttrPtr xattr)
4451
{
45-
return Data_Wrap_Struct(cXMLAttr, rxml_attr_mark, NULL, xattr);
52+
return TypedData_Wrap_Struct(cXMLAttr, &rxml_attr_type, xattr);
4653
}
4754

4855
static VALUE rxml_attr_alloc(VALUE klass)
4956
{
50-
return Data_Wrap_Struct(klass, rxml_attr_mark, NULL, NULL);
57+
return TypedData_Wrap_Struct(klass, &rxml_attr_type, NULL);
5158
}
5259

5360
/*
@@ -78,7 +85,7 @@ static VALUE rxml_attr_initialize(int argc, VALUE *argv, VALUE self)
7885
Check_Type(name, T_STRING);
7986
Check_Type(value, T_STRING);
8087

81-
Data_Get_Struct(node, xmlNode, xnode);
88+
TypedData_Get_Struct(node, xmlNode, &rxml_node_data_type, xnode);
8289

8390
if (xnode->type != XML_ELEMENT_NODE)
8491
rb_raise(rb_eArgError, "Attributes can only be created on element nodes.");
@@ -90,14 +97,14 @@ static VALUE rxml_attr_initialize(int argc, VALUE *argv, VALUE self)
9097
else
9198
{
9299
xmlNsPtr xns;
93-
Data_Get_Struct(ns, xmlNs, xns);
100+
TypedData_Get_Struct(ns, xmlNs, &rxml_namespace_type, xns);
94101
xattr = xmlNewNsProp(xnode, xns, (xmlChar*)StringValuePtr(name), (xmlChar*)StringValuePtr(value));
95102
}
96103

97104
if (!xattr)
98105
rb_raise(rb_eRuntimeError, "Could not create attribute.");
99106

100-
DATA_PTR( self) = xattr;
107+
RTYPEDDATA_DATA( self) = xattr;
101108
return self;
102109
}
103110

@@ -110,7 +117,7 @@ static VALUE rxml_attr_initialize(int argc, VALUE *argv, VALUE self)
110117
static VALUE rxml_attr_child_get(VALUE self)
111118
{
112119
xmlAttrPtr xattr;
113-
Data_Get_Struct(self, xmlAttr, xattr);
120+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
114121
if (xattr->children == NULL)
115122
return Qnil;
116123
else
@@ -129,7 +136,7 @@ static VALUE rxml_attr_child_get(VALUE self)
129136
static VALUE rxml_attr_doc_get(VALUE self)
130137
{
131138
xmlAttrPtr xattr;
132-
Data_Get_Struct(self, xmlAttr, xattr);
139+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
133140
if (xattr->doc == NULL)
134141
return Qnil;
135142
else
@@ -145,7 +152,7 @@ static VALUE rxml_attr_doc_get(VALUE self)
145152
static VALUE rxml_attr_last_get(VALUE self)
146153
{
147154
xmlAttrPtr xattr;
148-
Data_Get_Struct(self, xmlAttr, xattr);
155+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
149156
if (xattr->last == NULL)
150157
return Qnil;
151158
else
@@ -161,7 +168,7 @@ static VALUE rxml_attr_last_get(VALUE self)
161168
static VALUE rxml_attr_name_get(VALUE self)
162169
{
163170
xmlAttrPtr xattr;
164-
Data_Get_Struct(self, xmlAttr, xattr);
171+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
165172

166173
if (xattr->name == NULL)
167174
return Qnil;
@@ -178,7 +185,7 @@ static VALUE rxml_attr_name_get(VALUE self)
178185
static VALUE rxml_attr_next_get(VALUE self)
179186
{
180187
xmlAttrPtr xattr;
181-
Data_Get_Struct(self, xmlAttr, xattr);
188+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
182189
if (xattr->next == NULL)
183190
return Qnil;
184191
else
@@ -194,7 +201,7 @@ static VALUE rxml_attr_next_get(VALUE self)
194201
static VALUE rxml_attr_node_type(VALUE self)
195202
{
196203
xmlAttrPtr xattr;
197-
Data_Get_Struct(self, xmlAttr, xattr);
204+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
198205
return INT2NUM(xattr->type);
199206
}
200207

@@ -207,7 +214,7 @@ static VALUE rxml_attr_node_type(VALUE self)
207214
static VALUE rxml_attr_ns_get(VALUE self)
208215
{
209216
xmlAttrPtr xattr;
210-
Data_Get_Struct(self, xmlAttr, xattr);
217+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
211218
if (xattr->ns == NULL)
212219
return Qnil;
213220
else
@@ -223,7 +230,7 @@ static VALUE rxml_attr_ns_get(VALUE self)
223230
static VALUE rxml_attr_parent_get(VALUE self)
224231
{
225232
xmlAttrPtr xattr;
226-
Data_Get_Struct(self, xmlAttr, xattr);
233+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
227234
if (xattr->parent == NULL)
228235
return Qnil;
229236
else
@@ -239,7 +246,7 @@ static VALUE rxml_attr_parent_get(VALUE self)
239246
static VALUE rxml_attr_prev_get(VALUE self)
240247
{
241248
xmlAttrPtr xattr;
242-
Data_Get_Struct(self, xmlAttr, xattr);
249+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
243250
if (xattr->prev == NULL)
244251
return Qnil;
245252
else
@@ -258,12 +265,10 @@ static VALUE rxml_attr_prev_get(VALUE self)
258265
static VALUE rxml_attr_remove_ex(VALUE self)
259266
{
260267
xmlAttrPtr xattr;
261-
Data_Get_Struct(self, xmlAttr, xattr);
268+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
262269
xmlRemoveProp(xattr);
263270

264-
RDATA(self)->data = NULL;
265-
RDATA(self)->dfree = NULL;
266-
RDATA(self)->dmark = NULL;
271+
RTYPEDDATA_DATA(self) = NULL;
267272

268273
return Qnil;
269274
}
@@ -280,7 +285,7 @@ VALUE rxml_attr_value_get(VALUE self)
280285
xmlChar *value;
281286
VALUE result = Qnil;
282287

283-
Data_Get_Struct(self, xmlAttr, xattr);
288+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
284289
value = xmlNodeGetContent((xmlNodePtr)xattr);
285290

286291
if (value != NULL)
@@ -302,7 +307,7 @@ VALUE rxml_attr_value_set(VALUE self, VALUE val)
302307
xmlAttrPtr xattr;
303308

304309
Check_Type(val, T_STRING);
305-
Data_Get_Struct(self, xmlAttr, xattr);
310+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
306311

307312
if (xattr->ns)
308313
xmlSetNsProp(xattr->parent, xattr->ns, xattr->name,

ext/libxml/ruby_xml_attr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define __RXML_ATTR__
55

66
extern VALUE cXMLAttr;
7+
extern const rb_data_type_t rxml_attr_type;
78

89
void rxml_init_attr(void);
910
VALUE rxml_attr_wrap(xmlAttrPtr xattr);

ext/libxml/ruby_xml_attr_decl.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@
1313

1414
VALUE cXMLAttrDecl;
1515

16-
void rxml_attr_decl_mark(xmlAttributePtr xattr)
16+
static void rxml_attr_decl_mark(void *data)
1717
{
18-
rxml_node_mark((xmlNodePtr) xattr);
18+
xmlAttributePtr xattr = (xmlAttributePtr)data;
19+
rxml_node_mark((xmlNodePtr)xattr);
1920
}
2021

22+
static const rb_data_type_t rxml_attr_decl_type = {
23+
"LibXML::XML::AttrDecl",
24+
{rxml_attr_decl_mark, NULL, 0},
25+
&rxml_attr_type, 0, RUBY_TYPED_FREE_IMMEDIATELY
26+
};
27+
2128
VALUE rxml_attr_decl_wrap(xmlAttributePtr xattr)
2229
{
23-
return Data_Wrap_Struct(cXMLAttrDecl, rxml_attr_decl_mark, NULL, xattr);
30+
return TypedData_Wrap_Struct(cXMLAttrDecl, &rxml_attr_decl_type, xattr);
2431
}
2532

2633
/*
@@ -32,7 +39,7 @@ VALUE rxml_attr_decl_wrap(xmlAttributePtr xattr)
3239
static VALUE rxml_attr_decl_doc_get(VALUE self)
3340
{
3441
xmlAttributePtr xattr;
35-
Data_Get_Struct(self, xmlAttribute, xattr);
42+
TypedData_Get_Struct(self, xmlAttribute, &rxml_attr_decl_type, xattr);
3643
if (xattr->doc == NULL)
3744
return Qnil;
3845
else
@@ -49,7 +56,7 @@ static VALUE rxml_attr_decl_doc_get(VALUE self)
4956
static VALUE rxml_attr_decl_name_get(VALUE self)
5057
{
5158
xmlAttributePtr xattr;
52-
Data_Get_Struct(self, xmlAttribute, xattr);
59+
TypedData_Get_Struct(self, xmlAttribute, &rxml_attr_decl_type, xattr);
5360

5461
if (xattr->name == NULL)
5562
return Qnil;
@@ -66,7 +73,7 @@ static VALUE rxml_attr_decl_name_get(VALUE self)
6673
static VALUE rxml_attr_decl_next_get(VALUE self)
6774
{
6875
xmlAttributePtr xattr;
69-
Data_Get_Struct(self, xmlAttribute, xattr);
76+
TypedData_Get_Struct(self, xmlAttribute, &rxml_attr_decl_type, xattr);
7077
if (xattr->next == NULL)
7178
return Qnil;
7279
else
@@ -82,7 +89,7 @@ static VALUE rxml_attr_decl_next_get(VALUE self)
8289
static VALUE rxml_attr_decl_node_type(VALUE self)
8390
{
8491
xmlAttrPtr xattr;
85-
Data_Get_Struct(self, xmlAttr, xattr);
92+
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_decl_type, xattr);
8693
return INT2NUM(xattr->type);
8794
}
8895

@@ -96,7 +103,7 @@ static VALUE rxml_attr_decl_node_type(VALUE self)
96103
static VALUE rxml_attr_decl_parent_get(VALUE self)
97104
{
98105
xmlAttributePtr xattr;
99-
Data_Get_Struct(self, xmlAttribute, xattr);
106+
TypedData_Get_Struct(self, xmlAttribute, &rxml_attr_decl_type, xattr);
100107

101108
if (xattr->parent == NULL)
102109
return Qnil;
@@ -114,7 +121,7 @@ static VALUE rxml_attr_decl_parent_get(VALUE self)
114121
static VALUE rxml_attr_decl_prev_get(VALUE self)
115122
{
116123
xmlAttributePtr xattr;
117-
Data_Get_Struct(self, xmlAttribute, xattr);
124+
TypedData_Get_Struct(self, xmlAttribute, &rxml_attr_decl_type, xattr);
118125

119126
if (xattr->prev == NULL)
120127
return Qnil;
@@ -132,7 +139,7 @@ VALUE rxml_attr_decl_value_get(VALUE self)
132139
{
133140
xmlAttributePtr xattr;
134141

135-
Data_Get_Struct(self, xmlAttribute, xattr);
142+
TypedData_Get_Struct(self, xmlAttribute, &rxml_attr_decl_type, xattr);
136143

137144
if (xattr->defaultValue)
138145
return rxml_new_cstr(xattr->defaultValue, NULL);

ext/libxml/ruby_xml_attributes.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,26 @@
3232

3333
VALUE cXMLAttributes;
3434

35-
void rxml_attributes_mark(xmlNodePtr xnode)
35+
void rxml_attributes_mark(void *data)
3636
{
37-
rxml_node_mark(xnode);
37+
rxml_node_mark((xmlNodePtr)data);
3838
}
3939

40+
static const rb_data_type_t rxml_attributes_data_type = {
41+
.wrap_struct_name = "LibXML::XML::Attributes",
42+
.function = {
43+
.dmark = rxml_attributes_mark,
44+
.dfree = NULL,
45+
},
46+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
47+
};
48+
4049
/*
4150
* Creates a new attributes instance. Not exposed to ruby.
4251
*/
4352
VALUE rxml_attributes_new(xmlNodePtr xnode)
4453
{
45-
return Data_Wrap_Struct(cXMLAttributes, rxml_attributes_mark, NULL, xnode);
54+
return TypedData_Wrap_Struct(cXMLAttributes, &rxml_attributes_data_type, xnode);
4655
}
4756

4857
/*
@@ -56,7 +65,7 @@ VALUE rxml_attributes_new(xmlNodePtr xnode)
5665
VALUE rxml_attributes_node_get(VALUE self)
5766
{
5867
xmlNodePtr xnode;
59-
Data_Get_Struct(self, xmlNode, xnode);
68+
TypedData_Get_Struct(self, xmlNode, &rxml_attributes_data_type, xnode);
6069
return rxml_node_wrap(xnode);
6170
}
6271

@@ -80,7 +89,7 @@ static VALUE rxml_attributes_get_attribute(VALUE self, VALUE name)
8089

8190
name = rb_obj_as_string(name);
8291

83-
Data_Get_Struct(self, xmlNode, xnode);
92+
TypedData_Get_Struct(self, xmlNode, &rxml_attributes_data_type, xnode);
8493

8594
xattr = xmlHasProp(xnode, (xmlChar*) StringValuePtr(name));
8695

@@ -114,7 +123,7 @@ static VALUE rxml_attributes_get_attribute_ns(VALUE self, VALUE namespace,
114123

115124
name = rb_obj_as_string(name);
116125

117-
Data_Get_Struct(self, xmlNode, xnode);
126+
TypedData_Get_Struct(self, xmlNode, &rxml_attributes_data_type, xnode);
118127

119128
xattr = xmlHasNsProp(xnode, (xmlChar*) StringValuePtr(name),
120129
(xmlChar*) StringValuePtr(namespace));
@@ -191,7 +200,7 @@ static VALUE rxml_attributes_each(VALUE self)
191200
{
192201
xmlNodePtr xnode;
193202
xmlAttrPtr xattr;
194-
Data_Get_Struct(self, xmlNode, xnode);
203+
TypedData_Get_Struct(self, xmlNode, &rxml_attributes_data_type, xnode);
195204

196205
xattr = xnode->properties;
197206

@@ -222,7 +231,7 @@ static VALUE rxml_attributes_length(VALUE self)
222231
int length = 0;
223232
xmlNodePtr xnode;
224233
xmlAttrPtr xattr;
225-
Data_Get_Struct(self, xmlNode, xnode);
234+
TypedData_Get_Struct(self, xmlNode, &rxml_attributes_data_type, xnode);
226235

227236
xattr = xnode->properties;
228237

@@ -246,7 +255,7 @@ static VALUE rxml_attributes_length(VALUE self)
246255
static VALUE rxml_attributes_first(VALUE self)
247256
{
248257
xmlNodePtr xnode;
249-
Data_Get_Struct(self, xmlNode, xnode);
258+
TypedData_Get_Struct(self, xmlNode, &rxml_attributes_data_type, xnode);
250259

251260
if (xnode->type == XML_ELEMENT_NODE)
252261
{

0 commit comments

Comments
 (0)