Move common obj_map API functions to obj_map.c

This commit is contained in:
Niels Dossche 2025-06-21 22:32:11 +02:00
parent 37549e4563
commit 8526de84a5
14 changed files with 114 additions and 109 deletions

View file

@ -22,6 +22,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "obj_map.h"
#include "dom_properties.h"
#include "internal_helpers.h"
@ -53,7 +54,7 @@ zend_result dom_documenttype_entities_read(dom_object *obj, zval *retval)
xmlHashTable *entityht = (xmlHashTable *) dtdptr->entities;
dom_object *intern = Z_DOMOBJ_P(retval);
dom_namednode_iter(obj, intern, entityht, NULL, NULL, &php_dom_obj_map_entities);
php_dom_create_obj_map(obj, intern, entityht, NULL, NULL, &php_dom_obj_map_entities);
return SUCCESS;
}
@ -74,7 +75,7 @@ zend_result dom_documenttype_notations_read(dom_object *obj, zval *retval)
xmlHashTable *notationht = (xmlHashTable *) dtdptr->notations;
dom_object *intern = Z_DOMOBJ_P(retval);
dom_namednode_iter(obj, intern, notationht, NULL, NULL, &php_dom_obj_map_notations);
php_dom_create_obj_map(obj, intern, notationht, NULL, NULL, &php_dom_obj_map_notations);
return SUCCESS;
}

View file

@ -22,7 +22,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "dom_ce.h"
#include "obj_map.h"
typedef struct nodeIterator {
int cur;

View file

@ -23,8 +23,8 @@
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "zend_enum.h"
#include "php_dom.h"
#include "obj_map.h"
#include "namespace_compat.h"
#include "private_data.h"
#include "internal_helpers.h"
#include "dom_properties.h"
#include "token_list.h"
@ -825,7 +825,7 @@ static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, z
object_init_ex(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
dom_namednode_iter(intern, namednode, NULL, name, NULL, &php_dom_obj_map_by_tag_name);
php_dom_create_obj_map(intern, namednode, NULL, name, NULL, &php_dom_obj_map_by_tag_name);
}
PHP_METHOD(DOMElement, getElementsByTagName)
@ -1257,7 +1257,7 @@ static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS
object_init_ex(return_value, iter_ce);
namednode = Z_DOMOBJ_P(return_value);
dom_namednode_iter(intern, namednode, NULL, name, uri, &php_dom_obj_map_by_tag_name);
php_dom_create_obj_map(intern, namednode, NULL, name, uri, &php_dom_obj_map_by_tag_name);
}
PHP_METHOD(DOMElement, getElementsByTagNameNS)

View file

@ -21,6 +21,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "obj_map.h"
#include "nodelist.h"
#include "html_collection.h"
#include "namespace_compat.h"
@ -115,7 +116,7 @@ zval *dom_html_collection_read_dimension(zend_object *object, zval *offset, int
dom_html_collection_named_item_into_zval(rv, index.str, object);
} else {
ZEND_ASSERT(index.type == DOM_NODELIST_DIM_LONG);
php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
php_dom_obj_map_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
}
return rv;

View file

@ -22,6 +22,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "obj_map.h"
#include "zend_interfaces.h"
/*
@ -54,16 +55,6 @@ zend_result dom_namednodemap_length_read(dom_object *obj, zval *retval)
/* }}} */
void php_dom_named_node_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, const char *ns, zval *return_value)
{
xmlNodePtr itemnode = objmap->handler->get_named_item(objmap, named, ns);
if (itemnode) {
DOM_RET_OBJ(itemnode, objmap->baseobj);
} else {
RETURN_NULL();
}
}
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1074577549
Since:
*/
@ -76,7 +67,7 @@ PHP_METHOD(DOMNamedNodeMap, getNamedItem)
}
dom_nnodemap_object *objmap = Z_DOMOBJ_P(ZEND_THIS)->ptr;
php_dom_named_node_map_get_named_item_into_zval(objmap, named, NULL, return_value);
php_dom_obj_map_get_named_item_into_zval(objmap, named, NULL, return_value);
}
/* }}} end dom_namednodemap_get_named_item */
@ -121,7 +112,7 @@ PHP_METHOD(DOMNamedNodeMap, getNamedItemNS)
objmap = (dom_nnodemap_object *)intern->ptr;
if (objmap != NULL) {
php_dom_named_node_map_get_named_item_into_zval(objmap, named, uri, return_value);
php_dom_obj_map_get_named_item_into_zval(objmap, named, uri, return_value);
}
}
/* }}} end dom_namednodemap_get_named_item_ns */

