Skip to content

Commit 8526de8

Browse files
committed
Move common obj_map API functions to obj_map.c
1 parent 37549e4 commit 8526de8

File tree

14 files changed

+114
-109
lines changed

14 files changed

+114
-109
lines changed

ext/dom/documenttype.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "php.h"
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
25+
#include "obj_map.h"
2526
#include "dom_properties.h"
2627
#include "internal_helpers.h"
2728

@@ -53,7 +54,7 @@ zend_result dom_documenttype_entities_read(dom_object *obj, zval *retval)
5354
xmlHashTable *entityht = (xmlHashTable *) dtdptr->entities;
5455

5556
dom_object *intern = Z_DOMOBJ_P(retval);
56-
dom_namednode_iter(obj, intern, entityht, NULL, NULL, &php_dom_obj_map_entities);
57+
php_dom_create_obj_map(obj, intern, entityht, NULL, NULL, &php_dom_obj_map_entities);
5758

5859
return SUCCESS;
5960
}
@@ -74,7 +75,7 @@ zend_result dom_documenttype_notations_read(dom_object *obj, zval *retval)
7475
xmlHashTable *notationht = (xmlHashTable *) dtdptr->notations;
7576

7677
dom_object *intern = Z_DOMOBJ_P(retval);
77-
dom_namednode_iter(obj, intern, notationht, NULL, NULL, &php_dom_obj_map_notations);
78+
php_dom_create_obj_map(obj, intern, notationht, NULL, NULL, &php_dom_obj_map_notations);
7879

7980
return SUCCESS;
8081
}

ext/dom/dom_iterators.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "php.h"
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
25-
#include "dom_ce.h"
25+
#include "obj_map.h"
2626

2727
typedef struct nodeIterator {
2828
int cur;

ext/dom/element.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "zend_enum.h"
2525
#include "php_dom.h"
26+
#include "obj_map.h"
2627
#include "namespace_compat.h"
27-
#include "private_data.h"
2828
#include "internal_helpers.h"
2929
#include "dom_properties.h"
3030
#include "token_list.h"
@@ -825,7 +825,7 @@ static void dom_element_get_elements_by_tag_name(INTERNAL_FUNCTION_PARAMETERS, z
825825

826826
object_init_ex(return_value, iter_ce);
827827
namednode = Z_DOMOBJ_P(return_value);
828-
dom_namednode_iter(intern, namednode, NULL, name, NULL, &php_dom_obj_map_by_tag_name);
828+
php_dom_create_obj_map(intern, namednode, NULL, name, NULL, &php_dom_obj_map_by_tag_name);
829829
}
830830

831831
PHP_METHOD(DOMElement, getElementsByTagName)
@@ -1257,7 +1257,7 @@ static void dom_element_get_elements_by_tag_name_ns(INTERNAL_FUNCTION_PARAMETERS
12571257

12581258
object_init_ex(return_value, iter_ce);
12591259
namednode = Z_DOMOBJ_P(return_value);
1260-
dom_namednode_iter(intern, namednode, NULL, name, uri, &php_dom_obj_map_by_tag_name);
1260+
php_dom_create_obj_map(intern, namednode, NULL, name, uri, &php_dom_obj_map_by_tag_name);
12611261
}
12621262

12631263
PHP_METHOD(DOMElement, getElementsByTagNameNS)

ext/dom/html_collection.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "php.h"
2222
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2323
#include "php_dom.h"
24+
#include "obj_map.h"
2425
#include "nodelist.h"
2526
#include "html_collection.h"
2627
#include "namespace_compat.h"
@@ -115,7 +116,7 @@ zval *dom_html_collection_read_dimension(zend_object *object, zval *offset, int
115116
dom_html_collection_named_item_into_zval(rv, index.str, object);
116117
} else {
117118
ZEND_ASSERT(index.type == DOM_NODELIST_DIM_LONG);
118-
php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
119+
php_dom_obj_map_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
119120
}
120121

121122
return rv;

ext/dom/namednodemap.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "php.h"
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
25+
#include "obj_map.h"
2526
#include "zend_interfaces.h"
2627

2728
/*
@@ -54,16 +55,6 @@ zend_result dom_namednodemap_length_read(dom_object *obj, zval *retval)
5455

5556
/* }}} */
5657

57-
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)
58-
{
59-
xmlNodePtr itemnode = objmap->handler->get_named_item(objmap, named, ns);
60-
if (itemnode) {
61-
DOM_RET_OBJ(itemnode, objmap->baseobj);
62-
} else {
63-
RETURN_NULL();
64-
}
65-
}
66-
6758
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1074577549
6859
Since:
6960
*/
@@ -76,7 +67,7 @@ PHP_METHOD(DOMNamedNodeMap, getNamedItem)
7667
}
7768

