xref: /PHP-8.0/Zend/zend_weakrefs.c (revision ede92a86)
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_weakref_unref(obj_addr, tagged_ptr);
177 		zend_hash_index_del(&EG(weakrefs), obj_addr);
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(zval * referent,zval * return_value)200 static zend_always_inline zend_bool zend_weakref_find(zval *referent, zval *return_value) {
201 	void *tagged_ptr = zend_hash_index_find_ptr(&EG(weakrefs), (zend_ulong) Z_OBJ_P(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(zval * referent,zval * return_value)228 static zend_always_inline void zend_weakref_create(zval *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 = Z_OBJ_P(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 	zval *referent;
265 
266 	ZEND_PARSE_PARAMETERS_START(1,1)
267 		Z_PARAM_OBJECT(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 		zval pair;
430 		zval obj_zv;
431 		array_init(&pair);
432 
433 		ZVAL_OBJ_COPY(&obj_zv, (zend_object *) obj_addr);
434 		add_assoc_zval(&pair, "key", &obj_zv);
435 		Z_TRY_ADDREF_P(val);
436 		add_assoc_zval(&pair, "value", val);
437 
438 		zend_hash_next_index_insert(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 	*table = NULL;
448 	*n = 0;
449 	return &wm->ht;
450 }
451 
zend_weakmap_clone_obj(zend_object * old_object)452 static zend_object *zend_weakmap_clone_obj(zend_object *old_object)
453 {
454 	zend_object *new_object = zend_weakmap_create_object(zend_ce_weakmap);
455 	zend_weakmap *old_wm = zend_weakmap_from(old_object);
456 	zend_weakmap *new_wm = zend_weakmap_from(new_object);
457 	zend_hash_copy(&new_wm->ht, &old_wm->ht, NULL);
458 
459 	zend_ulong obj_addr;
460 	zval *val;
461 	ZEND_HASH_FOREACH_NUM_KEY_VAL(&new_wm->ht, obj_addr, val) {
462 		zend_weakref_register(
463 			(zend_object *) obj_addr, ZEND_WEAKREF_ENCODE(new_wm, ZEND_WEAKREF_TAG_MAP));
464 		zval_add_ref(val);
465 	} ZEND_HASH_FOREACH_END();
466 	return new_object;
467 }
468 
zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator * iter)469 static HashPosition *zend_weakmap_iterator_get_pos_ptr(zend_weakmap_iterator *iter) {
470 	ZEND_ASSERT(iter->ht_iter != (uint32_t) -1);
471 	return &EG(ht_iterators)[iter->ht_iter].pos;
472 }
473 
zend_weakmap_iterator_dtor(zend_object_iterator * obj_iter)474 static void zend_weakmap_iterator_dtor(zend_object_iterator *obj_iter)
475 {
476 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
477 	zend_hash_iterator_del(iter->ht_iter);
478 	zval_ptr_dtor(&iter->it.data);
479 }
480 
zend_weakmap_iterator_valid(zend_object_iterator * obj_iter)481 static int zend_weakmap_iterator_valid(zend_object_iterator *obj_iter)
482 {
483 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
484 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
485 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
486 	return zend_hash_has_more_elements_ex(&wm->ht, pos);
487 }
488 
zend_weakmap_iterator_get_current_data(zend_object_iterator * obj_iter)489 static zval *zend_weakmap_iterator_get_current_data(zend_object_iterator *obj_iter)
490 {
491 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
492 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
493 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
494 	return zend_hash_get_current_data_ex(&wm->ht, pos);
495 }
496 
zend_weakmap_iterator_get_current_key(zend_object_iterator * obj_iter,zval * key)497 static void zend_weakmap_iterator_get_current_key(zend_object_iterator *obj_iter, zval *key)
498 {
499 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
500 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
501 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
502 
503 	zend_string *string_key;
504 	zend_ulong num_key;
505 	int key_type = zend_hash_get_current_key_ex(&wm->ht, &string_key, &num_key, pos);
506 	if (key_type != HASH_KEY_IS_LONG) {
507 		ZEND_ASSERT(0 && "Must have integer key");
508 	}
509 
510 	ZVAL_OBJ_COPY(key, (zend_object *) num_key);
511 }
512 
zend_weakmap_iterator_move_forward(zend_object_iterator * obj_iter)513 static void zend_weakmap_iterator_move_forward(zend_object_iterator *obj_iter)
514 {
515 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
516 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
517 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
518 	zend_hash_move_forward_ex(&wm->ht, pos);
519 }
520 
zend_weakmap_iterator_rewind(zend_object_iterator * obj_iter)521 static void zend_weakmap_iterator_rewind(zend_object_iterator *obj_iter)
522 {
523 	zend_weakmap_iterator *iter = (zend_weakmap_iterator *) obj_iter;
524 	zend_weakmap *wm = zend_weakmap_fetch(&iter->it.data);
525 	HashPosition *pos = zend_weakmap_iterator_get_pos_ptr(iter);
526 	zend_hash_internal_pointer_reset_ex(&wm->ht, pos);
527 }
528 
529 static const zend_object_iterator_funcs zend_weakmap_iterator_funcs = {
530 	zend_weakmap_iterator_dtor,
531 	zend_weakmap_iterator_valid,
532 	zend_weakmap_iterator_get_current_data,
533 	zend_weakmap_iterator_get_current_key,
534 	zend_weakmap_iterator_move_forward,
535 	zend_weakmap_iterator_rewind,
536 	NULL,
537 	NULL, /* get_gc */
538 };
539 
540 /* by_ref is int due to Iterator API */
zend_weakmap_get_iterator(zend_class_entry * ce,zval * object,int by_ref)541 static zend_object_iterator *zend_weakmap_get_iterator(
542 		zend_class_entry *ce, zval *object, int by_ref)
543 {
544 	zend_weakmap *wm = zend_weakmap_fetch(object);
545 	zend_weakmap_iterator *iter = emalloc(sizeof(zend_weakmap_iterator));
546 	zend_iterator_init(&iter->it);
547 	iter->it.funcs = &zend_weakmap_iterator_funcs;
548 	ZVAL_COPY(&iter->it.data, object);
549 	iter->ht_iter = zend_hash_iterator_add(&wm->ht, 0);
550 	return &iter->it;
551 }
552 
ZEND_METHOD(WeakMap,offsetGet)553 ZEND_METHOD(WeakMap, offsetGet)
554 {
555 	zval *key;
556 
557 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
558 		return;
559 	}
560 
561 	zval *zv = zend_weakmap_read_dimension(Z_OBJ_P(ZEND_THIS), key, BP_VAR_R, NULL);
562 	if (!zv) {
563 		return;
564 	}
565 
566 	ZVAL_COPY(return_value, zv);
567 }
568 
ZEND_METHOD(WeakMap,offsetSet)569 ZEND_METHOD(WeakMap, offsetSet)
570 {
571 	zval *key, *value;
572 
573 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &key, &value) == FAILURE) {
574 		return;
575 	}
576 
577 	zend_weakmap_write_dimension(Z_OBJ_P(ZEND_THIS), key, value);
578 }
579 
ZEND_METHOD(WeakMap,offsetExists)580 ZEND_METHOD(WeakMap, offsetExists)
581 {
582 	zval *key;
583 
584 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
585 		return;
586 	}
587 
588 	RETURN_BOOL(zend_weakmap_has_dimension(Z_OBJ_P(ZEND_THIS), key, /* check_empty */ 0));
589 }
590 
ZEND_METHOD(WeakMap,offsetUnset)591 ZEND_METHOD(WeakMap, offsetUnset)
592 {
593 	zval *key;
594 
595 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &key) == FAILURE) {
596 		return;
597 	}
598 
599 	zend_weakmap_unset_dimension(Z_OBJ_P(ZEND_THIS), key);
600 }
601 
ZEND_METHOD(WeakMap,count)602 ZEND_METHOD(WeakMap, count)
603 {
604 	if (zend_parse_parameters_none() == FAILURE) {
605 		return;
606 	}
607 
608 	zend_long count;
609 	zend_weakmap_count_elements(Z_OBJ_P(ZEND_THIS), &count);
610 	RETURN_LONG(count);
611 }
612 
ZEND_METHOD(WeakMap,getIterator)613 ZEND_METHOD(WeakMap, getIterator)
614 {
615 	if (zend_parse_parameters_none() == FAILURE) {
616 		return;
617 	}
618 
619 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
620 }
621 
zend_register_weakref_ce(void)622 void zend_register_weakref_ce(void) /* {{{ */
623 {
624 	zend_class_entry ce;
625 
626 	INIT_CLASS_ENTRY(ce, "WeakReference", class_WeakReference_methods);
627 	zend_ce_weakref = zend_register_internal_class(&ce);
628 	zend_ce_weakref->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES;
629 
630 	zend_ce_weakref->create_object = zend_weakref_new;
631 	zend_ce_weakref->serialize = zend_class_serialize_deny;
632 	zend_ce_weakref->unserialize = zend_class_unserialize_deny;
633 
634 	memcpy(&zend_weakref_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
635 	zend_weakref_handlers.offset = XtOffsetOf(zend_weakref, std);
636 
637 	zend_weakref_handlers.free_obj = zend_weakref_free;
638 	zend_weakref_handlers.clone_obj = NULL;
639 
640 	INIT_CLASS_ENTRY(ce, "WeakMap", class_WeakMap_methods);
641 	zend_ce_weakmap = zend_register_internal_class(&ce);
642 	zend_ce_weakmap->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES;
643 	zend_class_implements(
644 		zend_ce_weakmap, 3, zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);
645 
646 	zend_ce_weakmap->create_object = zend_weakmap_create_object;
647 	zend_ce_weakmap->get_iterator = zend_weakmap_get_iterator;
648 	zend_ce_weakmap->serialize = zend_class_serialize_deny;
649 	zend_ce_weakmap->unserialize = zend_class_unserialize_deny;
650 
651 	memcpy(&zend_weakmap_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
652 	zend_weakmap_handlers.offset = XtOffsetOf(zend_weakmap, std);
653 	zend_weakmap_handlers.free_obj = zend_weakmap_free_obj;
654 	zend_weakmap_handlers.read_dimension = zend_weakmap_read_dimension;
655 	zend_weakmap_handlers.write_dimension = zend_weakmap_write_dimension;
656 	zend_weakmap_handlers.has_dimension = zend_weakmap_has_dimension;
657 	zend_weakmap_handlers.unset_dimension = zend_weakmap_unset_dimension;
658 	zend_weakmap_handlers.count_elements = zend_weakmap_count_elements;
659 	zend_weakmap_handlers.get_properties_for = zend_weakmap_get_properties_for;
660 	zend_weakmap_handlers.get_gc = zend_weakmap_get_gc;
661 	zend_weakmap_handlers.clone_obj = zend_weakmap_clone_obj;
662 }
663 /* }}} */
664 
665