Mercurial > octave-nkf
changeset 14139:8758dfa21cac
maint: periodic merge of stable to default
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:29:37 -0500 |
parents | a74954d70c18 (diff) 72c96de7a403 (current diff) |
children | b6d5c60c941c |
files | src/ov-usr-fcn.cc test/test_contin.m |
diffstat | 1 files changed, 92 insertions(+), 46 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ov-usr-fcn.cc Mon Jan 02 14:25:41 2012 -0500 +++ b/src/ov-usr-fcn.cc Mon Jan 02 14:29:37 2012 -0500 @@ -51,6 +51,7 @@ #include "parse.h" #include "profiler.h" #include "variables.h" +#include "ov-fcn-handle.h" // Whether to optimize subsasgn method calls. static bool Voptimize_subsasgn_calls = true; @@ -629,12 +630,15 @@ DEFUN (nargin, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} nargin ()\n\ -@deftypefnx {Built-in Function} {} nargin (@var{fcn_name})\n\ +@deftypefnx {Built-in Function} {} nargin (@var{fcn})\n\ Within a function, return the number of arguments passed to the function.\n\ At the top level, return the number of command line arguments passed to\n\ -Octave. If called with the optional argument @var{fcn_name}, return the\n\ -maximum number of arguments the named function can accept, or -1 if the\n\ -function accepts a variable number of arguments.\n\ +Octave.\n\ +\n\ +If called with the optional argument @var{fcn}, a function name or handle,\n\ +return the declared number of arguments that the function can accept.\n\ +If the last argument is @var{varargin} the returned value is negative.\n\ +This feature does not work on builtin functions.\n\ @seealso{nargout, varargin, isargout, varargout, nthargout}\n\ @end deftypefn") { @@ -644,40 +648,37 @@ if (nargin == 1) { - std::string fname = args(0).string_value (); + octave_value func = args(0); - if (! error_state) + if (func.is_string ()) { - octave_value fcn_val = symbol_table::find_function (fname); - - if (fcn_val.is_user_function ()) - { - octave_user_function *fcn = fcn_val.user_function_value (true); + std::string name = func.string_value (); + func = symbol_table::find_function (name); + if (func.is_undefined ()) + error ("nargout: invalid function name: %s", name.c_str ()); + } - if (fcn) - { - if (fcn->takes_varargs ()) - retval = -1; - else - { - tree_parameter_list *param_list = fcn->parameter_list (); + octave_function *fcn_val = func.function_value (); + if (fcn_val) + { + octave_user_function *fcn = fcn_val->user_function_value (true); - retval = param_list ? param_list->length () : 0; - } - } - else - error ("nargin: loading user-defined function failed"); + if (fcn) + { + tree_parameter_list *param_list = fcn->parameter_list (); + + retval = param_list ? param_list->length () : 0; + if (fcn->takes_varargs ()) + retval = -1 - retval; } else { - // FIXME -- what about built-in functions or functions - // defined in .oct files or .mex files? - - error ("nargin: FCN_NAME must be a user-defined function"); + // Matlab gives up for histc, so maybe it's ok we give up somtimes too. + error ("nargin: nargin information not available for builtin functions"); } } else - error ("nargin: FCN_NAME must be a string"); + error ("nargin: FCN must be a string or function handle"); } else if (nargin == 0) { @@ -695,11 +696,12 @@ DEFUN (nargout, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} nargout ()\n\ -@deftypefnx {Built-in Function} {} nargout (@var{fcn_name})\n\ +@deftypefnx {Built-in Function} {} nargout (@var{fcn})\n\ Within a function, return the number of values the caller expects to\n\ -receive. If called with the optional argument @var{fcn_name}, return the\n\ -maximum number of values the named function can produce, or -1 if the\n\ -function can produce a variable number of values.\n\ +receive. If called with the optional argument @var{fcn}, a function\n\ +name or handle, return the number of declared output values that the\n\ +function can produce. If the final output argument is @var{varargout}\n\ +the returned value is negative.\n\ \n\ For example,\n\ \n\ @@ -718,7 +720,23 @@ will cause @code{nargout} to return 2 inside the function\n\ @code{f}.\n\ \n\ -At the top level, @code{nargout} is undefined.\n\ +In the second usage,\n\ +\n\ +@example\n\ +nargout (@@histc) \% or nargout ('histc')\n\ +@end example\n\ +\n\ +will return 2, because @code{histc} has two outputs, whereas\n\ +\n\ +@example\n\ +nargout (@@deal)\n\ +@end example\n\ +\n\ +will return -1, because @code{deal} has a variable number of outputs.\n\ +\n\ +At the top level, @code{nargout} with no argument is undefined.\n\ +@code{nargout} does not work on builtin functions.\n\ +@code{nargout} returns -1 for all anonymous functions.\n\ @seealso{nargin, varargin, isargout, varargout, nthargout}\n\ @end deftypefn") { @@ -728,30 +746,58 @@ if (nargin == 1) { - std::string fname = args(0).string_value (); + octave_value func = args(0); - if (! error_state) + if (func.is_string ()) + { + std::string name = func.string_value (); + func = symbol_table::find_function (name); + if (func.is_undefined ()) + error ("nargout: invalid function name: %s", name.c_str ()); + } + + if (func.is_inline_function ()) { - octave_value fcn_val = symbol_table::find_user_function (fname); + retval = 1; + return retval; + } + + if (func.is_function_handle ()) + { + octave_fcn_handle *fh = func.fcn_handle_value (); + std::string fh_nm = fh->fcn_name (); - octave_user_function *fcn = fcn_val.user_function_value (true); + if (fh_nm == octave_fcn_handle::anonymous) + { + retval = -1; + return retval; + } + } + + octave_function *fcn_val = func.function_value (); + if (fcn_val) + { + octave_user_function *fcn = fcn_val->user_function_value (true); if (fcn) { + tree_parameter_list *ret_list = fcn->return_list (); + + retval = ret_list ? ret_list->length () : 0; + if (fcn->takes_var_return ()) - retval = -1; - else - { - tree_parameter_list *ret_list = fcn->return_list (); - - retval = ret_list ? ret_list->length () : 0; - } + retval = -1 - retval; } else - error ("nargout: invalid function"); + { + // JWE said this information is not available (currently, 2011-03-10) + // without making intrusive changes to Octave. + // Matlab gives up for histc, so maybe it's ok we give up somtimes too. + error ("nargout: nargout information not available for builtin functions."); + } } else - error ("nargout: FCN_NAME must be a string"); + error ("nargout: FCN must be a string or function handle"); } else if (nargin == 0) {