7869
dom_nnodemap_object *objmap = Z_DOMOBJ_P(ZEND_THIS)->ptr;
79-
php_dom_named_node_map_get_named_item_into_zval(objmap, named, NULL, return_value);
70+
php_dom_obj_map_get_named_item_into_zval(objmap, named, NULL, return_value);
8071
}
8172
/* }}} end dom_namednodemap_get_named_item */
8273

@@ -121,7 +112,7 @@ PHP_METHOD(DOMNamedNodeMap, getNamedItemNS)
121112
objmap = (dom_nnodemap_object *)intern->ptr;
122113

123114
if (objmap != NULL) {
124-
php_dom_named_node_map_get_named_item_into_zval(objmap, named, uri, return_value);
115+
php_dom_obj_map_get_named_item_into_zval(objmap, named, uri, return_value);
125116
}
126117
}
127118
/* }}} end dom_namednodemap_get_named_item_ns */

ext/dom/node.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "php.h"
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
25+
#include "obj_map.h"
2526
#include "namespace_compat.h"
2627
#include "private_data.h"
2728
#include "internal_helpers.h"
@@ -288,7 +289,7 @@ zend_result dom_node_child_nodes_read(dom_object *obj, zval *retval)
288289

289290
object_init_ex(retval, dom_get_nodelist_ce(php_dom_follow_spec_intern(obj)));
290291
dom_object *intern = Z_DOMOBJ_P(retval);
291-
dom_namednode_iter(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_child_nodes);
292+
php_dom_create_obj_map(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_child_nodes);
292293

293294
return SUCCESS;
294295
}
@@ -422,7 +423,7 @@ zend_result dom_node_attributes_read(dom_object *obj, zval *retval)
422423
if (nodep->type == XML_ELEMENT_NODE) {
423424
object_init_ex(retval, dom_get_namednodemap_ce(php_dom_follow_spec_intern(obj)));
424425
dom_object *intern = Z_DOMOBJ_P(retval);
425-
dom_namednode_iter(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_attributes);
426+
php_dom_create_obj_map(obj, intern, NULL, NULL, NULL, &php_dom_obj_map_attributes);
426427
} else {
427428
ZVAL_NULL(retval);
428429
}

ext/dom/nodelist.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "php.h"
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
25+
#include "obj_map.h"
2526
#include "nodelist.h"
2627
#include "zend_interfaces.h"
2728

@@ -63,15 +64,6 @@ PHP_METHOD(DOMNodeList, count)
6364
}
6465
/* }}} end dom_nodelist_count */
6566

66-
void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
67-
{
68-
if (EXPECTED(objmap)) {
69-
objmap->handler->get_item(objmap, index, return_value);
70-
} else {
71-
RETURN_NULL();
72-
}
73-
}
74-
7567
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
7668
Since:
7769
*/
@@ -85,7 +77,7 @@ PHP_METHOD(DOMNodeList, item)
8577
zval *id = ZEND_THIS;
8678
dom_object *intern = Z_DOMOBJ_P(id);
8779
dom_nnodemap_object *objmap = intern->ptr;
88-
php_dom_nodelist_get_item_into_zval(objmap, index, return_value);
80+
php_dom_obj_map_get_item_into_zval(objmap, index, return_value);
8981
}
9082
/* }}} end dom_nodelist_item */
9183

@@ -136,7 +128,7 @@ zval *dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int
136128
return NULL;
137129
}
138130

139-
php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
131+
php_dom_obj_map_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
140132
return rv;
141133
}
142134

ext/dom/nodelist.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ typedef struct dom_nodelist_dimension_index {
3131
enum dom_nodelist_dimension_index_type type;
3232
} dom_nodelist_dimension_index;
3333

34-
void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
35-
zend_long php_dom_get_nodelist_length(dom_object *obj);
3634
dom_nodelist_dimension_index dom_modern_nodelist_get_index(const zval *offset);
3735
zval *dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv);
3836
int dom_modern_nodelist_has_dimension(zend_object *object, zval *member, int check_empty);

ext/dom/obj_map.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,67 @@ zend_long php_dom_get_nodelist_length(dom_object *obj)
288288
return count;
289289
}
290290

