|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP Version 7 arraylist | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 2019-2023 Wxxiong | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 3.01 of the PHP license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available through the world-wide-web at the following url: | |
| 10 | + | http://www.php.net/license/3_01.txt | |
| 11 | + | If you did not receive a copy of the PHP license and are unable to | |
| 12 | + | obtain it through the world-wide-web, please send a note to | |
| 13 | + | license@php.net so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | + | Author: Wxxiong <wxxiong6@gmail.com> | |
| 16 | + | | |
| 17 | + +----------------------------------------------------------------------+ |
| 18 | +*/ |
| 19 | + |
| 20 | + |
| 21 | +#ifdef HAVE_CONFIG_H |
| 22 | +# include "config.h" |
| 23 | +#endif |
| 24 | + |
| 25 | +#include "php.h" |
| 26 | +#include "ext/standard/info.h" |
| 27 | +#include "zend_exceptions.h" |
| 28 | + |
| 29 | +#include "php_arraylist.h" |
| 30 | + |
| 31 | +/* For compatibility with older PHP versions */ |
| 32 | +#ifndef ZEND_PARSE_PARAMETERS_NONE |
| 33 | +#define ZEND_PARSE_PARAMETERS_NONE() \ |
| 34 | + ZEND_PARSE_PARAMETERS_START(0, 0) \ |
| 35 | + ZEND_PARSE_PARAMETERS_END() |
| 36 | +#endif |
| 37 | + |
| 38 | +zend_class_entry *array_list_ce; |
| 39 | + |
| 40 | +typedef struct _arraylist { /* {{{ */ |
| 41 | + zend_long size; //数组大小 |
| 42 | + zend_long key; //数组当前使用key |
| 43 | + zval *elements; //数组元素 |
| 44 | +} arraylist; |
| 45 | +/* }}} */ |
| 46 | + |
| 47 | +typedef struct _arraylist_object { /* {{{ */ |
| 48 | + arraylist array; |
| 49 | + zend_function *fptr_offset_get; |
| 50 | + zend_function *fptr_offset_set; |
| 51 | + zend_function *fptr_offset_has; |
| 52 | + zend_function *fptr_offset_del; |
| 53 | + zend_function *fptr_count; |
| 54 | + int current; |
| 55 | + int flags; |
| 56 | + zend_class_entry *ce_get_iterator; |
| 57 | + zend_object std; |
| 58 | +} arraylist_object; |
| 59 | +/* }}} */ |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +static inline arraylist_object *arraylist_from_obj(zend_object *obj) /* {{{ */ { |
| 65 | + return (arraylist_object*)((char*)(obj) - XtOffsetOf(arraylist_object, std)); |
| 66 | +} |
| 67 | +/* }}} */ |
| 68 | + |
| 69 | +#define Z_ARRAYLIST_P(zv) arraylist_from_obj(Z_OBJ_P((zv))) |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +static void arraylist_init(arraylist *array, zend_long size) /* {{{ */ |
| 75 | +{ |
| 76 | + if (size > 0) { |
| 77 | + array->size = 0; /* reset size in case ecalloc() fails */ |
| 78 | + array->key = 0; |
| 79 | + array->elements = ecalloc(size, sizeof(zval)); |
| 80 | + array->size = size; |
| 81 | + } else { |
| 82 | + array->elements = NULL; |
| 83 | + array->size = 0; |
| 84 | + array->key = 0; |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | +/* }}} */ |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +static void resize(arraylist *array) |
| 93 | +{ |
| 94 | + if (array->key == array->size) |
| 95 | + { |
| 96 | + zend_long oldSize = array->size == 1? 2 : array->size; |
| 97 | + zend_long newSize = oldSize + (oldSize >> 1); |
| 98 | + |
| 99 | + zval *elements; |
| 100 | + elements = ecalloc(newSize, sizeof(zval)); |
| 101 | + zend_long i = 0; |
| 102 | + for (; i < newSize; i++) |
| 103 | + { |
| 104 | + elements[i] = array->elements[i]; |
| 105 | + } |
| 106 | + efree(&array->elements[i]); |
| 107 | + array->elements = elements; |
| 108 | + array->size = newSize; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +static void arraylist_add(arraylist *array, zval *val) /* {{{ */ |
| 113 | +{ |
| 114 | + if (!array) |
| 115 | + { |
| 116 | + return ; |
| 117 | + } |
| 118 | + resize(array); |
| 119 | + ZVAL_COPY(&array->elements[array->key], val); |
| 120 | + array->key++; |
| 121 | +} |
| 122 | +/* }}} */ |
| 123 | + |
| 124 | + |
| 125 | +/* }}}*/ |
| 126 | + |
| 127 | +/* {{{ PHP_RINIT_FUNCTION |
| 128 | + */ |
| 129 | +PHP_RINIT_FUNCTION(arraylist) |
| 130 | +{ |
| 131 | +#if defined(ZTS) && defined(COMPILE_DL_ARRAYLIST) |
| 132 | + ZEND_TSRMLS_CACHE_UPDATE(); |
| 133 | +#endif |
| 134 | + |
| 135 | + return SUCCESS; |
| 136 | +} |
| 137 | +/* }}} */ |
| 138 | + |
| 139 | +/* {{{ PHP_MINFO_FUNCTION phpinfo显示扩展信息 |
| 140 | + */ |
| 141 | +PHP_MINFO_FUNCTION(arraylist) |
| 142 | +{ |
| 143 | + php_info_print_table_start(); |
| 144 | + php_info_print_table_row(2, "arraylist support", "enabled"); |
| 145 | + php_info_print_table_row(2, "Version", PHP_ARRAYLIST_VERSION); |
| 146 | + php_info_print_table_end(); |
| 147 | +} |
| 148 | +/* }}} */ |
| 149 | + |
| 150 | +/* {{{ arginfo |
| 151 | + */ |
| 152 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_arraylist__construct, 0, 0, 1) |
| 153 | + ZEND_ARG_INFO(0, size) |
| 154 | +ZEND_END_ARG_INFO() |
| 155 | + |
| 156 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_arraylist_add, 0, 0, 1) |
| 157 | + ZEND_ARG_INFO(0, val) |
| 158 | +ZEND_END_ARG_INFO() |
| 159 | + |
| 160 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_arraylist_get, 0, 0, 1) |
| 161 | + ZEND_ARG_INFO(0, key) |
| 162 | +ZEND_END_ARG_INFO() |
| 163 | + |
| 164 | +ZEND_BEGIN_ARG_INFO(arginfo_arraylist_void, 0) |
| 165 | +ZEND_END_ARG_INFO() |
| 166 | +/* }}} */ |
| 167 | + |
| 168 | + |
| 169 | +/* {{{ void arraylist::__construct() |
| 170 | + */ |
| 171 | +PHP_METHOD(arraylist, __construct) |
| 172 | +{ |
| 173 | + zend_long size = 16; |
| 174 | + |
| 175 | + zval *object = getThis(); |
| 176 | + arraylist_object *intern; |
| 177 | + intern = Z_ARRAYLIST_P(object); |
| 178 | + |
| 179 | + ZEND_PARSE_PARAMETERS_START(0, 1) |
| 180 | + Z_PARAM_OPTIONAL |
| 181 | + Z_PARAM_LONG(size) |
| 182 | + ZEND_PARSE_PARAMETERS_END(); |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + arraylist_init(&intern->array, size); |
| 187 | + |
| 188 | +} |
| 189 | +/* }}} */ |
| 190 | + |
| 191 | +/* {{{ string arraylist::add( [ $val ] ) |
| 192 | + */ |
| 193 | +PHP_METHOD(arraylist, add) |
| 194 | +{ |
| 195 | + |
| 196 | + zval *object = getThis(); |
| 197 | + arraylist_object *intern; |
| 198 | + size_t var_len; |
| 199 | + zval *val; |
| 200 | + |
| 201 | + ZEND_PARSE_PARAMETERS_START(1, 1) |
| 202 | + Z_PARAM_ZVAL(val) |
| 203 | + ZEND_PARSE_PARAMETERS_END(); |
| 204 | + |
| 205 | + |
| 206 | + intern = Z_ARRAYLIST_P(object); |
| 207 | + arraylist_add(&intern->array, val); |
| 208 | + |
| 209 | + RETURN_TRUE; |
| 210 | +} |
| 211 | + |
| 212 | +/* {{{ zval arraylist::get(int $i) |
| 213 | + */ |
| 214 | +PHP_METHOD(arraylist, get) |
| 215 | +{ |
| 216 | + zval *object = getThis(); |
| 217 | + arraylist_object *intern; |
| 218 | + zend_long key; |
| 219 | + zval *element; |
| 220 | + |
| 221 | + ZEND_PARSE_PARAMETERS_START(1, 1) |
| 222 | + Z_PARAM_LONG(key) |
| 223 | + ZEND_PARSE_PARAMETERS_END(); |
| 224 | + |
| 225 | + |
| 226 | + intern = Z_ARRAYLIST_P(object); |
| 227 | + if (key <= intern->array.size) |
| 228 | + { |
| 229 | + RETURN_ZVAL(&intern->array.elements[key], 0, 1); |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + RETURN_NULL(); |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +PHP_METHOD(arraylist, count) |
| 238 | +{ |
| 239 | + zval *object = getThis(); |
| 240 | + arraylist_object *intern; |
| 241 | + |
| 242 | + if (zend_parse_parameters_none() == FAILURE) { |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + intern = Z_ARRAYLIST_P(object); |
| 247 | + RETURN_LONG(intern->array.key); |
| 248 | +} |
| 249 | + |
| 250 | +PHP_METHOD(arraylist, getSize) |
| 251 | +{ |
| 252 | + zval *object = getThis(); |
| 253 | + arraylist_object *intern; |
| 254 | + |
| 255 | + if (zend_parse_parameters_none() == FAILURE) { |
| 256 | + return; |
| 257 | + } |
| 258 | + |
| 259 | + intern = Z_ARRAYLIST_P(object); |
| 260 | + RETURN_LONG(intern->array.size); |
| 261 | +} |
| 262 | + |
| 263 | +/* {{{ proto object arraylist::toArray() |
| 264 | +*/ |
| 265 | +PHP_METHOD(arraylist, toArray) |
| 266 | +{ |
| 267 | + arraylist_object *intern; |
| 268 | + |
| 269 | + if (zend_parse_parameters_none() == FAILURE) { |
| 270 | + return; |
| 271 | + } |
| 272 | + |
| 273 | + intern = Z_ARRAYLIST_P(getThis()); |
| 274 | + |
| 275 | + if (intern->array.size > 0) { |
| 276 | + int i = 0; |
| 277 | + |
| 278 | + array_init_size(return_value, intern->array.size); |
| 279 | + for (; i < intern->array.size; i++) { |
| 280 | + if (!Z_ISUNDEF(intern->array.elements[i])) { |
| 281 | + zend_hash_index_update(Z_ARRVAL_P(return_value), i, &intern->array.elements[i]); |
| 282 | + Z_TRY_ADDREF(intern->array.elements[i]); |
| 283 | + } else { |
| 284 | + zend_hash_index_update(Z_ARRVAL_P(return_value), i, &EG(uninitialized_zval)); |
| 285 | + } |
| 286 | + } |
| 287 | + } else { |
| 288 | + RETURN_NULL(); |
| 289 | + } |
| 290 | +} |
| 291 | +/* }}} */ |
| 292 | + |
| 293 | +/* {{{ arraylist_functions[] 扩展函数 |
| 294 | + */ |
| 295 | +static const zend_function_entry arraylist_functions[] = { |
| 296 | + PHP_FE_END |
| 297 | +}; |
| 298 | +/* }}} */ |
| 299 | + |
| 300 | +/* {{{ arraylist__methods[] 扩展类方法 |
| 301 | + */ |
| 302 | +static const zend_function_entry arraylist_methods[] = { |
| 303 | + PHP_ME(arraylist, __construct, arginfo_arraylist__construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 304 | + PHP_ME(arraylist, add, arginfo_arraylist_add, ZEND_ACC_PUBLIC) |
| 305 | + PHP_ME(arraylist, get, arginfo_arraylist_get, ZEND_ACC_PUBLIC) |
| 306 | + PHP_ME(arraylist, count, arginfo_arraylist_void, ZEND_ACC_PUBLIC) |
| 307 | + PHP_ME(arraylist, toArray, arginfo_arraylist_void, ZEND_ACC_PUBLIC) |
| 308 | + PHP_ME(arraylist, getSize, arginfo_arraylist_void, ZEND_ACC_PUBLIC) |
| 309 | + PHP_FE_END |
| 310 | +}; |
| 311 | +/* }}} */ |
| 312 | + |
| 313 | +/* {{{ arraylist_module_entry |
| 314 | + */ |
| 315 | +zend_module_entry arraylist_module_entry = { |
| 316 | + STANDARD_MODULE_HEADER, |
| 317 | + "arraylist", /* 扩展名 name */ |
| 318 | + arraylist_functions, /* 注册函数,注意这里不是注册方法 */ |
| 319 | + PHP_MINIT(arraylist), /* PHP_MINIT - Module initialization */ |
| 320 | + NULL, /* PHP_MSHUTDOWN - Module shutdown */ |
| 321 | + NULL, /* PHP_RINIT - Request initialization */ |
| 322 | + NULL, /* PHP_RSHUTDOWN - Request shutdown */ |
| 323 | + NULL, /* |
| 324 | + - Module info */ |
| 325 | + PHP_ARRAYLIST_VERSION, /* Version */ |
| 326 | + STANDARD_MODULE_PROPERTIES |
| 327 | +}; |
| 328 | +/* }}} */ |
| 329 | + |
| 330 | +// 模块初始化时调用 |
| 331 | +PHP_MINIT_FUNCTION(arraylist) /* {{{ */ { |
| 332 | + zend_class_entry ce; |
| 333 | + //REGISTER_INI_ENTRIES(); // 注册ini |
| 334 | + |
| 335 | + //注册常量 |
| 336 | + //第一个参数是常量的名字, |
| 337 | + //第二个参数来表明长度 |
| 338 | + //CONST_CS标识是否大小写敏感 |
| 339 | + REGISTER_STRINGL_CONSTANT( |
| 340 | + "ARRAYLIST_VERSION", |
| 341 | + PHP_ARRAYLIST_VERSION, |
| 342 | + sizeof(PHP_ARRAYLIST_VERSION)-1, |
| 343 | + CONST_CS|CONST_PERSISTENT |
| 344 | + ); |
| 345 | + |
| 346 | + INIT_CLASS_ENTRY(ce, "ArrayList", arraylist_methods); //注册类及类方法 |
| 347 | + array_list_ce = zend_register_internal_class(&ce); |
| 348 | + |
| 349 | + return SUCCESS; |
| 350 | +} |
| 351 | +/* }}} */ |
| 352 | + |
| 353 | + |
| 354 | +#ifdef COMPILE_DL_ARRAYLIST |
| 355 | +# ifdef ZTS |
| 356 | +ZEND_TSRMLS_CACHE_DEFINE() |
| 357 | +# endif |
| 358 | +ZEND_GET_MODULE(arraylist) |
| 359 | +#endif |
| 360 | + |
0 commit comments