From d4e649f8bf34237958fb2518d7f487ef4f62f3b8 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 24 Aug 2022 16:49:10 +0900 Subject: [PATCH] class.c: refactoring mrb_get_args(); ref #5596 --- src/class.c | 117 +++++++++++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 60 deletions(-) diff --git a/src/class.c b/src/class.c index 5b806c090..14be8ca9d 100644 --- a/src/class.c +++ b/src/class.c @@ -890,68 +890,10 @@ mrb_block_given_p(mrb_state *mrb) return !mrb_nil_p(b); } -static mrb_int mrb_get_args_v(mrb_state *mrb, mrb_args_format format, void** ptr, va_list ap); - -/* - retrieve arguments from mrb_state. - - mrb_get_args(mrb, format, ...) - - returns number of arguments parsed. - - format specifiers: - - string mruby type C type note - ---------------------------------------------------------------------------------------------- - o: Object [mrb_value] - C: Class/Module [mrb_value] when ! follows, the value may be nil - S: String [mrb_value] when ! follows, the value may be nil - A: Array [mrb_value] when ! follows, the value may be nil - H: Hash [mrb_value] when ! follows, the value may be nil - s: String [const char*,mrb_int] Receive two arguments; s! gives (NULL,0) for nil - z: String [const char*] NUL terminated string; z! gives NULL for nil - a: Array [const mrb_value*,mrb_int] Receive two arguments; a! gives (NULL,0) for nil - c: Class/Module [strcut RClass*] c! gives NULL for nil - f: Integer/Float [mrb_float] - i: Integer/Float [mrb_int] - b: boolean [mrb_bool] - n: String/Symbol [mrb_sym] - d: data [void*,mrb_data_type const] 2nd argument will be used to check data type so it won't be modified; when ! follows, the value may be nil - &: block [mrb_value] &! raises exception if no block given - *: rest argument [const mrb_value*,mrb_int] The rest of the arguments as an array; *! avoid copy of the stack - |: optional Following arguments are optional - ?: optional given [mrb_bool] true if preceding argument (optional) is given - ':': keyword args [mrb_kwargs const] Get keyword arguments - - format modifiers: - - string note - ---------------------------------------------------------------------------------------------- - !: Switch to the alternate mode; The behaviour changes depending on the specifier - +: Request a not frozen object; However, except nil value - */ -MRB_API mrb_int -mrb_get_args(mrb_state *mrb, mrb_args_format format, ...) -{ - va_list ap; - va_start(ap, format); - mrb_int rc = mrb_get_args_v(mrb, format, NULL, ap); - va_end(ap); - return rc; -} - -MRB_API mrb_int -mrb_get_args_a(mrb_state *mrb, mrb_args_format format, void** args) -{ - va_list ap; - mrb_int rc = mrb_get_args_v(mrb, format, args, ap); - return rc; -} - #define GET_ARG(_type) (ptr ? ((_type)(*ptr++)) : va_arg(ap, _type)) -MRB_API mrb_int -mrb_get_args_v(mrb_state *mrb, mrb_args_format format, void** ptr, va_list ap) +static mrb_int +get_args_v(mrb_state *mrb, mrb_args_format format, void** ptr, va_list ap) { const char *fmt = format; char c; @@ -1363,6 +1305,61 @@ finish: return i; } +/* + retrieve arguments from mrb_state. + + mrb_get_args(mrb, format, ...) + + returns number of arguments parsed. + + format specifiers: + + string mruby type C type note + ---------------------------------------------------------------------------------------------- + o: Object [mrb_value] + C: Class/Module [mrb_value] when ! follows, the value may be nil + S: String [mrb_value] when ! follows, the value may be nil + A: Array [mrb_value] when ! follows, the value may be nil + H: Hash [mrb_value] when ! follows, the value may be nil + s: String [const char*,mrb_int] Receive two arguments; s! gives (NULL,0) for nil + z: String [const char*] NUL terminated string; z! gives NULL for nil + a: Array [const mrb_value*,mrb_int] Receive two arguments; a! gives (NULL,0) for nil + c: Class/Module [strcut RClass*] c! gives NULL for nil + f: Integer/Float [mrb_float] + i: Integer/Float [mrb_int] + b: boolean [mrb_bool] + n: String/Symbol [mrb_sym] + d: data [void*,mrb_data_type const] 2nd argument will be used to check data type so it won't be modified; when ! follows, the value may be nil + &: block [mrb_value] &! raises exception if no block given + *: rest argument [const mrb_value*,mrb_int] The rest of the arguments as an array; *! avoid copy of the stack + |: optional Following arguments are optional + ?: optional given [mrb_bool] true if preceding argument (optional) is given + ':': keyword args [mrb_kwargs const] Get keyword arguments + + format modifiers: + + string note + ---------------------------------------------------------------------------------------------- + !: Switch to the alternate mode; The behaviour changes depending on the specifier + +: Request a not frozen object; However, except nil value + */ +MRB_API mrb_int +mrb_get_args(mrb_state *mrb, mrb_args_format format, ...) +{ + va_list ap; + va_start(ap, format); + mrb_int rc = get_args_v(mrb, format, NULL, ap); + va_end(ap); + return rc; +} + +MRB_API mrb_int +mrb_get_args_a(mrb_state *mrb, mrb_args_format format, void** args) +{ + va_list ap; + return get_args_v(mrb, format, args, ap); +} + static struct RClass* boot_defclass(mrb_state *mrb, struct RClass *super) {