/ext/reflection/php_reflection.c
C | 6370 lines | 4715 code | 900 blank | 755 comment | 1196 complexity | 65db7fe1e3d541645c5f50f6d0594b66 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | Copyright (c) The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Timm Friebe <thekid@thekid.de> |
- | George Schlossnagle <george@omniti.com> |
- | Andrei Zmievski <andrei@gravitonic.com> |
- | Marcus Boerger <helly@php.net> |
- | Johannes Schlueter <johannes@php.net> |
- +----------------------------------------------------------------------+
- */
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include "php.h"
- #include "php_ini.h"
- #include "php_reflection.h"
- #include "ext/standard/info.h"
- #include "ext/standard/sha1.h"
- #include "ext/standard/php_random.h"
- #include "zend.h"
- #include "zend_API.h"
- #include "zend_exceptions.h"
- #include "zend_operators.h"
- #include "zend_constants.h"
- #include "zend_ini.h"
- #include "zend_interfaces.h"
- #include "zend_closures.h"
- #include "zend_generators.h"
- #include "zend_extensions.h"
- #include "zend_builtin_functions.h"
- #include "zend_smart_str.h"
- #include "php_reflection_arginfo.h"
- /* Key used to avoid leaking addresses in ReflectionProperty::getId() */
- #define REFLECTION_KEY_LEN 16
- ZEND_BEGIN_MODULE_GLOBALS(reflection)
- zend_bool key_initialized;
- unsigned char key[REFLECTION_KEY_LEN];
- ZEND_END_MODULE_GLOBALS(reflection)
- ZEND_DECLARE_MODULE_GLOBALS(reflection)
- #define REFLECTION_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(reflection, v)
- static zend_always_inline zval *reflection_prop_name(zval *object) {
- /* $name is always in the first property slot. */
- ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 1);
- return &Z_OBJ_P(object)->properties_table[0];
- }
- static zend_always_inline zval *reflection_prop_class(zval *object) {
- /* $class is always in the second property slot. */
- ZEND_ASSERT(Z_OBJCE_P(object)->default_properties_count >= 2);
- return &Z_OBJ_P(object)->properties_table[1];
- }
- /* Class entry pointers */
- PHPAPI zend_class_entry *reflector_ptr;
- PHPAPI zend_class_entry *reflection_exception_ptr;
- PHPAPI zend_class_entry *reflection_ptr;
- PHPAPI zend_class_entry *reflection_function_abstract_ptr;
- PHPAPI zend_class_entry *reflection_function_ptr;
- PHPAPI zend_class_entry *reflection_generator_ptr;
- PHPAPI zend_class_entry *reflection_parameter_ptr;
- PHPAPI zend_class_entry *reflection_type_ptr;
- PHPAPI zend_class_entry *reflection_named_type_ptr;
- PHPAPI zend_class_entry *reflection_union_type_ptr;
- PHPAPI zend_class_entry *reflection_class_ptr;
- PHPAPI zend_class_entry *reflection_object_ptr;
- PHPAPI zend_class_entry *reflection_method_ptr;
- PHPAPI zend_class_entry *reflection_property_ptr;
- PHPAPI zend_class_entry *reflection_class_constant_ptr;
- PHPAPI zend_class_entry *reflection_extension_ptr;
- PHPAPI zend_class_entry *reflection_zend_extension_ptr;
- PHPAPI zend_class_entry *reflection_reference_ptr;
- /* Exception throwing macro */
- #define _DO_THROW(msg) \
- zend_throw_exception(reflection_exception_ptr, msg, 0);
- #define GET_REFLECTION_OBJECT() do { \
- intern = Z_REFLECTION_P(ZEND_THIS); \
- if (intern->ptr == NULL) { \
- if (EG(exception) && EG(exception)->ce == reflection_exception_ptr) { \
- RETURN_THROWS(); \
- } \
- zend_throw_error(NULL, "Internal error: Failed to retrieve the reflection object"); \
- RETURN_THROWS(); \
- } \
- } while (0)
- #define GET_REFLECTION_OBJECT_PTR(target) do { \
- GET_REFLECTION_OBJECT(); \
- target = intern->ptr; \
- } while (0)
- /* Class constants */
- #define REGISTER_REFLECTION_CLASS_CONST_LONG(class_name, const_name, value) \
- zend_declare_class_constant_long(reflection_ ## class_name ## _ptr, const_name, sizeof(const_name)-1, (zend_long)value);
- /* {{{ Object structure */
- /* Struct for properties */
- typedef struct _property_reference {
- zend_property_info *prop;
- zend_string *unmangled_name;
- } property_reference;
- /* Struct for parameters */
- typedef struct _parameter_reference {
- uint32_t offset;
- zend_bool required;
- struct _zend_arg_info *arg_info;
- zend_function *fptr;
- } parameter_reference;
- /* Struct for type hints */
- typedef struct _type_reference {
- zend_type type;
- /* Whether to use backwards compatible null representation */
- zend_bool legacy_behavior;
- } type_reference;
- typedef enum {
- REF_TYPE_OTHER, /* Must be 0 */
- REF_TYPE_FUNCTION,
- REF_TYPE_GENERATOR,
- REF_TYPE_PARAMETER,
- REF_TYPE_TYPE,
- REF_TYPE_PROPERTY,
- REF_TYPE_CLASS_CONSTANT
- } reflection_type_t;
- /* Struct for reflection objects */
- typedef struct {
- zval obj;
- void *ptr;
- zend_class_entry *ce;
- reflection_type_t ref_type;
- unsigned int ignore_visibility:1;
- zend_object zo;
- } reflection_object;
- static inline reflection_object *reflection_object_from_obj(zend_object *obj) {
- return (reflection_object*)((char*)(obj) - XtOffsetOf(reflection_object, zo));
- }
- #define Z_REFLECTION_P(zv) reflection_object_from_obj(Z_OBJ_P((zv)))
- /* }}} */
- static zend_object_handlers reflection_object_handlers;
- static zend_always_inline uint32_t prop_get_flags(property_reference *ref) {
- return ref->prop ? ref->prop->flags : ZEND_ACC_PUBLIC;
- }
- static inline zend_bool is_closure_invoke(zend_class_entry *ce, zend_string *lcname) {
- return ce == zend_ce_closure
- && zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME);
- }
- static void _default_get_name(zval *object, zval *return_value) /* {{{ */
- {
- zval *name = reflection_prop_name(object);
- if (Z_ISUNDEF_P(name)) {
- RETURN_FALSE;
- }
- ZVAL_COPY(return_value, name);
- }
- /* }}} */
- static zend_function *_copy_function(zend_function *fptr) /* {{{ */
- {
- if (fptr
- && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
- {
- zend_function *copy_fptr;
- copy_fptr = emalloc(sizeof(zend_function));
- memcpy(copy_fptr, fptr, sizeof(zend_function));
- copy_fptr->internal_function.function_name = zend_string_copy(fptr->internal_function.function_name);
- return copy_fptr;
- } else {
- /* no copy needed */
- return fptr;
- }
- }
- /* }}} */
- static void _free_function(zend_function *fptr) /* {{{ */
- {
- if (fptr
- && (fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
- {
- zend_string_release_ex(fptr->internal_function.function_name, 0);
- zend_free_trampoline(fptr);
- }
- }
- /* }}} */
- static void reflection_free_objects_storage(zend_object *object) /* {{{ */
- {
- reflection_object *intern = reflection_object_from_obj(object);
- parameter_reference *reference;
- property_reference *prop_reference;
- if (intern->ptr) {
- switch (intern->ref_type) {
- case REF_TYPE_PARAMETER:
- reference = (parameter_reference*)intern->ptr;
- _free_function(reference->fptr);
- efree(intern->ptr);
- break;
- case REF_TYPE_TYPE:
- {
- type_reference *type_ref = intern->ptr;
- if (ZEND_TYPE_HAS_NAME(type_ref->type)) {
- zend_string_release(ZEND_TYPE_NAME(type_ref->type));
- }
- efree(type_ref);
- break;
- }
- case REF_TYPE_FUNCTION:
- _free_function(intern->ptr);
- break;
- case REF_TYPE_PROPERTY:
- prop_reference = (property_reference*)intern->ptr;
- zend_string_release_ex(prop_reference->unmangled_name, 0);
- efree(intern->ptr);
- break;
- case REF_TYPE_GENERATOR:
- case REF_TYPE_CLASS_CONSTANT:
- case REF_TYPE_OTHER:
- break;
- }
- }
- intern->ptr = NULL;
- zval_ptr_dtor(&intern->obj);
- zend_object_std_dtor(object);
- }
- /* }}} */
- static HashTable *reflection_get_gc(zend_object *obj, zval **gc_data, int *gc_data_count) /* {{{ */
- {
- reflection_object *intern = reflection_object_from_obj(obj);
- *gc_data = &intern->obj;
- *gc_data_count = 1;
- return zend_std_get_properties(obj);
- }
- /* }}} */
- static zend_object *reflection_objects_new(zend_class_entry *class_type) /* {{{ */
- {
- reflection_object *intern = zend_object_alloc(sizeof(reflection_object), class_type);
- zend_object_std_init(&intern->zo, class_type);
- object_properties_init(&intern->zo, class_type);
- intern->zo.handlers = &reflection_object_handlers;
- return &intern->zo;
- }
- /* }}} */
- static zval *reflection_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
- {
- object_init_ex(object, pce);
- return object;
- }
- /* }}} */
- static void _const_string(smart_str *str, char *name, zval *value, char *indent);
- static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent);
- static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent);
- static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char* indent);
- static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent);
- static void _extension_string(smart_str *str, zend_module_entry *module, char *indent);
- static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent);
- /* {{{ _class_string */
- static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj, char *indent)
- {
- int count, count_static_props = 0, count_static_funcs = 0, count_shadow_props = 0;
- zend_string *sub_indent = strpprintf(0, "%s ", indent);
- /* TBD: Repair indenting of doc comment (or is this to be done in the parser?) */
- if (ce->type == ZEND_USER_CLASS && ce->info.user.doc_comment) {
- smart_str_append_printf(str, "%s%s", indent, ZSTR_VAL(ce->info.user.doc_comment));
- smart_str_appendc(str, '\n');
- }
- if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
- smart_str_append_printf(str, "%sObject of class [ ", indent);
- } else {
- char *kind = "Class";
- if (ce->ce_flags & ZEND_ACC_INTERFACE) {
- kind = "Interface";
- } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
- kind = "Trait";
- }
- smart_str_append_printf(str, "%s%s [ ", indent, kind);
- }
- smart_str_append_printf(str, (ce->type == ZEND_USER_CLASS) ? "<user" : "<internal");
- if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module) {
- smart_str_append_printf(str, ":%s", ce->info.internal.module->name);
- }
- smart_str_append_printf(str, "> ");
- if (ce->get_iterator != NULL) {
- smart_str_append_printf(str, "<iterateable> ");
- }
- if (ce->ce_flags & ZEND_ACC_INTERFACE) {
- smart_str_append_printf(str, "interface ");
- } else if (ce->ce_flags & ZEND_ACC_TRAIT) {
- smart_str_append_printf(str, "trait ");
- } else {
- if (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
- smart_str_append_printf(str, "abstract ");
- }
- if (ce->ce_flags & ZEND_ACC_FINAL) {
- smart_str_append_printf(str, "final ");
- }
- smart_str_append_printf(str, "class ");
- }
- smart_str_append_printf(str, "%s", ZSTR_VAL(ce->name));
- if (ce->parent) {
- smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->parent->name));
- }
- if (ce->num_interfaces) {
- uint32_t i;
- ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
- if (ce->ce_flags & ZEND_ACC_INTERFACE) {
- smart_str_append_printf(str, " extends %s", ZSTR_VAL(ce->interfaces[0]->name));
- } else {
- smart_str_append_printf(str, " implements %s", ZSTR_VAL(ce->interfaces[0]->name));
- }
- for (i = 1; i < ce->num_interfaces; ++i) {
- smart_str_append_printf(str, ", %s", ZSTR_VAL(ce->interfaces[i]->name));
- }
- }
- smart_str_append_printf(str, " ] {\n");
- /* The information where a class is declared is only available for user classes */
- if (ce->type == ZEND_USER_CLASS) {
- smart_str_append_printf(str, "%s @@ %s %d-%d\n", indent, ZSTR_VAL(ce->info.user.filename),
- ce->info.user.line_start, ce->info.user.line_end);
- }
- /* Constants */
- smart_str_append_printf(str, "\n");
- count = zend_hash_num_elements(&ce->constants_table);
- smart_str_append_printf(str, "%s - Constants [%d] {\n", indent, count);
- if (count > 0) {
- zend_string *key;
- zend_class_constant *c;
- ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, c) {
- _class_const_string(str, ZSTR_VAL(key), c, ZSTR_VAL(sub_indent));
- if (UNEXPECTED(EG(exception))) {
- return;
- }
- } ZEND_HASH_FOREACH_END();
- }
- smart_str_append_printf(str, "%s }\n", indent);
- /* Static properties */
- /* counting static properties */
- count = zend_hash_num_elements(&ce->properties_info);
- if (count > 0) {
- zend_property_info *prop;
- ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
- if ((prop->flags & ZEND_ACC_PRIVATE) && prop->ce != ce) {
- count_shadow_props++;
- } else if (prop->flags & ZEND_ACC_STATIC) {
- count_static_props++;
- }
- } ZEND_HASH_FOREACH_END();
- }
- /* static properties */
- smart_str_append_printf(str, "\n%s - Static properties [%d] {\n", indent, count_static_props);
- if (count_static_props > 0) {
- zend_property_info *prop;
- ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
- if ((prop->flags & ZEND_ACC_STATIC) && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
- _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
- }
- } ZEND_HASH_FOREACH_END();
- }
- smart_str_append_printf(str, "%s }\n", indent);
- /* Static methods */
- /* counting static methods */
- count = zend_hash_num_elements(&ce->function_table);
- if (count > 0) {
- zend_function *mptr;
- ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
- if (mptr->common.fn_flags & ZEND_ACC_STATIC
- && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
- {
- count_static_funcs++;
- }
- } ZEND_HASH_FOREACH_END();
- }
- /* static methods */
- smart_str_append_printf(str, "\n%s - Static methods [%d] {", indent, count_static_funcs);
- if (count_static_funcs > 0) {
- zend_function *mptr;
- ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
- if (mptr->common.fn_flags & ZEND_ACC_STATIC
- && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
- {
- smart_str_append_printf(str, "\n");
- _function_string(str, mptr, ce, ZSTR_VAL(sub_indent));
- }
- } ZEND_HASH_FOREACH_END();
- } else {
- smart_str_append_printf(str, "\n");
- }
- smart_str_append_printf(str, "%s }\n", indent);
- /* Default/Implicit properties */
- count = zend_hash_num_elements(&ce->properties_info) - count_static_props - count_shadow_props;
- smart_str_append_printf(str, "\n%s - Properties [%d] {\n", indent, count);
- if (count > 0) {
- zend_property_info *prop;
- ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
- if (!(prop->flags & ZEND_ACC_STATIC)
- && (!(prop->flags & ZEND_ACC_PRIVATE) || prop->ce == ce)) {
- _property_string(str, prop, NULL, ZSTR_VAL(sub_indent));
- }
- } ZEND_HASH_FOREACH_END();
- }
- smart_str_append_printf(str, "%s }\n", indent);
- if (obj && Z_TYPE_P(obj) == IS_OBJECT) {
- HashTable *properties = Z_OBJ_HT_P(obj)->get_properties(Z_OBJ_P(obj));
- zend_string *prop_name;
- smart_str prop_str = {0};
- count = 0;
- if (properties && zend_hash_num_elements(properties)) {
- ZEND_HASH_FOREACH_STR_KEY(properties, prop_name) {
- if (prop_name && ZSTR_LEN(prop_name) && ZSTR_VAL(prop_name)[0]) { /* skip all private and protected properties */
- if (!zend_hash_exists(&ce->properties_info, prop_name)) {
- count++;
- _property_string(&prop_str, NULL, ZSTR_VAL(prop_name), ZSTR_VAL(sub_indent));
- }
- }
- } ZEND_HASH_FOREACH_END();
- }
- smart_str_append_printf(str, "\n%s - Dynamic properties [%d] {\n", indent, count);
- smart_str_append_smart_str(str, &prop_str);
- smart_str_append_printf(str, "%s }\n", indent);
- smart_str_free(&prop_str);
- }
- /* Non static methods */
- count = zend_hash_num_elements(&ce->function_table) - count_static_funcs;
- if (count > 0) {
- zend_function *mptr;
- smart_str method_str = {0};
- count = 0;
- ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
- if ((mptr->common.fn_flags & ZEND_ACC_STATIC) == 0
- && ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
- {
- zend_function *closure;
- /* see if this is a closure */
- if (obj && is_closure_invoke(ce, mptr->common.function_name)
- && (closure = zend_get_closure_invoke_method(Z_OBJ_P(obj))) != NULL)
- {
- mptr = closure;
- } else {
- closure = NULL;
- }
- smart_str_appendc(&method_str, '\n');
- _function_string(&method_str, mptr, ce, ZSTR_VAL(sub_indent));
- count++;
- _free_function(closure);
- }
- } ZEND_HASH_FOREACH_END();
- smart_str_append_printf(str, "\n%s - Methods [%d] {", indent, count);
- smart_str_append_smart_str(str, &method_str);
- if (!count) {
- smart_str_append_printf(str, "\n");
- }
- smart_str_free(&method_str);
- } else {
- smart_str_append_printf(str, "\n%s - Methods [0] {\n", indent);
- }
- smart_str_append_printf(str, "%s }\n", indent);
- smart_str_append_printf(str, "%s}\n", indent);
- zend_string_release_ex(sub_indent, 0);
- }
- /* }}} */
- /* {{{ _const_string */
- static void _const_string(smart_str *str, char *name, zval *value, char *indent)
- {
- const char *type = zend_zval_type_name(value);
- if (Z_TYPE_P(value) == IS_ARRAY) {
- smart_str_append_printf(str, "%s Constant [ %s %s ] { Array }\n",
- indent, type, name);
- } else if (Z_TYPE_P(value) == IS_STRING) {
- smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
- indent, type, name, Z_STRVAL_P(value));
- } else {
- zend_string *tmp_value_str;
- zend_string *value_str = zval_get_tmp_string(value, &tmp_value_str);
- smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
- indent, type, name, ZSTR_VAL(value_str));
- zend_tmp_string_release(tmp_value_str);
- }
- }
- /* }}} */
- /* {{{ _class_const_string */
- static void _class_const_string(smart_str *str, char *name, zend_class_constant *c, char *indent)
- {
- char *visibility = zend_visibility_string(Z_ACCESS_FLAGS(c->value));
- const char *type;
- zval_update_constant_ex(&c->value, c->ce);
- type = zend_zval_type_name(&c->value);
- if (Z_TYPE(c->value) == IS_ARRAY) {
- smart_str_append_printf(str, "%sConstant [ %s %s %s ] { Array }\n",
- indent, visibility, type, name);
- } else {
- zend_string *tmp_value_str;
- zend_string *value_str = zval_get_tmp_string(&c->value, &tmp_value_str);
- smart_str_append_printf(str, "%sConstant [ %s %s %s ] { %s }\n",
- indent, visibility, type, name, ZSTR_VAL(value_str));
- zend_tmp_string_release(tmp_value_str);
- }
- }
- /* }}} */
- static zend_op *get_recv_op(zend_op_array *op_array, uint32_t offset)
- {
- zend_op *op = op_array->opcodes;
- zend_op *end = op + op_array->last;
- ++offset;
- while (op < end) {
- if ((op->opcode == ZEND_RECV || op->opcode == ZEND_RECV_INIT
- || op->opcode == ZEND_RECV_VARIADIC) && op->op1.num == offset)
- {
- return op;
- }
- ++op;
- }
- ZEND_ASSERT(0 && "Failed to find op");
- return NULL;
- }
- static zval *get_default_from_recv(zend_op_array *op_array, uint32_t offset) {
- zend_op *recv = get_recv_op(op_array, offset);
- if (!recv || recv->opcode != ZEND_RECV_INIT) {
- return NULL;
- }
- return RT_CONSTANT(recv, recv->op2);
- }
- static int format_default_value(smart_str *str, zval *value, zend_class_entry *scope) {
- zval zv;
- ZVAL_COPY(&zv, value);
- if (UNEXPECTED(zval_update_constant_ex(&zv, scope) == FAILURE)) {
- zval_ptr_dtor(&zv);
- return FAILURE;
- }
- if (Z_TYPE(zv) == IS_TRUE) {
- smart_str_appends(str, "true");
- } else if (Z_TYPE(zv) == IS_FALSE) {
- smart_str_appends(str, "false");
- } else if (Z_TYPE(zv) == IS_NULL) {
- smart_str_appends(str, "NULL");
- } else if (Z_TYPE(zv) == IS_STRING) {
- smart_str_appendc(str, '\'');
- smart_str_appendl(str, Z_STRVAL(zv), MIN(Z_STRLEN(zv), 15));
- if (Z_STRLEN(zv) > 15) {
- smart_str_appends(str, "...");
- }
- smart_str_appendc(str, '\'');
- } else if (Z_TYPE(zv) == IS_ARRAY) {
- smart_str_appends(str, "Array");
- } else {
- zend_string *tmp_zv_str;
- zend_string *zv_str = zval_get_tmp_string(&zv, &tmp_zv_str);
- smart_str_append(str, zv_str);
- zend_tmp_string_release(tmp_zv_str);
- }
- zval_ptr_dtor(&zv);
- return SUCCESS;
- }
- static inline zend_bool has_internal_arg_info(const zend_function *fptr) {
- return fptr->type == ZEND_INTERNAL_FUNCTION
- && !(fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO);
- }
- /* {{{ _parameter_string */
- static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, char* indent)
- {
- smart_str_append_printf(str, "Parameter #%d [ ", offset);
- if (!required) {
- smart_str_append_printf(str, "<optional> ");
- } else {
- smart_str_append_printf(str, "<required> ");
- }
- if (ZEND_TYPE_IS_SET(arg_info->type)) {
- zend_string *type_str = zend_type_to_string(arg_info->type);
- smart_str_append(str, type_str);
- smart_str_appendc(str, ' ');
- zend_string_release(type_str);
- }
- if (ZEND_ARG_SEND_MODE(arg_info)) {
- smart_str_appendc(str, '&');
- }
- if (ZEND_ARG_IS_VARIADIC(arg_info)) {
- smart_str_appends(str, "...");
- }
- smart_str_append_printf(str, "$%s", has_internal_arg_info(fptr)
- ? ((zend_internal_arg_info*)arg_info)->name : ZSTR_VAL(arg_info->name));
- if (!required) {
- if (fptr->type == ZEND_INTERNAL_FUNCTION) {
- smart_str_appends(str, " = ");
- if (((zend_internal_arg_info*)arg_info)->default_value) {
- smart_str_appends(str, ((zend_internal_arg_info*)arg_info)->default_value);
- } else {
- smart_str_appends(str, "<default>");
- }
- } else {
- zval *default_value = get_default_from_recv((zend_op_array*)fptr, offset);
- if (default_value) {
- smart_str_appends(str, " = ");
- if (format_default_value(str, default_value, fptr->common.scope) == FAILURE) {
- return;
- }
- }
- }
- }
- smart_str_appends(str, " ]");
- }
- /* }}} */
- /* {{{ _function_parameter_string */
- static void _function_parameter_string(smart_str *str, zend_function *fptr, char* indent)
- {
- struct _zend_arg_info *arg_info = fptr->common.arg_info;
- uint32_t i, num_args, num_required = fptr->common.required_num_args;
- if (!arg_info) {
- return;
- }
- num_args = fptr->common.num_args;
- if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
- num_args++;
- }
- smart_str_appendc(str, '\n');
- smart_str_append_printf(str, "%s- Parameters [%d] {\n", indent, num_args);
- for (i = 0; i < num_args; i++) {
- smart_str_append_printf(str, "%s ", indent);
- _parameter_string(str, fptr, arg_info, i, i < num_required, indent);
- smart_str_appendc(str, '\n');
- arg_info++;
- }
- smart_str_append_printf(str, "%s}\n", indent);
- }
- /* }}} */
- /* {{{ _function_closure_string */
- static void _function_closure_string(smart_str *str, zend_function *fptr, char* indent)
- {
- uint32_t i, count;
- zend_string *key;
- HashTable *static_variables;
- if (fptr->type != ZEND_USER_FUNCTION || !fptr->op_array.static_variables) {
- return;
- }
- static_variables = ZEND_MAP_PTR_GET(fptr->op_array.static_variables_ptr);
- count = zend_hash_num_elements(static_variables);
- if (!count) {
- return;
- }
- smart_str_append_printf(str, "\n");
- smart_str_append_printf(str, "%s- Bound Variables [%d] {\n", indent, zend_hash_num_elements(static_variables));
- i = 0;
- ZEND_HASH_FOREACH_STR_KEY(static_variables, key) {
- smart_str_append_printf(str, "%s Variable #%d [ $%s ]\n", indent, i++, ZSTR_VAL(key));
- } ZEND_HASH_FOREACH_END();
- smart_str_append_printf(str, "%s}\n", indent);
- }
- /* }}} */
- /* {{{ _function_string */
- static void _function_string(smart_str *str, zend_function *fptr, zend_class_entry *scope, char* indent)
- {
- smart_str param_indent = {0};
- zend_function *overwrites;
- zend_string *lc_name;
- /* TBD: Repair indenting of doc comment (or is this to be done in the parser?)
- * What's "wrong" is that any whitespace before the doc comment start is
- * swallowed, leading to an unaligned comment.
- */
- if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) {
- smart_str_append_printf(str, "%s%s\n", indent, ZSTR_VAL(fptr->op_array.doc_comment));
- }
- smart_str_appendl(str, indent, strlen(indent));
- smart_str_append_printf(str, fptr->common.fn_flags & ZEND_ACC_CLOSURE ? "Closure [ " : (fptr->common.scope ? "Method [ " : "Function [ "));
- smart_str_append_printf(str, (fptr->type == ZEND_USER_FUNCTION) ? "<user" : "<internal");
- if (fptr->common.fn_flags & ZEND_ACC_DEPRECATED) {
- smart_str_appends(str, ", deprecated");
- }
- if (fptr->type == ZEND_INTERNAL_FUNCTION && ((zend_internal_function*)fptr)->module) {
- smart_str_append_printf(str, ":%s", ((zend_internal_function*)fptr)->module->name);
- }
- if (scope && fptr->common.scope) {
- if (fptr->common.scope != scope) {
- smart_str_append_printf(str, ", inherits %s", ZSTR_VAL(fptr->common.scope->name));
- } else if (fptr->common.scope->parent) {
- lc_name = zend_string_tolower(fptr->common.function_name);
- if ((overwrites = zend_hash_find_ptr(&fptr->common.scope->parent->function_table, lc_name)) != NULL) {
- if (fptr->common.scope != overwrites->common.scope) {
- smart_str_append_printf(str, ", overwrites %s", ZSTR_VAL(overwrites->common.scope->name));
- }
- }
- zend_string_release_ex(lc_name, 0);
- }
- }
- if (fptr->common.prototype && fptr->common.prototype->common.scope) {
- smart_str_append_printf(str, ", prototype %s", ZSTR_VAL(fptr->common.prototype->common.scope->name));
- }
- if (fptr->common.fn_flags & ZEND_ACC_CTOR) {
- smart_str_appends(str, ", ctor");
- }
- smart_str_appends(str, "> ");
- if (fptr->common.fn_flags & ZEND_ACC_ABSTRACT) {
- smart_str_appends(str, "abstract ");
- }
- if (fptr->common.fn_flags & ZEND_ACC_FINAL) {
- smart_str_appends(str, "final ");
- }
- if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
- smart_str_appends(str, "static ");
- }
- if (fptr->common.scope) {
- /* These are mutually exclusive */
- switch (fptr->common.fn_flags & ZEND_ACC_PPP_MASK) {
- case ZEND_ACC_PUBLIC:
- smart_str_appends(str, "public ");
- break;
- case ZEND_ACC_PRIVATE:
- smart_str_appends(str, "private ");
- break;
- case ZEND_ACC_PROTECTED:
- smart_str_appends(str, "protected ");
- break;
- default:
- smart_str_appends(str, "<visibility error> ");
- break;
- }
- smart_str_appends(str, "method ");
- } else {
- smart_str_appends(str, "function ");
- }
- if (fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) {
- smart_str_appendc(str, '&');
- }
- smart_str_append_printf(str, "%s ] {\n", ZSTR_VAL(fptr->common.function_name));
- /* The information where a function is declared is only available for user classes */
- if (fptr->type == ZEND_USER_FUNCTION) {
- smart_str_append_printf(str, "%s @@ %s %d - %d\n", indent,
- ZSTR_VAL(fptr->op_array.filename),
- fptr->op_array.line_start,
- fptr->op_array.line_end);
- }
- smart_str_append_printf(¶m_indent, "%s ", indent);
- smart_str_0(¶m_indent);
- if (fptr->common.fn_flags & ZEND_ACC_CLOSURE) {
- _function_closure_string(str, fptr, ZSTR_VAL(param_indent.s));
- }
- _function_parameter_string(str, fptr, ZSTR_VAL(param_indent.s));
- smart_str_free(¶m_indent);
- if (fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
- smart_str_append_printf(str, " %s- Return [ ", indent);
- if (ZEND_TYPE_IS_SET(fptr->common.arg_info[-1].type)) {
- zend_string *type_str = zend_type_to_string(fptr->common.arg_info[-1].type);
- smart_str_append_printf(str, "%s ", ZSTR_VAL(type_str));
- zend_string_release(type_str);
- }
- smart_str_appends(str, "]\n");
- }
- smart_str_append_printf(str, "%s}\n", indent);
- }
- /* }}} */
- static zval *property_get_default(zend_property_info *prop_info) {
- zend_class_entry *ce = prop_info->ce;
- if (prop_info->flags & ZEND_ACC_STATIC) {
- zval *prop = &ce->default_static_members_table[prop_info->offset];
- ZVAL_DEINDIRECT(prop);
- return prop;
- } else {
- return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
- }
- }
- /* {{{ _property_string */
- static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent)
- {
- smart_str_append_printf(str, "%sProperty [ ", indent);
- if (!prop) {
- smart_str_append_printf(str, "<dynamic> public $%s", prop_name);
- } else {
- /* These are mutually exclusive */
- switch (prop->flags & ZEND_ACC_PPP_MASK) {
- case ZEND_ACC_PUBLIC:
- smart_str_appends(str, "public ");
- break;
- case ZEND_ACC_PRIVATE:
- smart_str_appends(str, "private ");
- break;
- case ZEND_ACC_PROTECTED:
- smart_str_appends(str, "protected ");
- break;
- }
- if (prop->flags & ZEND_ACC_STATIC) {
- smart_str_appends(str, "static ");
- }
- if (ZEND_TYPE_IS_SET(prop->type)) {
- zend_string *type_str = zend_type_to_string(prop->type);
- smart_str_append(str, type_str);
- smart_str_appendc(str, ' ');
- zend_string_release(type_str);
- }
- if (!prop_name) {
- const char *class_name;
- zend_unmangle_property_name(prop->name, &class_name, &prop_name);
- }
- smart_str_append_printf(str, "$%s", prop_name);
- zval *default_value = property_get_default(prop);
- if (!Z_ISUNDEF_P(default_value)) {
- smart_str_appends(str, " = ");
- if (format_default_value(str, default_value, prop->ce) == FAILURE) {
- return;
- }
- }
- }
- smart_str_appends(str, " ]\n");
- }
- /* }}} */
- static void _extension_ini_string(zend_ini_entry *ini_entry, smart_str *str, char *indent, int number) /* {{{ */
- {
- char *comma = "";
- if (number == ini_entry->module_number) {
- smart_str_append_printf(str, " %sEntry [ %s <", indent, ZSTR_VAL(ini_entry->name));
- if (ini_entry->modifiable == ZEND_INI_ALL) {
- smart_str_appends(str, "ALL");
- } else {
- if (ini_entry->modifiable & ZEND_INI_USER) {
- smart_str_appends(str, "USER");
- comma = ",";
- }
- if (ini_entry->modifiable & ZEND_INI_PERDIR) {
- smart_str_append_printf(str, "%sPERDIR", comma);
- comma = ",";
- }
- if (ini_entry->modifiable & ZEND_INI_SYSTEM) {
- smart_str_append_printf(str, "%sSYSTEM", comma);
- }
- }
- smart_str_appends(str, "> ]\n");
- smart_str_append_printf(str, " %s Current = '%s'\n", indent, ini_entry->value ? ZSTR_VAL(ini_entry->value) : "");
- if (ini_entry->modified) {
- smart_str_append_printf(str, " %s Default = '%s'\n", indent, ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : "");
- }
- smart_str_append_printf(str, " %s}\n", indent);
- }
- }
- /* }}} */
- static void _extension_class_string(zend_class_entry *ce, zend_string *key, smart_str *str, char *indent, zend_module_entry *module, int *num_classes) /* {{{ */
- {
- if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module && !strcasecmp(ce->info.internal.module->name, module->name)) {
- /* dump class if it is not an alias */
- if (zend_string_equals_ci(ce->name, key)) {
- smart_str_append_printf(str, "\n");
- _class_string(str, ce, NULL, indent);
- (*num_classes)++;
- }
- }
- }
- /* }}} */
- static void _extension_string(smart_str *str, zend_module_entry *module, char *indent) /* {{{ */
- {
- smart_str_append_printf(str, "%sExtension [ ", indent);
- if (module->type == MODULE_PERSISTENT) {
- smart_str_appends(str, "<persistent>");
- }
- if (module->type == MODULE_TEMPORARY) {
- smart_str_appends(str, "<temporary>" );
- }
- smart_str_append_printf(str, " extension #%d %s version %s ] {\n",
- module->module_number, module->name,
- (module->version == NO_VERSION_YET) ? "<no_version>" : module->version);
- if (module->deps) {
- const zend_module_dep* dep = module->deps;
- smart_str_appends(str, "\n - Dependencies {\n");
- while(dep->name) {
- smart_str_append_printf(str, "%s Dependency [ %s (", indent, dep->name);
- switch(dep->type) {
- case MODULE_DEP_REQUIRED:
- smart_str_appends(str, "Required");
- break;
- case MODULE_DEP_CONFLICTS:
- smart_str_appends(str, "Conflicts");
- break;
- case MODULE_DEP_OPTIONAL:
- smart_str_appends(str, "Optional");
- break;
- default:
- smart_str_appends(str, "Error"); /* shouldn't happen */
- break;
- }
- if (dep->rel) {
- smart_str_append_printf(str, " %s", dep->rel);
- }
- if (dep->version) {
- smart_str_append_printf(str, " %s", dep->version);
- }
- smart_str_appends(str, ") ]\n");
- dep++;
- }
- smart_str_append_printf(str, "%s }\n", indent);
- }
- {
- smart_str str_ini = {0};
- zend_ini_entry *ini_entry;
- ZEND_HASH_FOREACH_PTR(EG(ini_directives), ini_entry) {
- _extension_ini_string(ini_entry, &str_ini, indent, module->module_number);
- } ZEND_HASH_FOREACH_END();
- if (smart_str_get_len(&str_ini) > 0) {
- smart_str_append_printf(str, "\n - INI {\n");
- smart_str_append_smart_str(str, &str_ini);
- smart_str_append_printf(str, "%s }\n", indent);
- }
- smart_str_free(&str_ini);
- }
- {
- smart_str str_constants = {0};
- zend_constant *constant;
- int num_constants = 0;
- ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
- if (ZEND_CONSTANT_MODULE_NUMBER(constant) == module->module_number) {
- _const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value, indent);
- num_constants++;
- }
- } ZEND_HASH_FOREACH_END();
- if (num_constants) {
- smart_str_append_printf(str, "\n - Constants [%d] {\n", num_constants);
- smart_str_append_smart_str(str, &str_constants);
- smart_str_append_printf(str, "%s }\n", indent);
- }
- smart_str_free(&str_constants);
- }
- {
- zend_function *fptr;
- int first = 1;
- ZEND_HASH_FOREACH_PTR(CG(function_table), fptr) {
- if (fptr->common.type==ZEND_INTERNAL_FUNCTION
- && fptr->internal_function.module == module) {
- if (first) {
- smart_str_append_printf(str, "\n - Functions {\n");
- first = 0;
- }
- _function_string(str, fptr, NULL, " ");
- }
- } ZEND_HASH_FOREACH_END();
- if (!first) {
- smart_str_append_printf(str, "%s }\n", indent);
- }
- }
- {
- zend_string *sub_indent = strpprintf(0, "%s ", indent);
- smart_str str_classes = {0};
- zend_string *key;
- zend_class_entry *ce;
- int num_classes = 0;
- ZEND_HASH_FOREACH_STR_KEY_PTR(EG(class_table), key, ce) {
- _extension_class_string(ce, key, &str_classes, ZSTR_VAL(sub_indent), module, &num_classes);
- } ZEND_HASH_FOREACH_END();
- if (num_classes) {
- smart_str_append_printf(str, "\n - Classes [%d] {", num_classes);
- smart_str_append_smart_str(str, &str_classes);
- smart_str_append_printf(str, "%s }\n", indent);
- }
- smart_str_free(&str_classes);
- zend_string_release_ex(sub_indent, 0);
- }
- smart_str_append_printf(str, "%s}\n", indent);
- }
- /* }}} */
- static void _zend_extension_string(smart_str *str, zend_extension *extension, char *indent) /* {{{ */
- {
- smart_str_append_printf(str, "%sZend Extension [ %s ", indent, extension->name);
- if (extension->version) {
- smart_str_append_printf(str, "%s ", extension->version);
- }
- if (extension->copyright) {
- smart_str_append_printf(str, "%s ", extension->copyright);
- }
- if (extension->author) {
- smart_str_append_printf(str, "by %s ", extension->author);
- }
- if (extension->URL) {
- smart_str_append_printf(str, "<%s> ", extension->URL);
- }
- smart_str_appends(str, "]\n");
- }
- /* }}} */
- /* {{{ _function_check_flag */
- static void _function_check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask)
- {
- reflection_object *intern;
- zend_function *mptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(mptr);
- RETURN_BOOL(mptr->common.fn_flags & mask);
- }
- /* }}} */
- /* {{{ zend_reflection_class_factory */
- PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
- {
- reflection_object *intern;
- reflection_instantiate(reflection_class_ptr, object);
- intern = Z_REFLECTION_P(object);
- intern->ptr = ce;
- intern->ref_type = REF_TYPE_OTHER;
- intern->ce = ce;
- ZVAL_STR_COPY(reflection_prop_name(object), ce->name);
- }
- /* }}} */
- /* {{{ reflection_extension_factory */
- static void reflection_extension_factory(zval *object, const char *name_str)
- {
- reflection_object *intern;
- size_t name_len = strlen(name_str);
- zend_string *lcname;
- struct _zend_module_entry *module;
- lcname = zend_string_alloc(name_len, 0);
- zend_str_tolower_copy(ZSTR_VAL(lcname), name_str, name_len);
- module = zend_hash_find_ptr(&module_registry, lcname);
- zend_string_efree(lcname);
- if (!module) {
- return;
- }
- reflection_instantiate(reflection_extension_ptr, object);
- intern = Z_REFLECTION_P(object);
- intern->ptr = module;
- intern->ref_type = REF_TYPE_OTHER;
- intern->ce = NULL;
- ZVAL_STRINGL(reflection_prop_name(object), module->name, name_len);
- }
- /* }}} */
- /* {{{ reflection_parameter_factory */
- static void reflection_parameter_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, zval *object)
- {
- reflection_object *intern;
- parameter_reference *reference;
- zval *prop_name;
- reflection_instantiate(reflection_parameter_ptr, object);
- intern = Z_REFLECTION_P(object);
- reference = (parameter_reference*) emalloc(sizeof(parameter_reference));
- reference->arg_info = arg_info;
- reference->offset = offset;
- reference->required = required;
- reference->fptr = fptr;
- intern->ptr = reference;
- intern->ref_type = REF_TYPE_PARAMETER;
- intern->ce = fptr->common.scope;
- if (closure_object) {
- Z_ADDREF_P(closure_object);
- ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure_object));
- }
- prop_name = reflection_prop_name(object);
- if (has_internal_arg_info(fptr)) {
- ZVAL_STRING(prop_name, ((zend_internal_arg_info*)arg_info)->name);
- } else {
- ZVAL_STR_COPY(prop_name, arg_info->name);
- }
- }
- /* }}} */
- /* For backwards compatibility reasons, we need to return T|null style unions
- * as a ReflectionNamedType. Here we determine what counts as a union type and
- * what doesn't. */
- static zend_bool is_union_type(zend_type type) {
- if (ZEND_TYPE_HAS_LIST(type)) {
- return 1;
- }
- uint32_t type_mask_without_null = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(type);
- if (ZEND_TYPE_HAS_CLASS(type)) {
- return type_mask_without_null != 0;
- }
- if (type_mask_without_null == MAY_BE_BOOL) {
- return 0;
- }
- /* Check that only one bit is set. */
- return (type_mask_without_null & (type_mask_without_null - 1)) != 0;
- }
- /* {{{ reflection_type_factory */
- static void reflection_type_factory(zend_type type, zval *object, zend_bool legacy_behavior)
- {
- reflection_object *intern;
- type_reference *reference;
- zend_bool is_union = is_union_type(type);
- reflection_instantiate(
- is_union ? reflection_union_type_ptr : reflection_named_type_ptr, object);
- intern = Z_REFLECTION_P(object);
- reference = (type_reference*) emalloc(sizeof(type_reference));
- reference->type = type;
- reference->legacy_behavior = legacy_behavior && !is_union;
- intern->ptr = reference;
- intern->ref_type = REF_TYPE_TYPE;
- /* Property types may be resolved during the lifetime of the ReflectionType.
- * If we reference a string, make sure it doesn't get released. However, only
- * do this for the top-level type, as resolutions inside type lists will be
- * fully visible to us (we'd have to do a fully copy of the type if we wanted
- * to prevent that). */
- if (ZEND_TYPE_HAS_NAME(type)) {
- zend_string_addref(ZEND_TYPE_NAME(type));
- }
- }
- /* }}} */
- /* {{{ reflection_function_factory */
- static void reflection_function_factory(zend_function *function, zval *closure_object, zval *object)
- {
- reflection_object *intern;
- reflection_instantiate(reflection_function_ptr, object);
- intern = Z_REFLECTION_P(object);
- intern->ptr = function;
- intern->ref_type = REF_TYPE_FUNCTION;
- intern->ce = NULL;
- if (closure_object) {
- Z_ADDREF_P(closure_object);
- ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure_object));
- }
- ZVAL_STR_COPY(reflection_prop_name(object), function->common.function_name);
- }
- /* }}} */
- /* {{{ reflection_method_factory */
- static void reflection_method_factory(zend_class_entry *ce, zend_function *method, zval *closure_object, zval *object)
- {
- reflection_object *intern;
- reflection_instantiate(reflection_method_ptr, object);
- intern = Z_REFLECTION_P(object);
- intern->ptr = method;
- intern->ref_type = REF_TYPE_FUNCTION;
- intern->ce = ce;
- if (closure_object) {
- Z_ADDREF_P(closure_object);
- ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure_object));
- }
- ZVAL_STR_COPY(reflection_prop_name(object), method->common.function_name);
- ZVAL_STR_COPY(reflection_prop_class(object), method->common.scope->name);
- }
- /* }}} */
- /* {{{ reflection_property_factory */
- static void reflection_property_factory(zend_class_entry *ce, zend_string *name, zend_property_info *prop, zval *object)
- {
- reflection_object *intern;
- property_reference *reference;
- reflection_instantiate(reflection_property_ptr, object);
- intern = Z_REFLECTION_P(object);
- reference = (property_reference*) emalloc(sizeof(property_reference));
- reference->prop = prop;
- reference->unmangled_name = zend_string_copy(name);
- intern->ptr = reference;
- intern->ref_type = REF_TYPE_PROPERTY;
- intern->ce = ce;
- intern->ignore_visibility = 0;
- ZVAL_STR_COPY(reflection_prop_name(object), name);
- ZVAL_STR_COPY(reflection_prop_class(object), prop ? prop->ce->name : ce->name);
- }
- /* }}} */
- static void reflection_property_factory_str(zend_class_entry *ce, const char *name_str, size_t name_len, zend_property_info *prop, zval *object)
- {
- zend_string *name = zend_string_init(name_str, name_len, 0);
- reflection_property_factory(ce, name, prop, object);
- zend_string_release(name);
- }
- /* {{{ reflection_class_constant_factory */
- static void reflection_class_constant_factory(zend_string *name_str, zend_class_constant *constant, zval *object)
- {
- reflection_object *intern;
- reflection_instantiate(reflection_class_constant_ptr, object);
- intern = Z_REFLECTION_P(object);
- intern->ptr = constant;
- intern->ref_type = REF_TYPE_CLASS_CONSTANT;
- intern->ce = constant->ce;
- intern->ignore_visibility = 0;
- ZVAL_STR_COPY(reflection_prop_name(object), name_str);
- ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
- }
- /* }}} */
- static int get_parameter_default(zval *result, parameter_reference *param) {
- if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
- return zend_get_default_from_internal_arg_info(
- result, (zend_internal_arg_info *) param->arg_info);
- } else {
- zval *default_value = get_default_from_recv((zend_op_array *) param->fptr, param->offset);
- if (!default_value) {
- return FAILURE;
- }
- ZVAL_COPY(result, default_value);
- return SUCCESS;
- }
- }
- /* {{{ Preventing __clone from being called */
- ZEND_METHOD(ReflectionClass, __clone)
- {
- /* Should never be executable */
- _DO_THROW("Cannot clone object using __clone()");
- }
- /* }}} */
- /* {{{ proto public static array Reflection::getModifierNames(int modifiers)
- Returns an array of modifier names */
- ZEND_METHOD(Reflection, getModifierNames)
- {
- zend_long modifiers;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &modifiers) == FAILURE) {
- RETURN_THROWS();
- }
- array_init(return_value);
- if (modifiers & (ZEND_ACC_ABSTRACT | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
- add_next_index_stringl(return_value, "abstract", sizeof("abstract")-1);
- }
- if (modifiers & ZEND_ACC_FINAL) {
- add_next_index_stringl(return_value, "final", sizeof("final")-1);
- }
- /* These are mutually exclusive */
- switch (modifiers & ZEND_ACC_PPP_MASK) {
- case ZEND_ACC_PUBLIC:
- add_next_index_stringl(return_value, "public", sizeof("public")-1);
- break;
- case ZEND_ACC_PRIVATE:
- add_next_index_stringl(return_value, "private", sizeof("private")-1);
- break;
- case ZEND_ACC_PROTECTED:
- add_next_index_stringl(return_value, "protected", sizeof("protected")-1);
- break;
- }
- if (modifiers & ZEND_ACC_STATIC) {
- add_next_index_stringl(return_value, "static", sizeof("static")-1);
- }
- }
- /* }}} */
- /* {{{ proto public void ReflectionFunction::__construct(string name)
- Constructor. Throws an Exception in case the given function does not exist */
- ZEND_METHOD(ReflectionFunction, __construct)
- {
- zval *object;
- zval *closure = NULL;
- reflection_object *intern;
- zend_function *fptr;
- zend_string *fname, *lcname;
- object = ZEND_THIS;
- intern = Z_REFLECTION_P(object);
- if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &closure, zend_ce_closure) == SUCCESS) {
- fptr = (zend_function*)zend_get_closure_method_def(closure);
- Z_ADDREF_P(closure);
- } else {
- ALLOCA_FLAG(use_heap)
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &fname) == FAILURE) {
- RETURN_THROWS();
- }
- if (UNEXPECTED(ZSTR_VAL(fname)[0] == '\\')) {
- /* Ignore leading "\" */
- ZSTR_ALLOCA_ALLOC(lcname, ZSTR_LEN(fname) - 1, use_heap);
- zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(fname) + 1, ZSTR_LEN(fname) - 1);
- fptr = zend_fetch_function(lcname);
- ZSTR_ALLOCA_FREE(lcname, use_heap);
- } else {
- lcname = zend_string_tolower(fname);
- fptr = zend_fetch_function(lcname);
- zend_string_release(lcname);
- }
- if (fptr == NULL) {
- zend_throw_exception_ex(reflection_exception_ptr, 0,
- "Function %s() does not exist", ZSTR_VAL(fname));
- RETURN_THROWS();
- }
- }
- ZVAL_STR_COPY(reflection_prop_name(object), fptr->common.function_name);
- intern->ptr = fptr;
- intern->ref_type = REF_TYPE_FUNCTION;
- if (closure) {
- ZVAL_OBJ(&intern->obj, Z_OBJ_P(closure));
- } else {
- ZVAL_UNDEF(&intern->obj);
- }
- intern->ce = NULL;
- }
- /* }}} */
- /* {{{ proto public string ReflectionFunction::__toString()
- Returns a string representation */
- ZEND_METHOD(ReflectionFunction, __toString)
- {
- reflection_object *intern;
- zend_function *fptr;
- smart_str str = {0};
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- _function_string(&str, fptr, intern->ce, "");
- RETURN_STR(smart_str_extract(&str));
- }
- /* }}} */
- /* {{{ proto public string ReflectionFunction::getName()
- Returns this function's name */
- ZEND_METHOD(ReflectionFunctionAbstract, getName)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- RETURN_STR_COPY(fptr->common.function_name);
- }
- /* }}} */
- /* {{{ proto public bool ReflectionFunction::isClosure()
- Returns whether this is a closure */
- ZEND_METHOD(ReflectionFunctionAbstract, isClosure)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- RETURN_BOOL(fptr->common.fn_flags & ZEND_ACC_CLOSURE);
- }
- /* }}} */
- /* {{{ proto public bool ReflectionFunction::getClosureThis()
- Returns this pointer bound to closure */
- ZEND_METHOD(ReflectionFunctionAbstract, getClosureThis)
- {
- reflection_object *intern;
- zval* closure_this;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT();
- if (!Z_ISUNDEF(intern->obj)) {
- closure_this = zend_get_closure_this_ptr(&intern->obj);
- if (!Z_ISUNDEF_P(closure_this)) {
- Z_ADDREF_P(closure_this);
- ZVAL_OBJ(return_value, Z_OBJ_P(closure_this));
- }
- }
- }
- /* }}} */
- /* {{{ proto public ReflectionClass ReflectionFunction::getClosureScopeClass()
- Returns the scope associated to the closure */
- ZEND_METHOD(ReflectionFunctionAbstract, getClosureScopeClass)
- {
- reflection_object *intern;
- const zend_function *closure_func;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT();
- if (!Z_ISUNDEF(intern->obj)) {
- closure_func = zend_get_closure_method_def(&intern->obj);
- if (closure_func && closure_func->common.scope) {
- zend_reflection_class_factory(closure_func->common.scope, return_value);
- }
- }
- }
- /* }}} */
- /* {{{ proto public mixed ReflectionFunction::getClosure()
- Returns a dynamically created closure for the function */
- ZEND_METHOD(ReflectionFunction, getClosure)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- if (!Z_ISUNDEF(intern->obj)) {
- /* Closures are immutable objects */
- Z_ADDREF(intern->obj);
- ZVAL_OBJ(return_value, Z_OBJ(intern->obj));
- } else {
- zend_create_fake_closure(return_value, fptr, NULL, NULL, NULL);
- }
- }
- /* }}} */
- /* {{{ proto public bool ReflectionFunction::isInternal()
- Returns whether this is an internal function */
- ZEND_METHOD(ReflectionFunctionAbstract, isInternal)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- RETURN_BOOL(fptr->type == ZEND_INTERNAL_FUNCTION);
- }
- /* }}} */
- /* {{{ proto public bool ReflectionFunction::isUserDefined()
- Returns whether this is a user-defined function */
- ZEND_METHOD(ReflectionFunctionAbstract, isUserDefined)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- RETURN_BOOL(fptr->type == ZEND_USER_FUNCTION);
- }
- /* }}} */
- /* {{{ proto public bool ReflectionFunction::isDisabled()
- Returns whether this function has been disabled or not */
- ZEND_METHOD(ReflectionFunction, isDisabled)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- /* A disabled function cannot be queried using Reflection. */
- RETURN_FALSE;
- }
- /* }}} */
- /* {{{ proto public string ReflectionFunction::getFileName()
- Returns the filename of the file this function was declared in */
- ZEND_METHOD(ReflectionFunctionAbstract, getFileName)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- if (fptr->type == ZEND_USER_FUNCTION) {
- RETURN_STR_COPY(fptr->op_array.filename);
- }
- RETURN_FALSE;
- }
- /* }}} */
- /* {{{ proto public int ReflectionFunction::getStartLine()
- Returns the line this function's declaration starts at */
- ZEND_METHOD(ReflectionFunctionAbstract, getStartLine)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- GET_REFLECTION_OBJECT_PTR(fptr);
- if (fptr->type == ZEND_USER_FUNCTION) {
- RETURN_LONG(fptr->op_array.line_start);
- }
- RETURN_FALSE;
- }
- /* }}} */
- /* {{{ proto public int ReflectionFunction::getEndLine()
- Returns the line this function's declaration ends at */
- ZEND_METHOD(ReflectionFunctionAbstract, getEndLine)
- {
- reflection_object *intern;
- zend_function *fptr;
- if (zend_parse_parameters_none() == FAILURE)…
Large files files are truncated, but you can click here to view the full file