Skip to content

Commit

Permalink
Require visitors to provide a name
Browse files Browse the repository at this point in the history
  • Loading branch information
jbboehr committed Feb 10, 2024
1 parent cac6a8e commit 6ba5f9c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
10 changes: 5 additions & 5 deletions php_vyrtue.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ typedef zend_ast *(*vyrtue_ast_callback)(zend_ast *ast, struct vyrtue_preprocess

ZEND_BEGIN_MODULE_GLOBALS(vyrtue)
HashTable attribute_visitors;
HashTable kind_hooks;
HashTable function_hooks;
HashTable function_visitors;
HashTable kind_visitors;
ZEND_END_MODULE_GLOBALS(vyrtue)

ZEND_EXTERN_MODULE_GLOBALS(vyrtue);
Expand All @@ -92,13 +92,13 @@ VYRTUE_ATTR_NONNULL_ALL
void vyrtue_ast_process_file(zend_ast *ast);

VYRTUE_PUBLIC
void vyrtue_register_attribute_visitor(zend_string *attribute_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave);
void vyrtue_register_attribute_visitor(const char *visitor_name, zend_string *attribute_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave);

VYRTUE_PUBLIC
void vyrtue_register_function_visitor(zend_string *function_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave);
void vyrtue_register_function_visitor(const char *visitor_name, zend_string *function_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave);

VYRTUE_PUBLIC
void vyrtue_register_kind_visitor(enum _zend_ast_kind kind, vyrtue_ast_callback enter, vyrtue_ast_callback leave);
void vyrtue_register_kind_visitor(const char *visitor_name, enum _zend_ast_kind kind, vyrtue_ast_callback enter, vyrtue_ast_callback leave);

VYRTUE_PUBLIC
VYRTUE_ATTR_NONNULL_ALL
Expand Down
6 changes: 3 additions & 3 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ VYRTUE_LOCAL PHP_MINIT_FUNCTION(vyrtue_debug)
zend_string *tmp;

tmp = zend_string_init_interned(ZEND_STRL("VyrtueExt\\Debug\\sample_replacement_function"), 1);
vyrtue_register_function_visitor(tmp, vyrtue_debug_sample_replacement_enter, vyrtue_debug_sample_replacement_leave);
vyrtue_register_function_visitor("vyrtue internal debug", tmp, vyrtue_debug_sample_replacement_enter, vyrtue_debug_sample_replacement_leave);
zend_string_release(tmp);

tmp = zend_string_init_interned(ZEND_STRL("VyrtueExt\\Debug\\sample_function"), 1);
vyrtue_register_function_visitor(tmp, vyrtue_debug_sample_function_enter, vyrtue_debug_sample_function_leave);
vyrtue_register_function_visitor("vyrtue internal debug", tmp, vyrtue_debug_sample_function_enter, vyrtue_debug_sample_function_leave);
zend_string_release(tmp);

tmp = zend_string_init_interned(ZEND_STRL("VyrtueExt\\Debug\\SampleAttribute"), 1);
vyrtue_register_attribute_visitor(tmp, vyrtue_debug_sample_attribute_enter, vyrtue_debug_sample_attribute_leave);
vyrtue_register_attribute_visitor("vyrtue internal debug", tmp, vyrtue_debug_sample_attribute_enter, vyrtue_debug_sample_attribute_leave);
zend_string_release(tmp);

return SUCCESS;
Expand Down
43 changes: 39 additions & 4 deletions src/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,43 @@ static PHP_MINFO_FUNCTION(vyrtue)
php_info_print_table_row(2, "Version", PHP_VYRTUE_VERSION);
php_info_print_table_row(2, "Released", PHP_VYRTUE_RELEASE);
php_info_print_table_row(2, "Authors", PHP_VYRTUE_AUTHORS);
php_info_print_table_end();

DISPLAY_INI_ENTRIES();

php_info_print_table_start();

const struct vyrtue_visitor_array *visitors;
zend_ulong num_key;
zend_string *str_key;
char buffer[1000];

ZEND_HASH_FOREACH_NUM_KEY_PTR(&VYRTUE_G(kind_visitors), num_key, visitors)
{
for (size_t i = 0; i < visitors->length; i++) {
snprintf(buffer, sizeof(buffer) - 1, "%lu", num_key);
php_info_print_table_row(2, buffer, visitors->data[i].name);
}
}
ZEND_HASH_FOREACH_END();

ZEND_HASH_FOREACH_STR_KEY_PTR(&VYRTUE_G(function_visitors), str_key, visitors)
{
for (size_t i = 0; i < visitors->length; i++) {
php_info_print_table_row(2, ZSTR_VAL(str_key), visitors->data[i].name);
}
}
ZEND_HASH_FOREACH_END();

ZEND_HASH_FOREACH_STR_KEY_PTR(&VYRTUE_G(attribute_visitors), str_key, visitors)
{
for (size_t i = 0; i < visitors->length; i++) {
php_info_print_table_row(2, ZSTR_VAL(str_key), visitors->data[i].name);
}
}
ZEND_HASH_FOREACH_END();

php_info_print_table_end();
}