View file

@ -22,6 +22,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "obj_map.h"
#include "namespace_compat.h"
#include "private_data.h"
#include "internal_helpers.h"
@ -288,7 +289,7 @@ zend_result dom_node_child_nodes_read(dom_object *obj, zval *retval)
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, intern, NULL, NULL, NULL, &php_dom_obj_map_child_nodes);
php_dom_create_obj_map(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_child_nodes);
return SUCCESS;
}
@ -422,7 +423,7 @@ zend_result dom_node_attributes_read(dom_object *obj, zval *retval)
if (nodep->type == XML_ELEMENT_NODE) {
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, intern, NULL, NULL, NULL, &php_dom_obj_map_attributes);
php_dom_create_obj_map(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_attributes);
} else {
ZVAL_NULL(retval);
}

View file

@ -22,6 +22,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "obj_map.h"
#include "nodelist.h"
#include "zend_interfaces.h"
@ -63,15 +64,6 @@ PHP_METHOD(DOMNodeList, count)
}
/* }}} end dom_nodelist_count */
void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
{
if (EXPECTED(objmap)) {
objmap->handler->get_item(objmap, index, return_value);
} else {
RETURN_NULL();
}
}
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
Since:
*/
@ -85,7 +77,7 @@ PHP_METHOD(DOMNodeList, item)
zval *id = ZEND_THIS;
dom_object *intern = Z_DOMOBJ_P(id);
dom_nnodemap_object *objmap = intern->ptr;
php_dom_nodelist_get_item_into_zval(objmap, index, return_value);
php_dom_obj_map_get_item_into_zval(objmap, index, return_value);
}
/* }}} end dom_nodelist_item */
@ -136,7 +128,7 @@ zval *dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int
return NULL;
}
php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
php_dom_obj_map_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
return rv;
}

View file

@ -31,8 +31,6 @@ typedef struct dom_nodelist_dimension_index {
enum dom_nodelist_dimension_index_type type;
} dom_nodelist_dimension_index;
void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
zend_long php_dom_get_nodelist_length(dom_object *obj);
dom_nodelist_dimension_index dom_modern_nodelist_get_index(const zval *offset);
zval *dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
int dom_modern_nodelist_has_dimension(zend_object *object, zval *member, int check_empty);

View file

@ -288,6 +288,67 @@ zend_long php_dom_get_nodelist_length(dom_object *obj)
return count;
}
void php_dom_create_obj_map(dom_object *basenode, dom_object *intern, xmlHashTablePtr ht, zend_string *local, zend_string *ns, const php_dom_obj_map_handler *handler)
{
dom_nnodemap_object *mapptr = intern->ptr;
ZEND_ASSERT(basenode != NULL);
ZVAL_OBJ_COPY(&mapptr->baseobj_zv, &basenode->std);
xmlDocPtr doc = basenode->document ? basenode->document->ptr : NULL;
mapptr->handler = handler;
mapptr->baseobj = basenode;
mapptr->ht = ht;
if (EXPECTED(doc != NULL)) {
mapptr->dict = doc->dict;
xmlDictReference(doc->dict);
}
const xmlChar* tmp;
if (local) {
int len = (int) ZSTR_LEN(local);
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(local), len)) != NULL) {
mapptr->local = BAD_CAST tmp;
} else {
mapptr->local = BAD_CAST ZSTR_VAL(zend_string_copy(local));
mapptr->release_local = true;
}
mapptr->local_lower = zend_string_tolower(local);
}
if (ns) {
int len = (int) ZSTR_LEN(ns);
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(ns), len)) != NULL) {
mapptr->ns = BAD_CAST tmp;
} else {
mapptr->ns = BAD_CAST ZSTR_VAL(zend_string_copy(ns));
mapptr->release_ns = true;
}
}
}
void php_dom_obj_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
{
if (EXPECTED(objmap)) {
objmap->handler->get_item(objmap, index, return_value);
} else {
RETURN_NULL();
}
}
void php_dom_obj_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, const char *ns, zval *return_value)
{
xmlNodePtr itemnode = objmap->handler->get_named_item(objmap, named, ns);
if (itemnode) {
DOM_RET_OBJ(itemnode, objmap->baseobj);
} else {
RETURN_NULL();
}
}
/**********************
* === Named item === *
**********************/

