-
Notifications
You must be signed in to change notification settings - Fork 136
/
runkit_classes.c
342 lines (283 loc) · 12 KB
/
runkit_classes.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group, (c) 2008-2015 Dmitry Zenovich |
+----------------------------------------------------------------------+
| This source file is subject to the new BSD license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.opensource.org/licenses/BSD-3-Clause |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| dzenovich@gmail.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <pollita@php.net> |
| Modified by Dmitry Zenovich <dzenovich@gmail.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php_runkit.h"
#include "php_runkit_zval.h"
#ifdef PHP_RUNKIT_MANIPULATION
/* {{{ php_runkit_remove_inherited_methods */
static int php_runkit_remove_inherited_methods(zend_function *fe, zend_class_entry *ce TSRMLS_DC)
{
const char *fname = fe->common.function_name;
int fname_len = strlen(fname);
PHP_RUNKIT_DECL_STRING_PARAM(fname_lower)
zend_class_entry *ancestor_class;
PHP_RUNKIT_MAKE_LOWERCASE_COPY(fname);
if (fname_lower == NULL) {
PHP_RUNKIT_NOT_ENOUGH_MEMORY_ERROR;
return ZEND_HASH_APPLY_KEEP;
}
ancestor_class = php_runkit_locate_scope(ce, fe, fname_lower, fname_lower_len);
if (ancestor_class == ce) {
efree(fname_lower);
return ZEND_HASH_APPLY_KEEP;
}
zend_hash_apply_with_arguments(RUNKIT_53_TSRMLS_PARAM(EG(class_table)), (apply_func_args_t)php_runkit_clean_children_methods, 5,
ancestor_class, ce, fname_lower, fname_lower_len, fe);
PHP_RUNKIT_DEL_MAGIC_METHOD(ce, fe TSRMLS_CC);
php_runkit_remove_function_from_reflection_objects(fe TSRMLS_CC);
efree(fname_lower);
return ZEND_HASH_APPLY_REMOVE;
}
/* }}} */
/* {{{ php_runkit_memrchr */
static inline const void *php_runkit_memrchr(const void *s, int c, size_t n)
{
register const unsigned char *e;
if (n <= 0) {
return NULL;
}
for (e = (const unsigned char *)s + n - 1; e >= (const unsigned char *)s; e--) {
if (*e == (const unsigned char)c) {
return (const void *)e;
}
}
return NULL;
}
/* }}} */
/* {{{ proto bool runkit_class_emancipate(string classname)
Convert an inherited class to a base class, removes any method whose scope is ancestral */
PHP_FUNCTION(runkit_class_emancipate)
{
zend_class_entry *ce;
PHP_RUNKIT_DECL_STRING_PARAM(classname)
HashPosition pos;
char *key;
uint key_len;
ulong idx;
zend_property_info *property_info_ptr = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/", &classname, &classname_len) == FAILURE) {
RETURN_FALSE;
}
if (php_runkit_fetch_class(classname, classname_len, &ce TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
if (!ce->parent) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Class %s has no parent to emancipate from", classname);
RETURN_TRUE;
}
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
php_runkit_clear_all_functions_runtime_cache(TSRMLS_C);
#endif
zend_hash_apply_with_argument(&ce->function_table, (apply_func_arg_t)php_runkit_remove_inherited_methods, ce TSRMLS_CC);
zend_hash_internal_pointer_reset_ex(&ce->parent->properties_info, &pos);
while (zend_hash_get_current_data_ex(&ce->parent->properties_info, (void*) &property_info_ptr, &pos) == SUCCESS && property_info_ptr) {
if (zend_hash_get_current_key_ex(&ce->parent->properties_info, &key, &key_len, &idx, 0, &pos) == HASH_KEY_IS_STRING) {
const char *propname = property_info_ptr->name;
int propname_len = property_info_ptr->name_length;
const char *last_null;
last_null = php_runkit_memrchr(propname, 0, propname_len);
if (last_null) {
propname_len -= last_null - propname + 1;
propname = last_null+1;
}
php_runkit_def_prop_remove_int(ce, propname, propname_len,
ce->parent, (property_info_ptr->flags & ZEND_ACC_STATIC) != 0,
1 /* remove_from_objects */, property_info_ptr TSRMLS_CC);
}
zend_hash_move_forward_ex(&ce->parent->properties_info, &pos);
}
ce->parent = NULL;
RETURN_TRUE;
}
/* }}} */
/* {{{ php_runkit_inherit_methods
Inherit methods from a new ancestor */
static int php_runkit_inherit_methods(zend_function *fe, zend_class_entry *ce TSRMLS_DC)
{
const char *fname = fe->common.function_name;
int fname_len = strlen(fname);
PHP_RUNKIT_DECL_STRING_PARAM(fname_lower)
zend_class_entry *ancestor_class;
/* method name keys must be lower case */
PHP_RUNKIT_MAKE_LOWERCASE_COPY(fname);
if (fname_lower == NULL) {
PHP_RUNKIT_NOT_ENOUGH_MEMORY_ERROR;
return ZEND_HASH_APPLY_KEEP;
}
if (zend_hash_exists(&ce->function_table, (char *) fname_lower, fname_lower_len + 1)) {
efree(fname_lower);
return ZEND_HASH_APPLY_KEEP;
}
ancestor_class = php_runkit_locate_scope(ce, fe, fname_lower, fname_lower_len);
if (zend_hash_add_or_update(&ce->function_table, fname_lower, fname_lower_len + 1, fe, sizeof(zend_function), NULL, HASH_ADD) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error inheriting parent method: %s()", fe->common.function_name);
efree(fname_lower);
return ZEND_HASH_APPLY_KEEP;
}
if (zend_hash_find(&ce->function_table, fname_lower, fname_lower_len + 1, (void*)&fe) == FAILURE ||
!fe) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate newly added method");
efree(fname_lower);
return ZEND_HASH_APPLY_KEEP;
}
PHP_RUNKIT_FUNCTION_ADD_REF(fe);
PHP_RUNKIT_ADD_MAGIC_METHOD(ce, fname_lower, fname_lower_len, fe, NULL TSRMLS_CC);
zend_hash_apply_with_arguments(RUNKIT_53_TSRMLS_PARAM(EG(class_table)), (apply_func_args_t)php_runkit_update_children_methods, 6,
ancestor_class, ce, fe, fname_lower, fname_lower_len, NULL);
efree(fname_lower);
return ZEND_HASH_APPLY_KEEP;
}
/* }}} */
/* {{{ php_runkit_class_copy
Copy class into class table */
int php_runkit_class_copy(zend_class_entry *src, const char *classname, int classname_len TSRMLS_DC)
{
zend_class_entry *new_class_entry, *parent = NULL;
PHP_RUNKIT_DECL_STRING_PARAM(classname_lower)
PHP_RUNKIT_MAKE_LOWERCASE_COPY(classname);
if (!classname_lower) {
PHP_RUNKIT_NOT_ENOUGH_MEMORY_ERROR;
return FAILURE;
}
new_class_entry = emalloc(sizeof(zend_class_entry));
if (src->parent && src->parent->name) {
php_runkit_fetch_class_int(src->parent->name, src->parent->name_length, &parent TSRMLS_CC);
}
new_class_entry->type = ZEND_USER_CLASS;
new_class_entry->name = estrndup(classname, classname_len);
new_class_entry->name_length = classname_len;
zend_initialize_class_data(new_class_entry, 1 TSRMLS_CC);
new_class_entry->parent = parent;
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
new_class_entry->info.user.filename = src->info.user.filename;
new_class_entry->info.user.line_start = src->info.user.line_start;
new_class_entry->info.user.doc_comment = src->info.user.doc_comment;
new_class_entry->info.user.doc_comment_len = src->info.user.doc_comment_len;
new_class_entry->info.user.line_end = src->info.user.line_end;
new_class_entry->num_traits = src->num_traits;
new_class_entry->traits = src->traits;
#else
new_class_entry->filename = src->filename;
new_class_entry->line_start = src->line_start;
new_class_entry->doc_comment = src->doc_comment;
new_class_entry->doc_comment_len = src->doc_comment_len;
new_class_entry->line_end = src->line_end;
#endif
new_class_entry->ce_flags = src->ce_flags;
zend_hash_update(EG(class_table), classname_lower, classname_lower_len + 1, &new_class_entry, sizeof(zend_class_entry *), NULL);
new_class_entry->num_interfaces = src->num_interfaces;
efree(classname_lower);
if (new_class_entry->parent) {
zend_hash_apply_with_argument(&(new_class_entry->parent->function_table),
(apply_func_arg_t)php_runkit_inherit_methods, new_class_entry TSRMLS_CC);
}
return SUCCESS;
}
/* }}} */
/* {{{ proto bool runkit_class_adopt(string classname, string parentname)
Convert a base class to an inherited class, add ancestral methods when appropriate */
PHP_FUNCTION(runkit_class_adopt)
{
zend_class_entry *ce, *parent;
PHP_RUNKIT_DECL_STRING_PARAM(classname)
PHP_RUNKIT_DECL_STRING_PARAM(parentname)
HashPosition pos;
char *key;
uint key_len;
ulong idx;
zend_property_info *property_info_ptr = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/s/", &classname, &classname_len, &parentname, &parentname_len) == FAILURE) {
RETURN_FALSE;
}
if (php_runkit_fetch_class(classname, classname_len, &ce TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
if (ce->parent) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class %s already has a parent", classname);
RETURN_FALSE;
}
if (php_runkit_fetch_class(parentname, parentname_len, &parent TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
ce->parent = parent;
zend_hash_internal_pointer_reset_ex(&parent->properties_info, &pos);
while (zend_hash_get_current_data_ex(&parent->properties_info, (void*) &property_info_ptr, &pos) == SUCCESS && property_info_ptr) {
if (zend_hash_get_current_key_ex(&parent->properties_info, &key, &key_len, &idx, 0, &pos) == HASH_KEY_IS_STRING) {
zval **pp;
const char *propname = property_info_ptr->name;
int propname_len = property_info_ptr->name_length;
const char *last_null;
if (property_info_ptr->flags & ZEND_ACC_STATIC) {
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
pp = &CE_STATIC_MEMBERS(parent)[property_info_ptr->offset];
#else
zend_hash_quick_find(CE_STATIC_MEMBERS(parent), property_info_ptr->name, property_info_ptr->name_length + 1, property_info_ptr->h, (void*) &pp);
#endif
} else {
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4)
pp = &parent->default_properties_table[property_info_ptr->offset];
#else
if (zend_hash_quick_find(&parent->default_properties, property_info_ptr->name, property_info_ptr->name_length + 1, property_info_ptr->h, (void*) &pp) != SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Cannot inherit broken default property %s->%s", parent->name, key);
zend_hash_move_forward_ex(&ce->properties_info, &pos);
continue;
}
#endif // (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
}
php_runkit_zval_resolve_class_constant(pp, parent TSRMLS_CC);
last_null = php_runkit_memrchr(propname, 0, propname_len);
if (last_null) {
propname_len -= last_null - propname + 1;
propname = last_null+1;
}
php_runkit_def_prop_add_int(ce, propname, propname_len,
*pp, property_info_ptr->flags/*visibility*/,
property_info_ptr->doc_comment, property_info_ptr->doc_comment_len,
property_info_ptr->ce /*definer_class*/, 0 /*override*/, 1 /*override_in_objects*/ TSRMLS_CC);
}
zend_hash_move_forward_ex(&ce->properties_info, &pos);
}
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) || (PHP_MAJOR_VERSION > 5)
php_runkit_clear_all_functions_runtime_cache(TSRMLS_C);
#endif
zend_hash_apply_with_argument(&parent->function_table, (apply_func_arg_t)php_runkit_inherit_methods, ce TSRMLS_CC);
RETURN_TRUE;
}
/* }}} */
#endif /* PHP_RUNKIT_MANIPULATION */
/* {{{ proto int runkit_object_id(object instance)
Fetch the Object Handle ID from an instance */
PHP_FUNCTION(runkit_object_id)
{
zval *obj;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
RETURN_NULL();
}
RETURN_LONG(Z_OBJ_HANDLE_P(obj));
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/