Skip to content

Add C API for getting backed enum case by value #8518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
28 changes: 28 additions & 0 deletions Zend/tests/enum/internal_enums.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ var_dump($s = serialize($foo));
var_dump(unserialize($s));
var_dump(unserialize($s) === $foo);

function test_int_enum(int|string $case) {
try {
var_dump(ZendTestIntEnum::from($case));
} catch (\Error $e) {
echo get_class($e) . ': ' . $e->getMessage() . "\n";
}
var_dump(ZendTestIntEnum::tryFrom($case));
}

test_int_enum(1);
test_int_enum('1');
test_int_enum(2);
test_int_enum('2');
test_int_enum(-1);
test_int_enum('-1');

?>
--EXPECT--
enum(ZendTestUnitEnum::Bar)
Expand Down Expand Up @@ -66,3 +82,15 @@ array(4) {
string(30) "E:22:"ZendTestStringEnum:Foo";"
enum(ZendTestStringEnum::Foo)
bool(true)
enum(ZendTestIntEnum::Foo)
enum(ZendTestIntEnum::Foo)
enum(ZendTestIntEnum::Foo)
enum(ZendTestIntEnum::Foo)
ValueError: 2 is not a valid backing value for enum "ZendTestIntEnum"
NULL
ValueError: 2 is not a valid backing value for enum "ZendTestIntEnum"
NULL
enum(ZendTestIntEnum::Baz)
enum(ZendTestIntEnum::Baz)
enum(ZendTestIntEnum::Baz)
enum(ZendTestIntEnum::Baz)
80 changes: 51 additions & 29 deletions Zend/zend_enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,59 @@ static ZEND_NAMED_FUNCTION(zend_enum_cases_func)
} ZEND_HASH_FOREACH_END();
}

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)
{
zval *case_name_zv;
if (ce->enum_backing_type == IS_LONG) {
case_name_zv = zend_hash_index_find(ce->backed_enum_table, long_key);
} else {
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);
ZEND_ASSERT(string_key != NULL);
case_name_zv = zend_hash_find(ce->backed_enum_table, string_key);
}

if (case_name_zv == NULL) {
if (try) {
*result = NULL;
return SUCCESS;
}

if (ce->enum_backing_type == IS_LONG) {
zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", long_key, ZSTR_VAL(ce->name));
} else {
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);
zend_value_error("\"%s\" is not a valid backing value for enum \"%s\"", ZSTR_VAL(string_key), ZSTR_VAL(ce->name));
}
return FAILURE;
}

// TODO: We might want to store pointers to constants in backed_enum_table instead of names,
// to make this lookup more efficient.
ZEND_ASSERT(Z_TYPE_P(case_name_zv) == IS_STRING);
zend_class_constant *c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), Z_STR_P(case_name_zv));
ZEND_ASSERT(c != NULL);
zval *case_zv = &c->value;
if (Z_TYPE_P(case_zv) == IS_CONSTANT_AST) {
if (zval_update_constant_ex(case_zv, c->ce) == FAILURE) {
return FAILURE;
}
}

*result = Z_OBJ_P(case_zv);
return SUCCESS;
}

static void zend_enum_from_base(INTERNAL_FUNCTION_PARAMETERS, bool try)
{
zend_class_entry *ce = execute_data->func->common.scope;
bool release_string = false;
zend_string *string_key;
zend_long long_key;
zend_string *string_key = NULL;
zend_long long_key = 0;

zval *case_name_zv;
if (ce->enum_backing_type == IS_LONG) {
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(long_key)
ZEND_PARSE_PARAMETERS_END();

case_name_zv = zend_hash_index_find(ce->backed_enum_table, long_key);
} else {
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);

Expand All @@ -242,40 +281,23 @@ static void zend_enum_from_base(INTERNAL_FUNCTION_PARAMETERS, bool try)
string_key = zend_long_to_str(long_key);
}
}

case_name_zv = zend_hash_find(ce->backed_enum_table, string_key);
}

