]> granicus.if.org Git - php/commitdiff
- Implement #29728: Reflection API Feature: Default parameter value.
authorMarcus Boerger <helly@php.net>
Thu, 19 Aug 2004 07:42:02 +0000 (07:42 +0000)
committerMarcus Boerger <helly@php.net>
Thu, 19 Aug 2004 07:42:02 +0000 (07:42 +0000)
  . ReflectionParameter::isDefaultValueAvailable()
  . ReflectionParameter::getDefaultValue()

Zend/zend_reflection_api.c
ext/reflection/php_reflection.c

index 0cf78467c4a27d46d9654216bc2cc2d9086323e3..5af951427d8fca0bed29ca414db70de616538ee2 100644 (file)
@@ -1680,6 +1680,66 @@ ZEND_METHOD(reflection_parameter, isOptional)
 }
 /* }}} */
 
+/* {{{ proto public bool ReflectionParameter::isDefaultValueAvailable()
+   Returns whether the default value of this parameter is available */
+ZEND_METHOD(reflection_parameter, isDefaultValueAvailable)
+{
+       reflection_object *intern;
+       parameter_reference *param;
+       zend_op *precv;
+
+       METHOD_NOTSTATIC_NUMPARAMS(0);
+       GET_REFLECTION_OBJECT_PTR(param);
+
+       if (param->fptr->type != ZEND_USER_FUNCTION)
+       {
+               RETURN_FALSE;
+       }
+       if (param->offset < param->required) {
+               RETURN_FALSE;
+       }
+       precv = &((zend_op_array*)param->fptr)->opcodes[param->offset*2 + 1];
+       if (precv->opcode != ZEND_RECV_INIT || precv->op2.op_type == IS_UNUSED) {
+               RETURN_FALSE;
+       }
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto public bool ReflectionParameter::getDefaultValue()
+   Returns the default value of this parameter or throws an exception */
+ZEND_METHOD(reflection_parameter, getDefaultValue)
+{
+       reflection_object *intern;
+       parameter_reference *param;
+       zend_op *precv;
+       zval *zv, zv_copy;
+
+       METHOD_NOTSTATIC_NUMPARAMS(0);
+       GET_REFLECTION_OBJECT_PTR(param);
+
+       if (param->fptr->type != ZEND_USER_FUNCTION)
+       {
+               zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Cannot determine default value for internal functions");
+               return;
+       }
+       if (param->offset < param->required) {
+               zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Parameter is not optional"); 
+               return;
+       }
+       precv = &((zend_op_array*)param->fptr)->opcodes[param->offset*2 + 1];
+       if (precv->opcode != ZEND_RECV_INIT || precv->op2.op_type == IS_UNUSED) {
+               zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Internal error"); 
+               return;
+       }
+
+       zv_copy = precv->op2.u.constant;
+       zv = &zv_copy;
+       zval_update_constant(&zv, (void*)1 TSRMLS_CC);
+       RETURN_ZVAL(zv, 1, 1);
+}
+/* }}} */
+
 /* {{{ proto public static mixed ReflectionMethod::export(mixed class, string name, [, bool return]) throws ReflectionException
    Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */
 ZEND_METHOD(reflection_method, export)
@@ -3419,6 +3479,8 @@ static zend_function_entry reflection_parameter_functions[] = {
        ZEND_ME(reflection_parameter, getClass, NULL, 0)
        ZEND_ME(reflection_parameter, allowsNull, NULL, 0)
        ZEND_ME(reflection_parameter, isOptional, NULL, 0)
+       ZEND_ME(reflection_parameter, isDefaultValueAvailable, NULL, 0)
+       ZEND_ME(reflection_parameter, getDefaultValue, NULL, 0)
        {NULL, NULL, NULL}
 };
 
index 0cf78467c4a27d46d9654216bc2cc2d9086323e3..5af951427d8fca0bed29ca414db70de616538ee2 100644 (file)
@@ -1680,6 +1680,66 @@ ZEND_METHOD(reflection_parameter, isOptional)
 }
 /* }}} */
 
+/* {{{ proto public bool ReflectionParameter::isDefaultValueAvailable()
+   Returns whether the default value of this parameter is available */
+ZEND_METHOD(reflection_parameter, isDefaultValueAvailable)
+{
+       reflection_object *intern;
+       parameter_reference *param;
+       zend_op *precv;
+
+       METHOD_NOTSTATIC_NUMPARAMS(0);
+       GET_REFLECTION_OBJECT_PTR(param);
+
+       if (param->fptr->type != ZEND_USER_FUNCTION)
+       {
+               RETURN_FALSE;
+       }
+       if (param->offset < param->required) {
+               RETURN_FALSE;
+       }
+       precv = &((zend_op_array*)param->fptr)->opcodes[param->offset*2 + 1];
+       if (precv->opcode != ZEND_RECV_INIT || precv->op2.op_type == IS_UNUSED) {
+               RETURN_FALSE;
+       }
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto public bool ReflectionParameter::getDefaultValue()
+   Returns the default value of this parameter or throws an exception */
+ZEND_METHOD(reflection_parameter, getDefaultValue)
+{
+       reflection_object *intern;
+       parameter_reference *param;
+       zend_op *precv;
+       zval *zv, zv_copy;
+
+       METHOD_NOTSTATIC_NUMPARAMS(0);
+       GET_REFLECTION_OBJECT_PTR(param);
+
+       if (param->fptr->type != ZEND_USER_FUNCTION)
+       {
+               zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Cannot determine default value for internal functions");
+               return;
+       }
+       if (param->offset < param->required) {
+               zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Parameter is not optional"); 
+               return;
+       }
+       precv = &((zend_op_array*)param->fptr)->opcodes[param->offset*2 + 1];
+       if (precv->opcode != ZEND_RECV_INIT || precv->op2.op_type == IS_UNUSED) {
+               zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Internal error"); 
+               return;
+       }
+
+       zv_copy = precv->op2.u.constant;
+       zv = &zv_copy;
+       zval_update_constant(&zv, (void*)1 TSRMLS_CC);
+       RETURN_ZVAL(zv, 1, 1);
+}
+/* }}} */
+
 /* {{{ proto public static mixed ReflectionMethod::export(mixed class, string name, [, bool return]) throws ReflectionException
    Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */
 ZEND_METHOD(reflection_method, export)
@@ -3419,6 +3479,8 @@ static zend_function_entry reflection_parameter_functions[] = {
        ZEND_ME(reflection_parameter, getClass, NULL, 0)
        ZEND_ME(reflection_parameter, allowsNull, NULL, 0)
        ZEND_ME(reflection_parameter, isOptional, NULL, 0)
+       ZEND_ME(reflection_parameter, isDefaultValueAvailable, NULL, 0)
+       ZEND_ME(reflection_parameter, getDefaultValue, NULL, 0)
        {NULL, NULL, NULL}
 };