Permalink
Browse files

extend the parameter parsing API by two functions

for parsing method parameters with automatic
detection if the function was called as such or as
a class method (with a valid this ptr).
if called as a function the first parameter has to be
the object it is operating on, if called as a method
this is used.

#not yet testet, only commiting so that georg can
#continue working on ext/mysqli
  • Loading branch information...
1 parent ccf8365 commit c4528f82a0ab47613b7e94aeb42b23d3e90a499f Harald Radi committed Feb 2, 2003
Showing with 88 additions and 0 deletions.
  1. +85 −0 Zend/zend_API.c
  2. +3 −0 Zend/zend_API.h
View
@@ -563,6 +563,91 @@ ZEND_API int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...)
return retval;
}
+ZEND_API int zend_parse_method_parameters(int num_args TSRMLS_DC, zval *this_ptr, char *type_spec, zend_class_entry *ce, zval **object, ...)
+{
+ void **arg_stack = EG(argument_stack).top_element;
+ va_list va;
+ int retval;
+
+ *object = this_ptr;
+
+ if (!*object) {
+ zval **parameter;
+
+ if (zend_get_parameters_ex(1, &parameter) != SUCCESS) {
+ zend_error(E_WARNING, "%s() expects an object of class %s as first parameter, none was given",
+ get_active_function_name(TSRMLS_C), ce->name);
+
+ return FAILURE;
+ } else {
+ if (!zend_check_class(*parameter, ce)) {
+ zend_error(E_WARNING, "%s() expects parameter 1 to be %s, %s given",
+ get_active_function_name(TSRMLS_C), ce->name,
+ zend_zval_type_name(*parameter));
+
+ return FAILURE;
+ } else {
+ *object = *parameter;
+ }
+
+ EG(argument_stack).top_element++;
+ }
+ }
+
+ va_start(va, type_spec);
+ retval = zend_parse_va_args(num_args, type_spec, &va, 0 TSRMLS_CC);
+ va_end(va);
+
+ EG(argument_stack).top_element = arg_stack;
+
+ return retval;
+}
+
+ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args TSRMLS_DC, zval *this_ptr, char *type_spec, zend_class_entry *ce, void **object, ...)
+{
+ void **arg_stack = EG(argument_stack).top_element;
+ va_list va;
+ int retval;
+ int quiet = flags & ZEND_PARSE_PARAMS_QUIET;
+
+ *object = this_ptr;
+
+ if (!*object) {
+ zval **parameter;
+
+ if (zend_get_parameters_ex(1, &parameter) != SUCCESS) {
+ if (!quiet) {
+ zend_error(E_WARNING, "%s() expects an object of class %s as first parameter, none was given",
+ get_active_function_name(TSRMLS_C), ce->name);
+ }
+
+ return FAILURE;
+ } else {
+ if (!zend_check_class(*parameter, ce)) {
+ if (!quiet) {
+ zend_error(E_WARNING, "%s() expects parameter 1 to be %s, %s given",
+ get_active_function_name(TSRMLS_C), ce->name,
+ zend_zval_type_name(*parameter));
+ }
+
+ return FAILURE;
+ } else {
+ *object = *parameter;
+ }
+
+ EG(argument_stack).top_element++;
+ }
+ }
+
+ va_start(va, type_spec);
+ retval = zend_parse_va_args(num_args, type_spec, &va, flags TSRMLS_CC);
+ va_end(va);
+
+ EG(argument_stack).top_element = arg_stack;
+
+ return retval;
+}
+
/* Argument parsing API -- andrei */
View
@@ -123,6 +123,9 @@ ZEND_API int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...)
ZEND_API int zend_parse_parameters_ex(int flags, int num_args TSRMLS_DC, char *type_spec, ...);
ZEND_API char *zend_zval_type_name(zval *arg);
+ZEND_API int zend_parse_method_parameters(int num_args TSRMLS_DC, zval *this_ptr, char *type_spec, zend_class_entry *ce, void **object, ...);
+ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args TSRMLS_DC, zval *this_ptr, char *type_spec, zend_class_entry *ce, void **object, ...);
+
/* End of parameter parsing API -- andrei */
int zend_register_functions(zend_class_entry *scope, zend_function_entry *functions, HashTable *function_table, int type TSRMLS_DC);

0 comments on commit c4528f8

Please sign in to comment.