Use known strings for reflection properties

This commit is contained in:
Nikita Popov 2017-10-31 15:20:34 +01:00
parent 98d6d1189c
commit 5b044aacbe
2 changed files with 51 additions and 50 deletions

View File

@ -418,6 +418,7 @@ EMPTY_SWITCH_DEFAULT_CASE()
_(ZEND_STR_ARRAY, "array") \
_(ZEND_STR_RESOURCE, "resource") \
_(ZEND_STR_CLOSED_RESOURCE, "resource (closed)") \
_(ZEND_STR_NAME, "name") \
typedef enum _zend_known_string_id {

View File

@ -46,12 +46,18 @@
#define reflection_update_property(object, name, value) do { \
zval member; \
ZVAL_STRINGL(&member, name, sizeof(name)-1); \
ZVAL_STR(&member, name); \
zend_std_write_property(object, &member, value, NULL); \
if (Z_REFCOUNTED_P(value)) Z_DELREF_P(value); \
zval_ptr_dtor(&member); \
} while (0)
#define reflection_update_property_name(object, value) \
reflection_update_property(object, ZSTR_KNOWN(ZEND_STR_NAME), value)
#define reflection_update_property_class(object, value) \
reflection_update_property(object, ZSTR_KNOWN(ZEND_STR_CLASS), value)
/* Class entry pointers */
PHPAPI zend_class_entry *reflector_ptr;
PHPAPI zend_class_entry *reflection_exception_ptr;
@ -157,22 +163,17 @@ static inline reflection_object *reflection_object_from_obj(zend_object *obj) {
static zend_object_handlers reflection_object_handlers;
static zval *_default_load_entry(zval *object, char *name, size_t name_len) /* {{{ */
static zval *_default_load_name(zval *object) /* {{{ */
{
zval *value;
if ((value = zend_hash_str_find_ind(Z_OBJPROP_P(object), name, name_len)) == NULL) {
return NULL;
}
return value;
return zend_hash_find_ind(Z_OBJPROP_P(object), ZSTR_KNOWN(ZEND_STR_NAME));
}
/* }}} */
static void _default_get_entry(zval *object, char *name, int name_len, zval *return_value) /* {{{ */
static void _default_get_name(zval *object, zval *return_value) /* {{{ */
{
zval *value;
if ((value = _default_load_entry(object, name, name_len)) == NULL) {
if ((value = _default_load_name(object)) == NULL) {
RETURN_FALSE;
}
ZVAL_COPY(return_value, value);
@ -1113,7 +1114,7 @@ PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object)
intern->ptr = ce;
intern->ref_type = REF_TYPE_OTHER;
intern->ce = ce;
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
}
/* }}} */
@ -1140,7 +1141,7 @@ static void reflection_extension_factory(zval *object, const char *name_str)
intern->ptr = module;
intern->ref_type = REF_TYPE_OTHER;
intern->ce = NULL;
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
}
/* }}} */
@ -1175,7 +1176,7 @@ static void reflection_parameter_factory(zend_function *fptr, zval *closure_obje
Z_ADDREF_P(closure_object);
ZVAL_COPY_VALUE(&intern->obj, closure_object);
}
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
}
/* }}} */
@ -1217,7 +1218,7 @@ static void reflection_function_factory(zend_function *function, zval *closure_o
Z_ADDREF_P(closure_object);
ZVAL_COPY_VALUE(&intern->obj, closure_object);
}
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
}
/* }}} */
@ -1240,8 +1241,8 @@ static void reflection_method_factory(zend_class_entry *ce, zend_function *metho
Z_ADDREF_P(closure_object);
ZVAL_COPY_VALUE(&intern->obj, closure_object);
}
reflection_update_property(object, "name", &name);
reflection_update_property(object, "class", &classname);
reflection_update_property_name(object, &name);
reflection_update_property_class(object, &classname);
}
/* }}} */
@ -1286,8 +1287,8 @@ static void reflection_property_factory(zend_class_entry *ce, zend_property_info
intern->ref_type = REF_TYPE_PROPERTY;
intern->ce = ce;
intern->ignore_visibility = 0;
reflection_update_property(object, "name", &name);
reflection_update_property(object, "class", &classname);
reflection_update_property_name(object, &name);
reflection_update_property_class(object, &classname);
}
/* }}} */
@ -1307,8 +1308,8 @@ static void reflection_class_constant_factory(zend_class_entry *ce, zend_string
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
intern->ce = constant->ce;
intern->ignore_visibility = 0;
reflection_update_property(object, "name", &name);
reflection_update_property(object, "class", &classname);
reflection_update_property_name(object, &name);
reflection_update_property_class(object, &classname);
}
/* }}} */
@ -1587,7 +1588,7 @@ ZEND_METHOD(reflection_function, __construct)
}
ZVAL_STR_COPY(&name, fptr->common.function_name);
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
intern->ptr = fptr;
intern->ref_type = REF_TYPE_FUNCTION;
if (closure) {
@ -1623,7 +1624,7 @@ ZEND_METHOD(reflection_function, getName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
_default_get_entry(getThis(), "name", sizeof("name")-1, return_value);
_default_get_name(getThis(), return_value);
}
/* }}} */
@ -2457,7 +2458,7 @@ ZEND_METHOD(reflection_parameter, __construct)
} else {
ZVAL_NULL(&name);
}
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
ref = (parameter_reference*) emalloc(sizeof(parameter_reference));
ref->arg_info = &arg_info[position];
@ -2499,7 +2500,7 @@ ZEND_METHOD(reflection_parameter, getName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
_default_get_entry(getThis(), "name", sizeof("name")-1, return_value);
_default_get_name(getThis(), return_value);
}
/* }}} */
@ -3056,9 +3057,9 @@ ZEND_METHOD(reflection_method, __construct)
efree(lcname);
ZVAL_STR_COPY(&name, mptr->common.scope->name);
reflection_update_property(object, "class", &name);
reflection_update_property_class(object, &name);
ZVAL_STR_COPY(&name, mptr->common.function_name);
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
intern->ptr = mptr;
intern->ref_type = REF_TYPE_FUNCTION;
intern->ce = ce;
@ -3344,7 +3345,7 @@ ZEND_METHOD(reflection_function, inNamespace)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((name = _default_load_entry(getThis(), "name", sizeof("name")-1)) == NULL) {
if ((name = _default_load_name(getThis())) == NULL) {
RETURN_FALSE;
}
if (Z_TYPE_P(name) == IS_STRING
@ -3367,7 +3368,7 @@ ZEND_METHOD(reflection_function, getNamespaceName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((name = _default_load_entry(getThis(), "name", sizeof("name")-1)) == NULL) {
if ((name = _default_load_name(getThis())) == NULL) {
RETURN_FALSE;
}
if (Z_TYPE_P(name) == IS_STRING
@ -3390,7 +3391,7 @@ ZEND_METHOD(reflection_function, getShortName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((name = _default_load_entry(getThis(), "name", sizeof("name")-1)) == NULL) {
if ((name = _default_load_name(getThis())) == NULL) {
RETURN_FALSE;
}
if (Z_TYPE_P(name) == IS_STRING
@ -3600,8 +3601,8 @@ ZEND_METHOD(reflection_class_constant, __construct)
intern->ref_type = REF_TYPE_CLASS_CONSTANT;
intern->ce = constant->ce;
intern->ignore_visibility = 0;
reflection_update_property(object, "name", &name);
reflection_update_property(object, "class", &cname);
reflection_update_property_name(object, &name);
reflection_update_property_class(object, &cname);
}
/* }}} */
@ -3618,7 +3619,7 @@ ZEND_METHOD(reflection_class_constant, __toString)
return;
}
GET_REFLECTION_OBJECT_PTR(ref);
_default_get_entry(getThis(), "name", sizeof("name")-1, &name);
_default_get_name(getThis(), &name);
_class_const_string(&str, Z_STRVAL(name), ref, "");
zval_ptr_dtor(&name);
RETURN_STR(smart_str_extract(&str));
@ -3632,7 +3633,7 @@ ZEND_METHOD(reflection_class_constant, getName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
_default_get_entry(getThis(), "name", sizeof("name")-1, return_value);
_default_get_name(getThis(), return_value);
}
/* }}} */
@ -3764,7 +3765,7 @@ static void reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS, int is_ob
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z/", &argument) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &argument) == FAILURE) {
return;
}
}
@ -3774,11 +3775,10 @@ static void reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS, int is_ob
if (Z_TYPE_P(argument) == IS_OBJECT) {
ZVAL_STR_COPY(&classname, Z_OBJCE_P(argument)->name);
reflection_update_property(object, "name", &classname);
reflection_update_property_name(object, &classname);
intern->ptr = Z_OBJCE_P(argument);
if (is_object) {
ZVAL_COPY_VALUE(&intern->obj, argument);
zval_add_ref(argument);
ZVAL_COPY(&intern->obj, argument);
}
} else {
convert_to_string_ex(argument);
@ -3790,7 +3790,7 @@ static void reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS, int is_ob
}
ZVAL_STR_COPY(&classname, ce->name);
reflection_update_property(object, "name", &classname);
reflection_update_property_name(object, &classname);
intern->ptr = ce;
}
@ -3979,7 +3979,7 @@ ZEND_METHOD(reflection_class, getName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
_default_get_entry(getThis(), "name", sizeof("name")-1, return_value);
_default_get_name(getThis(), return_value);
}
/* }}} */
@ -5229,7 +5229,7 @@ ZEND_METHOD(reflection_class, inNamespace)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((name = _default_load_entry(getThis(), "name", sizeof("name")-1)) == NULL) {
if ((name = _default_load_name(getThis())) == NULL) {
RETURN_FALSE;
}
if (Z_TYPE_P(name) == IS_STRING
@ -5252,7 +5252,7 @@ ZEND_METHOD(reflection_class, getNamespaceName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((name = _default_load_entry(getThis(), "name", sizeof("name")-1)) == NULL) {
if ((name = _default_load_name(getThis())) == NULL) {
RETURN_FALSE;
}
if (Z_TYPE_P(name) == IS_STRING
@ -5275,7 +5275,7 @@ ZEND_METHOD(reflection_class, getShortName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((name = _default_load_entry(getThis(), "name", sizeof("name")-1)) == NULL) {
if ((name = _default_load_name(getThis())) == NULL) {
RETURN_FALSE;
}
if (Z_TYPE_P(name) == IS_STRING
@ -5396,8 +5396,8 @@ ZEND_METHOD(reflection_property, __construct)
ZVAL_STR_COPY(&cname, ce->name);
ZVAL_STRINGL(&propname, name_str, name_len);
}
reflection_update_property(object, "class", &cname);
reflection_update_property(object, "name", &propname);
reflection_update_property_class(object, &cname);
reflection_update_property_name(object, &propname);
reference = (property_reference*) emalloc(sizeof(property_reference));
if (dynam_prop) {
@ -5440,7 +5440,7 @@ ZEND_METHOD(reflection_property, getName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
_default_get_entry(getThis(), "name", sizeof("name")-1, return_value);
_default_get_name(getThis(), return_value);
}
/* }}} */
@ -5527,7 +5527,7 @@ ZEND_METHOD(reflection_property, getValue)
GET_REFLECTION_OBJECT_PTR(ref);
if (!(ref->prop.flags & (ZEND_ACC_PUBLIC | ZEND_ACC_IMPLICIT_PUBLIC)) && intern->ignore_visibility == 0) {
name = _default_load_entry(getThis(), "name", sizeof("name")-1);
name = _default_load_name(getThis());
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Cannot access non-public member %s::%s", ZSTR_VAL(intern->ce->name), Z_STRVAL_P(name));
return;
@ -5588,7 +5588,7 @@ ZEND_METHOD(reflection_property, setValue)
GET_REFLECTION_OBJECT_PTR(ref);
if (!(ref->prop.flags & ZEND_ACC_PUBLIC) && intern->ignore_visibility == 0) {
name = _default_load_entry(getThis(), "name", sizeof("name")-1);
name = _default_load_name(getThis());
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Cannot access non-public member %s::%s", ZSTR_VAL(intern->ce->name), Z_STRVAL_P(name));
return;
@ -5745,7 +5745,7 @@ ZEND_METHOD(reflection_extension, __construct)
}
free_alloca(lcname, use_heap);
ZVAL_STRING(&name, module->name);
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
intern->ptr = module;
intern->ref_type = REF_TYPE_OTHER;
intern->ce = NULL;
@ -5776,7 +5776,7 @@ ZEND_METHOD(reflection_extension, getName)
if (zend_parse_parameters_none() == FAILURE) {
return;
}
_default_get_entry(getThis(), "name", sizeof("name")-1, return_value);
_default_get_name(getThis(), return_value);
}
/* }}} */
@ -6107,7 +6107,7 @@ ZEND_METHOD(reflection_zend_extension, __construct)
return;
}
ZVAL_STRING(&name, extension->name);
reflection_update_property(object, "name", &name);
reflection_update_property_name(object, &name);
intern->ptr = extension;
intern->ref_type = REF_TYPE_OTHER;
intern->ce = NULL;