-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjco_darray.c
576 lines (438 loc) · 16.4 KB
/
jco_darray.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "php.h"
#include "zend_interfaces.h"
#include "jco_darray.h"
#include "ds/darray.h"
zend_class_entry *jco_darray_ce;
zend_object_handlers jco_darray_handlers;
typedef struct jco_darray {
zend_object std;
jco_ds_darray *array;
} jco_darray;
typedef struct _jco_darray_iterator_data {
zval *object_zval;
jco_darray *object;
size_t offset;
zval *current;
} jco_darray_iterator_data;
static inline long zval_to_long(zval *zv) {
if (Z_TYPE_P(zv) == IS_LONG) {
return Z_LVAL_P(zv);
} else {
zval tmp = *zv;
zval_copy_ctor(&tmp);
convert_to_long(&tmp);
return Z_LVAL(tmp);
}
}
static void jco_darray_free_object_storage(jco_darray *intern TSRMLS_DC) {
zend_object_std_dtor(&intern->std TSRMLS_CC);
if (intern->array) {
jco_ds_darray_destroy(intern->array);
}
efree(intern);
}
zend_object_value jco_darray_create_object(zend_class_entry *class_type TSRMLS_DC) {
zend_object_value retval;
jco_darray *intern = emalloc(sizeof(jco_darray));
memset(intern, 0, sizeof(jco_darray));
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
retval.handle = zend_objects_store_put(
intern,
(zend_objects_store_dtor_t) zend_objects_destroy_object,
(zend_objects_free_object_storage_t) jco_darray_free_object_storage,
NULL TSRMLS_CC
);
retval.handlers = &jco_darray_handlers;
return retval;
}
static zend_object_value jco_darray_clone(zval *object TSRMLS_DC) {
jco_darray *old_object = zend_object_store_get_object(object TSRMLS_CC);
zend_object_value new_object_val = jco_darray_create_object(Z_OBJCE_P(object) TSRMLS_CC);
jco_darray *new_object = zend_object_store_get_object_by_handle(new_object_val.handle TSRMLS_CC);
zend_objects_clone_members(
&new_object->std, new_object_val,
&old_object->std, Z_OBJ_HANDLE_P(object) TSRMLS_CC
);
new_object->array = jco_ds_darray_clone(old_object->array);
if (!new_object->array) {
zend_throw_exception(NULL, "Failed to clone jco_darray", 0 TSRMLS_CC);
}
return new_object_val;
}
static zval *jco_darray_read_dimension(zval *object, zval *zv_offset, int type TSRMLS_DC) {
jco_darray *intern = zend_object_store_get_object(object TSRMLS_CC);
if (intern->std.ce->parent) {
return zend_get_std_object_handlers()->read_dimension(object, zv_offset, type TSRMLS_CC);
}
if (!zv_offset) {
zend_throw_exception(NULL, "Cannot append to a jco_darray", 0 TSRMLS_CC);
return NULL;
}
long offset = zval_to_long(zv_offset);
if (offset < 0 || offset > jco_ds_darray_length(intern->array)) {
zend_throw_exception(NULL, "Offset out of range", 0 TSRMLS_CC);
return NULL;
}
zval *return_value;
zval *value = jco_ds_darray_get(intern->array, offset);
if (value) {
if (type == BP_VAR_W) {
return_value = value;
Z_SET_ISREF_P(return_value);
} else {
MAKE_STD_ZVAL(return_value);
ZVAL_ZVAL(return_value, value, 1, 0);
Z_DELREF_P(return_value);
}
} else {
MAKE_STD_ZVAL(return_value);
ZVAL_NULL(return_value);
Z_DELREF_P(return_value);
}
return return_value;
}
static void jco_darray_write_dimension(zval *object, zval *zv_offset, zval *value TSRMLS_DC) {
jco_darray *intern = zend_object_store_get_object(object TSRMLS_CC);
if (intern->std.ce->parent) {
return zend_get_std_object_handlers()->write_dimension(object, zv_offset, value TSRMLS_CC);
}
if (!zv_offset) {
zend_throw_exception(NULL, "Cannot append to a jco_darray", 0 TSRMLS_CC);
}
long offset = zval_to_long(zv_offset);
if (offset < 0) {
zend_throw_exception(NULL, "Offset out of range", 0 TSRMLS_CC);
}
zval *saved_val = jco_ds_darray_set(intern->array, (size_t)offset, value);
if (saved_val == NULL) {
zend_throw_exception(NULL, "Error occured during dimension write", 0 TSRMLS_CC);
}
}
static int jco_darray_has_dimension(zval *object, zval *zv_offset, int check_empty TSRMLS_DC) {
jco_darray *intern = zend_object_store_get_object(object TSRMLS_CC);
if (intern->std.ce->parent) {
return zend_get_std_object_handlers()->has_dimension(object, zv_offset, check_empty TSRMLS_CC);
}
long offset = zval_to_long(zv_offset);
if (offset < 0 || offset > jco_ds_darray_length(intern->array)) {
return 0;
}
if (check_empty) {
zval *value = jco_ds_darray_get(intern->array, offset);
if (value == NULL) {
return 0;
}
return zend_is_true(value);
}
}
static void jco_darray_unset_dimension(zval *object, zval *zv_offset TSRMLS_DC) {
jco_darray *intern = zend_object_store_get_object(object TSRMLS_CC);
if (intern->std.ce->parent) {
return zend_get_std_object_handlers()->unset_dimension(object, zv_offset TSRMLS_CC);
}
long offset = zval_to_long(zv_offset);
if (offset < 0 || offset > jco_ds_darray_length(intern->array)) {
zend_throw_exception(NULL, "Offset out of range", 0 TSRMLS_CC);
}
jco_ds_darray_unset(intern->array, offset);
}
int jco_darray_count_elements(zval *object, long *count TSRMLS_DC) {
jco_darray *intern = zend_object_store_get_object(object TSRMLS_CC);
if (intern->std.ce->parent) {
return zend_get_std_object_handlers()->count_elements(object, count TSRMLS_CC);
}
if (intern && intern->array) {
*count = (long)jco_ds_darray_count(intern->array);
return SUCCESS;
} else {
*count = 0;
return FAILURE;
}
}
static void jco_darray_iterator_dtor(zend_object_iterator *intern TSRMLS_DC) {
jco_darray_iterator_data *data = (jco_darray_iterator_data *)intern->data;
if (data->current != NULL) {
zval_ptr_dtor(&data->current);
}
zval_ptr_dtor((zval **)&data->object_zval);
efree(data);
efree(intern);
}
static int jco_darray_iterator_valid(zend_object_iterator *intern TSRMLS_DC) {
jco_darray_iterator_data *data = (jco_darray_iterator_data *)intern->data;
return jco_ds_darray_length(data->object->array) > data->offset ? SUCCESS : FAILURE;
}
static void jco_darray_iterator_get_current_data(zend_object_iterator *intern, zval ***data TSRMLS_DC) {
jco_darray_iterator_data *iter_data = (jco_darray_iterator_data *)intern->data;
if (iter_data->current != NULL) {
zval_ptr_dtor(&iter_data->current);
iter_data->current = NULL;
}
if (iter_data->offset < jco_ds_darray_length(iter_data->object->array)) {
zval *value = jco_ds_darray_get(iter_data->object->array, iter_data->offset);
if (value != NULL) {
MAKE_STD_ZVAL(iter_data->current);
ZVAL_ZVAL(iter_data->current, value, 1, 0);
*data = &iter_data->current;
} else {
*data = NULL;
}
} else {
*data = NULL;
}
}
#if ZEND_MODULE_API_NO >= 20121212
static void jco_darray_iterator_get_current_key(zend_object_iterator *intern, zval *key TSRMLS_DC) {
jco_darray_iterator_data *data = (jco_darray_iterator_data *) intern->data;
ZVAL_LONG(key, data->offset);
}
#else
static int jco_darray_iterator_get_current_key(
zend_object_iterator *intern, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC
) {
jco_darray_iterator_data *data = (jco_darray_iterator_data *) intern->data;
*int_key = (ulong) data->offset;
return HASH_KEY_IS_LONG;
}
#endif
static void jco_darray_iterator_move_forward(zend_object_iterator *intern TSRMLS_DC) {
jco_darray_iterator_data *data = (jco_darray_iterator_data *) intern->data;
data->offset++;
}
static void jco_darray_iterator_rewind(zend_object_iterator *intern TSRMLS_DC)
{
jco_darray_iterator_data *data = (jco_darray_iterator_data *) intern->data;
data->offset = 0;
data->current = NULL;
}
static zend_object_iterator_funcs jco_darray_iterator_funcs = {
jco_darray_iterator_dtor,
jco_darray_iterator_valid,
jco_darray_iterator_get_current_data,
jco_darray_iterator_get_current_key,
jco_darray_iterator_move_forward,
jco_darray_iterator_rewind,
NULL
};
zend_object_iterator *jco_darray_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) {
zend_object_iterator *iter;
jco_darray_iterator_data *iter_data;
if (by_ref) {
zend_throw_exception(NULL, "UPS, no by reference iteration!", 0 TSRMLS_CC);
return NULL;
}
iter = emalloc(sizeof(zend_object_iterator));
iter->funcs = &jco_darray_iterator_funcs;
iter_data = emalloc(sizeof(jco_darray_iterator_data));
iter_data->object_zval = object;
Z_ADDREF_P(object);
iter_data->object = zend_object_store_get_object(object TSRMLS_CC);
iter_data->offset = 0;
iter_data->current = NULL;
iter->data = iter_data;
return iter;
}
PHP_METHOD(jco_darray, __construct)
{
jco_darray *intern;
long size = 0;
long capacity = 0;
zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &size, &capacity) == FAILURE) {
zend_restore_error_handling(&error_handling TSRMLS_CC);
return;
}
zend_restore_error_handling(&error_handling TSRMLS_CC);
if (size <= 0) {
zend_throw_exception(NULL, "Array size must be positive", 0 TSRMLS_CC);
return;
}
if (capacity < 0) {
zend_throw_exception(NULL, "Array capacity must be positive or 0", 0 TSRMLS_CC);
return;
}
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
intern->array = jco_ds_darray_create((size_t)size, (size_t)capacity);
if (!intern->array) {
zend_throw_exception(NULL, "Failed to allocate array", 0 TSRMLS_CC);
}
return;
}
PHP_METHOD(jco_darray, count)
{
jco_darray *intern;
long count;
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
count = (long)jco_ds_darray_count(intern->array);
ZVAL_LONG(return_value, count);
}
PHP_METHOD(jco_darray, length)
{
jco_darray *intern;
long length;
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
length = (long) jco_ds_darray_length(intern->array);
ZVAL_LONG(return_value, length);
}
PHP_METHOD(jco_darray, offsetSet)
{
jco_darray *intern;
zval *val;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &index, &val) == FAILURE) {
zend_throw_exception(NULL, "Failed to parse arguments", 0 TSRMLS_CC);
return;
}
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
jco_ds_darray_set(intern->array, (size_t)index, val);
}
PHP_METHOD(jco_darray, offsetUnset)
{
jco_darray *intern;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
zend_throw_exception(NULL, "Invalid index passed", 0 TSRMLS_CC);
return;
}
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
jco_ds_darray_unset(intern->array, (size_t)index);
}
PHP_METHOD(jco_darray, offsetGet)
{
jco_darray *intern;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
zend_throw_exception(NULL, "Invalid index passed", 0 TSRMLS_CC);
return;
}
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
zval *val = jco_ds_darray_get(intern->array, (size_t)index);
if (val) {
ZVAL_ZVAL(return_value, val, 1, 0);
} else {
ZVAL_NULL(return_value);
}
}
PHP_METHOD(jco_darray, offsetExists)
{
jco_darray *intern;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
zend_throw_exception(NULL, "Invalid index passed", 0 TSRMLS_CC);
return;
}
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
zval *val = jco_ds_darray_get(intern->array, (size_t)index);
if (val) {
ZVAL_TRUE(return_value);
} else {
ZVAL_FALSE(return_value);
}
}
PHP_METHOD(jco_darray, filter)
{
jco_darray *intern;
jco_darray *newArray;
zval *retArray;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
int have_callback = 0;
if (ZEND_NUM_ARGS() > 0) {
have_callback = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|f", &fci, &fci_cache) == FAILURE) {
zend_throw_exception(NULL, "Invalid callback passed", 0 TSRMLS_CC);
return;
}
}
intern = zend_object_store_get_object(getThis() TSRMLS_CC);
MAKE_STD_ZVAL(retArray);
object_init_ex(retArray, jco_darray_ce);
newArray = zend_object_store_get_object(retArray TSRMLS_CC);
newArray->array = jco_ds_darray_create(jco_ds_darray_min_length(intern->array), jco_ds_darray_capacity(intern->array));
if (!newArray->array) {
zend_throw_exception(NULL, "Failed to allocate new array", 0 TSRMLS_CC);
zval_ptr_dtor(&retArray);
return;
}
zval *val;
size_t i = 0;
size_t newIndex = 0;
if (!have_callback) {
for (i = 0; i < jco_ds_darray_length(intern->array); i++) {
val = jco_ds_darray_get(intern->array, i);
if (val != NULL && zend_is_true(val)) {
jco_ds_darray_set(newArray->array, newIndex++, val);
}
}
} else {
zval **args[1];
zval *retval;
fci.no_separation = 1;
fci.retval_ptr_ptr = &retval;
fci.param_count = 1;
for (i = 0; i < jco_ds_darray_length(intern->array); i++) {
val = jco_ds_darray_get(intern->array, i);
if (val != NULL) {
args[0] = &val;
fci.params = args;
if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == SUCCESS && retval) {
if (zend_is_true(retval)) {
jco_ds_darray_set(newArray->array, newIndex++, val);
}
zval_ptr_dtor(&retval);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the filter callback");
return;
}
}
}
}
RETVAL_ZVAL(retArray, 1, 1);
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 1)
ZEND_ARG_INFO(0, size)
ZEND_ARG_INFO(0, capacity)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_jco_darray_offset, 0, 0, 1)
ZEND_ARG_INFO(0, offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_jco_darray_offset_value, 0, 0, 2)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_jarray_filter, 0, 0, 1)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
const zend_function_entry jco_darray_functions[] = {
PHP_ME(jco_darray, __construct, arginfo_construct, ZEND_ACC_PUBLIC)
PHP_ME(jco_darray, offsetSet, arginfo_jco_darray_offset_value, ZEND_ACC_PUBLIC)
PHP_ME(jco_darray, offsetGet, arginfo_jco_darray_offset, ZEND_ACC_PUBLIC)
PHP_ME(jco_darray, offsetUnset, arginfo_jco_darray_offset, ZEND_ACC_PUBLIC)
PHP_ME(jco_darray, offsetExists, arginfo_jco_darray_offset, ZEND_ACC_PUBLIC)
PHP_ME(jco_darray, filter, arginfo_jarray_filter, ZEND_ACC_PUBLIC)
PHP_ME(jco_darray, count, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(jco_darray, length, arginfo_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void jco_darray_init(TSRMLS_D)
{
zend_class_entry tmp_ce;
INIT_CLASS_ENTRY(tmp_ce, "JCO\\DArray", jco_darray_functions);
jco_darray_ce = zend_register_internal_class(&tmp_ce TSRMLS_CC);
jco_darray_ce->create_object = jco_darray_create_object;
jco_darray_ce->get_iterator = jco_darray_get_iterator;
jco_darray_ce->iterator_funcs.funcs = &jco_darray_iterator_funcs;
memcpy(&jco_darray_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
zend_class_implements(jco_darray_ce TSRMLS_CC, 2, zend_ce_arrayaccess, zend_ce_traversable);
jco_darray_handlers.has_dimension = jco_darray_has_dimension;
jco_darray_handlers.read_dimension = jco_darray_read_dimension;
jco_darray_handlers.write_dimension = jco_darray_write_dimension;
jco_darray_handlers.unset_dimension = jco_darray_unset_dimension;
jco_darray_handlers.count_elements = jco_darray_count_elements;
jco_darray_handlers.clone_obj = jco_darray_clone;
return;
}