Skip to content

Commit b297e8f

Browse files
committed
Fix array access
1 parent f7cd99a commit b297e8f

22 files changed

+309
-31
lines changed

src/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ int name##_unserialize( \
159159
/** EXCEPTIONS **************************************************************/
160160

161161
#define ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED() ds_throw_exception( \
162-
spl_ce_OutOfBoundsException, \
162+
zend_ce_error, \
163163
"Array access by key is not supported")
164164

165165
#define INDEX_OUT_OF_RANGE(index, max) ds_throw_exception( \

src/php/classes/php_collection_ce.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ void php_ds_register_collection()
1919
};
2020

2121
INIT_CLASS_ENTRY(ce, PHP_DS_NS(Collection), methods);
22+
2223
collection_ce = zend_register_internal_interface(&ce);
24+
2325
zend_class_implements(collection_ce, 3,
2426
zend_ce_aggregate, // IteratorAggregate
2527
spl_ce_Countable, // Countable

src/php/classes/php_deque_ce.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ METHOD(capacity)
5252
RETURN_LONG((THIS_DS_DEQUE())->capacity);
5353
}
5454

55-
METHOD(pushAll)
56-
{
57-
PARSE_ZVAL(values);
58-
ds_deque_push_all(THIS_DS_DEQUE(), values);
59-
}
60-
6155
METHOD(map)
6256
{
6357
PARSE_CALLABLE();
@@ -265,8 +259,40 @@ METHOD(jsonSerialize)
265259

266260
METHOD(getIterator) {
267261
PARSE_NONE;
268-
// zend_object_iterator *iterator = php_ds_deque_get_iterator(php_ds_deque_ce, ZEND_THIS, 0);
269-
ZVAL_COPY(return_value, ZEND_THIS);
262+
ZVAL_COPY(return_value, getThis());
263+
}
264+
265+
METHOD(offsetExists)
266+
{
267+
PARSE_LONG(index);
268+
RETURN_BOOL(ds_deque_isset(THIS_DS_DEQUE(), index, false));
269+
}
270+
271+
METHOD(offsetGet)
272+
{
273+
PARSE_LONG(index);
274+
RETURN_ZVAL_COPY(ds_deque_get(THIS_DS_DEQUE(), index));
275+
}
276+
277+
METHOD(offsetSet)
278+
{
279+
PARSE_ZVAL_ZVAL(offset, value);
280+
281+
if (Z_TYPE_P(offset) == IS_NULL) {
282+
ds_deque_push(THIS_DS_DEQUE(), value);
283+
} else {
284+
if (Z_TYPE_P(offset) != IS_LONG) {
285+
INTEGER_INDEX_REQUIRED(offset);
286+
} else {
287+
ds_deque_set(THIS_DS_DEQUE(), Z_LVAL_P(offset), value);
288+
}
289+
}
290+
}
291+
292+
METHOD(offsetUnset)
293+
{
294+
PARSE_LONG(index);
295+
ds_deque_remove(THIS_DS_DEQUE(), index, return_value);
270296
}
271297

272298
void php_ds_register_deque()

src/php/classes/php_map_ce.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,31 @@ METHOD(xor)
283283

284284
METHOD(getIterator) {
285285
PARSE_NONE;
286-
ZVAL_COPY(return_value, ZEND_THIS);
286+
ZVAL_COPY(return_value, getThis());
287+
}
288+
289+
METHOD(offsetExists)
290+
{
291+
PARSE_ZVAL(offset);
292+
RETURN_BOOL(ds_htable_isset(THIS_DS_MAP()->table, offset, false));
293+
}
294+
295+
METHOD(offsetGet)
296+
{
297+
PARSE_ZVAL(offset);
298+
RETURN_ZVAL_COPY(ds_map_get(THIS_DS_MAP(), offset, NULL));
299+
}
300+
301+
METHOD(offsetSet)
302+
{
303+
PARSE_ZVAL_ZVAL(offset, value);
304+
ds_map_put(THIS_DS_MAP(), offset, value);
305+
}
306+
307+
METHOD(offsetUnset)
308+
{
309+
PARSE_ZVAL(offset);
310+
ds_map_remove(THIS_DS_MAP(), offset, NULL, return_value);
287311
}
288312

289313
void php_ds_register_map()
@@ -325,6 +349,11 @@ void php_ds_register_map()
325349
PHP_DS_ME(Map, xor)
326350
PHP_DS_ME(Map, getIterator)
327351

352+
PHP_DS_ME(Map, offsetExists)
353+
PHP_DS_ME(Map, offsetGet)
354+
PHP_DS_ME(Map, offsetSet)
355+
PHP_DS_ME(Map, offsetUnset)
356+
328357
PHP_DS_COLLECTION_ME_LIST(Map)
329358
PHP_FE_END
330359
};
@@ -343,7 +372,11 @@ void php_ds_register_map()
343372
STR_AND_LEN("MIN_CAPACITY"),
344373
DS_HTABLE_MIN_CAPACITY
345374
);
375+
376+
zend_class_implements(php_ds_map_ce, 2,
377+
collection_ce,
378+
zend_ce_arrayaccess
379+
);
346380

