From 14c4c5773641585a6bb1e859549a15128e6b8c2c Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 18 Jan 2019 20:44:05 +0800 Subject: [PATCH 1/4] Add PHP 7.3 to travis. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index b9fcc96..6fef494 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ language: php php: - 7.1 - 7.2 + - 7.3 - nightly script: From fddc87e95d65c3c0aff1a36260f7e2495a3a7b64 Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 18 Jan 2019 20:58:05 +0800 Subject: [PATCH 2/4] Compatible for PHP 7.4 --- src/collections_me.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/collections_me.c b/src/collections_me.c index 0e06003..de65a88 100644 --- a/src/collections_me.c +++ b/src/collections_me.c @@ -6,6 +6,12 @@ #include "php_collections_me.h" +// ZEND_ACC_CTOR is removed in PHP 7.4 +// Removing the flag does not affect the constructor from being recognized. +#if PHP_VERSION_ID >= 70400 +#define ZEND_ACC_CTOR 0 +#endif + ZEND_BEGIN_ARG_INFO(action_arginfo, 0) ZEND_ARG_CALLABLE_INFO(0, action, 0) ZEND_END_ARG_INFO() From cbc03da7fbc48d1dbe059a84df8ad23d00c4f807 Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 18 Jan 2019 21:37:42 +0800 Subject: [PATCH 3/4] Add `zip()`. --- src/collections_methods.c | 46 +++++++++++++++++++++++++++++++++++++-- tests/061-zip.phpt | 20 +++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/061-zip.phpt diff --git a/src/collections_methods.c b/src/collections_methods.c index 8a16098..c4e42d9 100644 --- a/src/collections_methods.c +++ b/src/collections_methods.c @@ -3325,7 +3325,49 @@ PHP_METHOD(Collection, windowed) PHP_METHOD(Collection, zip) { - + zval* elements; + zend_fcall_info fci; + zend_fcall_info_cache fcc; + ZEND_PARSE_PARAMETERS_START(1, 2) + Z_PARAM_ZVAL(elements) + Z_PARAM_OPTIONAL + Z_PARAM_FUNC(fci, fcc) + ZEND_PARSE_PARAMETERS_END(); + ELEMENTS_VALIDATE(elements, ERR_BAD_ARGUMENT_TYPE, return); + zend_array* current = THIS_COLLECTION; + if (UNEXPECTED(!HT_IS_PACKED(current)) || UNEXPECTED(!HT_IS_PACKED(elements_arr))) { + ERR_NOT_PACKED(); + RETURN_NULL(); + } + zend_bool has_transform = EX_NUM_ARGS() > 1; + INIT_FCI(&fci, 2); + uint32_t num_elements = zend_hash_num_elements(current); + uint32_t num_other = zend_hash_num_elements(elements_arr); + if (num_other < num_elements) { + num_elements = num_other; + } + ARRAY_NEW(zipped, num_elements); + Bucket* bucket_this = current->arData; + Bucket* bucket_other = elements_arr->arData; + uint32_t idx; + for (idx = 0; idx < num_elements; ++idx) { + if (has_transform) { + ZVAL_COPY_VALUE(¶ms[0], &(bucket_this + idx)->val); + ZVAL_COPY_VALUE(¶ms[1], &(bucket_other + idx)->val); + zend_call_function(&fci, &fcc); + } else { + zend_object* obj = create_pair_obj(); + Z_TRY_ADDREF((bucket_this + idx)->val); + Z_TRY_ADDREF((bucket_other + idx)->val); + pair_update_first(obj, &(bucket_this + idx)->val); + pair_update_second(obj, &(bucket_other + idx)->val); + zval pair; + ZVAL_OBJ(&pair, obj); + ZVAL_COPY_VALUE(&retval, &pair); + } + zend_hash_next_index_insert(zipped, &retval); + } + RETVAL_NEW_COLLECTION(zipped); } PHP_METHOD(Collection, zipWithNext) @@ -3337,7 +3379,7 @@ PHP_METHOD(Collection, zipWithNext) Z_PARAM_FUNC(fci, fcc) ZEND_PARSE_PARAMETERS_END(); zend_array* current = THIS_COLLECTION; - if (!HT_IS_PACKED(current)) { + if (UNEXPECTED(!HT_IS_PACKED(current))) { ERR_NOT_PACKED(); RETURN_NULL(); } diff --git a/tests/061-zip.phpt b/tests/061-zip.phpt new file mode 100644 index 0000000..4d43c11 --- /dev/null +++ b/tests/061-zip.phpt @@ -0,0 +1,20 @@ +--TEST-- +Test Collection::zip(). +--FILE-- +zip($array1)->toArray(); +if ($zipped != [new Pair('a', 'd'), new Pair('b', 'e')]) { + echo 'Collection::zip() failed.', PHP_EOL; +} + +$transform = function ($v1, $v2) { + return $v1.$v2; +}; +$zipped = Collection::init($array1)->zip($array, $transform)->toArray(); +if ($zipped != ['da', 'eb']) { + echo 'Collection::zip() failed.', PHP_EOL; +} +?> +--EXPECT-- From fea371d6b22b2edaef829c22d3bc253fa5c721e2 Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 18 Jan 2019 21:43:49 +0800 Subject: [PATCH 4/4] Prevent compiler warning for PHP 7.3 and above. --- src/collections_methods.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/collections_methods.c b/src/collections_methods.c index c4e42d9..cc68d16 100644 --- a/src/collections_methods.c +++ b/src/collections_methods.c @@ -183,7 +183,7 @@ static zend_always_inline zend_object* create_collection_obj() static zend_always_inline zend_object* create_pair_obj() { - return create_object(collections_pair_ce, &std_object_handlers); + return create_object(collections_pair_ce, (zend_object_handlers*)&std_object_handlers); } static zend_always_inline void array_release(zend_array* ht)