if (case_name_zv == NULL) {
if (try) {
goto return_null;
}

if (ce->enum_backing_type == IS_LONG) {
zend_value_error(ZEND_LONG_FMT " is not a valid backing value for enum \"%s\"", long_key, ZSTR_VAL(ce->name));
} else {
ZEND_ASSERT(ce->enum_backing_type == IS_STRING);
zend_value_error("\"%s\" is not a valid backing value for enum \"%s\"", ZSTR_VAL(string_key), ZSTR_VAL(ce->name));
}
zend_object *case_obj;
if (zend_enum_get_case_by_value(&case_obj, ce, long_key, string_key, try) == FAILURE) {
goto throw;
}

// TODO: We might want to store pointers to constants in backed_enum_table instead of names,
// to make this lookup more efficient.
ZEND_ASSERT(Z_TYPE_P(case_name_zv) == IS_STRING);
zend_class_constant *c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(ce), Z_STR_P(case_name_zv));
ZEND_ASSERT(c != NULL);
zval *case_zv = &c->value;
if (Z_TYPE_P(case_zv) == IS_CONSTANT_AST) {
if (zval_update_constant_ex(case_zv, c->ce) == FAILURE) {
goto throw;
}
if (case_obj == NULL) {
ZEND_ASSERT(try);
goto return_null;
}

if (release_string) {
zend_string_release(string_key);
}
RETURN_COPY(case_zv);
ZVAL_OBJ_COPY(return_value, case_obj);
return;

throw:
if (release_string) {
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ZEND_API void zend_enum_add_case(zend_class_entry *ce, zend_string *case_name, z
ZEND_API void zend_enum_add_case_cstr(zend_class_entry *ce, const char *name, zval *value);
ZEND_API zend_object *zend_enum_get_case(zend_class_entry *ce, zend_string *name);
ZEND_API zend_object *zend_enum_get_case_cstr(zend_class_entry *ce, const char *name);
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);

static zend_always_inline zval *zend_enum_fetch_case_name(zend_object *zobj)
{
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static zend_class_entry *zend_test_ns2_foo_class;
static zend_class_entry *zend_test_ns2_ns_foo_class;
static zend_class_entry *zend_test_unit_enum;
static zend_class_entry *zend_test_string_enum;
static zend_class_entry *zend_test_int_enum;
static zend_object_handlers zend_test_class_handlers;

static ZEND_FUNCTION(zend_test_func)
Expand Down Expand Up @@ -589,6 +590,7 @@ PHP_MINIT_FUNCTION(zend_test)

zend_test_unit_enum = register_class_ZendTestUnitEnum();
zend_test_string_enum = register_class_ZendTestStringEnum();
zend_test_int_enum = register_class_ZendTestIntEnum();

// Loading via dl() not supported with the observer API
if (type != MODULE_TEMPORARY) {
Expand Down
6 changes: 6 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ enum ZendTestStringEnum: string {
case FortyTwo = "42";
}

enum ZendTestIntEnum: int {
case Foo = 1;
case Bar = 3;
case Baz = -1;
}

function zend_test_array_return(): array {}

function zend_test_nullable_array_return(): ?array {}
Expand Down
26 changes: 25 additions & 1 deletion ext/zend_test/test_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 7a0436c43deff3960c15c278644021a039c4946f */
* Stub hash: e498f52a68debccd85bf1c086d05563174130e62 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -236,6 +236,11 @@ static const zend_function_entry class_ZendTestStringEnum_methods[] = {
};


static const zend_function_entry class_ZendTestIntEnum_methods[] = {
ZEND_FE_END
};


static const zend_function_entry class_ZendTestNS_Foo_methods[] = {
ZEND_ME(ZendTestNS_Foo, method, arginfo_class_ZendTestNS_Foo_method, ZEND_ACC_PUBLIC)
ZEND_FE_END
Expand Down Expand Up @@ -432,6 +437,25 @@ static zend_class_entry *register_class_ZendTestStringEnum(void)
return class_entry;
}

static zend_class_entry *register_class_ZendTestIntEnum(void)
{
zend_class_entry *class_entry = zend_register_internal_enum("ZendTestIntEnum", IS_LONG, class_ZendTestIntEnum_methods);

zval enum_case_Foo_value;
ZVAL_LONG(&enum_case_Foo_value, 1);
zend_enum_add_case_cstr(class_entry, "Foo", &enum_case_Foo_value);

zval enum_case_Bar_value;
ZVAL_LONG(&enum_case_Bar_value, 3);
zend_enum_add_case_cstr(class_entry, "Bar", &enum_case_Bar_value);

zval enum_case_Baz_value;
ZVAL_LONG(&enum_case_Baz_value, -1);
zend_enum_add_case_cstr(class_entry, "Baz", &enum_case_Baz_value);

return class_entry;
}

static zend_class_entry *register_class_ZendTestNS_Foo(void)
{
zend_class_entry ce, *class_entry;
Expand Down