347-
zend_class_implements(php_ds_map_ce, 1, collection_ce);
348381
php_ds_register_map_handlers();
349382
}

src/php/classes/php_map_ce.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ ARGINFO_NONE_RETURN_DS( Map_values, Sequence);
4242
ARGINFO_DS_RETURN_DS( Map_xor, map, Map, Map);
4343
ARGINFO_NONE_RETURN_OBJ( Map_getIterator, Traversable);
4444

45+
ARGINFO_ZVAL_RETURN_BOOL( Map_offsetExists, offset);
46+
ARGINFO_ZVAL( Map_offsetGet, offset);
47+
ARGINFO_ZVAL_ZVAL( Map_offsetSet, offset, value);
48+
ARGINFO_ZVAL( Map_offsetUnset, offset);
49+
4550
void php_ds_register_map();
4651

4752
#endif

src/php/classes/php_priority_queue_ce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ METHOD(jsonSerialize)
8787

8888
METHOD(getIterator) {
8989
PARSE_NONE;
90-
ZVAL_COPY(return_value, ZEND_THIS);
90+
ZVAL_COPY(return_value, getThis());
9191
}
9292

9393
void php_ds_register_priority_queue()

src/php/classes/php_queue_ce.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,33 @@ METHOD(jsonSerialize)
9191

9292
METHOD(getIterator) {
9393
PARSE_NONE;
94-
ZVAL_COPY(return_value, ZEND_THIS);
94+
ZVAL_COPY(return_value, getThis());
95+
}
96+
97+
METHOD(offsetExists)
98+
{
99+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
100+
}
101+
102+
METHOD(offsetGet)
103+
{
104+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
105+
}
106+
107+
METHOD(offsetSet)
108+
{
109+
PARSE_ZVAL_ZVAL(offset, value);
110+
111+
if (Z_TYPE_P(offset) == IS_NULL) {
112+
ds_queue_push(THIS_DS_QUEUE(), 1, value);
113+
} else {
114+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
115+
}
116+
}
117+
118+
METHOD(offsetUnset)
119+
{
120+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
95121
}
96122

97123
void php_ds_register_queue()
@@ -107,6 +133,11 @@ void php_ds_register_queue()
107133
PHP_DS_ME(Queue, push)
108134
PHP_DS_ME(Queue, getIterator)
109135

136+
PHP_DS_ME(Queue, offsetExists)
137+
PHP_DS_ME(Queue, offsetGet)
138+
PHP_DS_ME(Queue, offsetSet)
139+
PHP_DS_ME(Queue, offsetUnset)
140+
110141
PHP_DS_COLLECTION_ME_LIST(Queue)
111142
PHP_FE_END
112143
};

src/php/classes/php_queue_ce.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ ARGINFO_NONE( Queue_pop);
1515
ARGINFO_NONE( Queue_peek);
1616
ARGINFO_NONE_RETURN_OBJ( Queue_getIterator, Traversable);
1717

18+
ARGINFO_ZVAL_RETURN_BOOL( Queue_offsetExists, offset);
19+
ARGINFO_ZVAL( Queue_offsetGet, offset);
20+
ARGINFO_ZVAL_ZVAL( Queue_offsetSet, offset, value);
21+
ARGINFO_ZVAL( Queue_offsetUnset, offset);
22+
1823
void php_ds_register_queue();
1924

