Skip to content

Commit

Permalink
- Add minimalistic closure support
Browse files Browse the repository at this point in the history
  • Loading branch information
helly25 committed Jan 3, 2009
1 parent de5a4f7 commit 0d02658
Showing 1 changed file with 63 additions and 19 deletions.
82 changes: 63 additions & 19 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ static zval * reflection_instantiate(zend_class_entry *pce, zval *object TSRMLS_
/* }}} */

static void _const_string(string *str, zstr name, zval *value, char *indent TSRMLS_DC);
static void _function_string(string *str, zend_function *fptr, zend_class_entry *scope, char *indent TSRMLS_DC);
static void _function_string(string *str, zend_function *fptr, zend_class_entry *scope, zval* closure, char *indent TSRMLS_DC);
static void _property_string(string *str, zend_property_info *prop, zstr prop_name, char* indent TSRMLS_DC);
static void _class_string(string *str, zend_class_entry *ce, zval *obj, char *indent TSRMLS_DC);
static void _extension_string(string *str, zend_module_entry *module, char *indent TSRMLS_DC);
Expand Down Expand Up @@ -489,7 +489,7 @@ static void _class_string(string *str, zend_class_entry *ce, zval *obj, char *in
&& ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) == 0 || mptr->common.scope == ce))
{
string_printf(str, "\n");
_function_string(str, mptr, ce, sub_indent.string TSRMLS_CC);
_function_string(str, mptr, ce, NULL, sub_indent.string TSRMLS_CC);
}
zend_hash_move_forward_ex(&ce->function_table, &pos);
}
Expand Down Expand Up @@ -595,7 +595,7 @@ static void _class_string(string *str, zend_class_entry *ce, zval *obj, char *in
}

string_printf(&dyn, "\n");
_function_string(&dyn, mptr, ce, sub_indent.string TSRMLS_CC);
_function_string(&dyn, mptr, ce, NULL, sub_indent.string TSRMLS_CC);
count++;
_free_function(closure TSRMLS_CC);
}
Expand Down Expand Up @@ -767,7 +767,7 @@ static void _function_parameter_string(string *str, zend_function *fptr, char* i
/* }}} */

/* {{{ _function_string */
static void _function_string(string *str, zend_function *fptr, zend_class_entry *scope, char* indent TSRMLS_DC)
static void _function_string(string *str, zend_function *fptr, zend_class_entry *scope, zval* closure, char* indent TSRMLS_DC)
{
string param_indent;
zend_function *overwrites;
Expand All @@ -782,7 +782,8 @@ static void _function_string(string *str, zend_function *fptr, zend_class_entry
string_printf(str, "%s%v\n", indent, fptr->op_array.doc_comment.v);
}

string_printf(str, fptr->common.scope ? "%sMethod [ " : "%sFunction [ ", indent);
string_write(str, indent, strlen(indent));
string_printf(str, closure ? "Closure [ " : (fptr->common.scope ? "Method [ " : "Function [ "));
string_printf(str, (fptr->type == ZEND_USER_FUNCTION) ? "<user" : "<internal");
if (fptr->common.fn_flags & ZEND_ACC_DEPRECATED) {
string_printf(str, ", deprecated");
Expand Down Expand Up @@ -858,6 +859,28 @@ static void _function_string(string *str, zend_function *fptr, zend_class_entry
}
string_init(&param_indent);
string_printf(&param_indent, "%s ", indent);
if (closure) {
const zend_function *closure_fptr = zend_get_closure_method_def(closure TSRMLS_CC);
if (closure_fptr->type == ZEND_USER_FUNCTION && closure_fptr->op_array.static_variables) {
HashTable *static_variables = closure_fptr->op_array.static_variables;
HashPosition pos;
uint key_len;
zstr key;
ulong num_index, index = 0;
int count = zend_hash_num_elements(static_variables);
if (count) {
string_printf(str, "\n");
string_printf(str, "%s - Static Parameters [%d] {\n", indent, count);
zend_hash_internal_pointer_reset_ex(static_variables, &pos);
while (index++ < count) {
zend_hash_get_current_key_ex(static_variables, &key, &key_len, &num_index, 0, &pos);
string_printf(str, "%s Parameter #%d [ $%v ]\n", indent, index++, key);
zend_hash_move_forward_ex(static_variables, &pos);
}
string_printf(str, "%s }\n", indent);
}
}
}
_function_parameter_string(str, fptr, param_indent.string TSRMLS_CC);
string_free(&param_indent);
string_printf(str, "%s}\n", indent);
Expand Down Expand Up @@ -1062,7 +1085,7 @@ static void _extension_string(string *str, zend_module_entry *module, char *inde
continue;
}

_function_string(str, fptr, NULL, " " TSRMLS_CC);
_function_string(str, fptr, NULL, NULL, " " TSRMLS_CC);
func++;
}
string_printf(str, "%s }\n", indent);
Expand Down Expand Up @@ -1465,6 +1488,7 @@ ZEND_METHOD(reflection_function, __construct)
{
zval *name;
zval *object;
zval *closure = NULL;
unsigned int lcname_len;
zstr lcname;
reflection_object *intern;
Expand All @@ -1473,28 +1497,34 @@ ZEND_METHOD(reflection_function, __construct)
int name_len;
zend_uchar type;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &name_str, &name_len, &type) == FAILURE) {
return;
}

object = getThis();
intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC);
if (intern == NULL) {
return;
}
lcname = zend_u_str_case_fold(type, name_str, name_len, 1, &lcname_len);
if (zend_u_hash_find(EG(function_table), type, lcname, lcname_len + 1, (void **)&fptr) == FAILURE) {

if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "O", &closure, zend_ce_closure) == SUCCESS) {
fptr = zend_get_closure_invoke_method(closure TSRMLS_CC);
Z_ADDREF_P(closure);
} else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &name_str, &name_len, &type) == SUCCESS) {
lcname = zend_u_str_case_fold(type, name_str, name_len, 1, &lcname_len);
if (zend_u_hash_find(EG(function_table), type, lcname, lcname_len + 1, (void **)&fptr) == FAILURE) {
efree(lcname.v);
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC,
"Function %R() does not exist", type, name_str);
return;
}
efree(lcname.v);
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC,
"Function %R() does not exist", type, name_str);
} else {
return;
}
efree(lcname.v);