static PHP_GINIT_FUNCTION(vyrtue)
Expand All @@ -112,15 +147,15 @@ static PHP_GINIT_FUNCTION(vyrtue)
#endif
memset(vyrtue_globals, 0, sizeof(zend_vyrtue_globals));
zend_hash_init(&vyrtue_globals->attribute_visitors, 16, NULL, NULL, 1);
zend_hash_init(&vyrtue_globals->function_hooks, 16, NULL, NULL, 1);
zend_hash_init(&vyrtue_globals->kind_hooks, 16, NULL, NULL, 1);
zend_hash_init(&vyrtue_globals->function_visitors, 16, NULL, NULL, 1);
zend_hash_init(&vyrtue_globals->kind_visitors, 16, NULL, NULL, 1);
}

static PHP_GSHUTDOWN_FUNCTION(vyrtue)
{
zend_hash_destroy(&vyrtue_globals->attribute_visitors);
zend_hash_destroy(&vyrtue_globals->function_hooks);
zend_hash_destroy(&vyrtue_globals->kind_hooks);
zend_hash_destroy(&vyrtue_globals->function_visitors);
zend_hash_destroy(&vyrtue_globals->kind_visitors);
}

const zend_function_entry vyrtue_functions[] = {
Expand Down
10 changes: 5 additions & 5 deletions src/preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,11 @@ void vyrtue_ast_process_file(zend_ast *ast)
VYRTUE_LOCAL
PHP_MINIT_FUNCTION(vyrtue_preprocess)
{
vyrtue_register_kind_visitor(ZEND_AST_USE, vyrtue_ast_process_use_enter, NULL);
vyrtue_register_kind_visitor(ZEND_AST_GROUP_USE, vyrtue_ast_process_group_use_enter, vyrtue_ast_process_group_use_leave);
vyrtue_register_kind_visitor(ZEND_AST_NAMESPACE, vyrtue_ast_process_namespace_enter, vyrtue_ast_process_namespace_leave);
vyrtue_register_kind_visitor(ZEND_AST_CALL, vyrtue_ast_process_call_enter, vyrtue_ast_process_call_leave);
vyrtue_register_kind_visitor(ZEND_AST_CLASS, vyrtue_ast_process_class_enter, vyrtue_ast_process_class_leave);
vyrtue_register_kind_visitor("vyrtue internal", ZEND_AST_USE, vyrtue_ast_process_use_enter, NULL);
vyrtue_register_kind_visitor("vyrtue internal", ZEND_AST_GROUP_USE, vyrtue_ast_process_group_use_enter, vyrtue_ast_process_group_use_leave);
vyrtue_register_kind_visitor("vyrtue internal", ZEND_AST_NAMESPACE, vyrtue_ast_process_namespace_enter, vyrtue_ast_process_namespace_leave);
vyrtue_register_kind_visitor("vyrtue internal", ZEND_AST_CALL, vyrtue_ast_process_call_enter, vyrtue_ast_process_call_leave);
vyrtue_register_kind_visitor("vyrtue internal", ZEND_AST_CLASS, vyrtue_ast_process_class_enter, vyrtue_ast_process_class_leave);

return SUCCESS;
}
1 change: 1 addition & 0 deletions src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct vyrtue_preprocess_context

struct vyrtue_visitor
{
const char *name;
vyrtue_ast_callback enter;
vyrtue_ast_callback leave;
};
Expand Down
21 changes: 12 additions & 9 deletions src/visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ static const struct vyrtue_visitor_array EMPTY_VISITOR_ARRAY = {
};

VYRTUE_PUBLIC
void vyrtue_register_attribute_visitor(zend_string *attribute_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
void vyrtue_register_attribute_visitor(const char *visitor_name, zend_string *attribute_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
{
struct vyrtue_visitor visitor = {
.name = visitor_name,
.enter = enter,
.leave = leave,
};
Expand All @@ -64,14 +65,15 @@ void vyrtue_register_attribute_visitor(zend_string *attribute_name, vyrtue_ast_c
}

VYRTUE_PUBLIC
void vyrtue_register_function_visitor(zend_string *function_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
void vyrtue_register_function_visitor(const char *visitor_name, zend_string *function_name, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
{
struct vyrtue_visitor visitor = {
.name = visitor_name,
.enter = enter,
.leave = leave,
};

struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(function_hooks), function_name);
struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(function_visitors), function_name);
if (!arr) {
size_t size = sizeof(*arr) + sizeof(arr->data[0]);
arr = pemalloc(size, 1);
Expand All @@ -86,18 +88,19 @@ void vyrtue_register_function_visitor(zend_string *function_name, vyrtue_ast_cal
arr->data[arr->length] = visitor;
arr->length++;

zend_hash_update_ptr(&VYRTUE_G(function_hooks), function_name, arr);
zend_hash_update_ptr(&VYRTUE_G(function_visitors), function_name, arr);
}

VYRTUE_PUBLIC
void vyrtue_register_kind_visitor(enum _zend_ast_kind kind, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
void vyrtue_register_kind_visitor(const char *visitor_name, enum _zend_ast_kind kind, vyrtue_ast_callback enter, vyrtue_ast_callback leave)
{
struct vyrtue_visitor visitor = {
.name = visitor_name,
.enter = enter,
.leave = leave,
};

struct vyrtue_visitor_array *arr = zend_hash_index_find_ptr(&VYRTUE_G(kind_hooks), (zend_ulong) kind);
struct vyrtue_visitor_array *arr = zend_hash_index_find_ptr(&VYRTUE_G(kind_visitors), (zend_ulong) kind);
if (!arr) {
size_t size = sizeof(*arr) + sizeof(arr->data[0]);
arr = pemalloc(size, 1);
Expand All @@ -112,7 +115,7 @@ void vyrtue_register_kind_visitor(enum _zend_ast_kind kind, vyrtue_ast_callback
arr->data[arr->length] = visitor;
arr->length++;

zend_hash_index_update_ptr(&VYRTUE_G(kind_hooks), (zend_ulong) kind, arr);
zend_hash_index_update_ptr(&VYRTUE_G(kind_visitors), (zend_ulong) kind, arr);
}

VYRTUE_PUBLIC
Expand All @@ -135,7 +138,7 @@ VYRTUE_ATTR_RETURNS_NONNULL
VYRTUE_ATTR_WARN_UNUSED_RESULT
const struct vyrtue_visitor_array *vyrtue_get_function_visitors(zend_string *function_name)
{
const struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(function_hooks), function_name);
const struct vyrtue_visitor_array *arr = zend_hash_find_ptr(&VYRTUE_G(function_visitors), function_name);
if (arr) {
return arr;
} else {
Expand All @@ -149,7 +152,7 @@ VYRTUE_ATTR_RETURNS_NONNULL
VYRTUE_ATTR_WARN_UNUSED_RESULT
const struct vyrtue_visitor_array *vyrtue_get_kind_visitors(enum _zend_ast_kind kind)
{
const struct vyrtue_visitor_array *arr = zend_hash_index_find_ptr(&VYRTUE_G(kind_hooks), (zend_ulong) kind);
const struct vyrtue_visitor_array *arr = zend_hash_index_find_ptr(&VYRTUE_G(kind_visitors), (zend_ulong) kind);
if (arr) {
return arr;
} else {
Expand Down

0 comments on commit 6ba5f9c

Please sign in to comment.