Skip to content
Open
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
51 changes: 22 additions & 29 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6765,11 +6765,11 @@ PHP_FUNCTION(array_all)
PHP_FUNCTION(array_map)
{
zval *arrays = NULL;
int n_arrays = 0;
uint32_t n_arrays = 0;
zval result;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
int i;
uint32_t i;
uint32_t k, maxlen = 0;

ZEND_PARSE_PARAMETERS_START(2, -1)
Expand Down Expand Up @@ -6811,8 +6811,7 @@ PHP_FUNCTION(array_map)
ZEND_IGNORE_VALUE(ret);
if (UNEXPECTED(Z_ISUNDEF(result))) {
ZEND_HASH_FILL_FINISH();
zend_array_destroy(output);
RETURN_NULL();
RETURN_THROWS();
}
} else {
ZVAL_UNDEF(&result);
Expand All @@ -6835,8 +6834,7 @@ PHP_FUNCTION(array_map)
ZEND_ASSERT(ret == SUCCESS);
ZEND_IGNORE_VALUE(ret);
if (UNEXPECTED(Z_ISUNDEF(result))) {
zend_array_destroy(output);
RETURN_NULL();
RETURN_THROWS();
}
if (str_key) {
_zend_hash_append(output, str_key, &result);
Expand All @@ -6846,12 +6844,9 @@ PHP_FUNCTION(array_map)
} ZEND_HASH_FOREACH_END();
}
} else {
uint32_t *array_pos = (HashPosition *)ecalloc(n_arrays, sizeof(HashPosition));

for (i = 0; i < n_arrays; i++) {
if (Z_TYPE(arrays[i]) != IS_ARRAY) {
zend_argument_type_error(i + 2, "must be of type array, %s given", zend_zval_value_name(&arrays[i]));
efree(array_pos);
RETURN_THROWS();
}
if (zend_hash_num_elements(Z_ARRVAL(arrays[i])) > maxlen) {
Expand All @@ -6862,6 +6857,7 @@ PHP_FUNCTION(array_map)
array_init_size(return_value, maxlen);

if (!ZEND_FCI_INITIALIZED(fci)) {
uint32_t *array_pos = ecalloc(n_arrays, sizeof(HashPosition));
zval zv;

/* We iterate through all the arrays at once. */
Expand Down Expand Up @@ -6905,23 +6901,34 @@ PHP_FUNCTION(array_map)

zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &result);
}

efree(array_pos);
} else {
zval *params = (zval *)safe_emalloc(n_arrays, sizeof(zval), 0);

/* Remember next starting point in the array, initialize those as zeros. */
for (i = 0; i < n_arrays; i++) {
Z_EXTRA(params[i]) = 0;
}

fci.retval = &result;
fci.param_count = n_arrays;
fci.params = params;

/* We iterate through all the arrays at once. */
for (k = 0; k < maxlen; k++) {
for (i = 0; i < n_arrays; i++) {
/* If this array still has elements, add the current one to the
* parameter list, otherwise use null value. */
uint32_t pos = array_pos[i];
uint32_t pos = Z_EXTRA(params[i]);
if (HT_IS_PACKED(Z_ARRVAL(arrays[i]))) {
while (1) {
if (pos >= Z_ARRVAL(arrays[i])->nNumUsed) {
ZVAL_NULL(&params[i]);
break;
} else if (Z_TYPE(Z_ARRVAL(arrays[i])->arPacked[pos]) != IS_UNDEF) {
ZVAL_COPY(&params[i], &Z_ARRVAL(arrays[i])->arPacked[pos]);
array_pos[i] = pos + 1;
ZVAL_COPY_VALUE(&params[i], &Z_ARRVAL(arrays[i])->arPacked[pos]);
Z_EXTRA(params[i]) = pos + 1;
break;
}
pos++;
Expand All @@ -6932,43 +6939,29 @@ PHP_FUNCTION(array_map)
ZVAL_NULL(&params[i]);
break;
} else if (Z_TYPE(Z_ARRVAL(arrays[i])->arData[pos].val) != IS_UNDEF) {
ZVAL_COPY(&params[i], &Z_ARRVAL(arrays[i])->arData[pos].val);
array_pos[i] = pos + 1;
ZVAL_COPY_VALUE(&params[i], &Z_ARRVAL(arrays[i])->arData[pos].val);
Z_EXTRA(params[i]) = pos + 1;
break;
}
pos++;
}
}
}

fci.retval = &result;
fci.param_count = n_arrays;
fci.params = params;

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++) {
zval_ptr_dtor(&params[i]);
}
efree(params);
RETURN_NULL();
} else {
for (i = 0; i < n_arrays; i++) {
zval_ptr_dtor(&params[i]);
}
RETURN_THROWS();
}

zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &result);
}

efree(params);
}
efree(array_pos);
}
}
/* }}} */
Expand Down