Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 15 additions & 7 deletions ext/intl/ERROR_CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ void intl_error_reset(NULL); /* reset global error */
void intl_errors_reset(intl_error* err); /* reset global and object error */
```

In practice, `intl_errors_reset()` is not used because most classes have also
plain functions mapped to the same internal functions as their instance methods.
Fetching of the object is done with `zend_parse_method_parameters()` instead of
directly using `getThis()`. Therefore, no reference to object is obtained until
the arguments are fully parsed. Without a reference to the object, there's no
way to reset the object's internal error code. Instead, resetting of the
object's internal error code is done upon fetching the object from its zval.
Procedural functions that reset the global error should normally use
`PHP_INTL_FUNCTION_WITH_ERROR_RESET(name)`, which resets the global error before
entering the implementation body.
Class-specific method helper macros may use the same pattern when all methods
covered by the macro reset the global error.

`intl_errors_reset()` may be used directly when the object is available before
argument parsing, for example in methods that only operate on `ZEND_THIS()`.
For classes that have plain functions mapped to the same internal functions as
their instance methods, fetching of the object is done with
`zend_parse_method_parameters()` instead of directly using `getThis()`.
Therefore, no reference to object is obtained until the arguments are fully
parsed. Without a reference to the object, there's no way to reset the object's
internal error code. Instead, resetting of the object's internal error code is
done upon fetching the object from its zval.

Example:

Expand Down
17 changes: 5 additions & 12 deletions ext/intl/calendar/calendar_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@ U_CFUNC PHP_METHOD(IntlCalendar, __construct)
0 );
}

U_CFUNC PHP_FUNCTION(intlcal_create_instance)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(intlcal_create_instance)
{
zend_object *timezone_object = nullptr;
zend_string *timezone_string = nullptr;
char *locale_str = NULL;
size_t locale_len = 0;
UErrorCode status = U_ZERO_ERROR;
intl_error_reset(NULL);

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Expand Down Expand Up @@ -158,15 +157,14 @@ class BugStringCharEnumeration : public StringEnumeration
};
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BugStringCharEnumeration)

U_CFUNC PHP_FUNCTION(intlcal_get_keyword_values_for_locale)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(intlcal_get_keyword_values_for_locale)
{
UErrorCode status = U_ZERO_ERROR;
char *key,
*locale;
size_t key_len,
locale_len;
bool commonly_used;
intl_error_reset(NULL);

ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STRING(key, key_len)
Expand All @@ -186,19 +184,15 @@ U_CFUNC PHP_FUNCTION(intlcal_get_keyword_values_for_locale)
IntlIterator_from_StringEnumeration(se, return_value);
}

U_CFUNC PHP_FUNCTION(intlcal_get_now)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(intlcal_get_now)
{
intl_error_reset(NULL);

ZEND_PARSE_PARAMETERS_NONE();

RETURN_DOUBLE((double)Calendar::getNow());
}

U_CFUNC PHP_FUNCTION(intlcal_get_available_locales)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(intlcal_get_available_locales)
{
intl_error_reset(NULL);

ZEND_PARSE_PARAMETERS_NONE();

int32_t count;
Expand Down Expand Up @@ -1018,7 +1012,7 @@ U_CFUNC PHP_FUNCTION(intlcal_set_skipped_wall_time_option)
RETURN_TRUE;
}

U_CFUNC PHP_FUNCTION(intlcal_from_date_time)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(intlcal_from_date_time)
{
zend_object *date_obj;
zend_string *date_str;
Expand All @@ -1029,7 +1023,6 @@ U_CFUNC PHP_FUNCTION(intlcal_from_date_time)
TimeZone *timeZone;
UErrorCode status = U_ZERO_ERROR;
Calendar *cal;
intl_error_reset(NULL);

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJ_OF_CLASS_OR_STR(date_obj, php_date_get_date_ce(), date_str)
Expand Down
18 changes: 6 additions & 12 deletions ext/intl/locale/locale_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ U_CFUNC PHP_FUNCTION(locale_get_display_keyword_value)
/* {{{ return an associative array containing keyword-value
* pairs for this locale. The keys are keys to the array (doh!)
*/
U_CFUNC PHP_FUNCTION( locale_get_keywords )
PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_get_keywords)
{
UEnumeration* e = NULL;
UErrorCode status = U_ZERO_ERROR;
Expand All @@ -847,7 +847,6 @@ U_CFUNC PHP_FUNCTION( locale_get_keywords )
char* loc_name = NULL;
size_t loc_name_len = 0;

intl_error_reset( NULL );

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH(loc_name, loc_name_len)
Expand Down Expand Up @@ -1051,15 +1050,14 @@ static int handleAppendResult( int result, smart_str* loc_name)
* }}} */
/* {{{ Creates a locale by combining the parts of locale-ID passed
* }}} */
U_CFUNC PHP_FUNCTION(locale_compose)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_compose)
{
smart_str loc_name_s = {NULL, 0};
smart_str *loc_name = &loc_name_s;
zval* arr = NULL;
HashTable* hash_arr = NULL;
int result = 0;

intl_error_reset( NULL );

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(arr)
Expand Down Expand Up @@ -1232,13 +1230,12 @@ static int add_array_entry(const char* loc_name, zval* hash_arr, const char* key
/* }}} */

/* {{{ parses a locale-id into an array the different parts of it */
U_CFUNC PHP_FUNCTION(locale_parse)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_parse)
{
char* loc_name = NULL;
size_t loc_name_len = 0;
int grOffset = 0;

intl_error_reset( NULL );

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH(loc_name, loc_name_len)
Expand Down Expand Up @@ -1268,7 +1265,7 @@ U_CFUNC PHP_FUNCTION(locale_parse)
/* }}} */

/* {{{ gets an array containing the list of variants, or null */
U_CFUNC PHP_FUNCTION(locale_get_all_variants)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_get_all_variants)
{
char* loc_name = NULL;
size_t loc_name_len = 0;
Expand All @@ -1278,7 +1275,6 @@ U_CFUNC PHP_FUNCTION(locale_get_all_variants)
zend_string* variant = NULL;
char* saved_ptr = NULL;

intl_error_reset( NULL );

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_PATH(loc_name, loc_name_len)
Expand Down Expand Up @@ -1352,7 +1348,7 @@ static int strToMatch(const char* str ,char *retstr)
/* {{{ Checks if a $langtag filter matches with $locale according to RFC 4647's basic filtering algorithm */
/* }}} */
/* {{{ Checks if a $langtag filter matches with $locale according to RFC 4647's basic filtering algorithm */
U_CFUNC PHP_FUNCTION(locale_filter_matches)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_filter_matches)
{
char* lang_tag = NULL;
size_t lang_tag_len = 0;
Expand All @@ -1372,7 +1368,6 @@ U_CFUNC PHP_FUNCTION(locale_filter_matches)
bool boolCanonical = 0;
UErrorCode status = U_ZERO_ERROR;

intl_error_reset( NULL );

ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_PATH(lang_tag, lang_tag_len)
Expand Down Expand Up @@ -1640,7 +1635,7 @@ static zend_string* lookup_loc_range(const char* loc_range, HashTable* hash_arr,
/* {{{ Searches the items in $langtag for the best match to the language
* range
*/
U_CFUNC PHP_FUNCTION(locale_lookup)
PHP_INTL_FUNCTION_WITH_ERROR_RESET(locale_lookup)
{
zend_string* fallback_loc_str = NULL;
char* loc_range = NULL;
Expand All @@ -1651,7 +1646,6 @@ U_CFUNC PHP_FUNCTION(locale_lookup)
bool boolCanonical = 0;
zend_string* result_str = NULL;

intl_error_reset( NULL );

ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_ARRAY(arr)
Expand Down
9 changes: 3 additions & 6 deletions ext/intl/normalizer/normalizer_normalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static UBool intl_is_normalized(zend_long form, const UChar *uinput, int32_t uin
}/*}}}*/

/* {{{ Normalize a string. */
U_CFUNC PHP_FUNCTION( normalizer_normalize )
PHP_INTL_FUNCTION_WITH_ERROR_RESET(normalizer_normalize)
{
char* input = NULL;
/* form is optional, defaults to FORM_C */
Expand All @@ -92,7 +92,6 @@ U_CFUNC PHP_FUNCTION( normalizer_normalize )

int32_t size_needed;

intl_error_reset( NULL );

/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "s|l",
Expand Down Expand Up @@ -202,7 +201,7 @@ U_CFUNC PHP_FUNCTION( normalizer_normalize )
/* }}} */

/* {{{ Test if a string is in a given normalization form. */
U_CFUNC PHP_FUNCTION( normalizer_is_normalized )
PHP_INTL_FUNCTION_WITH_ERROR_RESET(normalizer_is_normalized)
{
char* input = NULL;
/* form is optional, defaults to FORM_C */
Expand All @@ -215,7 +214,6 @@ U_CFUNC PHP_FUNCTION( normalizer_is_normalized )

UBool uret = false;

intl_error_reset( NULL );

/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "s|l",
Expand Down Expand Up @@ -278,7 +276,7 @@ U_CFUNC PHP_FUNCTION( normalizer_is_normalized )
/* }}} */

/* {{{ Returns the Decomposition_Mapping property for the given UTF-8 encoded code point. */
U_CFUNC PHP_FUNCTION( normalizer_get_raw_decomposition )
PHP_INTL_FUNCTION_WITH_ERROR_RESET(normalizer_get_raw_decomposition)
{
char* input = NULL;
size_t input_length = 0;
Expand All @@ -293,7 +291,6 @@ U_CFUNC PHP_FUNCTION( normalizer_get_raw_decomposition )

zend_long form = NORMALIZER_DEFAULT;

intl_error_reset(NULL);

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(input, input_length)
Expand Down
9 changes: 9 additions & 0 deletions ext/intl/php_intl.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ PHP_MINFO_FUNCTION(intl);
const char *intl_locale_get_default( void );
char *canonicalize_locale_string(const char* locale);

#define PHP_INTL_FUNCTION_WITH_ERROR_RESET(name) \
static void php_intl_##name##_impl(INTERNAL_FUNCTION_PARAMETERS); \
U_CFUNC PHP_FUNCTION(name) \
{ \
intl_error_reset(NULL); \
php_intl_##name##_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU); \
} \
static void php_intl_##name##_impl(INTERNAL_FUNCTION_PARAMETERS)

#define PHP_INTL_VERSION PHP_VERSION

#endif /* PHP_INTL_H */
4 changes: 1 addition & 3 deletions ext/intl/resourcebundle/resourcebundle_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ U_CFUNC PHP_FUNCTION( resourcebundle_count )
}

/* {{{ Get available locales from ResourceBundle name */
U_CFUNC PHP_FUNCTION( resourcebundle_locales )
PHP_INTL_FUNCTION_WITH_ERROR_RESET(resourcebundle_locales)
{
char * bundlename;
size_t bundlename_len = 0;
Expand All @@ -343,8 +343,6 @@ U_CFUNC PHP_FUNCTION( resourcebundle_locales )
UEnumeration *icuenum;
UErrorCode icuerror = U_ZERO_ERROR;

intl_errors_reset( NULL );

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(bundlename, bundlename_len)
ZEND_PARSE_PARAMETERS_END();
Expand Down
Loading
Loading