-
Notifications
You must be signed in to change notification settings - Fork 1
/
aspect.c
242 lines (196 loc) · 7.33 KB
/
aspect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* aspect extension for PHP */
#ifdef HAVE_CONFIG_H
# include <config.h>
# include <zend_smart_str.h>
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "ext/standard/php_standard.h"
#include "php_aspect.h"
#include "aspect_arginfo.h"
#include "zend_attributes.h"
#include "zend_exceptions.h"
/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
ZEND_PARSE_PARAMETERS_START(0, 0) \
ZEND_PARSE_PARAMETERS_END()
#endif
ZEND_API zend_class_entry *zend_ce_memoize;
ZEND_DECLARE_MODULE_GLOBALS(aspect)
static void (*original_zend_execute_ex)(zend_execute_data *execute_data);
ZEND_API static void aspect_execute_ex(zend_execute_data *execute_data);
static void handle_memoize_functions(zend_execute_data *execute_data);
static zend_string *compute_cache_key(zend_execute_data *execute_data) {
smart_str key = {0};
zend_function *func = execute_data->func;
// Check if it's a method call
if (func->common.scope) {
// Check if it's a static method call
if (func->common.fn_flags & ZEND_ACC_STATIC) {
// Append fully qualified class name
smart_str_appends(&key, ZSTR_VAL(func->common.scope->name));
} else {
// Append object handle
smart_str_append_long(&key, (long) Z_OBJ_HANDLE(execute_data->This));
}
// Append method name
smart_str_appends(&key, "::");
smart_str_appends(&key, ZSTR_VAL(func->common.function_name));
} else {
// Append function name
smart_str_appends(&key, ZSTR_VAL(func->common.function_name));
}
// Serialize arguments
zval *params = ZEND_CALL_ARG(execute_data, 1);
for (uint32_t i = 0; i < ZEND_CALL_NUM_ARGS(execute_data); i++) {
switch (Z_TYPE(params[i])) {
case IS_OBJECT: {
// Use object handle (unique for each object instance)
smart_str_append_long(&key, (long) Z_OBJ_HANDLE(params[i]));
break;
}
case IS_CALLABLE: {
// Serialize callable as string
smart_str_appendc(&key, ':');
zend_string *callable_str = zval_get_string(¶ms[i]);
smart_str_appends(&key, ZSTR_VAL(callable_str));
zend_string_release(callable_str);
break;
}
case IS_RESOURCE: {
// Serialize resource as string
smart_str_appendc(&key, ':');
smart_str_append_long(&key, Z_RES_HANDLE(params[i]));
break;
}
default: {
// Serialize other types as usual
smart_str_appendc(&key, ':');
php_serialize_data_t var_hash;
PHP_VAR_SERIALIZE_INIT(var_hash);
php_var_serialize(&key, ¶ms[i], &var_hash);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
break;
}
}
}
smart_str_0(&key);
return key.s;
}
static void aspect_execute_ex(zend_execute_data *execute_data) {
zend_function *func = execute_data->func;
// Check if function name is set
if (!func->common.function_name) {
original_zend_execute_ex(execute_data);
return;
}
if (func->common.attributes && zend_get_attribute_str(func->common.attributes, "memoize", sizeof("memoize") - 1)) {
if (func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
if (func->common.arg_info != NULL) {
zend_arg_info *ret_info = func->common.arg_info - 1;
if (ret_info != NULL) {
zend_type return_type = ret_info->type;
uint32_t type_mask = return_type.type_mask;
if (type_mask & ((uint32_t)1 << IS_VOID) || type_mask & ((uint32_t)1 << IS_NEVER)) {
// cannot memoize functions with void or never return types
// @TODO: Should this be an error/exception?
original_zend_execute_ex(execute_data);
return;
}
}
}
}
handle_memoize_functions(execute_data);
return;
}
// Default execution for non-memoized functions
original_zend_execute_ex(execute_data);
}
static void handle_memoize_functions(zend_execute_data *execute_data) {
zend_string *cache_key = compute_cache_key(execute_data);
zval *cached_value = zend_hash_find(&ASPECT_G(memoize_cache), cache_key);
if (cached_value) {
// Return the cached value
if (EX(return_value)) {
ZVAL_COPY(EX(return_value), cached_value);
}
zend_string_release(cache_key);
return;
}
zval temp_return_value, *actual_return_value;
if (EX(return_value)) {
actual_return_value = EX(return_value);
} else {
actual_return_value = &temp_return_value;
}
// Call the original function
original_zend_execute_ex(execute_data);
if (EG(exception) || EG(exit_status)) {
zend_string_release(cache_key);
return;
}
if (Z_TYPE_P(actual_return_value) == IS_UNDEF) {
zend_string_release(cache_key);
return;
}
// Cache the result if no exception or exit occurred
zval cache_copy;
ZVAL_DUP(&cache_copy, actual_return_value);
if (zend_hash_add(&ASPECT_G(memoize_cache), cache_key, &cache_copy) == NULL) {
php_error_docref(NULL, E_WARNING, "Failed to add cache entry");
zval_ptr_dtor(&cache_copy); // Clean up if adding fails
zend_string_release(cache_key);
return;
}
zend_string_release(cache_key);
}
PHP_MINIT_FUNCTION (aspect) {
// Initialize the memoize cache
zend_hash_init(&ASPECT_G(memoize_cache), 8, NULL, ZVAL_PTR_DTOR, 1);
original_zend_execute_ex = zend_execute_ex;
zend_execute_ex = aspect_execute_ex;
// Register the Memoize attribute
zend_ce_memoize = register_class_Memoize();
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION (aspect) {
// Destroy the memoize cache
zend_hash_destroy(&ASPECT_G(memoize_cache));
zend_execute_ex = original_zend_execute_ex;
return SUCCESS;
}
PHP_RINIT_FUNCTION (aspect) {
#if defined(ZTS) && defined(COMPILE_DL_ASPECT)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION (aspect) {
zend_hash_clean(&ASPECT_G(memoize_cache));
return SUCCESS;
}
PHP_MINFO_FUNCTION (aspect) {
php_info_print_table_start();
php_info_print_table_row(2, "aspect support", "enabled");
php_info_print_table_row(2, "Version", PHP_ASPECT_VERSION);
php_info_print_table_end();
}
zend_module_entry aspect_module_entry = {
STANDARD_MODULE_HEADER,
"aspect", /* Extension name */
NULL, /* zend_function_entry */
PHP_MINIT(aspect), /* PHP_MINIT - Module initialization */
PHP_MSHUTDOWN(aspect), /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(aspect), /* PHP_RINIT - Request initialization */
PHP_RSHUTDOWN(aspect), /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(aspect), /* PHP_MINFO - Module info */
PHP_ASPECT_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_ASPECT
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(aspect)
#endif