-
Notifications
You must be signed in to change notification settings - Fork 13
/
wr_weakmap.c
359 lines (277 loc) · 10 KB
/
wr_weakmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
+----------------------------------------------------------------------+
| Weakref |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Etienne Kneuss <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "ext/spl/spl_exceptions.h"
#include "ext/spl/spl_iterators.h"
#include "Zend/zend_interfaces.h"
#include "ext/standard/info.h"
#include "wr_weakmap.h"
#include "wr_store.h"
#include "php_weakref.h"
zend_object_handlers wr_handler_WeakMap;
WEAKREF_API zend_class_entry *wr_ce_WeakMap;
static inline wr_weakmap_object* wr_weakmap_fetch(zend_object *obj) {
return (wr_weakmap_object *)((char *)obj - XtOffsetOf(wr_weakmap_object, std));
}
#define Z_WEAKMAP_OBJ_P(zv) wr_weakmap_fetch(Z_OBJ_P(zv));
static void wr_weakmap_ref_dtor(zend_object *wmap_obj, zend_object *ref_obj) { /* {{{ */
wr_weakmap_object *wmap = wr_weakmap_fetch(wmap_obj);
zend_hash_index_del(&wmap->map, ref_obj->handle);
}
/* }}} */
static void wr_weakmap_object_free_storage(zend_object *wmap_obj) /* {{{ */
{
wr_weakmap_object *wmap = wr_weakmap_fetch(wmap_obj);
wr_weakmap_refval *refval;
ZEND_HASH_FOREACH_PTR(&wmap->map, refval) {
wr_store_untrack(wmap_obj, refval->ref);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&wmap->map);
zend_object_std_dtor(&wmap->std);
} /* }}} */
static void wr_weakmap_refval_dtor(zval *data) /* {{{ */
{
wr_weakmap_refval *refval = Z_PTR_P(data);
zval_ptr_dtor(&refval->val);
efree(refval);
} /* }}} */
static zend_object* wr_weakmap_object_new_ex(zend_class_entry *ce, zend_object *cloneOf) /* {{{ */
{
wr_weakmap_object *wmap = ecalloc(1, sizeof(wr_weakmap_object) + zend_object_properties_size(ce));
zend_object_std_init(&wmap->std, ce);
zend_hash_init(&wmap->map, 0, NULL, wr_weakmap_refval_dtor, 0);
if (cloneOf) {
wr_weakmap_refval *refval;
wr_weakmap_object *other = wr_weakmap_fetch(cloneOf);
ZEND_HASH_FOREACH_PTR(&other->map, refval) {
wr_store_track(&wmap->std, wr_weakmap_ref_dtor, refval->ref);
Z_TRY_ADDREF_P(&refval->val);
wr_weakmap_refval *refval_copy = emalloc(sizeof(wr_weakmap_refval));
refval_copy->ref = refval->ref;
ZVAL_COPY_VALUE(&refval_copy->val, &refval->val);
zend_hash_index_update_ptr(&wmap->map, refval->ref->handle, refval_copy);
} ZEND_HASH_FOREACH_END();
}
wmap->std.handlers = &wr_handler_WeakMap;
return &wmap->std;
} /* }}} */
static zend_object* wr_weakmap_object_clone(zval *cloneOf_zv) /* {{{ */
{
zend_object *cloneOf = Z_OBJ_P(cloneOf_zv);
zend_object *clone = wr_weakmap_object_new_ex(cloneOf->ce, cloneOf);
zend_objects_clone_members(clone, Z_OBJ_P(cloneOf_zv));
return clone;
}
/* }}} */
static zend_object* wr_weakmap_object_new(zend_class_entry *ce) /* {{{ */
{
return wr_weakmap_object_new_ex(ce, NULL);
}
/* }}} */
/* {{{ proto void WeakMap::__construct()
*/
PHP_METHOD(WeakMap, __construct)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
} /* }}} */
/* {{{ proto int WeakMap::count(void)
*/
PHP_METHOD(WeakMap, count)
{
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_LONG(zend_hash_num_elements(&wmap->map));
} /* }}} */
/* {{{ proto bool WeakMap::offsetExists(object $index) U
Returns whether the requested $index exists. */
PHP_METHOD(WeakMap, offsetExists)
{
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
zval *ref_zv;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &ref_zv) == FAILURE) {
return;
}
RETURN_BOOL(zend_hash_index_exists(&wmap->map, Z_OBJ_HANDLE_P(ref_zv)));
} /* }}} */
/* {{{ proto mixed WeakMap::offsetGet(object $index) U
Returns the value at the specified $index. */
PHP_METHOD(WeakMap, offsetGet)
{
zval *ref_zv;
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &ref_zv) == FAILURE) {
return;
}
wr_weakmap_refval *refval = zend_hash_index_find_ptr(&wmap->map, Z_OBJ_HANDLE_P(ref_zv));
if (refval) {
RETURN_ZVAL(&refval->val, 1, 0);
} else {
RETURN_NULL();
}
} /* }}} */
/* {{{ proto void WeakMap::offsetSet(object $index, mixed $newval) U
Sets the value at the specified $index to $newval. */
PHP_METHOD(WeakMap, offsetSet)
{
zval *ref_zv, *val_zv;
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS(), "oz", &ref_zv, &val_zv) == FAILURE) {
return;
}
if (!zend_hash_index_exists(&wmap->map, Z_OBJ_HANDLE_P(ref_zv))) {
// Object not already in the weakmap, we attach it
wr_store_track(&wmap->std, wr_weakmap_ref_dtor, Z_OBJ_P(ref_zv));
}
Z_TRY_ADDREF_P(val_zv);
wr_weakmap_refval *refval = emalloc(sizeof(wr_weakmap_refval));
refval->ref = Z_OBJ_P(ref_zv);
ZVAL_COPY_VALUE(&refval->val, val_zv);
zend_hash_index_update_ptr(&wmap->map, Z_OBJ_HANDLE_P(ref_zv), refval);
} /* }}} */
/* {{{ proto void WeakMap::offsetUnset(mixed $index) U
Unsets the value at the specified $index. */
PHP_METHOD(WeakMap, offsetUnset)
{
zval *ref_zv;
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &ref_zv) == FAILURE) {
return;
}
if (zend_hash_index_del(&wmap->map, Z_OBJ_HANDLE_P(ref_zv)) == SUCCESS) {
wr_store_untrack(&wmap->std, Z_OBJ_P(ref_zv));
}
} /* }}} */
/* {{{ proto void WeakMap::rewind()
Rewind to first position */
PHP_METHOD(WeakMap, rewind)
{
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) {
return;
}
zend_hash_internal_pointer_reset_ex(&wmap->map, &wmap->pos);
} /* }}} */
/* {{{ proto bool WeakMap::valid()
Returns whether current position is valid */
PHP_METHOD(WeakMap, valid)
{
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_BOOL(zend_hash_has_more_elements_ex(&wmap->map, &wmap->pos) == SUCCESS);
} /* }}} */
/* {{{ proto mixed WeakMap::key()
Returns current key */
PHP_METHOD(WeakMap, key)
{
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
zval *refval_zv = zend_hash_get_current_data_ex(&wmap->map, &wmap->pos);
if (refval_zv) {
wr_weakmap_refval *refval = Z_PTR_P(refval_zv);
zval ret;
ZVAL_OBJ(&ret, refval->ref);
RETURN_ZVAL(&ret, 1, 0);
} else {
RETURN_NULL();
}
} /* }}} */
/* {{{ proto mixed WeakMap::current()
Returns current element */
PHP_METHOD(WeakMap, current)
{
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) {
return;
}
zval *refval_zv = zend_hash_get_current_data_ex(&wmap->map, &wmap->pos);
if (refval_zv) {
wr_weakmap_refval *refval = Z_PTR_P(refval_zv);
RETURN_ZVAL(&refval->val, 1, 0);
} else {
RETURN_NULL();
}
} /* }}} */
/* {{{ proto void SplObjectStorage::next()
Moves position forward */
PHP_METHOD(WeakMap, next)
{
wr_weakmap_object *wmap = Z_WEAKMAP_OBJ_P(getThis());
if (zend_parse_parameters_none() == FAILURE) {
return;
}
zend_hash_move_forward_ex(&wmap->map, &wmap->pos);
} /* }}} */
/* Function/Class/Method definitions */
ZEND_BEGIN_ARG_INFO(arginfo_wr_weakmap_void, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_wr_weakmap_obj, 0)
ZEND_ARG_INFO(0, object)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_wr_weakmap_obj_val, 0)
ZEND_ARG_INFO(0, object)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
static const zend_function_entry wr_funcs_WeakMap[] = {
PHP_ME(WeakMap, __construct, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, count, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
/* ArrayAccess */
PHP_ME(WeakMap, offsetExists, arginfo_wr_weakmap_obj, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, offsetGet, arginfo_wr_weakmap_obj, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, offsetSet, arginfo_wr_weakmap_obj_val, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, offsetUnset, arginfo_wr_weakmap_obj, ZEND_ACC_PUBLIC)
/* Iterator */
PHP_ME(WeakMap, rewind, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, valid, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, key, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, current, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
PHP_ME(WeakMap, next, arginfo_wr_weakmap_void, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
PHP_MINIT_FUNCTION(wr_weakmap) /* {{{ */
{
zend_class_entry weakmap_ce;
INIT_CLASS_ENTRY(weakmap_ce, "WeakMap", wr_funcs_WeakMap);
wr_ce_WeakMap = zend_register_internal_class(&weakmap_ce);
wr_ce_WeakMap->ce_flags |= ZEND_ACC_FINAL;
wr_ce_WeakMap->create_object = wr_weakmap_object_new;
memcpy(&wr_handler_WeakMap, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
wr_handler_WeakMap.clone_obj = wr_weakmap_object_clone;
wr_handler_WeakMap.free_obj = wr_weakmap_object_free_storage;
wr_handler_WeakMap.offset = XtOffsetOf(wr_weakmap_object, std);
zend_class_implements(wr_ce_WeakMap, 3, spl_ce_Countable, spl_ce_ArrayAccess, spl_ce_Iterator);
return SUCCESS;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/