291+
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)
292+
{
293+
dom_nnodemap_object *mapptr = intern->ptr;
294+
295+
ZEND_ASSERT(basenode != NULL);
296+
297+
ZVAL_OBJ_COPY(&mapptr->baseobj_zv, &basenode->std);
298+
299+
xmlDocPtr doc = basenode->document ? basenode->document->ptr : NULL;
300+
301+
mapptr->handler = handler;
302+
mapptr->baseobj = basenode;
303+
mapptr->ht = ht;
304+
if (EXPECTED(doc != NULL)) {
305+
mapptr->dict = doc->dict;
306+
xmlDictReference(doc->dict);
307+
}
308+
309+
const xmlChar* tmp;
310+
311+
if (local) {
312+
int len = (int) ZSTR_LEN(local);
313+
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(local), len)) != NULL) {
314+
mapptr->local = BAD_CAST tmp;
315+
} else {
316+
mapptr->local = BAD_CAST ZSTR_VAL(zend_string_copy(local));
317+
mapptr->release_local = true;
318+
}
319+
mapptr->local_lower = zend_string_tolower(local);
320+
}
321+
322+
if (ns) {
323+
int len = (int) ZSTR_LEN(ns);
324+
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(ns), len)) != NULL) {
325+
mapptr->ns = BAD_CAST tmp;
326+
} else {
327+
mapptr->ns = BAD_CAST ZSTR_VAL(zend_string_copy(ns));
328+
mapptr->release_ns = true;
329+
}
330+
}
331+
}
332+
333+
void php_dom_obj_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
334+
{
335+
if (EXPECTED(objmap)) {
336+
objmap->handler->get_item(objmap, index, return_value);
337+
} else {
338+
RETURN_NULL();
339+
}
340+
}
341+
342+
void php_dom_obj_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, const char *ns, zval *return_value)
343+
{
344+
xmlNodePtr itemnode = objmap->handler->get_named_item(objmap, named, ns);
345+
if (itemnode) {
346+
DOM_RET_OBJ(itemnode, objmap->baseobj);
347+
} else {
348+
RETURN_NULL();
349+
}
350+
}
351+
291352
/**********************
292353
* === Named item === *
293354
**********************/

ext/dom/obj_map.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,36 @@
2020
typedef struct dom_nnodemap_object dom_nnodemap_object;
2121

2222
typedef struct php_dom_obj_map_handler {
23-
zend_long (*length)(dom_nnodemap_object *);
24-
void (*get_item)(dom_nnodemap_object *, zend_long, zval *);
25-
xmlNodePtr (*get_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
26-
bool (*has_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
27-
bool use_cache;
28-
bool nameless;
23+
zend_long (*length)(dom_nnodemap_object *);
24+
void (*get_item)(dom_nnodemap_object *, zend_long, zval *);
25+
xmlNodePtr (*get_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
26+
bool (*has_named_item)(dom_nnodemap_object *, const zend_string *, const char *);
27+
bool use_cache;
28+
bool nameless;
2929
} php_dom_obj_map_handler;
3030

31+
typedef struct dom_nnodemap_object {
32+
dom_object *baseobj;
33+
zval baseobj_zv;
34+
int cached_length;
35+
xmlHashTable *ht;
36+
xmlChar *local;
37+
zend_string *local_lower;
38+
xmlChar *ns;
39+
php_libxml_cache_tag cache_tag;
40+
dom_object *cached_obj;
41+
zend_long cached_obj_index;
42+
xmlDictPtr dict;
43+
const php_dom_obj_map_handler *handler;
44+
bool release_local : 1;
45+
bool release_ns : 1;
46+
} dom_nnodemap_object;
47+
48+
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);
49+
void php_dom_obj_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, const char *ns, zval *return_value);
50+
void php_dom_obj_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value);
51+
zend_long php_dom_get_nodelist_length(dom_object *obj);
52+
3153
extern const php_dom_obj_map_handler php_dom_obj_map_attributes;
3254
extern const php_dom_obj_map_handler php_dom_obj_map_by_tag_name;
3355
extern const php_dom_obj_map_handler php_dom_obj_map_child_nodes;

ext/dom/parentnode/css_selectors.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "php.h"
2222
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2323
#include "../php_dom.h"
24+
#include "../obj_map.h"
2425

2526
#include "ext/lexbor/lexbor/css/parser.h"
2627
#include "../lexbor/selectors-adapted/selectors.h"

0 commit comments

Comments
 (0)