diff options
Diffstat (limited to 'ext/intl/timezone/timezone_class.cpp')
-rw-r--r-- | ext/intl/timezone/timezone_class.cpp | 161 |
1 files changed, 81 insertions, 80 deletions
diff --git a/ext/intl/timezone/timezone_class.cpp b/ext/intl/timezone/timezone_class.cpp index 374b163851..d2aaa12344 100644 --- a/ext/intl/timezone/timezone_class.cpp +++ b/ext/intl/timezone/timezone_class.cpp @@ -1,6 +1,6 @@ /* +----------------------------------------------------------------------+ - | PHP Version 5 | + | PHP Version 7 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -60,13 +60,12 @@ U_CFUNC void timezone_object_construct(const TimeZone *zone, zval *object, int o * Convert from TimeZone to DateTimeZone object */ U_CFUNC zval *timezone_convert_to_datetimezone(const TimeZone *timeZone, intl_error *outside_error, - const char *func TSRMLS_DC) + const char *func, zval *ret TSRMLS_DC) { - zval *ret = NULL; UnicodeString id; char *message = NULL; php_timezone_obj *tzobj; - zval arg = zval_used_for_init; + zval arg; timeZone->getID(id); if (id.isBogus()) { @@ -76,9 +75,8 @@ U_CFUNC zval *timezone_convert_to_datetimezone(const TimeZone *timeZone, goto error; } - MAKE_STD_ZVAL(ret); object_init_ex(ret, php_date_get_timezone_ce()); - tzobj = (php_timezone_obj *)zend_objects_get_address(ret TSRMLS_CC); + tzobj = Z_PHPTIMEZONE_P(ret); if (id.compare(0, 3, UnicodeString("GMT", sizeof("GMT")-1, US_INV)) == 0) { /* The DateTimeZone constructor doesn't support offset time zones, @@ -88,31 +86,35 @@ U_CFUNC zval *timezone_convert_to_datetimezone(const TimeZone *timeZone, //convert offset from milliseconds to minutes tzobj->tzi.utc_offset = -1 * timeZone->getRawOffset() / (60 * 1000); } else { + char *str; + int str_len; /* Call the constructor! */ - Z_TYPE(arg) = IS_STRING; - if (intl_charFromString(id, &Z_STRVAL(arg), &Z_STRLEN(arg), - &INTL_ERROR_CODE(*outside_error)) == FAILURE) { + if (intl_charFromString(id, &str, &str_len, &INTL_ERROR_CODE(*outside_error)) == FAILURE) { spprintf(&message, 0, "%s: could not convert id to UTF-8", func); intl_errors_set(outside_error, INTL_ERROR_CODE(*outside_error), message, 1 TSRMLS_CC); goto error; } - zend_call_method_with_1_params(&ret, NULL, NULL, "__construct", - NULL, &arg); + ZVAL_STRINGL(&arg, str, str_len); + //??? + efree(str); + zend_call_method_with_1_params(ret, NULL, NULL, "__construct", NULL, &arg); if (EG(exception)) { spprintf(&message, 0, "%s: DateTimeZone constructor threw exception", func); intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1 TSRMLS_CC); - zend_object_store_ctor_failed(ret TSRMLS_CC); + zend_object_store_ctor_failed(Z_OBJ_P(ret) TSRMLS_CC); + zval_ptr_dtor(&arg); goto error; } + zval_ptr_dtor(&arg); } if (0) { error: if (ret) { - zval_ptr_dtor(&ret); + zval_ptr_dtor(ret); } ret = NULL; } @@ -120,34 +122,31 @@ error: if (message) { efree(message); } - if (Z_TYPE(arg) == IS_STRING) { - zval_dtor(&arg); - } return ret; } /* }}} */ /* {{{ timezone_process_timezone_argument * TimeZone argument processor. outside_error may be NULL (for static functions/constructors) */ -U_CFUNC TimeZone *timezone_process_timezone_argument(zval **zv_timezone, +U_CFUNC TimeZone *timezone_process_timezone_argument(zval *zv_timezone, intl_error *outside_error, const char *func TSRMLS_DC) { - zval local_zv_tz = zval_used_for_init, - *local_zv_tz_p = &local_zv_tz; + zval local_zv_tz; char *message = NULL; TimeZone *timeZone; - if (zv_timezone == NULL || Z_TYPE_PP(zv_timezone) == IS_NULL) { + if (zv_timezone == NULL || Z_TYPE_P(zv_timezone) == IS_NULL) { timelib_tzinfo *tzinfo = get_timezone_info(TSRMLS_C); - ZVAL_STRING(&local_zv_tz, tzinfo->name, 0); - zv_timezone = &local_zv_tz_p; + ZVAL_STRING(&local_zv_tz, tzinfo->name); + zv_timezone = &local_zv_tz; + } else { + ZVAL_NULL(&local_zv_tz); } - if (Z_TYPE_PP(zv_timezone) == IS_OBJECT && - instanceof_function(Z_OBJCE_PP(zv_timezone), TimeZone_ce_ptr TSRMLS_CC)) { - TimeZone_object *to = (TimeZone_object*)zend_objects_get_address( - *zv_timezone TSRMLS_CC); + if (Z_TYPE_P(zv_timezone) == IS_OBJECT && + instanceof_function(Z_OBJCE_P(zv_timezone), TimeZone_ce_ptr TSRMLS_CC)) { + TimeZone_object *to = Z_INTL_TIMEZONE_P(zv_timezone); if (to->utimezone == NULL) { spprintf(&message, 0, "%s: passed IntlTimeZone is not " "properly constructed", func); @@ -155,6 +154,7 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval **zv_timezone, intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1 TSRMLS_CC); efree(message); } + zval_dtor(&local_zv_tz); return NULL; } timeZone = to->utimezone->clone(); @@ -164,14 +164,15 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval **zv_timezone, intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1 TSRMLS_CC); efree(message); } + zval_dtor(&local_zv_tz); return NULL; } - } else if (Z_TYPE_PP(zv_timezone) == IS_OBJECT && - instanceof_function(Z_OBJCE_PP(zv_timezone), php_date_get_timezone_ce() TSRMLS_CC)) { + } else if (Z_TYPE_P(zv_timezone) == IS_OBJECT && + instanceof_function(Z_OBJCE_P(zv_timezone), php_date_get_timezone_ce() TSRMLS_CC)) { - php_timezone_obj *tzobj = (php_timezone_obj *)zend_objects_get_address( - *zv_timezone TSRMLS_CC); + php_timezone_obj *tzobj = Z_PHPTIMEZONE_P(zv_timezone); + zval_dtor(&local_zv_tz); return timezone_convert_datetimezone(tzobj->type, tzobj, 0, outside_error, func TSRMLS_CC); } else { @@ -179,7 +180,7 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval **zv_timezone, gottenId; UErrorCode status = U_ZERO_ERROR; /* outside_error may be NULL */ convert_to_string_ex(zv_timezone); - if (intl_stringFromChar(id, Z_STRVAL_PP(zv_timezone), Z_STRLEN_PP(zv_timezone), + if (intl_stringFromChar(id, Z_STRVAL_P(zv_timezone), Z_STRLEN_P(zv_timezone), &status) == FAILURE) { spprintf(&message, 0, "%s: Time zone identifier given is not a " "valid UTF-8 string", func); @@ -187,6 +188,7 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval **zv_timezone, intl_errors_set(outside_error, status, message, 1 TSRMLS_CC); efree(message); } + zval_dtor(&local_zv_tz); return NULL; } timeZone = TimeZone::createTimeZone(id); @@ -196,41 +198,43 @@ U_CFUNC TimeZone *timezone_process_timezone_argument(zval **zv_timezone, intl_errors_set(outside_error, U_MEMORY_ALLOCATION_ERROR, message, 1 TSRMLS_CC); efree(message); } + zval_dtor(&local_zv_tz); return NULL; } if (timeZone->getID(gottenId) != id) { spprintf(&message, 0, "%s: no such time zone: '%s'", - func, Z_STRVAL_PP(zv_timezone)); + func, Z_STRVAL_P(zv_timezone)); if (message) { intl_errors_set(outside_error, U_ILLEGAL_ARGUMENT_ERROR, message, 1 TSRMLS_CC); efree(message); } + zval_dtor(&local_zv_tz); delete timeZone; return NULL; } } + + zval_dtor(&local_zv_tz); return timeZone; } /* }}} */ /* {{{ clone handler for TimeZone */ -static zend_object_value TimeZone_clone_obj(zval *object TSRMLS_DC) +static zend_object *TimeZone_clone_obj(zval *object TSRMLS_DC) { TimeZone_object *to_orig, *to_new; - zend_object_value ret_val; + zend_object *ret_val; intl_error_reset(NULL TSRMLS_CC); - to_orig = (TimeZone_object*)zend_object_store_get_object(object TSRMLS_CC); + to_orig = Z_INTL_TIMEZONE_P(object); intl_error_reset(TIMEZONE_ERROR_P(to_orig) TSRMLS_CC); ret_val = TimeZone_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC); - to_new = (TimeZone_object*)zend_object_store_get_object_by_handle( - ret_val.handle TSRMLS_CC); + to_new = php_intl_timezone_fetch_object(ret_val); - zend_objects_clone_members(&to_new->zo, ret_val, - &to_orig->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC); + zend_objects_clone_members(&to_new->zo, &to_orig->zo TSRMLS_CC); if (to_orig->utimezone != NULL) { TimeZone *newTimeZone; @@ -238,14 +242,14 @@ static zend_object_value TimeZone_clone_obj(zval *object TSRMLS_DC) newTimeZone = to_orig->utimezone->clone(); to_new->should_delete = 1; if (!newTimeZone) { - char *err_msg; + zend_string *err_msg; intl_errors_set_code(TIMEZONE_ERROR_P(to_orig), U_MEMORY_ALLOCATION_ERROR TSRMLS_CC); intl_errors_set_custom_msg(TIMEZONE_ERROR_P(to_orig), "Could not clone IntlTimeZone", 0 TSRMLS_CC); err_msg = intl_error_get_message(TIMEZONE_ERROR_P(to_orig) TSRMLS_CC); - zend_throw_exception(NULL, err_msg, 0 TSRMLS_CC); - efree(err_msg); + zend_throw_exception(NULL, err_msg->val, 0 TSRMLS_CC); + zend_string_free(err_msg); } else { to_new->utimezone = newTimeZone; } @@ -263,8 +267,8 @@ static int TimeZone_compare_objects(zval *object1, zval *object2 TSRMLS_DC) { TimeZone_object *to1, *to2; - to1 = (TimeZone_object*)zend_object_store_get_object(object1 TSRMLS_CC); - to2 = (TimeZone_object*)zend_object_store_get_object(object2 TSRMLS_CC); + to1 = Z_INTL_TIMEZONE_P(object1); + to2 = Z_INTL_TIMEZONE_P(object2); if (to1->utimezone == NULL || to2->utimezone == NULL) { zend_throw_exception(NULL, "Comparison with at least one unconstructed " @@ -283,48 +287,56 @@ static int TimeZone_compare_objects(zval *object1, zval *object2 TSRMLS_DC) /* {{{ get_debug_info handler for TimeZone */ static HashTable *TimeZone_get_debug_info(zval *object, int *is_temp TSRMLS_DC) { - zval zv = zval_used_for_init; + zval zv; TimeZone_object *to; const TimeZone *tz; UnicodeString ustr; char *str; int str_len; + HashTable *debug_info; UErrorCode uec = U_ZERO_ERROR; *is_temp = 1; - array_init_size(&zv, 4); + ALLOC_HASHTABLE(debug_info); + zend_hash_init(debug_info, 8, NULL, ZVAL_PTR_DTOR, 0); - to = (TimeZone_object*)zend_object_store_get_object(object TSRMLS_CC); + to = Z_INTL_TIMEZONE_P(object); tz = to->utimezone; if (tz == NULL) { - add_assoc_bool_ex(&zv, "valid", sizeof("valid"), 0); - return Z_ARRVAL(zv); + ZVAL_FALSE(&zv); + zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv); + return debug_info; } - add_assoc_bool_ex(&zv, "valid", sizeof("valid"), 1); + ZVAL_TRUE(&zv); + zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv); tz->getID(ustr); intl_convert_utf16_to_utf8(&str, &str_len, ustr.getBuffer(), ustr.length(), &uec); if (U_FAILURE(uec)) { - return Z_ARRVAL(zv); + return debug_info; } - add_assoc_stringl_ex(&zv, "id", sizeof("id"), str, str_len, 0); + ZVAL_STRINGL(&zv, str, str_len); + zend_hash_str_update(debug_info, "id", sizeof("id") - 1, &zv); + // TODO: avoid reallocation ??? + efree(str); int32_t rawOffset, dstOffset; UDate now = Calendar::getNow(); tz->getOffset(now, FALSE, rawOffset, dstOffset, uec); if (U_FAILURE(uec)) { - return Z_ARRVAL(zv); + return debug_info; } - add_assoc_long_ex(&zv, "rawOffset", sizeof("rawOffset"), (long)rawOffset); - add_assoc_long_ex(&zv, "currentOffset", sizeof("currentOffset"), - (long)(rawOffset + dstOffset)); + ZVAL_LONG(&zv, (zend_long)rawOffset); + zend_hash_str_update(debug_info,"rawOffset", sizeof("rawOffset") - 1, &zv); + ZVAL_LONG(&zv, (zend_long)(rawOffset + dstOffset)); + zend_hash_str_update(debug_info,"currentOffset", sizeof("currentOffset") - 1, &zv); - return Z_ARRVAL(zv); + return debug_info; } /* }}} */ @@ -340,17 +352,16 @@ static void TimeZone_object_init(TimeZone_object *to TSRMLS_DC) /* }}} */ /* {{{ TimeZone_objects_dtor */ -static void TimeZone_objects_dtor(zend_object *object, - zend_object_handle handle TSRMLS_DC) +static void TimeZone_objects_dtor(zend_object *object TSRMLS_DC) { - zend_objects_destroy_object(object, handle TSRMLS_CC); + zend_objects_destroy_object(object TSRMLS_CC); } /* }}} */ /* {{{ TimeZone_objects_free */ static void TimeZone_objects_free(zend_object *object TSRMLS_DC) { - TimeZone_object* to = (TimeZone_object*) object; + TimeZone_object* to = php_intl_timezone_fetch_object(object); if (to->utimezone && to->should_delete) { delete to->utimezone; @@ -359,37 +370,23 @@ static void TimeZone_objects_free(zend_object *object TSRMLS_DC) intl_error_reset(TIMEZONE_ERROR_P(to) TSRMLS_CC); zend_object_std_dtor(&to->zo TSRMLS_CC); - - efree(to); } /* }}} */ /* {{{ TimeZone_object_create */ -static zend_object_value TimeZone_object_create(zend_class_entry *ce TSRMLS_DC) +static zend_object *TimeZone_object_create(zend_class_entry *ce TSRMLS_DC) { - zend_object_value retval; TimeZone_object* intern; - intern = (TimeZone_object*)ecalloc(1, sizeof(TimeZone_object)); + intern = (TimeZone_object*)ecalloc(1, sizeof(TimeZone_object) + sizeof(zval) * (ce->default_properties_count - 1)); zend_object_std_init(&intern->zo, ce TSRMLS_CC); -#if PHP_VERSION_ID < 50399 - zend_hash_copy(intern->zo.properties, &(ce->default_properties), - (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*)); -#else - object_properties_init((zend_object*) intern, ce); -#endif + object_properties_init(&intern->zo, ce); TimeZone_object_init(intern TSRMLS_CC); - retval.handle = zend_objects_store_put( - intern, - (zend_objects_store_dtor_t) TimeZone_objects_dtor, - (zend_objects_free_object_storage_t) TimeZone_objects_free, - NULL TSRMLS_CC); - - retval.handlers = &TimeZone_handlers; + intern->zo.handlers = &TimeZone_handlers; - return retval; + return &intern->zo; } /* }}} */ @@ -507,9 +504,13 @@ U_CFUNC void timezone_register_IntlTimeZone_class(TSRMLS_D) memcpy(&TimeZone_handlers, zend_get_std_object_handlers(), sizeof TimeZone_handlers); + TimeZone_handlers.offset = XtOffsetOf(TimeZone_object, zo); TimeZone_handlers.clone_obj = TimeZone_clone_obj; TimeZone_handlers.compare_objects = TimeZone_compare_objects; TimeZone_handlers.get_debug_info = TimeZone_get_debug_info; + TimeZone_handlers.dtor_obj = TimeZone_objects_dtor; + TimeZone_handlers.free_obj = TimeZone_objects_free; + /* Declare 'IntlTimeZone' class constants */ #define TIMEZONE_DECL_LONG_CONST(name, val) \ |