Skip to content

Commit c663e51

Browse files
committed
Improve zend_enum_get_case_by_value() API
1 parent 22220db commit c663e51

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

Zend/zend_enum.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static ZEND_NAMED_FUNCTION(zend_enum_cases_func)
207207
} ZEND_HASH_FOREACH_END();
208208
}
209209

210-
ZEND_API zend_object *zend_enum_get_case_by_value(zend_class_entry *ce, zend_long long_key, zend_string *string_key)
210+
ZEND_API zend_result zend_enum_get_case_by_value(zend_object **result, zend_class_entry *ce, zend_long long_key, zend_string *string_key, bool try)
211211
{
212212
zval *case_name_zv;
213213
if (ce->enum_backing_type == IS_LONG) {
@@ -219,7 +219,18 @@ ZEND_API zend_object *zend_enum_get_case_by_value(zend_class_entry *ce, zend_lon
219219
}
220220

221221
if (case_name_zv == NULL) {
222-
return NULL;
222+
if (try) {
223+
*result = NULL;
224+
return SUCCESS;
225+
}
226+
227+
if (ce->enum_backing_type == IS_LONG) {
228+
zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", long_key, ZSTR_VAL(ce->name));
229+
} else {
230+
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);
231+
zend_value_error("\"%s\" is not a valid backing value for enum \"%s\"", ZSTR_VAL(string_key), ZSTR_VAL(ce->name));
232+
}
233+
return FAILURE;
223234
}
224235

225236
// TODO: We might want to store pointers to constants in backed_enum_table instead of names,
@@ -230,11 +241,12 @@ ZEND_API zend_object *zend_enum_get_case_by_value(zend_class_entry *ce, zend_lon
230241
zval *case_zv = &c->value;
231242
if (Z_TYPE_P(case_zv) == IS_CONSTANT_AST) {
232243
if (zval_update_constant_ex(case_zv, c->ce) == FAILURE) {
233-
return NULL;
244+
return FAILURE;
234245
}
235246
}
236247

237-
return Z_OBJ_P(case_zv);
248+
*result = Z_OBJ_P(case_zv);
249+
return SUCCESS;
238250
}
239251

240252
static void zend_enum_from_base(INTERNAL_FUNCTION_PARAMETERS, bool try)
@@ -255,24 +267,14 @@ static void zend_enum_from_base(INTERNAL_FUNCTION_PARAMETERS, bool try)
255267
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);
256268
}
257269

258-
zend_object *case_obj = zend_enum_get_case_by_value(ce, long_key, string_key);
259-
// Updating of constants can fail
260-
if (EG(exception)) {
270+
zend_object *case_obj;
271+
if (zend_enum_get_case_by_value(&case_obj, ce, long_key, string_key, try) == FAILURE) {
261272
RETURN_THROWS();
262273
}
263274

264275
if (case_obj == NULL) {
265-
if (try) {
266-
RETURN_NULL();
267-
}
268-
269-
if (ce->enum_backing_type == IS_LONG) {
270-
zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", long_key, ZSTR_VAL(ce->name));
271-
} else {
272-
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);
273-
zend_value_error("\"%s\" is not a valid backing value for enum \"%s\"", ZSTR_VAL(string_key), ZSTR_VAL(ce->name));
274-
}
275-
RETURN_THROWS();
276+
ZEND_ASSERT(try);
277+
RETURN_NULL();
276278
}
277279

278280
ZVAL_OBJ_COPY(return_value, case_obj);

Zend/zend_enum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ZEND_API void zend_enum_add_case(zend_class_entry *ce, zend_string *case_name, z
4040
ZEND_API void zend_enum_add_case_cstr(zend_class_entry *ce, const char *name, zval *value);
4141
ZEND_API zend_object *zend_enum_get_case(zend_class_entry *ce, zend_string *name);
4242
ZEND_API zend_object *zend_enum_get_case_cstr(zend_class_entry *ce, const char *name);
43-
ZEND_API zend_object *zend_enum_get_case_by_value(zend_class_entry *ce, zend_long long_key, zend_string *string_key);
43+
ZEND_API zend_result zend_enum_get_case_by_value(zend_object **result, zend_class_entry *ce, zend_long long_key, zend_string *string_key, bool try);
4444

4545
static zend_always_inline zval *zend_enum_fetch_case_name(zend_object *zobj)
4646
{

0 commit comments

Comments
 (0)