2025
#endif

src/php/classes/php_sequence_ce.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ void php_ds_register_sequence()
3939
};
4040

4141
INIT_CLASS_ENTRY(ce, PHP_DS_NS(Sequence), methods);
42+
4243
sequence_ce = zend_register_internal_interface(&ce);
43-
zend_class_implements(sequence_ce, 1, collection_ce);
44+
45+
zend_class_implements(sequence_ce, 2,
46+
collection_ce,
47+
zend_ce_arrayaccess
48+
);
4449
}

src/php/classes/php_sequence_ce.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP_DS_SEQUENCE_ME(cls, join) \
2222
PHP_DS_SEQUENCE_ME(cls, last) \
2323
PHP_DS_SEQUENCE_ME(cls, map) \
2424
PHP_DS_SEQUENCE_ME(cls, merge) \
25+
PHP_DS_SEQUENCE_ME(cls, offsetExists) \
26+
PHP_DS_SEQUENCE_ME(cls, offsetGet) \
27+
PHP_DS_SEQUENCE_ME(cls, offsetSet) \
28+
PHP_DS_SEQUENCE_ME(cls, offsetUnset) \
2529
PHP_DS_SEQUENCE_ME(cls, pop) \
2630
PHP_DS_SEQUENCE_ME(cls, push) \
2731
PHP_DS_SEQUENCE_ME(cls, reduce) \
@@ -35,7 +39,7 @@ PHP_DS_SEQUENCE_ME(cls, slice) \
3539
PHP_DS_SEQUENCE_ME(cls, sort) \
3640
PHP_DS_SEQUENCE_ME(cls, sorted) \
3741
PHP_DS_SEQUENCE_ME(cls, sum) \
38-
PHP_DS_SEQUENCE_ME(cls, unshift)
42+
PHP_DS_SEQUENCE_ME(cls, unshift) \
3943

4044
ARGINFO_LONG( Sequence_allocate, capacity);
4145
ARGINFO_CALLABLE( Sequence_apply, callback);
@@ -50,6 +54,10 @@ ARGINFO_LONG_VARIADIC_ZVAL( Sequence_insert, index, values);
5054
ARGINFO_NONE( Sequence_last);
5155
ARGINFO_CALLABLE_RETURN_DS( Sequence_map, callback, Sequence);
5256
ARGINFO_ZVAL_RETURN_DS( Sequence_merge, values, Sequence);
57+
ARGINFO_ZVAL_RETURN_BOOL( Sequence_offsetExists, offset);
58+
ARGINFO_ZVAL( Sequence_offsetGet, offset);
59+
ARGINFO_ZVAL_ZVAL( Sequence_offsetSet, offset, value);
60+
ARGINFO_ZVAL( Sequence_offsetUnset, offset);
5361
ARGINFO_NONE( Sequence_pop);
5462
ARGINFO_VARIADIC_ZVAL( Sequence_push, values);
5563
ARGINFO_CALLABLE_OPTIONAL_ZVAL( Sequence_reduce, callback, initial);

src/php/classes/php_set_ce.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,35 @@ METHOD(jsonSerialize)
223223

224224
METHOD(getIterator) {
225225
PARSE_NONE;
226-
ZVAL_COPY(return_value, ZEND_THIS);
226+
ZVAL_COPY(return_value, getThis());
227+
}
228+
229+
230+
METHOD(offsetExists)
231+
{
232+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
233+
}
234+
235+
METHOD(offsetGet)
236+
{
237+
PARSE_LONG(index);
238+
RETURN_ZVAL_COPY(ds_set_get(THIS_DS_SET(), index));
239+
}
240+
241+
METHOD(offsetSet)
242+
{
243+
PARSE_ZVAL_ZVAL(offset, value);
244+
245+
if (Z_TYPE_P(offset) == IS_NULL) {
246+
ds_set_add_va(THIS_DS_SET(), 1, value);
247+
} else {
248+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
249+
}
250+
}
251+
252+
METHOD(offsetUnset)
253+
{
254+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
227255
}
228256

229257
void php_ds_register_set()
@@ -257,6 +285,11 @@ void php_ds_register_set()
257285
PHP_DS_ME(Set, xor)
258286
PHP_DS_ME(Set, getIterator)
259287

