xref: /ext-ds/src/php/handlers/php_set_handlers.c (revision 53b57937)
1 #include "php_common_handlers.h"
2 #include "php_set_handlers.h"
3 #include "../../ds/ds_set.h"
4 #include "../objects/php_set.h"
5 #include "../classes/php_set_ce.h"
6 
7 zend_object_handlers php_ds_set_handlers;
8 
php_ds_set_read_dimension(zend_object * obj,zval * offset,int type,zval * rv)9 static zval *php_ds_set_read_dimension
10 #if PHP_VERSION_ID >= 80000
11 (zend_object *obj, zval *offset, int type, zval *rv) {
12     ds_set_t *set = php_ds_set_fetch_object(obj)->set;
13 #else
14 (zval *obj, zval *offset, int type, zval *rv) {
15     ds_set_t *set = Z_DS_SET_P(obj);
16 #endif
17     if (Z_TYPE_P(offset) != IS_LONG) {
18         INTEGER_INDEX_REQUIRED(offset);
19         return NULL;
20     }
21 
22     // Only support read, not write.
23     if (type != BP_VAR_R && type != BP_VAR_IS) {
24         return &EG(uninitialized_zval);
25     }
26 
27     return ds_set_get(set, Z_LVAL_P(offset));
28 }
29 
30 static void php_ds_set_write_dimension
31 #if PHP_VERSION_ID >= 80000
32 (zend_object *obj, zval *offset, zval *value) {
33     ds_set_t *set = php_ds_set_fetch_object(obj)->set;
34 #else
35 (zval *obj, zval *offset, zval *value) {
36     ds_set_t *set = Z_DS_SET_P(obj);
37 #endif
38     if (offset == NULL) {
39         ds_set_add(set, value);
40         return;
41     }
42     ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
43 }
44 
45 static int php_ds_set_count_elements
46 #if PHP_VERSION_ID >= 80000
47 (zend_object *obj, zend_long *count) {
48     ds_set_t *set = php_ds_set_fetch_object(obj)->set;
49 #else
50 (zval *obj, zend_long *count) {
51     ds_set_t *set = Z_DS_SET_P(obj);
52 #endif
53     *count = DS_SET_SIZE(set);
54     return SUCCESS;
55 }
56 
57 static void php_ds_set_free_object(zend_object *object)
58 {
59     php_ds_set_t *obj = php_ds_set_fetch_object(object);
60     ds_set_free(obj->set);
61     zend_object_std_dtor(&obj->std);
62 }
63 
64 static HashTable *php_ds_set_get_debug_info
65 #if PHP_VERSION_ID >= 80000
66 (zend_object *obj, int *is_temp) {
67     ds_set_t *set = php_ds_set_fetch_object(obj)->set;
68 #else
69 (zval *obj, int *is_temp) {
70     ds_set_t *set = Z_DS_SET_P(obj);
71 #endif
72     zval arr;
73     *is_temp = 1;
74 
75     ds_set_to_array(set, &arr);
76     return Z_ARRVAL(arr);
77 }
78 
79 static zend_object *php_ds_set_clone_obj
80 #if PHP_VERSION_ID >= 80000
81 (zend_object *obj) {
82     ds_set_t *set = php_ds_set_fetch_object(obj)->set;
83 #else
84 (zval *obj) {
85     ds_set_t *set = Z_DS_SET_P(obj);
86 #endif
87     return php_ds_set_create_clone(set);
88 }
89 
90 static HashTable *php_ds_set_get_gc
91 #if PHP_VERSION_ID >= 80000
92 (zend_object *obj, zval **gc_data, int *gc_count) {
93     ds_set_t *set = php_ds_set_fetch_object(obj)->set;
94 #else
95 (zval *obj, zval **gc_data, int *gc_count) {
96     ds_set_t *set = Z_DS_SET_P(obj);
97 #endif
98     if (DS_SET_IS_EMPTY(set)) {
99         *gc_data  = NULL;
100         *gc_count = 0;
101 
102     } else {
103         *gc_data  = (zval*) set->table->buckets;
104         *gc_count = (int)   set->table->next * 2;
105     }
106     return NULL;
107 }
108 
109 void php_ds_register_set_handlers()
110 {
111     memcpy(&php_ds_set_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
112 
113     php_ds_set_handlers.offset = XtOffsetOf(php_ds_set_t, std);
114 
115     php_ds_set_handlers.cast_object     = php_ds_default_cast_object;
116     php_ds_set_handlers.clone_obj       = php_ds_set_clone_obj;
117     php_ds_set_handlers.count_elements  = php_ds_set_count_elements;
118     php_ds_set_handlers.free_obj        = php_ds_set_free_object;
119     php_ds_set_handlers.get_debug_info  = php_ds_set_get_debug_info;
120     php_ds_set_handlers.get_gc          = php_ds_set_get_gc;
121     php_ds_set_handlers.read_dimension  = php_ds_set_read_dimension;
122     php_ds_set_handlers.write_dimension = php_ds_set_write_dimension;
123     php_ds_set_handlers.unset_dimension = php_ds_unset_dimension_by_key_not_supported;
124     php_ds_set_handlers.has_dimension   = php_ds_has_dimension_by_key_not_supported;
125 }
126