Skip to content

Commit c8cf129

Browse files
committed
[BROKEN] Experimenting with immutable arrays
1 parent 6df7557 commit c8cf129

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

ext/mbstring/mbstring.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,20 @@ ZEND_TSRMLS_CACHE_UPDATE();
10161016
mbstring_globals->internal_encoding_set = 0;
10171017
mbstring_globals->http_output_set = 0;
10181018
mbstring_globals->http_input_set = 0;
1019+
1020+
/* Initialize immutable array of supported encoding names
1021+
* This is done so that we can check if `mb_list_encodings()` is being
1022+
* passed to other mbstring functions using a cheap pointer equality check */
1023+
HashTable *array = malloc(sizeof(HashTable));
1024+
zend_hash_init(array, 80, NULL, NULL, true);
1025+
GC_ADD_FLAGS(array, IS_ARRAY_IMMUTABLE);
1026+
for (const mbfl_encoding **encodings = mbfl_get_supported_encodings(); *encodings; encodings++) {
1027+
zval tmp;
1028+
ZVAL_PSTRING(&tmp, (*encodings)->name);
1029+
zend_hash_next_index_insert(array, &tmp);
1030+
}
1031+
GC_SET_REFCOUNT(array, 2);
1032+
mbstring_globals->all_encodings_list = array;
10191033
}
10201034
/* }}} */
10211035

@@ -1031,6 +1045,8 @@ static PHP_GSHUTDOWN_FUNCTION(mbstring)
10311045
if (mbstring_globals->http_output_conv_mimetypes) {
10321046
_php_mb_free_regex(mbstring_globals->http_output_conv_mimetypes);
10331047
}
1048+
GC_DELREF(mbstring_globals->all_encodings_list);
1049+
zend_hash_destroy(mbstring_globals->all_encodings_list);
10341050
#ifdef HAVE_MBREGEX
10351051
php_mb_regex_globals_free(mbstring_globals->mb_regex_globals);
10361052
#endif
@@ -3205,10 +3221,7 @@ PHP_FUNCTION(mb_list_encodings)
32053221
{
32063222
ZEND_PARSE_PARAMETERS_NONE();
32073223

3208-
array_init(return_value);
3209-
for (const mbfl_encoding **encodings = mbfl_get_supported_encodings(); *encodings; encodings++) {
3210-
add_next_index_string(return_value, (*encodings)->name);
3211-
}
3224+
RETURN_ARR(MBSTRG(all_encodings_list));
32123225
}
32133226
/* }}} */
32143227

ext/mbstring/mbstring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ ZEND_BEGIN_MODULE_GLOBALS(mbstring)
8888
size_t current_detect_order_list_size;
8989
enum mbfl_no_encoding *default_detect_order_list;
9090
size_t default_detect_order_list_size;
91+
HashTable *all_encodings_list;
9192
int filter_illegal_mode;
9293
uint32_t filter_illegal_substchar;
9394
int current_filter_illegal_mode;

0 commit comments

Comments
 (0)