View file

@ -20,14 +20,36 @@
typedef struct dom_nnodemap_object dom_nnodemap_object;
typedef struct php_dom_obj_map_handler {
zend_long (*length)(dom_nnodemap_object *);
void (*get_item)(dom_nnodemap_object *, zend_long, zval *);
xmlNodePtr (*get_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
bool (*has_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
bool use_cache;
bool nameless;
zend_long (*length)(dom_nnodemap_object *);
void (*get_item)(dom_nnodemap_object *, zend_long, zval *);
xmlNodePtr (*get_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
bool (*has_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
bool use_cache;
bool nameless;
} php_dom_obj_map_handler;
typedef struct dom_nnodemap_object {
dom_object *baseobj;
zval baseobj_zv;
int cached_length;
xmlHashTable *ht;
xmlChar *local;
zend_string *local_lower;
xmlChar *ns;
php_libxml_cache_tag cache_tag;
dom_object *cached_obj;
zend_long cached_obj_index;
xmlDictPtr dict;
const php_dom_obj_map_handler *handler;
bool release_local : 1;
bool release_ns : 1;
} dom_nnodemap_object;
void php_dom_create_obj_map(dom_object *basenode, dom_object *intern, xmlHashTablePtr ht, zend_string *local, zend_string *ns, const php_dom_obj_map_handler *handler);
void php_dom_obj_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, const char *ns, zval *return_value);
void php_dom_obj_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
zend_long php_dom_get_nodelist_length(dom_object *obj);
extern const php_dom_obj_map_handler php_dom_obj_map_attributes;
extern const php_dom_obj_map_handler php_dom_obj_map_by_tag_name;
extern const php_dom_obj_map_handler php_dom_obj_map_child_nodes;

View file

@ -21,6 +21,7 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "../php_dom.h"
#include "../obj_map.h"
#include "ext/lexbor/lexbor/css/parser.h"
#include "../lexbor/selectors-adapted/selectors.h"

View file

@ -24,6 +24,7 @@
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "zend_enum.h"
#include "php_dom.h"
#include "obj_map.h"
#include "nodelist.h"
#include "html_collection.h"
#include "namespace_compat.h"
@ -1456,50 +1457,6 @@ void dom_objects_free_storage(zend_object *object)
}
/* }}} */
/* TODO: move me to obj_map.c */
void dom_namednode_iter(dom_object *basenode, dom_object *intern, xmlHashTablePtr ht, zend_string *local, zend_string *ns, const php_dom_obj_map_handler *handler) /* {{{ */
{
dom_nnodemap_object *mapptr = (dom_nnodemap_object *) intern->ptr;
ZEND_ASSERT(basenode != NULL);
ZVAL_OBJ_COPY(&mapptr->baseobj_zv, &basenode->std);
xmlDocPtr doc = basenode->document ? basenode->document->ptr : NULL;
mapptr->handler = handler;
mapptr->baseobj = basenode;
mapptr->ht = ht;
if (EXPECTED(doc != NULL)) {
mapptr->dict = doc->dict;
xmlDictReference(doc->dict);
}
const xmlChar* tmp;
if (local) {
int len = (int) ZSTR_LEN(local);
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(local), len)) != NULL) {
mapptr->local = BAD_CAST tmp;
} else {
mapptr->local = BAD_CAST ZSTR_VAL(zend_string_copy(local));
mapptr->release_local = true;
}
mapptr->local_lower = zend_string_tolower(local);
}
if (ns) {
int len = (int) ZSTR_LEN(ns);
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(ns), len)) != NULL) {
mapptr->ns = BAD_CAST tmp;
} else {
mapptr->ns = BAD_CAST ZSTR_VAL(zend_string_copy(ns));
mapptr->release_ns = true;
}
}
}
/* }}} */
static void dom_objects_set_class_ex(zend_class_entry *class_type, dom_object *intern)
{
zend_class_entry *base_class = class_type;
@ -2294,7 +2251,7 @@ static zval *dom_nodelist_read_dimension(zend_object *object, zval *offset, int
return rv;
}
php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, lval, rv);
php_dom_obj_map_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, lval, rv);
return rv;
}
@ -2382,7 +2339,7 @@ static zval *dom_nodemap_read_dimension(zend_object *object, zval *offset, int t
zend_long lval;
if (dom_nodemap_or_nodelist_process_offset_as_named(offset, &lval)) {
/* exceptional case, switch to named lookup */
php_dom_named_node_map_get_named_item_into_zval(php_dom_obj_from_obj(object)->ptr, Z_STR_P(offset), NULL, rv);
php_dom_obj_map_get_named_item_into_zval(php_dom_obj_from_obj(object)->ptr, Z_STR_P(offset), NULL, rv);
return rv;
}
@ -2429,7 +2386,7 @@ static zval *dom_modern_nodemap_read_dimension(zend_object *object, zval *offset
if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), lval)) {
map->handler->get_item(map, (zend_long) lval, rv);
} else {
php_dom_named_node_map_get_named_item_into_zval(map, Z_STR_P(offset), NULL, rv);
php_dom_obj_map_get_named_item_into_zval(map, Z_STR_P(offset), NULL, rv);
}
} else if (Z_TYPE_P(offset) == IS_LONG) {
map->handler->get_item(map, Z_LVAL_P(offset), rv);

View file

@ -56,7 +56,7 @@ extern zend_module_entry dom_module_entry;
#include "xpath_callbacks.h"
#include "zend_exceptions.h"
#include "dom_ce.h"
#include "obj_map.h"
/* DOM API_VERSION, please bump it up, if you change anything in the API
therefore it's easier for the script-programmers to check, what's working how
Can be checked with phpversion("dom");
@ -76,23 +76,6 @@ static inline dom_xpath_object *php_xpath_obj_from_obj(zend_object *obj) {
#define Z_XPATHOBJ_P(zv) php_xpath_obj_from_obj(Z_OBJ_P((zv)))
typedef struct dom_nnodemap_object {
dom_object *baseobj;
zval baseobj_zv;
int cached_length;
xmlHashTable *ht;
xmlChar *local;
zend_string *local_lower;
xmlChar *ns;
php_libxml_cache_tag cache_tag;
dom_object *cached_obj;
zend_long cached_obj_index;
xmlDictPtr dict;
const php_dom_obj_map_handler *handler;
bool release_local : 1;
bool release_ns : 1;
} dom_nnodemap_object;
typedef struct {
zend_object_iterator intern;
zval curobj;
@ -145,7 +128,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 dom_namednode_iter(dom_object *basenode, dom_object *intern, xmlHashTablePtr ht, zend_string *local, zend_string *ns, const php_dom_obj_map_handler *handler);
xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID);
xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, int index);
zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
@ -210,8 +192,6 @@ void dom_element_closest(xmlNodePtr thisp, dom_object *intern, zval *return_valu
xmlNodePtr dom_parse_fragment(dom_object *obj, xmlNodePtr context_node, const zend_string *input);
/* nodemap and nodelist APIs */
void php_dom_named_node_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, const char *ns, zval *return_value);
xmlNodePtr php_dom_named_node_map_get_item(dom_nnodemap_object *objmap, zend_long index);
zend_long php_dom_get_namednodemap_length(dom_object *obj);
xmlNodePtr dom_nodelist_iter_start_first_child(xmlNodePtr nodep);

View file

@ -22,8 +22,8 @@
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "obj_map.h"
#include "namespace_compat.h"
#include "private_data.h"
#include "internal_helpers.h"
#define PHP_DOM_XPATH_QUERY 0