MAKE_STD_ZVAL(name);
ZVAL_TEXT(name, fptr->common.function_name, 1);
zend_ascii_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) &name, sizeof(zval *), NULL);
intern->ptr = fptr;
intern->ref_type = REF_TYPE_FUNCTION;
intern->obj = closure;
intern->ce = NULL;
}
/* }}} */
Expand All @@ -1510,23 +1540,36 @@ ZEND_METHOD(reflection_function, __toString)
METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0);
GET_REFLECTION_OBJECT_PTR(fptr);
string_init(&str);
_function_string(&str, fptr, intern->ce, "" TSRMLS_CC);
_function_string(&str, fptr, intern->ce, intern->obj, "" TSRMLS_CC);
RETURN_U_STRINGL(ZEND_U_CONVERTER(UG(output_encoding_conv)), str.string, str.len - 1, ZSTR_AUTOFREE);
}
/* }}} */

/* {{{ proto public string ReflectionFunction::getName() U
Returns this function's name */
ZEND_METHOD(reflection, function_getName)
ZEND_METHOD(reflection_function, getName)
{
METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0);
_default_get_entry(getThis(), "name", sizeof("name"), return_value TSRMLS_CC);
}
/* }}} */

/* {{{ proto public bool ReflectionFunction::isClosure() U
Returns whether this is a closure */
ZEND_METHOD(reflection_function, isClosure)
{
reflection_object *intern;
zend_function *fptr;

METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0);
GET_REFLECTION_OBJECT_PTR(fptr);
RETURN_BOOL(intern->obj);
}
/* }}} */

/* {{{ proto public bool ReflectionFunction::isInternal() U
Returns whether this is an internal function */
ZEND_METHOD(reflection, function_isInternal)
ZEND_METHOD(reflection_function, isInternal)
{
reflection_object *intern;
zend_function *fptr;
Expand Down Expand Up @@ -2468,7 +2511,7 @@ ZEND_METHOD(reflection_method, __toString)
METHOD_NOTSTATIC_NUMPARAMS(reflection_method_ptr, 0);
GET_REFLECTION_OBJECT_PTR(mptr);
string_init(&str);
_function_string(&str, mptr, intern->ce, "" TSRMLS_CC);
_function_string(&str, mptr, intern->ce, intern->obj, "" TSRMLS_CC);
RETURN_U_STRINGL(ZEND_U_CONVERTER(UG(output_encoding_conv)), str.string, str.len - 1, ZSTR_AUTOFREE);
}
/* }}} */
Expand Down Expand Up @@ -5013,6 +5056,7 @@ ZEND_END_ARG_INFO()
static const zend_function_entry reflection_function_abstract_functions[] = {
ZEND_ME(reflection, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
PHP_ABSTRACT_ME(reflection_function, __toString, NULL)
ZEND_ME(reflection_function, isClosure, NULL, 0)
ZEND_ME(reflection_function, isInternal, NULL, 0)
ZEND_ME(reflection_function, isUserDefined, NULL, 0)
ZEND_ME(reflection_function, getName, NULL, 0)
Expand Down

0 comments on commit 0d02658

Please sign in to comment.