288+
PHP_DS_ME(Set, offsetExists)
289+
PHP_DS_ME(Set, offsetGet)
290+
PHP_DS_ME(Set, offsetSet)
291+
PHP_DS_ME(Set, offsetUnset)
292+
260293
PHP_DS_COLLECTION_ME_LIST(Set)
261294
PHP_FE_END
262295
};

src/php/classes/php_set_ce.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ ARGINFO_CALLABLE_RETURN_DS( Set_map, callback, Set);
3131
ARGINFO_NONE( Set_reverse);
3232
ARGINFO_NONE_RETURN_DS( Set_reversed, Set);
3333
ARGINFO_NONE( Set_sum);
34-
ARGINFO_NONE_RETURN_OBJ( Set_getIterator, Traversable);
34+
ARGINFO_NONE_RETURN_OBJ( Set_getIterator, Traversable);
35+
36+
ARGINFO_ZVAL_RETURN_BOOL( Set_offsetExists, offset);
37+
ARGINFO_ZVAL( Set_offsetGet, offset);
38+
ARGINFO_ZVAL_ZVAL( Set_offsetSet, offset, value);
39+
ARGINFO_ZVAL( Set_offsetUnset, offset);
3540

3641
void php_ds_register_set();
3742

src/php/classes/php_stack_ce.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,35 @@ METHOD(jsonSerialize)
9292

9393
METHOD(getIterator) {
9494
PARSE_NONE;
95-
ZVAL_COPY(return_value, ZEND_THIS);
95+
ZVAL_COPY(return_value, getThis());
96+
}
97+
98+
METHOD(offsetExists)
99+
{
100+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
101+
}
102+
103+
METHOD(offsetGet)
104+
{
105+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
106+
}
107+
108+
METHOD(offsetSet)
109+
{
110+
ds_stack_t *stack = THIS_DS_STACK();
111+
112+
PARSE_ZVAL_ZVAL(offset, value);
113+
114+
if (Z_TYPE_P(offset) == IS_NULL) {
115+
ds_stack_push(stack, value);
116+
} else {
117+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
118+
}
119+
}
120+
121+
METHOD(offsetUnset)
122+
{
123+
ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
96124
}
97125

98126
void php_ds_register_stack()
@@ -108,6 +136,11 @@ void php_ds_register_stack()
108136
PHP_DS_ME(Stack, push)
109137
PHP_DS_ME(Stack, getIterator)
110138

139+
PHP_DS_ME(Stack, offsetExists)
140+
PHP_DS_ME(Stack, offsetGet)
141+
PHP_DS_ME(Stack, offsetSet)
142+
PHP_DS_ME(Stack, offsetUnset)
143+
111144
PHP_DS_COLLECTION_ME_LIST(Stack)
112145
PHP_FE_END
113146
};
@@ -121,6 +154,10 @@ void php_ds_register_stack()
121154
php_ds_stack_ce->serialize = php_ds_stack_serialize;
122155
php_ds_stack_ce->unserialize = php_ds_stack_unserialize;
123156

124-
zend_class_implements(php_ds_stack_ce, 1, collection_ce);
157+
zend_class_implements(php_ds_stack_ce, 2,
158+
collection_ce,
159+
zend_ce_arrayaccess
160+
);
161+
125162
php_register_ds_stack_handlers();
126163
}

src/php/classes/php_stack_ce.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ ARGINFO_NONE_RETURN_LONG( Stack_capacity);
1313
ARGINFO_VARIADIC_ZVAL( Stack_push, values);
1414
ARGINFO_NONE( Stack_pop);
1515
ARGINFO_NONE( Stack_peek);
16-
ARGINFO_NONE_RETURN_OBJ( Stack_getIterator, Traversable);
16+
ARGINFO_NONE_RETURN_OBJ( Stack_getIterator, Traversable);
17+
18+
ARGINFO_ZVAL_RETURN_BOOL( Stack_offsetExists, offset);
19+
ARGINFO_ZVAL( Stack_offsetGet, offset);
20+
ARGINFO_ZVAL_ZVAL( Stack_offsetSet, offset, value);
21+
ARGINFO_ZVAL( Stack_offsetUnset, offset);
1722

1823
void php_ds_register_stack();
1924

0 commit comments

Comments
 (0)