Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 61 additions & 28 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6736,44 +6736,73 @@ PHP_FUNCTION(array_map)
ZEND_PARSE_PARAMETERS_END();

if (n_arrays == 1) {
zend_ulong num_key;
zend_string *str_key;
zval *zv, arg;
int ret;

if (Z_TYPE(arrays[0]) != IS_ARRAY) {
zend_argument_type_error(2, "must be of type array, %s given", zend_zval_value_name(&arrays[0]));
RETURN_THROWS();
}
maxlen = zend_hash_num_elements(Z_ARRVAL(arrays[0]));
const HashTable *input = Z_ARRVAL(arrays[0]);
maxlen = zend_hash_num_elements(input);

/* Short-circuit: if no callback and only one array, just return it. */
if (!ZEND_FCI_INITIALIZED(fci) || !maxlen) {
ZVAL_COPY(return_value, &arrays[0]);
return;
}

array_init_size(return_value, maxlen);
zend_hash_real_init(Z_ARRVAL_P(return_value), HT_IS_PACKED(Z_ARRVAL(arrays[0])));

ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, zv) {
fci.retval = &result;
fci.param_count = 1;
fci.params = &arg;

ZVAL_COPY(&arg, zv);
ret = zend_call_function(&fci, &fci_cache);
i_zval_ptr_dtor(&arg);
if (ret != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
zend_array_destroy(Z_ARR_P(return_value));
RETURN_NULL();
}
if (str_key) {
_zend_hash_append(Z_ARRVAL_P(return_value), str_key, &result);
} else {
zend_hash_index_add_new(Z_ARRVAL_P(return_value), num_key, &result);
}
} ZEND_HASH_FOREACH_END();
fci.retval = &result;
fci.param_count = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this isn't moved up so that the two HT_IS_PACKED(input) branches can be joined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, I'll merge them.


if (HT_IS_PACKED(input)) {
array_init_size(return_value, input->nNumUsed);
HashTable *output = Z_ARRVAL_P(return_value);
zend_hash_real_init_packed(output);

uint32_t undefs = 0;
ZEND_HASH_FILL_PACKED(output) {
/* Can't use ZEND_HASH_PACKED_FOREACH_VAL() because we need to also account for the UNDEF values
* so the keys in the output array will match those of the input array. */
for (zval *cur = input->arPacked, *end = input->arPacked + input->nNumUsed; cur != end; cur++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not using ZEND_HASH_PACKED_FOREACH_VAL() to avoid skipping undef values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment, and possibly a test even to make sure this doesn't get changed in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment now. Adding a test was not necessary as 3 tests already test this.

if (EXPECTED(!Z_ISUNDEF_P(cur))) {
fci.params = cur;
zend_result ret = zend_call_function(&fci, &fci_cache);
ZEND_ASSERT(ret == SUCCESS);
ZEND_IGNORE_VALUE(ret);
if (UNEXPECTED(Z_ISUNDEF(result))) {
ZEND_HASH_FILL_FINISH();
zend_array_destroy(output);
RETURN_NULL();
}
} else {
ZVAL_UNDEF(&result);
undefs++;
}
ZEND_HASH_FILL_ADD(&result);
}
} ZEND_HASH_FILL_END();
output->nNumOfElements -= undefs;
} else {
zend_ulong num_key;
zend_string *str_key;

array_init_size(return_value, maxlen);
HashTable *output = Z_ARRVAL_P(return_value);
zend_hash_real_init_mixed(output);

ZEND_HASH_MAP_FOREACH_KEY_VAL(input, num_key, str_key, fci.params) {
zend_result ret = zend_call_function(&fci, &fci_cache);
ZEND_ASSERT(ret == SUCCESS);
ZEND_IGNORE_VALUE(ret);
if (UNEXPECTED(Z_ISUNDEF(result))) {
zend_array_destroy(output);
RETURN_NULL();
}
if (str_key) {
_zend_hash_append(output, str_key, &result);
} else {
zend_hash_index_add_new(output, num_key, &result);
}
} ZEND_HASH_FOREACH_END();
}
} else {
uint32_t *array_pos = (HashPosition *)ecalloc(n_arrays, sizeof(HashPosition));

Expand Down Expand Up @@ -6874,7 +6903,11 @@ PHP_FUNCTION(array_map)
fci.param_count = n_arrays;
fci.params = params;

if (zend_call_function(&fci, &fci_cache) != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
zend_result ret = zend_call_function(&fci, &fci_cache);
ZEND_ASSERT(ret == SUCCESS);
ZEND_IGNORE_VALUE(ret);

if (Z_TYPE(result) == IS_UNDEF) {
efree(array_pos);
zend_array_destroy(Z_ARR_P(return_value));
for (i = 0; i < n_arrays; i++) {
Expand Down
Loading