1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 2.00 of the Zend license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.zend.com/license/2_00.txt. |
9 | If you did not receive a copy of the Zend license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@zend.com so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: krakjoe@php.net |
14 +----------------------------------------------------------------------+
15 */
16
17 #include "zend.h"
18 #include "zend_interfaces.h"
19 #include "zend_objects_API.h"
20 #include "zend_weakrefs.h"
21 #include "zend_weakrefs_arginfo.h"
22
23 typedef struct _zend_weakref {
24 zend_object *referent;
25 zend_object std;
26 } zend_weakref;
27
28 typedef struct _zend_weakmap {
29 HashTable ht;
30 zend_object std;
31 } zend_weakmap;
32
33 typedef struct _zend_weakmap_iterator {
34 zend_object_iterator it;
35 uint32_t ht_iter;
36 } zend_weakmap_iterator;
37
38 /* The EG(weakrefs) ht is a map from object address a tagged pointer, that may be one of
39 * zend_weakref*, zend_weakmap* or HashTable*. */
40 #define ZEND_WEAKREF_TAG_REF 0
41 #define ZEND_WEAKREF_TAG_MAP 1
42 #define ZEND_WEAKREF_TAG_HT 2
43 #define ZEND_WEAKREF_GET_TAG(p) (((uintptr_t) (p)) & 3)
44 #define ZEND_WEAKREF_GET_PTR(p) ((void *) (((uintptr_t) (p)) & ~3))
45 #define ZEND_WEAKREF_ENCODE(p, t) ((void *) (((uintptr_t) (p)) | (t)))
46
47 zend_class_entry *zend_ce_weakref;
48 zend_class_entry *zend_ce_weakmap;
49 static zend_object_handlers zend_weakref_handlers;
50 static zend_object_handlers zend_weakmap_handlers;
51
52 #define zend_weakref_from(o) ((zend_weakref*)(((char*) o) - XtOffsetOf(zend_weakref, std)))
53 #define zend_weakref_fetch(z) zend_weakref_from(Z_OBJ_P(z))
54
55 #define zend_weakmap_from(o) ((zend_weakmap*)(((char*) o) - XtOffsetOf(zend_weakmap, std)))
56 #define zend_weakmap_fetch(z) zend_weakmap_from(Z_OBJ_P(z))
57
zend_weakref_unref_single(void * ptr,uintptr_t tag,zend_ulong obj_addr)58 static inline void zend_weakref_unref_single(
59 void *ptr, uintptr_t tag, zend_ulong obj_addr)
60 {
61 if (tag == ZEND_WEAKREF_TAG_REF) {
62 zend_weakref *wr = ptr;
63 wr->referent = NULL;
64 } else {
65 ZEND_ASSERT(tag == ZEND_WEAKREF_TAG_MAP);
66 zend_hash_index_del((HashTable *) ptr, obj_addr);
67 }
68 }
69
zend_weakref_unref(zend_ulong obj_addr,void * tagged_ptr)70 static void zend_weakref_unref(zend_ulong obj_addr, void *tagged_ptr) {
71 void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
72 uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
73 if (tag == ZEND_WEAKREF_TAG_HT) {
74 HashTable *ht = ptr;
75 ZEND_HASH_FOREACH_PTR(ht, tagged_ptr) {
76 zend_weakref_unref_single(
77 ZEND_WEAKREF_GET_PTR(tagged_ptr), ZEND_WEAKREF_GET_TAG(tagged_ptr), obj_addr);
78 } ZEND_HASH_FOREACH_END();
79 zend_hash_destroy(ht);
80 FREE_HASHTABLE(ht);
81 } else {
82 zend_weakref_unref_single(ptr, tag, obj_addr);
83 }
84 }
85
zend_weakref_register(zend_object * object,void * payload)86 static void zend_weakref_register(zend_object *object, void *payload) {
87 GC_ADD_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
88
89 zend_ulong obj_addr = (zend_ulong) object;
90 zval *zv = zend_hash_index_find(&EG(weakrefs), obj_addr);
91 if (!zv) {
92 zend_hash_index_add_new_ptr(&EG(weakrefs), obj_addr, payload);
93 return;
94 }
95
96 void *tagged_ptr = Z_PTR_P(zv);
97 if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_HT) {
98 HashTable *ht = ZEND_WEAKREF_GET_PTR(tagged_ptr);
99 zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
100 return;
101 }
102
103 /* Convert simple pointer to hashtable. */
104 HashTable *ht = emalloc(sizeof(HashTable));
105 zend_hash_init(ht, 0, NULL, NULL, 0);
106 zend_hash_index_add_new_ptr(ht, (zend_ulong) tagged_ptr, tagged_ptr);
107 zend_hash_index_add_new_ptr(ht, (zend_ulong) payload, payload);
108 zend_hash_index_update_ptr(
109 &EG(weakrefs), obj_addr, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_HT));
110 }
111
zend_weakref_unregister(zend_object * object,void * payload)112 static void zend_weakref_unregister(zend_object *object, void *payload) {
113 zend_ulong obj_addr = (zend_ulong) object;
114 void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_addr);
115 ZEND_ASSERT(tagged_ptr && "Weakref not registered?");
116
117 void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
118 uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
119 if (tag != ZEND_WEAKREF_TAG_HT) {
120 ZEND_ASSERT(tagged_ptr == payload);
121 zend_hash_index_del(&EG(weakrefs), obj_addr);
122 GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
123
124 /* Do this last, as it may destroy the object. */
125 zend_weakref_unref_single(ptr, tag, obj_addr);
126 return;
127 }
128
129 HashTable *ht = ptr;
130 tagged_ptr = zend_hash_index_find_ptr(ht, (zend_ulong) payload);
131 ZEND_ASSERT(tagged_ptr && "Weakref not registered?");
132 ZEND_ASSERT(tagged_ptr == payload);
133 zend_hash_index_del(ht, (zend_ulong) payload);
134 if (zend_hash_num_elements(ht) == 0) {
135 GC_DEL_FLAGS(object, IS_OBJ_WEAKLY_REFERENCED);
136 zend_hash_destroy(ht);
137 FREE_HASHTABLE(ht);
138 zend_hash_index_del(&EG(weakrefs), obj_addr);
139 }
140
141 /* Do this last, as it may destroy the object. */
142 zend_weakref_unref_single(
143 ZEND_WEAKREF_GET_PTR(payload), ZEND_WEAKREF_GET_TAG(payload), obj_addr);
144 }
145
zend_weakrefs_hash_add(HashTable * ht,zend_object * key,zval * pData)146 ZEND_API zval *zend_weakrefs_hash_add(HashTable *ht, zend_object *key, zval *pData) {
147 zval *zv = zend_hash_index_add(ht, (zend_ulong) key, pData);
148 if (zv) {
149 zend_weakref_register(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP));
150 }
151 return zv;
152 }
153
zend_weakrefs_hash_del(HashTable * ht,zend_object * key)154 ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
155 zval *zv = zend_hash_index_find(ht, (zend_ulong) key);
156 if (zv) {
157 zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP));
158 return SUCCESS;
159 }
160 return FAILURE;
161 }
162
zend_weakrefs_init(void)163 void zend_weakrefs_init(void) {
164 zend_hash_init(&EG(weakrefs), 8, NULL, NULL, 0);
165 }
166
zend_weakrefs_notify(zend_object * object)167 void zend_weakrefs_notify(zend_object *object) {
168 /* Annoyingly we can't use the HT destructor here, because we need access to the key (which
169 * is the object address), which is not provided to the dtor. */
170 zend_ulong obj_addr = (zend_ulong) object;
171 void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), obj_addr);
172 #if ZEND_DEBUG
173 ZEND_ASSERT(tagged_ptr && "Tracking of the IS_OBJ_WEAKLY_REFERENCE flag should be precise");
174 #endif
175 if (tagged_ptr) {
176 zend_hash_index_del(&EG(weakrefs), obj_addr);
177 zend_weakref_unref(obj_addr, tagged_ptr);
178 }
179 }
180
zend_weakrefs_shutdown(void)181 void zend_weakrefs_shutdown(void) {
182 zend_ulong obj_addr;
183 void *tagged_ptr;
184 ZEND_HASH_FOREACH_NUM_KEY_PTR(&EG(weakrefs), obj_addr, tagged_ptr) {
185 zend_weakref_unref(obj_addr, tagged_ptr);
186 } ZEND_HASH_FOREACH_END();
187 zend_hash_destroy(&EG(weakrefs));
188 }
189
zend_weakref_new(zend_class_entry * ce)190 static zend_object* zend_weakref_new(zend_class_entry *ce) {
191 zend_weakref *wr = zend_object_alloc(sizeof(zend_weakref), zend_ce_weakref);
192
193 zend_object_std_init(&wr->std, zend_ce_weakref);
194
195 wr->std.handlers = &zend_weakref_handlers;
196
197 return &wr->std;
198 }
199
zend_weakref_find(zend_object * referent,zval * return_value)200 static zend_always_inline bool zend_weakref_find(zend_object *referent, zval *return_value) {
201 void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), (zend_ulong) referent);
202 if (!tagged_ptr) {
203 return 0;
204 }
205
206 void *ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
207 uintptr_t tag = ZEND_WEAKREF_GET_TAG(tagged_ptr);
208 if (tag == ZEND_WEAKREF_TAG_REF) {
209 zend_weakref *wr;
210 found_weakref:
211 wr = ptr;
212 RETVAL_OBJ_COPY(&wr->std);
213 return 1;
214 }
215
216 if (tag == ZEND_WEAKREF_TAG_HT) {
217 ZEND_HASH_FOREACH_PTR(ptr, tagged_ptr) {
218 if (ZEND_WEAKREF_GET_TAG(tagged_ptr) == ZEND_WEAKREF_TAG_REF) {
219 ptr = ZEND_WEAKREF_GET_PTR(tagged_ptr);
220 goto found_weakref;
221 }
222 } ZEND_HASH_FOREACH_END();
223 }
224
225 return 0;
226 }
227
zend_weakref_create(zend_object * referent,zval * return_value)228 static zend_always_inline void zend_weakref_create(zend_object *referent, zval *return_value) {
229 zend_weakref *wr;
230
231 object_init_ex(return_value, zend_ce_weakref);
232
233 wr = zend_weakref_fetch(return_value);
234 wr->referent = referent;
235
236 zend_weakref_register(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF));
237 }
238
zend_weakref_get(zval * weakref,zval * return_value)239 static zend_always_inline void zend_weakref_get(zval *weakref, zval *return_value) {
240 zend_weakref *wr = zend_weakref_fetch(weakref);
241
242 if (wr->referent) {
243 RETVAL_OBJ_COPY(wr->referent);
244 }
245 }
246
zend_weakref_free(zend_object * zo)247 static void zend_weakref_free(zend_object *zo) {
248 zend_weakref *wr = zend_weakref_from(zo);
249
250 if (wr->referent) {
251 zend_weakref_unregister(wr->referent, ZEND_WEAKREF_ENCODE(wr, ZEND_WEAKREF_TAG_REF));
252 }
253
254 zend_object_std_dtor(&wr->std);
255 }
256
ZEND_METHOD(WeakReference,__construct)257 ZEND_COLD ZEND_METHOD(WeakReference, __construct)
258 {
259 zend_throw_error(NULL, "Direct instantiation of WeakReference is not allowed, use WeakReference::create instead");
260 }
261
ZEND_METHOD(WeakReference,create)262 ZEND_METHOD(WeakReference, create)
263 {
264 zend_object *referent;
265
266 ZEND_PARSE_PARAMETERS_START(1,1)
267 Z_PARAM_OBJ(referent)
268 ZEND_PARSE_PARAMETERS_END();
269
270 if (zend_weakref_find(referent, return_value)) {
271 return;
272 }
273
274 zend_weakref_create(referent, return_value);
275 }
276
ZEND_METHOD(WeakReference,get)277 ZEND_METHOD(WeakReference, get)
278 {
279 ZEND_PARSE_PARAMETERS_NONE();
280
281 zend_weakref_get(getThis(), return_value);
282 }
283
zend_weakmap_create_object(zend_class_entry * ce)284 static zend_object *zend_weakmap_create_object(zend_class_entry *ce)
285 {
286 zend_weakmap *wm = zend_object_alloc(sizeof(zend_weakmap), ce);
287 zend_object_std_init(&wm->std, ce);
288 wm->std.handlers = &zend_weakmap_handlers;
289
290 zend_hash_init(&wm->ht, 0, NULL, ZVAL_PTR_DTOR, 0);
291 return &wm->std;
292 }
293
zend_weakmap_free_obj(zend_object * object)294 static void zend_weakmap_free_obj(zend_object *object)
295 {
296 zend_weakmap *wm = zend_weakmap_from(object);
297 zend_ulong obj_addr;
298 ZEND_HASH_FOREACH_NUM_KEY(&wm->ht, obj_addr) {
299 zend_weakref_unregister(
300 (zend_object *) obj_addr, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
301 } ZEND_HASH_FOREACH_END();
302 zend_hash_destroy(&wm->ht);
303 zend_object_std_dtor(&wm->std);
304 }
305
zend_weakmap_read_dimension(zend_object * object,zval * offset,int type,zval * rv)306 static zval *zend_weakmap_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
307 {
308 if (offset == NULL) {
309 zend_throw_error(NULL, "Cannot append to WeakMap");
310 return NULL;
311 }
312
313 ZVAL_DEREF(offset);
314 if (Z_TYPE_P(offset) != IS_OBJECT) {
315 zend_type_error("WeakMap key must be an object");
316 return NULL;
317 }
318
319 zend_weakmap *wm = zend_weakmap_from(object);
320 zend_object *obj_key = Z_OBJ_P(offset);
321 zval *zv = zend_hash_index_find(&wm->ht, (zend_ulong) obj_key);
322 if (zv == NULL) {
323 if (type != BP_VAR_IS) {
324 zend_throw_error(NULL,
325 "Object %s#%d not contained in WeakMap", ZSTR_VAL(obj_key->ce->name), obj_key->handle);
326 return NULL;
327 }
328 return NULL;
329 }
330
331 if (type == BP_VAR_W || type == BP_VAR_RW) {
332 ZVAL_MAKE_REF(zv);
333 }
334 return zv;
335 }
336
zend_weakmap_write_dimension(zend_object * object,zval * offset,zval * value)337 static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval *value)
338 {
339 if (offset == NULL) {
340 zend_throw_error(NULL, "Cannot append to WeakMap");
341 return;
342 }
343
344 ZVAL_DEREF(offset);
345 if (Z_TYPE_P(offset) != IS_OBJECT) {
346 zend_type_error("WeakMap key must be an object");
347 return;
348 }
349
350 zend_weakmap *wm = zend_weakmap_from(object);
351 zend_object *obj_key = Z_OBJ_P(offset);
352 Z_TRY_ADDREF_P(value);
353
354 zval *zv = zend_hash_index_find(&wm->ht, (zend_ulong) obj_key);
355 if (zv) {
356 /* Because the destructors can have side effects such as resizing or rehashing the WeakMap storage,
357 * free the zval only after overwriting the original value. */
358 zval zv_orig;
359 ZVAL_COPY_VALUE(&zv_orig, zv);
360 ZVAL_COPY_VALUE(zv, value);
361 zval_ptr_dtor(&zv_orig);
362 return;
363 }
364
365 zend_weakref_register(obj_key, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
366 zend_hash_index_add_new(&wm->ht, (zend_ulong) obj_key, value);
367 }
368
369 /* int return and check_empty due to Object Handler API */
zend_weakmap_has_dimension(zend_object * object,zval * offset,int check_empty)370 static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
371 {
372 ZVAL_DEREF(offset);
373 if (Z_TYPE_P(offset) != IS_OBJECT) {
374 zend_type_error("WeakMap key must be an object");
375 return 0;
376 }
377
378 zend_weakmap *wm = zend_weakmap_from(object);
379 zval *zv = zend_hash_index_find(&wm->ht, (zend_ulong) Z_OBJ_P(offset));
380 if (!zv) {
381 return 0;
382 }
383
384 if (check_empty) {
385 return i_zend_is_true(zv);
386 }
387 return Z_TYPE_P(zv) != IS_NULL;
388 }
389
zend_weakmap_unset_dimension(zend_object * object,zval * offset)390 static void zend_weakmap_unset_dimension(zend_object *object, zval *offset)
391 {
392 ZVAL_DEREF(offset);
393 if (Z_TYPE_P(offset) != IS_OBJECT) {
394 zend_type_error("WeakMap key must be an object");
395 return;
396 }
397
398 zend_weakmap *wm = zend_weakmap_from(object);
399 zend_object *obj_key = Z_OBJ_P(offset);
400 if (!zend_hash_index_exists(&wm->ht, (zend_ulong) Z_OBJ_P(offset))) {
401 /* Object not in WeakMap, do nothing. */
402 return;
403 }
404
405 zend_weakref_unregister(obj_key, ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP));
406 }
407
zend_weakmap_count_elements(zend_object * object,zend_long * count)408 static int zend_weakmap_count_elements(zend_object *object, zend_long *count)
409 {
410 zend_weakmap *wm = zend_weakmap_from(object);
411 *count = zend_hash_num_elements(&wm->ht);
412 return SUCCESS;
413 }
414
zend_weakmap_get_properties_for(zend_object * object,zend_prop_purpose purpose)415 static HashTable *zend_weakmap_get_properties_for(zend_object *object, zend_prop_purpose purpose)
416 {
417 if (purpose != ZEND_PROP_PURPOSE_DEBUG) {
418 return NULL;
419 }
420
421 zend_weakmap *wm = zend_weakmap_from(object);
422 HashTable *ht;
423 ALLOC_HASHTABLE(ht);
424 zend_hash_init(ht, zend_hash_num_elements(&wm->ht), NULL, ZVAL_PTR_DTOR, 0);
425
426 zend_ulong obj_addr;
427 zval *val;
428 ZEND_HASH_FOREACH_NUM_KEY_VAL(&wm->ht, obj_addr, val) {
429 zend_object *obj = (zend_object*)obj_addr;
430 zval pair;
431 array_init(&pair);
432
433 GC_ADDREF(obj);
434 add_assoc_object(&pair, "key", obj);
435 Z_TRY_ADDREF_P(val);
436 add_assoc_zval(&pair, "value", val);
437
438 zend_hash_next_index_insert_new(ht, &pair);
439 } ZEND_HASH_FOREACH_END();
440
441 return ht;
442 }
443
zend_weakmap_get_gc(zend_object * object,zval ** table,int * n)444 static HashTable *zend_weakmap_get_gc(zend_object *object, zval **table, int *n)
445 {
446 zend_weakmap *wm = zend_weakmap_from(object);
447 zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
448 zval *val;
449 ZEND_HASH_FOREACH_VAL(&wm->ht, val) {
450 zend_get_gc_buffer_add_zval(gc_buffer, val);
451 } ZEND_HASH_FOREACH_END();
452 zend_get_gc_buffer_use(gc_buffer, table, n);
453 return NULL;
454 }
455
zend_weakmap_clone_obj(zend_object * old_object)456 static zend_object *zend_weakmap_clone_obj(zend_object *old_object)
457 {
458 zend_object *new_object = zend_weakmap_create_object(zend_ce_weakmap);
459 zend_weakmap *old_wm = zend_weakmap_from(old_object);
460 zend_weakmap *new_wm = zend_weakmap_from(new_object);
461 zend_hash_copy(&new_wm->ht, &old_wm->ht, NULL);
462
463 zend_ulong obj_addr;
464 zval *val;
465 ZEND_HASH_FOREACH_NUM_KEY_VAL(&new_wm->ht, obj_addr, val) {
466 zend_weakref_register(
467 (zend_object *) obj_addr, ZEND_WEAKREF_ENCODE(new_wm, ZEND_WEAKREF_TAG_MAP));
468 zval_add_ref(val);
469 } ZEND_HASH_FOREACH_END();
470 return new_object;
471 }
472
zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator * iter)473 static HashPosition *zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator *iter) {
474 ZEND_ASSERT(iter->ht_iter != (uint32_t) -1);
475 return &EG(ht_iterators)[iter->ht_iter].pos;
476 }
477
zend_weakmap_iterator_dtor(zend_object_iterator * obj_iter)478 static void zend_weakmap_iterator_dtor(zend_object_iterator *obj_iter)
479 {
480 zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
481 zend_hash_iterator_del(iter->ht_iter);
482 zval_ptr_dtor(&iter->it.data);
483 }
484
zend_weakmap_iterator_valid(zend_object_iterator * obj_iter)485 static int zend_weakmap_iterator_valid(zend_object_iterator *obj_iter)
486 {
487 zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
488 zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
489 HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
490 return zend_hash_has_more_elements_ex(&wm->ht, pos);
491 }
492
zend_weakmap_iterator_get_current_data(zend_object_iterator * obj_iter)493 static zval *zend_weakmap_iterator_get_current_data(zend_object_iterator *obj_iter)
494 {
495 zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
496 zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
497 HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
498 return zend_hash_get_current_data_ex(&wm->ht, pos);
499 }
500
zend_weakmap_iterator_get_current_key(zend_object_iterator * obj_iter,zval * key)501 static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter, zval *key)
502 {
503 zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
504 zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
505 HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
506
507 zend_string *string_key;
508 zend_ulong num_key;
509 int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
510 if (key_type != HASH_KEY_IS_LONG) {
511 ZEND_ASSERT(0 && "Must have integer key");
512 }
513
514 ZVAL_OBJ_COPY(key, (zend_object *) num_key);
515 }
516
zend_weakmap_iterator_move_forward(zend_object_iterator * obj_iter)517 static void zend_weakmap_iterator_move_forward(zend_object_iterator *obj_iter)
518 {
519 zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
520 zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
521 HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
522 zend_hash_move_forward_ex(&wm->ht, pos);
523 }
524
zend_weakmap_iterator_rewind(zend_object_iterator * obj_iter)525 static void zend_weakmap_iterator_rewind(zend_object_iterator *obj_iter)
526 {
527 zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
528 zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
529 HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
530 zend_hash_internal_pointer_reset_ex(&wm->ht, pos);
531 }
532
533 static const zend_object_iterator_funcs zend_weakmap_iterator_funcs = {
534 zend_weakmap_iterator_dtor,
535 zend_weakmap_iterator_valid,
536 zend_weakmap_iterator_get_current_data,
537 zend_weakmap_iterator_get_current_key,
538 zend_weakmap_iterator_move_forward,
539 zend_weakmap_iterator_rewind,
540 NULL,
541 NULL, /* get_gc */
542 };
543
544 /* by_ref is int due to Iterator API */
zend_weakmap_get_iterator(zend_class_entry * ce,zval * object,int by_ref)545 static zend_object_iterator *zend_weakmap_get_iterator(
546 zend_class_entry *ce, zval *object, int by_ref)
547 {
548 zend_weakmap *wm = zend_weakmap_fetch(object);
549 zend_weakmap_iterator *iter = emalloc(sizeof(zend_weakmap_iterator));
550 zend_iterator_init(&iter->it);
551 iter->it.funcs = &zend_weakmap_iterator_funcs;
552 ZVAL_COPY(&iter->it.data, object);
553 iter->ht_iter = zend_hash_iterator_add(&wm->ht, 0);
554 return &iter->it;
555 }
556
ZEND_METHOD(WeakMap,offsetGet)557 ZEND_METHOD(WeakMap, offsetGet)
558 {
559 zval *key;
560
561 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
562 return;
563 }
564
565 zval *zv = zend_weakmap_read_dimension(Z_OBJ_P(ZEND_THIS), key, BP_VAR_R, NULL);
566 if (!zv) {
567 return;
568 }
569
570 ZVAL_COPY(return_value, zv);
571 }
572
ZEND_METHOD(WeakMap,offsetSet)573 ZEND_METHOD(WeakMap, offsetSet)
574 {
575 zval *key, *value;
576
577 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &key, &value) == FAILURE) {
578 return;
579 }
580
581 zend_weakmap_write_dimension(Z_OBJ_P(ZEND_THIS), key, value);
582 }
583
ZEND_METHOD(WeakMap,offsetExists)584 ZEND_METHOD(WeakMap, offsetExists)
585 {
586 zval *key;
587
588 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
589 return;
590 }
591
592 RETURN_BOOL(zend_weakmap_has_dimension(Z_OBJ_P(ZEND_THIS), key, /* check_empty */ 0));
593 }
594
ZEND_METHOD(WeakMap,offsetUnset)595 ZEND_METHOD(WeakMap, offsetUnset)
596 {
597 zval *key;
598
599 if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
600 return;
601 }
602
603 zend_weakmap_unset_dimension(Z_OBJ_P(ZEND_THIS), key);
604 }
605
ZEND_METHOD(WeakMap,count)606 ZEND_METHOD(WeakMap, count)
607 {
608 if (zend_parse_parameters_none() == FAILURE) {
609 return;
610 }
611
612 zend_long count;
613 zend_weakmap_count_elements(Z_OBJ_P(ZEND_THIS), &count);
614 RETURN_LONG(count);
615 }
616
ZEND_METHOD(WeakMap,getIterator)617 ZEND_METHOD(WeakMap, getIterator)
618 {
619 if (zend_parse_parameters_none() == FAILURE) {
620 return;
621 }
622
623 zend_create_internal_iterator_zval(return_value, ZEND_THIS);
624 }
625
zend_register_weakref_ce(void)626 void zend_register_weakref_ce(void) /* {{{ */
627 {
628 zend_ce_weakref = register_class_WeakReference();
629
630 zend_ce_weakref->create_object = zend_weakref_new;
631
632 memcpy(&zend_weakref_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
633 zend_weakref_handlers.offset = XtOffsetOf(zend_weakref, std);
634
635 zend_weakref_handlers.free_obj = zend_weakref_free;
636 zend_weakref_handlers.clone_obj = NULL;
637
638 zend_ce_weakmap = register_class_WeakMap(zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);
639
640 zend_ce_weakmap->create_object = zend_weakmap_create_object;
641 zend_ce_weakmap->get_iterator = zend_weakmap_get_iterator;
642
643 memcpy(&zend_weakmap_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
644 zend_weakmap_handlers.offset = XtOffsetOf(zend_weakmap, std);
645 zend_weakmap_handlers.free_obj = zend_weakmap_free_obj;
646 zend_weakmap_handlers.read_dimension = zend_weakmap_read_dimension;
647 zend_weakmap_handlers.write_dimension = zend_weakmap_write_dimension;
648 zend_weakmap_handlers.has_dimension = zend_weakmap_has_dimension;
649 zend_weakmap_handlers.unset_dimension = zend_weakmap_unset_dimension;
650 zend_weakmap_handlers.count_elements = zend_weakmap_count_elements;
651 zend_weakmap_handlers.get_properties_for = zend_weakmap_get_properties_for;
652 zend_weakmap_handlers.get_gc = zend_weakmap_get_gc;
653 zend_weakmap_handlers.clone_obj = zend_weakmap_clone_obj;
654 }
655 /* }}} */
656
657