@@ -5905,62 +5905,95 @@ PHP_FUNCTION(array_rand)
5905
5905
/* {{{ Returns the sum of the array entries */
5906
5906
PHP_FUNCTION (array_sum )
5907
5907
{
5908
- zval * input ,
5909
- * entry ,
5910
- entry_n ;
5908
+ HashTable * input ;
5909
+ zval * entry ;
5911
5910
5912
5911
ZEND_PARSE_PARAMETERS_START (1 , 1 )
5913
- Z_PARAM_ARRAY (input )
5912
+ Z_PARAM_ARRAY_HT (input )
5914
5913
ZEND_PARSE_PARAMETERS_END ();
5915
5914
5916
- ZVAL_LONG (return_value , 0 );
5915
+ if (zend_hash_num_elements (input ) == 0 ) {
5916
+ // TODO Deprecate empty array
5917
+ RETURN_LONG (0 );
5918
+ }
5917
5919
5918
- ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (input ), entry ) {
5919
- if (Z_TYPE_P (entry ) == IS_ARRAY || Z_TYPE_P (entry ) == IS_OBJECT ) {
5920
- continue ;
5920
+ ZVAL_LONG (return_value , 0 );
5921
+ ZEND_HASH_FOREACH_VAL (input , entry ) {
5922
+ zend_result status = add_function (return_value , return_value , entry );
5923
+ if (status == FAILURE ) {
5924
+ ZEND_ASSERT (EG (exception ));
5925
+ zend_clear_exception ();
5926
+ /* BC resources: previously resources were cast to int */
5927
+ if (Z_TYPE_P (entry ) == IS_RESOURCE ) {
5928
+ zval tmp ;
5929
+ ZVAL_LONG (& tmp , Z_RES_HANDLE_P (entry ));
5930
+ add_function (return_value , return_value , & tmp );
5931
+ }
5932
+ /* BC non numeric strings: previously were cast to 0 */
5933
+ if (Z_TYPE_P (entry ) == IS_STRING ) {
5934
+ zval tmp ;
5935
+ ZVAL_LONG (& tmp , 0 );
5936
+ add_function (return_value , return_value , & tmp );
5937
+ }
5938
+ // TODO Warning/Deprecation?
5921
5939
}
5922
- ZVAL_COPY (& entry_n , entry );
5923
- convert_scalar_to_number (& entry_n );
5924
- fast_add_function (return_value , return_value , & entry_n );
5925
5940
} ZEND_HASH_FOREACH_END ();
5941
+
5942
+ /* Traversal of array encountered a numerically catable object */
5943
+ if (Z_TYPE_P (return_value ) == IS_OBJECT ) {
5944
+ /* First try to convert to int */
5945
+ zval dst ;
5946
+ if (Z_OBJ_HT_P (return_value )-> cast_object (Z_OBJ_P (return_value ), & dst , IS_LONG ) == SUCCESS ) {
5947
+ zval_ptr_dtor (return_value );
5948
+ RETURN_COPY (& dst );
5949
+ }
5950
+ convert_scalar_to_number (return_value );
5951
+ }
5926
5952
}
5927
5953
/* }}} */
5928
5954
5929
5955
/* {{{ Returns the product of the array entries */
5930
5956
PHP_FUNCTION (array_product )
5931
5957
{
5932
- zval * input ,
5933
- * entry ,
5934
- entry_n ;
5935
- double dval ;
5958
+ HashTable * input ;
5959
+ zval * entry ;
5936
5960
5937
5961
ZEND_PARSE_PARAMETERS_START (1 , 1 )
5938
- Z_PARAM_ARRAY (input )
5962
+ Z_PARAM_ARRAY_HT (input )
5939
5963
ZEND_PARSE_PARAMETERS_END ();
5940
5964
5941
- ZVAL_LONG ( return_value , 1 );
5942
- if (! zend_hash_num_elements ( Z_ARRVAL_P ( input ))) {
5943
- return ;
5965
+ if ( zend_hash_num_elements ( input ) == 0 ) {
5966
+ // TODO Deprecate empty array
5967
+ RETURN_LONG ( 1 ) ;
5944
5968
}
5945
5969
5946
- ZEND_HASH_FOREACH_VAL (Z_ARRVAL_P (input ), entry ) {
5947
- if (Z_TYPE_P (entry ) == IS_ARRAY || Z_TYPE_P (entry ) == IS_OBJECT ) {
5948
- continue ;
5949
- }
5950
- ZVAL_COPY (& entry_n , entry );
5951
- convert_scalar_to_number (& entry_n );
5970
+ ZVAL_LONG (return_value , 1 );
5952
5971
5953
- if (Z_TYPE (entry_n ) == IS_LONG && Z_TYPE_P (return_value ) == IS_LONG ) {
5954
- dval = (double )Z_LVAL_P (return_value ) * (double )Z_LVAL (entry_n );
5955
- if ( (double )ZEND_LONG_MIN <= dval && dval <= (double )ZEND_LONG_MAX ) {
5956
- Z_LVAL_P (return_value ) *= Z_LVAL (entry_n );
5957
- continue ;
5972
+ ZEND_HASH_FOREACH_VAL (input , entry ) {
5973
+ zend_result status = mul_function (return_value , return_value , entry );
5974
+ if (status == FAILURE ) {
5975
+ ZEND_ASSERT (EG (exception ));
5976
+ zend_clear_exception ();
5977
+ /* BC resources: previously resources were cast to int */
5978
+ if (Z_TYPE_P (entry ) == IS_RESOURCE ) {
5979
+ zval tmp ;
5980
+ ZVAL_LONG (& tmp , Z_RES_HANDLE_P (entry ));
5981
+ mul_function (return_value , return_value , & tmp );
5958
5982
}
5983
+ /* BC non numeric strings: previously were cast to 0 */
5984
+ if (Z_TYPE_P (entry ) == IS_STRING ) {
5985
+ zval tmp ;
5986
+ ZVAL_LONG (& tmp , 0 );
5987
+ mul_function (return_value , return_value , & tmp );
5988
+ }
5989
+ // TODO Warning/Deprecation?
5959
5990
}
5960
- convert_to_double (return_value );
5961
- convert_to_double (& entry_n );
5962
- Z_DVAL_P (return_value ) *= Z_DVAL (entry_n );
5963
5991
} ZEND_HASH_FOREACH_END ();
5992
+
5993
+ /* Traversal of array encountered a numerically castable object */
5994
+ if (Z_TYPE_P (return_value ) == IS_OBJECT ) {
5995
+ convert_scalar_to_number (return_value );
5996
+ }
5964
5997
}
5965
5998
/* }}} */
5966
5999
0 commit comments