Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/include/mruby.h

    r331 r439  
    22** mruby - An embeddable Ruby implementation
    33**
    4 ** Copyright (c) mruby developers 2010-2017
     4** Copyright (c) mruby developers 2010-2020
    55**
    66** Permission is hereby granted, free of charge, to any person obtaining
     
    2626*/
    2727
     28/**
     29 * @file mruby.h
     30 */
     31
    2832#ifndef MRUBY_H
    2933#define MRUBY_H
     
    3539#endif
    3640
     41#include <stdarg.h>
    3742#include <stdint.h>
    3843#include <stddef.h>
     
    5863#endif
    5964
    60 #if __STDC_VERSION__ >= 201112L
     65#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
    6166#define mrb_static_assert(exp, str) _Static_assert(exp, str)
    6267#else
     
    6570
    6671#include "mrbconf.h"
     72
     73#include <mruby/common.h>
     74#include <mruby/value.h>
     75#include <mruby/gc.h>
     76#include <mruby/version.h>
     77
     78#ifdef _MSC_VER
     79#include <float.h>
     80#endif
     81
     82#ifndef MRB_WITHOUT_FLOAT
     83#ifndef FLT_EPSILON
     84#define FLT_EPSILON (1.19209290e-07f)
     85#endif
     86#ifndef DBL_EPSILON
     87#define DBL_EPSILON ((double)2.22044604925031308085e-16L)
     88#endif
     89#ifndef LDBL_EPSILON
     90#define LDBL_EPSILON (1.08420217248550443401e-19L)
     91#endif
    6792
    6893#ifdef MRB_USE_FLOAT
     
    7196#define MRB_FLOAT_EPSILON DBL_EPSILON
    7297#endif
    73 
    74 #include "mruby/common.h"
    75 #include <mruby/value.h>
    76 #include <mruby/gc.h>
    77 #include <mruby/version.h>
     98#endif
    7899
    79100/**
     
    82103MRB_BEGIN_DECL
    83104
    84 typedef uint32_t mrb_code;
    85 
    86 /**
    87  * Required arguments signature type.
     105typedef uint8_t mrb_code;
     106
     107/**
     108 * \class mrb_aspec
     109 *
     110 * Specifies the number of arguments a function takes
     111 *
     112 * Example: `MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1)` for a method that expects 2..3 arguments
    88113 */
    89114typedef uint32_t mrb_aspec;
    90 
    91115
    92116struct mrb_irep;
     
    112136  struct RProc *proc;
    113137  mrb_value *stackent;
    114   int nregs;
    115   int ridx;
    116   int epos;
     138  uint16_t ridx;
     139  uint16_t epos;
    117140  struct REnv *env;
    118   mrb_code *pc;                 /* return address */
    119   mrb_code *err;                /* error position */
     141  const mrb_code *pc;           /* return address */
     142  const mrb_code *err;          /* error position */
    120143  int argc;
    121144  int acc;
     
    141164  mrb_callinfo *cibase, *ciend;
    142165
    143   mrb_code **rescue;                      /* exception handler stack */
    144   int rsize;
     166  uint16_t *rescue;                       /* exception handler stack */
     167  uint16_t rsize;
    145168  struct RProc **ensure;                  /* ensure handler stack */
    146   int esize, eidx;
     169  uint16_t esize, eidx;
    147170
    148171  enum mrb_fiber_state status;
     
    151174};
    152175
     176#ifdef MRB_METHOD_CACHE_SIZE
     177# define MRB_METHOD_CACHE
     178#else
     179/* default method cache size: 128 */
     180/* cache size needs to be power of 2 */
     181# define MRB_METHOD_CACHE_SIZE (1<<7)
     182#endif
     183
     184/**
     185 * Function pointer type for a function callable by mruby.
     186 *
     187 * The arguments to the function are stored on the mrb_state. To get them see mrb_get_args
     188 *
     189 * @param mrb The mruby state
     190 * @param self The self object
     191 * @return [mrb_value] The function's return value
     192 */
     193typedef mrb_value (*mrb_func_t)(struct mrb_state *mrb, mrb_value self);
     194
     195#ifndef MRB_METHOD_T_STRUCT
     196typedef uintptr_t mrb_method_t;
     197#else
     198typedef struct {
     199  uint8_t flags;
     200  union {
     201    struct RProc *proc;
     202    mrb_func_t func;
     203  };
     204} mrb_method_t;
     205#endif
     206
     207#ifdef MRB_METHOD_CACHE
     208struct mrb_cache_entry {
     209  struct RClass *c, *c0;
     210  mrb_sym mid;
     211  mrb_method_t m;
     212};
     213#endif
     214
    153215struct mrb_jmpbuf;
    154216
    155217typedef void (*mrb_atexit_func)(struct mrb_state*);
    156 
    157 #define MRB_STATE_NO_REGEXP 1
    158 #define MRB_STATE_REGEXP    2
    159218
    160219typedef struct mrb_state {
    161220  struct mrb_jmpbuf *jmp;
    162221
    163   uint32_t flags;
    164222  mrb_allocf allocf;                      /* memory allocation function */
    165223  void *allocf_ud;                        /* auxiliary data of allocf */
     
    179237  struct RClass *array_class;
    180238  struct RClass *hash_class;
    181 
     239  struct RClass *range_class;
     240
     241#ifndef MRB_WITHOUT_FLOAT
    182242  struct RClass *float_class;
     243#endif
    183244  struct RClass *fixnum_class;
    184245  struct RClass *true_class;
     
    188249  struct RClass *kernel_module;
    189250
    190   struct alloca_header *mems;
    191251  mrb_gc gc;
    192252
     253#ifdef MRB_METHOD_CACHE
     254  struct mrb_cache_entry cache[MRB_METHOD_CACHE_SIZE];
     255#endif
     256
    193257  mrb_sym symidx;
    194   struct kh_n2s *name2sym;      /* symbol hash */
    195258  struct symbol_name *symtbl;   /* symbol table */
     259  mrb_sym symhash[256];
    196260  size_t symcapa;
     261#ifndef MRB_ENABLE_SYMBOLL_ALL
     262  char symbuf[8];               /* buffer for small symbol names */
     263#endif
    197264
    198265#ifdef MRB_ENABLE_DEBUG_HOOK
    199   void (*code_fetch_hook)(struct mrb_state* mrb, struct mrb_irep *irep, mrb_code *pc, mrb_value *regs);
    200   void (*debug_op_hook)(struct mrb_state* mrb, struct mrb_irep *irep, mrb_code *pc, mrb_value *regs);
     266  void (*code_fetch_hook)(struct mrb_state* mrb, struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
     267  void (*debug_op_hook)(struct mrb_state* mrb, struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
    201268#endif
    202269
     
    220287  mrb_atexit_func *atexit_stack;
    221288#endif
    222   mrb_int atexit_stack_len;
     289  uint16_t atexit_stack_len;
     290  uint16_t ecall_nest;                    /* prevent infinite recursive ecall() */
    223291} mrb_state;
    224 
    225 
    226 typedef mrb_value (*mrb_func_t)(mrb_state *mrb, mrb_value);
    227292
    228293/**
     
    241306 *      }
    242307 *
    243  * @param [mrb_state *] mrb The current mruby state.
    244  * @param [const char *] name The name of the defined class.
    245  * @param [struct RClass *] super The new class parent.
     308 * @param mrb The current mruby state.
     309 * @param name The name of the defined class.
     310 * @param super The new class parent.
    246311 * @return [struct RClass *] Reference to the newly defined class.
    247312 * @see mrb_define_class_under
     
    252317 * Defines a new module.
    253318 *
    254  * @param [mrb_state *] mrb_state* The current mruby state.
    255  * @param [const char *] char* The name of the module.
     319 * @param mrb The current mruby state.
     320 * @param name The name of the module.
    256321 * @return [struct RClass *] Reference to the newly defined module.
    257322 */
    258 MRB_API struct RClass *mrb_define_module(mrb_state *, const char*);
    259 MRB_API mrb_value mrb_singleton_class(mrb_state*, mrb_value);
     323MRB_API struct RClass *mrb_define_module(mrb_state *mrb, const char *name);
     324MRB_API mrb_value mrb_singleton_class(mrb_state *mrb, mrb_value val);
    260325
    261326/**
     
    266331 *     include A
    267332 *   end
    268  * @param [mrb_state *] mrb_state* The current mruby state.
    269  * @param [struct RClass *] RClass* A reference to module or a class.
    270  * @param [struct RClass *] RClass* A reference to the module to be included.
    271  */
    272 MRB_API void mrb_include_module(mrb_state*, struct RClass*, struct RClass*);
     333 * @param mrb The current mruby state.
     334 * @param cla A reference to module or a class.
     335 * @param included A reference to the module to be included.
     336 */
     337MRB_API void mrb_include_module(mrb_state *mrb, struct RClass *cla, struct RClass *included);
    273338
    274339/**
     
    279344 *    prepend A
    280345 *  end
    281  * @param [mrb_state *] mrb_state* The current mruby state.
    282  * @param [struct RClass *] RClass* A reference to module or a class.
    283  * @param [struct RClass *] RClass* A reference to the module to be prepended.
    284  */
    285 MRB_API void mrb_prepend_module(mrb_state*, struct RClass*, struct RClass*);
     346 * @param mrb The current mruby state.
     347 * @param cla A reference to module or a class.
     348 * @param prepended A reference to the module to be prepended.
     349 */
     350MRB_API void mrb_prepend_module(mrb_state *mrb, struct RClass *cla, struct RClass *prepended);
    286351
    287352/**
     
    292357 * Example:
    293358 *
    294  *     !!!c
    295359 *     mrb_value example_method(mrb_state* mrb, mrb_value self)
    296360 *     {
     
    304368 *     }
    305369 *
    306  * @param [mrb_state *] mrb The MRuby state reference.
    307  * @param [struct RClass *] cla The class pointer where the method will be defined.
    308  * @param [const char *] name The name of the method being defined.
    309  * @param [mrb_func_t] func The function pointer to the method definition.
    310  * @param [mrb_aspec] aspec The method parameters declaration.
     370 * @param mrb The MRuby state reference.
     371 * @param cla The class pointer where the method will be defined.
     372 * @param name The name of the method being defined.
     373 * @param func The function pointer to the method definition.
     374 * @param aspec The method parameters declaration.
    311375 */
    312376MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
     
    331395 *       mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
    332396 *     }
    333  * @param [mrb_state *] mrb_state* The MRuby state reference.
    334  * @param [struct RClass *] RClass* The class where the class method will be defined.
    335  * @param [const char *] char* The name of the class method being defined.
    336  * @param [mrb_func_t] mrb_func_t The function pointer to the class method definition.
    337  * @param [mrb_aspec] mrb_aspec The method parameters declaration.
    338  */
    339 MRB_API void mrb_define_class_method(mrb_state *, struct RClass *, const char *, mrb_func_t, mrb_aspec);
    340 MRB_API void mrb_define_singleton_method(mrb_state*, struct RObject*, const char*, mrb_func_t, mrb_aspec);
    341 
    342 /**
    343  *  Defines a module fuction.
     397 * @param mrb The MRuby state reference.
     398 * @param cla The class where the class method will be defined.
     399 * @param name The name of the class method being defined.
     400 * @param fun The function pointer to the class method definition.
     401 * @param aspec The method parameters declaration.
     402 */
     403MRB_API void mrb_define_class_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
     404
     405/**
     406 * Defines a singleton method
     407 *
     408 * @see mrb_define_class_method
     409 */
     410MRB_API void mrb_define_singleton_method(mrb_state *mrb, struct RObject *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
     411
     412/**
     413 *  Defines a module function.
    344414 *
    345415 * Example:
     
    359429 *          mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
    360430 *        }
    361  *  @param [mrb_state *] mrb_state* The MRuby state reference.
    362  *  @param [struct RClass *] RClass* The module where the module function will be defined.
    363  *  @param [const char *] char* The name of the module function being defined.
    364  *  @param [mrb_func_t] mrb_func_t The function pointer to the module function definition.
    365  *  @param [mrb_aspec] mrb_aspec The method parameters declaration.
    366  */
    367 MRB_API void mrb_define_module_function(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec);
     431 *  @param mrb The MRuby state reference.
     432 *  @param cla The module where the module function will be defined.
     433 *  @param name The name of the module function being defined.
     434 *  @param fun The function pointer to the module function definition.
     435 *  @param aspec The method parameters declaration.
     436 */
     437MRB_API void mrb_define_module_function(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
    368438
    369439/**
     
    388458 *          mrb_example_gem_final(mrb_state* mrb){
    389459 *          }
    390  *  @param [mrb_state *] mrb_state* The MRuby state reference.
    391  *  @param [struct RClass *] RClass* A class or module the constant is defined in.
    392  *  @param [const char *] name The name of the constant being defined.
    393  *  @param [mrb_value] mrb_value The value for the constant.
    394  */
    395 MRB_API void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value);
     460 *  @param mrb The MRuby state reference.
     461 *  @param cla A class or module the constant is defined in.
     462 *  @param name The name of the constant being defined.
     463 *  @param val The value for the constant.
     464 */
     465MRB_API void mrb_define_const(mrb_state* mrb, struct RClass* cla, const char *name, mrb_value val);
    396466
    397467/**
     
    439509 *     mrb_example_gem_final(mrb_state* mrb){
    440510 *     }
    441  * @param [mrb_state*] mrb_state* The mruby state reference.
    442  * @param [struct RClass*] RClass* A class the method will be undefined from.
    443  * @param [const char*] constchar* The name of the method to be undefined.
    444  */
    445 MRB_API void mrb_undef_method(mrb_state*, struct RClass*, const char*);
     511 * @param mrb The mruby state reference.
     512 * @param cla The class the method will be undefined from.
     513 * @param name The name of the method to be undefined.
     514 */
     515MRB_API void mrb_undef_method(mrb_state *mrb, struct RClass *cla, const char *name);
     516MRB_API void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym);
    446517
    447518/**
     
    478549 *      mrb_example_gem_final(mrb_state* mrb){
    479550 *      }
    480  * @param [mrb_state*] mrb_state* The mruby state reference.
    481  * @param [RClass*] RClass* A class the class method will be undefined from.
    482  * @param [constchar*] constchar* The name of the class method to be undefined.
    483  */
    484 MRB_API void mrb_undef_class_method(mrb_state*, struct RClass*, const char*);
    485 
    486 /**
    487  * Initialize a new object instace of c class.
     551 * @param mrb The mruby state reference.
     552 * @param cls A class the class method will be undefined from.
     553 * @param name The name of the class method to be undefined.
     554 */
     555MRB_API void mrb_undef_class_method(mrb_state *mrb, struct RClass *cls, const char *name);
     556
     557/**
     558 * Initialize a new object instance of c class.
    488559 *
    489560 * Example:
     
    506577 *       mrb_p(mrb, obj); // => Kernel#p
    507578 *      }
    508  * @param [mrb_state*] mrb The current mruby state.
    509  * @param [RClass*] c Reference to the class of the new object.
    510  * @param [mrb_int] argc Number of arguments in argv
    511  * @param [const mrb_value *] argv Array of mrb_value to initialize the object
     579 * @param mrb The current mruby state.
     580 * @param c Reference to the class of the new object.
     581 * @param argc Number of arguments in argv
     582 * @param argv Array of mrb_value to initialize the object
    512583 * @return [mrb_value] The newly initialized object
    513584 */
     
    520591}
    521592
    522 MRB_API mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
    523 
    524593/**
    525594 * Creates a new instance of Class, Class.
     
    537606 *       }
    538607 *
    539  * @param [mrb_state*] mrb The current mruby state.
    540  * @param [struct RClass *] super The super class or parent.
     608 * @param mrb The current mruby state.
     609 * @param super The super class or parent.
    541610 * @return [struct RClass *] Reference to the new class.
    542611 */
     
    554623 *      }
    555624 *
    556  * @param [mrb_state*] mrb The current mruby state.
     625 * @param mrb The current mruby state.
    557626 * @return [struct RClass *] Reference to the new module.
    558627 */
     
    581650 *      }
    582651 *
    583  * @param [mrb_state*] mrb The current mruby state.
    584  * @param [const char *] name A string representing the name of the class.
     652 * @param mrb The current mruby state.
     653 * @param name A string representing the name of the class.
    585654 * @return [mrb_bool] A boolean value.
    586655 */
     
    589658/**
    590659 * Gets a class.
    591  * @param [mrb_state*] mrb The current mruby state.
    592  * @param [const char *] name The name of the class.
     660 * @param mrb The current mruby state.
     661 * @param name The name of the class.
    593662 * @return [struct RClass *] A reference to the class.
    594663*/
     
    597666/**
    598667 * Gets a exception class.
    599  * @param [mrb_state*] mrb The current mruby state.
    600  * @param [const char *] name The name of the class.
     668 * @param mrb The current mruby state.
     669 * @param name The name of the class.
    601670 * @return [struct RClass *] A reference to the class.
    602671*/
     
    627696 *      }
    628697 *
    629  * @param [mrb_state*] mrb The current mruby state.
    630  * @param [struct RClass *] outer The name of the outer class.
    631  * @param [const char *] name A string representing the name of the inner class.
     698 * @param mrb The current mruby state.
     699 * @param outer The name of the outer class.
     700 * @param name A string representing the name of the inner class.
    632701 * @return [mrb_bool] A boolean value.
    633702 */
     
    636705/**
    637706 * Gets a child class.
    638  * @param [mrb_state*] mrb The current mruby state.
    639  * @param [struct RClass *] outer The name of the parent class.
    640  * @param [const char *] name The name of the class.
     707 * @param mrb The current mruby state.
     708 * @param outer The name of the parent class.
     709 * @param name The name of the class.
    641710 * @return [struct RClass *] A reference to the class.
    642711*/
     
    645714/**
    646715 * Gets a module.
    647  * @param [mrb_state*] mrb The current mruby state.
    648  * @param [const char *] name The name of the module.
     716 * @param mrb The current mruby state.
     717 * @param name The name of the module.
    649718 * @return [struct RClass *] A reference to the module.
    650719*/
     
    653722/**
    654723 * Gets a module defined under another module.
    655  * @param [mrb_state*] mrb The current mruby state.
    656  * @param [struct RClass *] outer The name of the outer module.
    657  * @param [const char *] name The name of the module.
     724 * @param mrb The current mruby state.
     725 * @param outer The name of the outer module.
     726 * @param name The name of the module.
    658727 * @return [struct RClass *] A reference to the module.
    659728*/
    660729MRB_API struct RClass * mrb_module_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
     730/* a function to raise NotImplementedError with current method name */
     731MRB_API void mrb_notimplement(mrb_state*);
     732/* a function to be replacement of unimplemented method */
    661733MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value);
    662734
     
    666738 * Equivalent to:
    667739 *   Object#dup
    668  * @param [mrb_state*] mrb The current mruby state.
    669  * @param [mrb_value] obj Object to be duplicate.
     740 * @param mrb The current mruby state.
     741 * @param obj Object to be duplicate.
    670742 * @return [mrb_value] The newly duplicated object.
    671743 */
    672744MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
    673 MRB_API mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method);
    674745
    675746/**
     
    708779 *      }
    709780 *
    710  * @param [mrb_state*] mrb The current mruby state.
    711  * @param [struct RClass *] c A reference to a class.
    712  * @param [mrb_sym] mid A symbol referencing a method id.
     781 * @param mrb The current mruby state.
     782 * @param c A reference to a class.
     783 * @param mid A symbol referencing a method id.
    713784 * @return [mrb_bool] A boolean value.
    714785 */
     
    718789 * Defines a new class under a given module
    719790 *
    720  * @param [mrb_state*] mrb The current mruby state.
    721  * @param [struct RClass *] outer Reference to the module under which the new class will be defined
    722  * @param [const char *] name The name of the defined class
    723  * @param [struct RClass *] super The new class parent
     791 * @param mrb The current mruby state.
     792 * @param outer Reference to the module under which the new class will be defined
     793 * @param name The name of the defined class
     794 * @param super The new class parent
    724795 * @return [struct RClass *] Reference to the newly defined class
    725796 * @see mrb_define_class
     
    738809
    739810/**
    740  * Funtion takes n optional arguments
     811 * Function takes n optional arguments
    741812 *
    742813 * @param n
     
    746817
    747818/**
    748  * Funtion takes n1 mandatory arguments and n2 optional arguments
     819 * Function takes n1 mandatory arguments and n2 optional arguments
    749820 *
    750821 * @param n1
     
    784855 * Must be a C string composed of the following format specifiers:
    785856 *
    786  * | char | Ruby type      | C types           | Notes                                               |
     857 * | char | Ruby type      | C types           | Notes                                              |
    787858 * |:----:|----------------|-------------------|----------------------------------------------------|
    788859 * | `o`  | {Object}       | {mrb_value}       | Could be used to retrieve any type of argument     |
     
    791862 * | `A`  | {Array}        | {mrb_value}       | when `!` follows, the value may be `nil`           |
    792863 * | `H`  | {Hash}         | {mrb_value}       | when `!` follows, the value may be `nil`           |
    793  * | `s`  | {String}       | char *, {mrb_int} |  Receive two arguments; `s!` gives (`NULL`,`0`) for `nil`       |
     864 * | `s`  | {String}       | char *, {mrb_int} | Receive two arguments; `s!` gives (`NULL`,`0`) for `nil`        |
    794865 * | `z`  | {String}       | char *            | `NULL` terminated string; `z!` gives `NULL` for `nil`           |
    795866 * | `a`  | {Array}        | {mrb_value} *, {mrb_int} | Receive two arguments; `a!` gives (`NULL`,`0`) for `nil` |
    796  * | `f`  | {Float}        | {mrb_float}       |                                                    |
    797  * | `i`  | {Integer}      | {mrb_int}         |                                                    |
     867 * | `f`  | {Fixnum}/{Float} | {mrb_float}       |                                                    |
     868 * | `i`  | {Fixnum}/{Float} | {mrb_int}         |                                                    |
    798869 * | `b`  | boolean        | {mrb_bool}        |                                                    |
    799  * | `n`  | {Symbol}       | {mrb_sym}         |                                                    |
    800  * | `&`  | block          | {mrb_value}       |                                                    |
    801  * | `*`  | rest arguments | {mrb_value} *, {mrb_int} | Receive the rest of arguments as an array.  |
    802  * | &vert; | optional     |                   | After this spec following specs would be optional. |
     870 * | `n`  | {String}/{Symbol} | {mrb_sym}         |                                                    |
     871 * | `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` |
     872 * | `I`  | inline struct  | void *          |                                                    |
     873 * | `&`  | block          | {mrb_value}       | &! raises exception if no block given.             |
     874 * | `*`  | rest arguments | {mrb_value} *, {mrb_int} | Receive the rest of arguments as an array; `*!` avoid copy of the stack.  |
     875 * | <code>\|</code> | optional     |                   | After this spec following specs would be optional. |
    803876 * | `?`  | optional given | {mrb_bool}        | `TRUE` if preceding argument is given. Used to check optional argument is given. |
     877 * | `:`  | keyword args   | {mrb_kwargs} const | Get keyword arguments. @see mrb_kwargs |
    804878 *
    805879 * @see mrb_get_args
     
    808882
    809883/**
     884 * Get keyword arguments by `mrb_get_args()` with `:` specifier.
     885 *
     886 * `mrb_kwargs::num` indicates that the number of keyword values.
     887 *
     888 * `mrb_kwargs::values` is an object array, and the keyword argument corresponding to the string array is assigned.
     889 * Note that `undef` is assigned if there is no keyword argument corresponding to `mrb_kwargs::optional`.
     890 *
     891 * `mrb_kwargs::table` accepts a string array.
     892 *
     893 * `mrb_kwargs::required` indicates that the specified number of keywords starting from the beginning of the string array are required.
     894 *
     895 * `mrb_kwargs::rest` is the remaining keyword argument that can be accepted as `**rest` in Ruby.
     896 * If `NULL` is specified, `ArgumentError` is raised when there is an undefined keyword.
     897 *
     898 * Examples:
     899 *
     900 *      // def method(a: 1, b: 2)
     901 *
     902 *      uint32_t kw_num = 2;
     903 *      const char *kw_names[kw_num] = { "a", "b" };
     904 *      uint32_t kw_required = 0;
     905 *      mrb_value kw_values[kw_num];
     906 *      const mrb_kwargs kwargs = { kw_num, kw_values, kw_names, kw_required, NULL };
     907 *
     908 *      mrb_get_args(mrb, ":", &kwargs);
     909 *      if (mrb_undef_p(kw_values[0])) { kw_values[0] = mrb_fixnum_value(1); }
     910 *      if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
     911 *
     912 *
     913 *      // def method(str, x:, y: 2, z: "default string", **opts)
     914 *
     915 *      mrb_value str, kw_rest;
     916 *      uint32_t kw_num = 3;
     917 *      const char *kw_names[kw_num] = { "x", "y", "z" };
     918 *      uint32_t kw_required = 1;
     919 *      mrb_value kw_values[kw_num];
     920 *      const mrb_kwargs kwargs = { kw_num, kw_values, kw_names, kw_required, &kw_rest };
     921 *
     922 *      mrb_get_args(mrb, "S:", &str, &kwargs);
     923 *      // or: mrb_get_args(mrb, ":S", &kwargs, &str);
     924 *      if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
     925 *      if (mrb_undef_p(kw_values[2])) { kw_values[2] = mrb_str_new_cstr(mrb, "default string"); }
     926 */
     927typedef struct mrb_kwargs mrb_kwargs;
     928
     929struct mrb_kwargs
     930{
     931  uint32_t num;
     932  mrb_value *values;
     933  const char *const *table;
     934  uint32_t required;
     935  mrb_value *rest;
     936};
     937
     938/**
    810939 * Retrieve arguments from mrb_state.
    811940 *
    812  * When applicable, implicit conversions (such as `to_str`, `to_ary`, `to_hash`) are
    813  * applied to received arguments.
    814  * Used inside a function of mrb_func_t type.
    815  *
    816941 * @param mrb The current MRuby state.
    817  * @param format [mrb_args_format] is a list of format specifiers
     942 * @param format is a list of format specifiers
    818943 * @param ... The passing variadic arguments must be a pointer of retrieving type.
    819944 * @return the number of arguments retrieved.
    820945 * @see mrb_args_format
     946 * @see mrb_kwargs
    821947 */
    822948MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...);
    823949
    824 static inline mrb_sym
     950MRB_INLINE mrb_sym
    825951mrb_get_mid(mrb_state *mrb) /* get method symbol */
    826952{
     
    828954}
    829955
    830 static inline int
    831 mrb_get_argc(mrb_state *mrb) /* get argc */
    832 {
    833   return mrb->c->ci->argc;
    834 }
     956/**
     957 * Retrieve number of arguments from mrb_state.
     958 *
     959 * Correctly handles *splat arguments.
     960 */
     961MRB_API mrb_int mrb_get_argc(mrb_state *mrb);
     962
     963MRB_API mrb_value* mrb_get_argv(mrb_state *mrb);
    835964
    836965/* `strlen` for character string literals (use with caution or `strlen` instead)
     
    845974 * Call existing ruby functions.
    846975 *
     976 * Example:
     977 *
    847978 *      #include <stdio.h>
    848979 *      #include <mruby.h>
     
    861992 *        fclose(fp);
    862993 *        mrb_close(mrb);
    863  *       }
    864  * @param [mrb_state*] mrb_state* The current mruby state.
    865  * @param [mrb_value] mrb_value A reference to an mruby value.
    866  * @param [const char*] const char* The name of the method.
    867  * @param [mrb_int] mrb_int The number of arguments the method has.
    868  * @param [...] ... Variadic values(not type safe!).
    869  * @return [mrb_value] mrb_value mruby function value.
    870  */
    871 MRB_API mrb_value mrb_funcall(mrb_state*, mrb_value, const char*, mrb_int,...);
     994 *      }
     995 *
     996 * @param mrb The current mruby state.
     997 * @param val A reference to an mruby value.
     998 * @param name The name of the method.
     999 * @param argc The number of arguments the method has.
     1000 * @param ... Variadic values(not type safe!).
     1001 * @return [mrb_value] mruby function value.
     1002 */
     1003MRB_API mrb_value mrb_funcall(mrb_state *mrb, mrb_value val, const char *name, mrb_int argc, ...);
    8721004/**
    8731005 * Call existing ruby functions. This is basically the type safe version of mrb_funcall.
     
    8911023 *        mrb_close(mrb);
    8921024 *       }
    893  * @param [mrb_state*] mrb_state* The current mruby state.
    894  * @param [mrb_value] mrb_value A reference to an mruby value.
    895  * @param [mrb_sym] mrb_sym The symbol representing the method.
    896  * @param [mrb_int] mrb_int The number of arguments the method has.
    897  * @param [const mrb_value*] mrb_value* Pointer to the object.
     1025 * @param mrb The current mruby state.
     1026 * @param val A reference to an mruby value.
     1027 * @param name_sym The symbol representing the method.
     1028 * @param argc The number of arguments the method has.
     1029 * @param obj Pointer to the object.
    8981030 * @return [mrb_value] mrb_value mruby function value.
    8991031 * @see mrb_funcall
    9001032 */
    901 MRB_API mrb_value mrb_funcall_argv(mrb_state*, mrb_value, mrb_sym, mrb_int, const mrb_value*);
     1033MRB_API mrb_value mrb_funcall_argv(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv);
    9021034/**
    9031035 * Call existing ruby functions with a block.
    9041036 */
    905 MRB_API mrb_value mrb_funcall_with_block(mrb_state*, mrb_value, mrb_sym, mrb_int, const mrb_value*, mrb_value);
     1037MRB_API mrb_value mrb_funcall_with_block(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv, mrb_value block);
    9061038/**
    9071039 * Create a symbol
     1040 *
     1041 * Example:
    9081042 *
    9091043 *     # Ruby style:
     
    9121046 *     // C style:
    9131047 *     mrb_sym m_sym = mrb_intern_lit(mrb, "pizza"); //  => :pizza
    914  * @param [mrb_state*] mrb_state* The current mruby state.
    915  * @param [const char*] const char* The name of the method.
     1048 *
     1049 * @param mrb The current mruby state.
     1050 * @param str The string to be symbolized
    9161051 * @return [mrb_sym] mrb_sym A symbol.
    9171052 */
    918 MRB_API mrb_sym mrb_intern_cstr(mrb_state*,const char*);
     1053MRB_API mrb_sym mrb_intern_cstr(mrb_state *mrb, const char* str);
    9191054MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t);
    9201055MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t);
     
    9241059MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t);
    9251060MRB_API mrb_value mrb_check_intern_str(mrb_state*,mrb_value);
    926 MRB_API const char *mrb_sym2name(mrb_state*,mrb_sym);
    927 MRB_API const char *mrb_sym2name_len(mrb_state*,mrb_sym,mrb_int*);
    928 MRB_API mrb_value mrb_sym2str(mrb_state*,mrb_sym);
     1061MRB_API const char *mrb_sym_name(mrb_state*,mrb_sym);
     1062MRB_API const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*);
     1063MRB_API const char *mrb_sym_dump(mrb_state*,mrb_sym);
     1064MRB_API mrb_value mrb_sym_str(mrb_state*,mrb_sym);
     1065#define mrb_sym2name(mrb,sym) mrb_sym_name(mrb,sym)
     1066#define mrb_sym2name_len(mrb,sym,len) mrb_sym_name_len(mrb,sym,len)
     1067#define mrb_sym2str(mrb,sym) mrb_sym_str(mrb,sym)
    9291068
    9301069MRB_API void *mrb_malloc(mrb_state*, size_t);         /* raise RuntimeError if no mem */
     
    9451084#define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
    9461085
     1086MRB_API mrb_value mrb_obj_freeze(mrb_state*, mrb_value);
     1087#define mrb_str_new_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new(mrb,p,len))
     1088#define mrb_str_new_cstr_frozen(mrb,p) mrb_obj_freeze(mrb,mrb_str_new_cstr(mrb,p))
     1089#define mrb_str_new_static_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new_static(mrb,p,len))
     1090#define mrb_str_new_lit_frozen(mrb,lit) mrb_obj_freeze(mrb,mrb_str_new_lit(mrb,lit))
     1091
    9471092#ifdef _WIN32
    948 char* mrb_utf8_from_locale(const char *p, size_t len);
    949 char* mrb_locale_from_utf8(const char *p, size_t len);
     1093MRB_API char* mrb_utf8_from_locale(const char *p, int len);
     1094MRB_API char* mrb_locale_from_utf8(const char *p, int len);
    9501095#define mrb_locale_free(p) free(p)
    9511096#define mrb_utf8_free(p) free(p)
    9521097#else
    953 #define mrb_utf8_from_locale(p, l) (p)
    954 #define mrb_locale_from_utf8(p, l) (p)
     1098#define mrb_utf8_from_locale(p, l) ((char*)(p))
     1099#define mrb_locale_from_utf8(p, l) ((char*)(p))
    9551100#define mrb_locale_free(p)
    9561101#define mrb_utf8_free(p)
     
    10071152MRB_API void* mrb_default_allocf(mrb_state*, void*, size_t, void*);
    10081153
    1009 MRB_API mrb_value mrb_top_self(mrb_state *);
    1010 MRB_API mrb_value mrb_run(mrb_state*, struct RProc*, mrb_value);
    1011 MRB_API mrb_value mrb_top_run(mrb_state*, struct RProc*, mrb_value, unsigned int);
    1012 MRB_API mrb_value mrb_vm_run(mrb_state*, struct RProc*, mrb_value, unsigned int);
    1013 MRB_API mrb_value mrb_vm_exec(mrb_state*, struct RProc*, mrb_code*);
     1154MRB_API mrb_value mrb_top_self(mrb_state *mrb);
     1155MRB_API mrb_value mrb_run(mrb_state *mrb, struct RProc* proc, mrb_value self);
     1156MRB_API mrb_value mrb_top_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep);
     1157MRB_API mrb_value mrb_vm_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep);
     1158MRB_API mrb_value mrb_vm_exec(mrb_state *mrb, struct RProc *proc, const mrb_code *iseq);
    10141159/* compatibility macros */
    10151160#define mrb_toplevel_run_keep(m,p,k) mrb_top_run((m),(p),mrb_top_self(m),(k))
     
    10211166MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name);
    10221167
    1023 MRB_API mrb_bool mrb_obj_eq(mrb_state*, mrb_value, mrb_value);
    1024 MRB_API mrb_bool mrb_obj_equal(mrb_state*, mrb_value, mrb_value);
     1168MRB_API mrb_bool mrb_obj_eq(mrb_state *mrb, mrb_value a, mrb_value b);
     1169MRB_API mrb_bool mrb_obj_equal(mrb_state *mrb, mrb_value a, mrb_value b);
    10251170MRB_API mrb_bool mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
    1026 MRB_API mrb_value mrb_convert_to_integer(mrb_state *mrb, mrb_value val, int base);
     1171MRB_API mrb_value mrb_convert_to_integer(mrb_state *mrb, mrb_value val, mrb_int base);
    10271172MRB_API mrb_value mrb_Integer(mrb_state *mrb, mrb_value val);
     1173#ifndef MRB_WITHOUT_FLOAT
    10281174MRB_API mrb_value mrb_Float(mrb_state *mrb, mrb_value val);
     1175#endif
    10291176MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj);
    10301177MRB_API mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
     1178/* mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error */
     1179MRB_API mrb_int mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
     1180
     1181MRB_INLINE int
     1182mrb_gc_arena_save(mrb_state *mrb)
     1183{
     1184  return mrb->gc.arena_idx;
     1185}
     1186
     1187MRB_INLINE void
     1188mrb_gc_arena_restore(mrb_state *mrb, int idx)
     1189{
     1190  mrb->gc.arena_idx = idx;
     1191}
    10311192
    10321193MRB_API void mrb_garbage_collect(mrb_state*);
    10331194MRB_API void mrb_full_gc(mrb_state*);
    10341195MRB_API void mrb_incremental_gc(mrb_state *);
    1035 MRB_API int mrb_gc_arena_save(mrb_state*);
    1036 MRB_API void mrb_gc_arena_restore(mrb_state*,int);
    10371196MRB_API void mrb_gc_mark(mrb_state*,struct RBasic*);
    10381197#define mrb_gc_mark_value(mrb,val) do {\
     
    10771236MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...);
    10781237MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...);
     1238MRB_API mrb_noreturn void mrb_frozen_error(mrb_state *mrb, void *frozen_obj);
     1239MRB_API mrb_noreturn void mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max);
    10791240MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...);
    10801241MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *fmt, ...);
    10811242MRB_API void mrb_print_backtrace(mrb_state *mrb);
    10821243MRB_API void mrb_print_error(mrb_state *mrb);
     1244/* function for `raisef` formatting */
     1245MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap);
    10831246
    10841247/* macros to get typical exception objects
     
    10981261#define E_LOCALJUMP_ERROR           (mrb_exc_get(mrb, "LocalJumpError"))
    10991262#define E_REGEXP_ERROR              (mrb_exc_get(mrb, "RegexpError"))
     1263#define E_FROZEN_ERROR              (mrb_exc_get(mrb, "FrozenError"))
    11001264
    11011265#define E_NOTIMP_ERROR              (mrb_exc_get(mrb, "NotImplementedError"))
     1266#ifndef MRB_WITHOUT_FLOAT
    11021267#define E_FLOATDOMAIN_ERROR         (mrb_exc_get(mrb, "FloatDomainError"))
     1268#endif
    11031269
    11041270#define E_KEY_ERROR                 (mrb_exc_get(mrb, "KeyError"))
     
    11221288MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
    11231289#define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val))
     1290/* string type checking (contrary to the name, it doesn't convert) */
     1291MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val);
    11241292MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
     1293
     1294MRB_INLINE void mrb_check_frozen(mrb_state *mrb, void *o)
     1295{
     1296  if (mrb_frozen_p((struct RBasic*)o)) mrb_frozen_error(mrb, o);
     1297}
    11251298
    11261299typedef enum call_type {
     
    11311304} call_type;
    11321305
    1133 MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *klass, const char *name1, const char *name2);
     1306MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b);
    11341307MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass);
    11351308MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val);
     
    11421315
    11431316
    1144 /*
     1317/**
    11451318 * Resume a Fiber
    11461319 *
    1147  * @mrbgem mruby-fiber
     1320 * Implemented in mruby-fiber
    11481321 */
    11491322MRB_API mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv);
    11501323
    1151 /*
     1324/**
    11521325 * Yield a Fiber
    11531326 *
    1154  * @mrbgem mruby-fiber
     1327 * Implemented in mruby-fiber
    11551328 */
    11561329MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv);
    11571330
    1158 /*
     1331/**
     1332 * Check if a Fiber is alive
     1333 *
     1334 * Implemented in mruby-fiber
     1335 */
     1336MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib);
     1337
     1338/**
    11591339 * FiberError reference
    11601340 *
    1161  * @mrbgem mruby-fiber
     1341 * Implemented in mruby-fiber
    11621342 */
    11631343#define E_FIBER_ERROR (mrb_exc_get(mrb, "FiberError"))
     1344MRB_API void mrb_stack_extend(mrb_state*, mrb_int);
    11641345
    11651346/* memory pool implementation */
     
    11701351MRB_API void* mrb_pool_realloc(struct mrb_pool*, void*, size_t oldlen, size_t newlen);
    11711352MRB_API mrb_bool mrb_pool_can_realloc(struct mrb_pool*, void*, size_t);
     1353/* temporary memory allocation, only effective while GC arena is kept */
    11721354MRB_API void* mrb_alloca(mrb_state *mrb, size_t);
    11731355
     
    11841366#undef memcpy
    11851367#undef memset
    1186 static inline void*
     1368static void*
    11871369mrbmemcpy(void *dst, const void *src, size_t n)
    11881370{
    1189   char *d = dst;
    1190   const char *s = src;
     1371  char *d = (char*)dst;
     1372  const char *s = (const char*)src;
    11911373  while (n--)
    11921374    *d++ = *s++;
     
    11951377#define memcpy(a,b,c) mrbmemcpy(a,b,c)
    11961378
    1197 static inline void*
     1379static void*
    11981380mrbmemset(void *s, int c, size_t n)
    11991381{
    1200   char *t = s;
     1382  char *t = (char*)s;
    12011383  while (n--)
    12021384    *t++ = c;
Note: See TracChangeset for help on using the changeset viewer.