From: Yukihiro "Matz" Matsumoto Date: Mon, 12 Oct 2015 12:04:19 +0000 (+0900) Subject: Merge pull request #2916 from felixjones/master X-Git-Tag: 1.2.0~39 X-Git-Url: https://repo.or.cz/mruby.git/commitdiff_plain/af3aecd94fef24844a28c0618994e43975c58cfb Merge pull request #2916 from felixjones/master mrb_get_mid and mrb_get_argc --- af3aecd94fef24844a28c0618994e43975c58cfb diff --cc include/mruby.h index fd64a07ec,e5d19b170..0db9002b6 --- a/include/mruby.h +++ b/include/mruby.h @@@ -381,111 -229,44 +381,123 @@@ MRB_API mrb_value mrb_notimplement_m(mr MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj); MRB_API mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method); MRB_API mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid); + +/** + * Defines a new class under a given module + * + * @param mrb The current mruby state. + * @param outer Reference to the module under which the new class will be defined + * @param name The name of the defined class + * @param super The new class parent + * @return Reference to the newly defined class + * @see mrb_define_class + */ MRB_API struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super); + MRB_API struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name); -/* required arguments */ +/** + * Function requires n arguments. + * + * @param n + * The number of required arguments. + */ #define MRB_ARGS_REQ(n) ((mrb_aspec)((n)&0x1f) << 18) -/* optional arguments */ + +/** + * Funtion takes n optional arguments + * + * @param n + * The number of optional arguments. + */ #define MRB_ARGS_OPT(n) ((mrb_aspec)((n)&0x1f) << 13) -/* mandatory and optinal arguments */ + +/** + * Funtion takes n1 mandatory arguments and n2 optional arguments + * + * @param n1 + * The number of required arguments. + * @param n2 + * The number of optional arguments. + */ #define MRB_ARGS_ARG(n1,n2) (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2)) -/* rest argument */ +/** rest argument */ #define MRB_ARGS_REST() ((mrb_aspec)(1 << 12)) -/* required arguments after rest */ + +/** required arguments after rest */ #define MRB_ARGS_POST(n) ((mrb_aspec)((n)&0x1f) << 7) -/* keyword arguments (n of keys, kdict) */ + +/** keyword arguments (n of keys, kdict) */ #define MRB_ARGS_KEY(n1,n2) ((mrb_aspec)((((n1)&0x1f) << 2) | ((n2)?(1<<1):0))) -/* block argument */ + +/** + * Function takes a block argument + */ #define MRB_ARGS_BLOCK() ((mrb_aspec)1) -/* accept any number of arguments */ +/** + * Function accepts any number of arguments + */ #define MRB_ARGS_ANY() MRB_ARGS_REST() -/* accept no arguments */ + +/** + * Function accepts no arguments + */ #define MRB_ARGS_NONE() ((mrb_aspec)0) -MRB_API mrb_int mrb_get_args(mrb_state *mrb, const char *format, ...); +/** + * Format specifiers for \ref mrb_get_args function + * + * Must be a list of following format specifiers: + * + * | char | mruby type | retrieve types |note | + * |:----:|----------------|---------------------|----------------------------------------------------| + * | o | Object | mrb_value | Could be used to retrieve any type of argument | + * | C | Class/Module | mrb_value | | + * | 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 | char *, mrb_int | Receive two arguments; s! gives (NULL,0) for nil | + * | z | String | char * | NUL terminated string; z! gives NULL for nil | + * | a | Array | mrb_value *, mrb_int | Receive two arguments; a! gives (NULL,0) for nil | + * | f | Float | mrb_float | | + * | i | Integer | mrb_int | | + * | b | boolean | mrb_bool | | + * | n | Symbol | mrb_sym | | + * | & | block | mrb_value | | + * | * | rest arguments | mrb_value *, mrb_int | Receive the rest of arguments as an array. | + * | \| | optional | | After this spec following specs would be optional. | + * | ? | optional given | mrb_bool | True if preceding argument is given. Used to check optional argument is given. | + */ +typedef const char *mrb_args_format; + +/** + * Retrieve arguments from mrb_state. + * + * When applicable, implicit conversions (such as to_str, to_ary, to_hash) are + * applied to received arguments. + * Use it inside a function pointed by mrb_func_t. + * + * @param mrb The current MRuby state. + * @param format is a list of format specifiers see @ref mrb_args_format + * @param ... The passing variadic arguments must be a pointer of retrieving type. + * @return the number of arguments retrieved. + */ +MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...); + static inline mrb_sym + mrb_get_mid(mrb_state *mrb) /* get method symbol */ + { + return mrb->c->ci->mid; + } + + static inline int + mrb_get_argc(mrb_state *mrb) /* get argc */ + { + return mrb->c->ci->argc; + } + /* `strlen` for character string literals (use with caution or `strlen` instead) Adjacent string literals are concatenated in C/C++ in translation phase 6. If `lit` is not one, the compiler will report a syntax error: