1 #include "../../common.h"
2
3 #include "../parameters.h"
4 #include "../arginfo.h"
5
6 #include "../objects/php_stack.h"
7
8 #include "../iterators/php_stack_iterator.h"
9 #include "../handlers/php_stack_handlers.h"
10
11 #include "php_collection_ce.h"
12 #include "php_stack_ce.h"
13
14 #define METHOD(name) PHP_METHOD(Stack, name)
15
16 zend_class_entry *php_ds_stack_ce;
17
METHOD(__construct)18 METHOD(__construct)
19 {
20 PARSE_OPTIONAL_ZVAL(values);
21
22 if (values) {
23 ds_stack_push_all(THIS_DS_STACK(), values);
24 }
25 }
26
METHOD(allocate)27 METHOD(allocate)
28 {
29 PARSE_LONG(capacity);
30 ds_stack_allocate(THIS_DS_STACK(), capacity);
31 }
32
METHOD(capacity)33 METHOD(capacity)
34 {
35 PARSE_NONE;
36 RETURN_LONG(DS_STACK_CAPACITY(THIS_DS_STACK()));
37 }
38
METHOD(push)39 METHOD(push)
40 {
41 PARSE_VARIADIC_ZVAL();
42 ds_stack_push_va(THIS_DS_STACK(), argc, argv);
43 }
44
METHOD(pop)45 METHOD(pop)
46 {
47 PARSE_NONE;
48 ds_stack_pop_throw(THIS_DS_STACK(), return_value);
49 }
50
METHOD(peek)51 METHOD(peek)
52 {
53 PARSE_NONE;
54 RETURN_ZVAL_COPY(ds_stack_peek_throw(THIS_DS_STACK()));
55 }
56
METHOD(count)57 METHOD(count)
58 {
59 PARSE_NONE;
60 RETURN_LONG(DS_STACK_SIZE(THIS_DS_STACK()));
61 }
62
METHOD(copy)63 METHOD(copy)
64 {
65 PARSE_NONE;
66 RETURN_OBJ(php_ds_stack_create_clone(THIS_DS_STACK()));
67 }
68
METHOD(clear)69 METHOD(clear)
70 {
71 PARSE_NONE;
72 ds_stack_clear(THIS_DS_STACK());
73 }
74
METHOD(toArray)75 METHOD(toArray)
76 {
77 PARSE_NONE;
78 ds_stack_to_array(THIS_DS_STACK(), return_value);
79 }
80
METHOD(isEmpty)81 METHOD(isEmpty)
82 {
83 PARSE_NONE;
84 RETURN_BOOL(DS_STACK_IS_EMPTY(THIS_DS_STACK()));
85 }
86
METHOD(jsonSerialize)87 METHOD(jsonSerialize)
88 {
89 PARSE_NONE;
90 ds_stack_to_array(THIS_DS_STACK(), return_value);
91 }
92
METHOD(getIterator)93 METHOD(getIterator) {
94 PARSE_NONE;
95 ZVAL_COPY(return_value, getThis());
96 }
97
METHOD(offsetExists)98 METHOD(offsetExists)
99 {
100 ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
101 }
102
METHOD(offsetGet)103 METHOD(offsetGet)
104 {
105 ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
106 }
107
METHOD(offsetSet)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
METHOD(offsetUnset)121 METHOD(offsetUnset)
122 {
123 ARRAY_ACCESS_BY_KEY_NOT_SUPPORTED();
124 }
125
php_ds_register_stack()126 void php_ds_register_stack()
127 {
128 zend_class_entry ce;
129
130 zend_function_entry methods[] = {
131 PHP_DS_ME(Stack, __construct)
132 PHP_DS_ME(Stack, allocate)
133 PHP_DS_ME(Stack, capacity)
134 PHP_DS_ME(Stack, peek)
135 PHP_DS_ME(Stack, pop)
136 PHP_DS_ME(Stack, push)
137 PHP_DS_ME(Stack, getIterator)
138
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
144 PHP_DS_COLLECTION_ME_LIST(Stack)
145 PHP_FE_END
146 };
147
148 INIT_CLASS_ENTRY(ce, PHP_DS_NS(Stack), methods);
149
150 php_ds_stack_ce = zend_register_internal_class(&ce);
151 php_ds_stack_ce->ce_flags |= ZEND_ACC_FINAL;
152 php_ds_stack_ce->create_object = php_ds_stack_create_object;
153 php_ds_stack_ce->get_iterator = php_ds_stack_get_iterator;
154 php_ds_stack_ce->serialize = php_ds_stack_serialize;
155 php_ds_stack_ce->unserialize = php_ds_stack_unserialize;
156
157 zend_class_implements(php_ds_stack_ce, 2,
158 collection_ce,
159 zend_ce_arrayaccess
160 );
161
162 php_register_ds_stack_handlers();
163 }
164