Skip to content

Commit

Permalink
Cleanup iterator instantiation code (php#17358)
Browse files Browse the repository at this point in the history
Just using object_init_ex() directly makes the code a bit simpler and
avoids unnecessary indirections.
  • Loading branch information
nielsdos authored Jan 4, 2025
1 parent b21c169 commit 7be3649
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 51 deletions.
5 changes: 3 additions & 2 deletions ext/dom/documenttype.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "dom_properties.h"
#include "internal_helpers.h"

/* {{{ name string
readonly=yes
Expand All @@ -47,7 +48,7 @@ zend_result dom_documenttype_entities_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);

php_dom_create_iterator(retval, DOM_DTD_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_dtd_namednodemap_ce(php_dom_follow_spec_intern(obj)));

xmlHashTable *entityht = (xmlHashTable *) dtdptr->entities;

Expand All @@ -68,7 +69,7 @@ zend_result dom_documenttype_notations_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlDtdPtr, dtdptr, obj);

php_dom_create_iterator(retval, DOM_DTD_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_dtd_namednodemap_ce(php_dom_follow_spec_intern(obj)));

xmlHashTable *notationht = (xmlHashTable *) dtdptr->notations;

Expand Down
24 changes: 8 additions & 16 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ PHP_METHOD(Dom_Element, removeAttributeNode)
Modern spec URL: https://dom.spec.whatwg.org/#concept-getelementsbytagname
Since:
*/
static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, bool modern)
static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *iter_ce)
{
dom_object *intern, *namednode;
zend_string *name;
Expand All @@ -818,23 +818,19 @@ static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, b

DOM_GET_THIS_INTERN(intern);

if (modern) {
php_dom_create_iterator(return_value, DOM_HTMLCOLLECTION, true);
} else {
php_dom_create_iterator(return_value, DOM_NODELIST, false);
}
object_init_ex(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
dom_namednode_iter(intern, 0, namednode, NULL, name, NULL);
}

PHP_METHOD(DOMElement, getElementsByTagName)
{
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_nodelist_class_entry);
}

PHP_METHOD(Dom_Element, getElementsByTagName)
{
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_html_collection_class_entry);
}
/* }}} end dom_element_get_elements_by_tag_name */

Expand Down Expand Up @@ -1229,7 +1225,7 @@ PHP_METHOD(Dom_Element, setAttributeNodeNS)
Modern spec URL: https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
Since: DOM Level 2
*/
static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS, bool modern)
static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *iter_ce)
{
dom_object *intern, *namednode;
zend_string *uri, *name;
Expand All @@ -1254,23 +1250,19 @@ static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS

DOM_GET_THIS_INTERN(intern);

if (modern) {
php_dom_create_iterator(return_value, DOM_HTMLCOLLECTION, true);
} else {
php_dom_create_iterator(return_value, DOM_NODELIST, false);
}
object_init_ex(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
dom_namednode_iter(intern, 0, namednode, NULL, name, uri);
}

PHP_METHOD(DOMElement, getElementsByTagNameNS)
{
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_nodelist_class_entry);
}

PHP_METHOD(Dom_Element, getElementsByTagNameNS)
{
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_html_collection_class_entry);
}
/* }}} end dom_element_get_elements_by_tag_name_ns */

Expand Down
4 changes: 2 additions & 2 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ zend_result dom_node_child_nodes_read(dom_object *obj, zval *retval)
{
DOM_PROP_NODE(xmlNodePtr, nodep, obj);

php_dom_create_iterator(retval, DOM_NODELIST, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_nodelist_ce(php_dom_follow_spec_intern(obj)));
dom_object *intern = Z_DOMOBJ_P(retval);
dom_namednode_iter(obj, XML_ELEMENT_NODE, intern, NULL, NULL, NULL);

Expand Down Expand Up @@ -415,7 +415,7 @@ zend_result dom_node_attributes_read(dom_object *obj, zval *retval)
DOM_PROP_NODE(xmlNodePtr, nodep, obj);

if (nodep->type == XML_ELEMENT_NODE) {
php_dom_create_iterator(retval, DOM_NAMEDNODEMAP, php_dom_follow_spec_intern(obj));
object_init_ex(retval, dom_get_namednodemap_ce(php_dom_follow_spec_intern(obj)));
dom_object *intern = Z_DOMOBJ_P(retval);
dom_namednode_iter(obj, XML_ATTRIBUTE_NODE, intern, NULL, NULL, NULL);
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/parentnode/css_selectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void dom_parent_node_query_selector_all(xmlNodePtr thisp, dom_object *intern, zv
zend_array_destroy(list);
RETURN_THROWS();
} else {
php_dom_create_iterator(return_value, DOM_NODELIST, true);
object_init_ex(return_value, dom_modern_nodelist_class_entry);
dom_object *ret_obj = Z_DOMOBJ_P(return_value);
dom_nnodemap_object *mapptr = (dom_nnodemap_object *) ret_obj->ptr;
ZVAL_ARR(&mapptr->baseobj_zv, list);
Expand Down
21 changes: 0 additions & 21 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1623,27 +1623,6 @@ zend_object *dom_nnodemap_objects_new(zend_class_entry *class_type)
return &intern->std;
}

void php_dom_create_iterator(zval *return_value, dom_iterator_type iterator_type, bool modern) /* {{{ */
{
zend_class_entry *ce;

if (iterator_type == DOM_NAMEDNODEMAP) {
ce = dom_get_namednodemap_ce(modern);
} else if (iterator_type == DOM_HTMLCOLLECTION) {
/* This only exists in modern DOM. */
ZEND_ASSERT(modern);
ce = dom_html_collection_class_entry;
} else if (iterator_type == DOM_DTD_NAMEDNODEMAP) {
ce = dom_get_dtd_namednodemap_ce(modern);
} else {
ZEND_ASSERT(iterator_type == DOM_NODELIST);
ce = dom_get_nodelist_ce(modern);
}

object_init_ex(return_value, ce);
}
/* }}} */

static zend_always_inline zend_class_entry *dom_get_element_ce(const xmlNode *node, bool modern)
{
if (modern) {
Expand Down
8 changes: 0 additions & 8 deletions ext/dom/php_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@ typedef struct {
dom_object dom;
} dom_object_namespace_node;

typedef enum dom_iterator_type {
DOM_NODELIST,
DOM_NAMEDNODEMAP,
DOM_DTD_NAMEDNODEMAP,
DOM_HTMLCOLLECTION,
} dom_iterator_type;

struct php_dom_libxml_ns_mapper;
typedef struct php_dom_libxml_ns_mapper php_dom_libxml_ns_mapper;

Expand Down Expand Up @@ -151,7 +144,6 @@ int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child);
bool dom_has_feature(zend_string *feature, zend_string *version);
bool dom_node_is_read_only(const xmlNode *node);
bool dom_node_children_valid(const xmlNode *node);
void php_dom_create_iterator(zval *return_value, dom_iterator_type iterator_type, bool modern);
void dom_namednode_iter(dom_object *basenode, int ntype, dom_object *intern, xmlHashTablePtr ht, zend_string *local, zend_string *ns);
xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID);
xmlNode *php_dom_libxml_hash_iter(dom_nnodemap_object *objmap, int index);
Expand Down
3 changes: 2 additions & 1 deletion ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "php_dom.h"
#include "namespace_compat.h"
#include "private_data.h"
#include "internal_helpers.h"

#define PHP_DOM_XPATH_QUERY 0
#define PHP_DOM_XPATH_EVALUATE 1
Expand Down Expand Up @@ -371,7 +372,7 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type, bool modern)
} else {
ZVAL_EMPTY_ARRAY(&retval);
}
php_dom_create_iterator(return_value, DOM_NODELIST, modern);
object_init_ex(return_value, dom_get_nodelist_ce(modern));
nodeobj = Z_DOMOBJ_P(return_value);
dom_xpath_iter(&retval, nodeobj);
break;
Expand Down

0 comments on commit 7be3649

Please sign in to comment.