Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/include/mruby.h
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- 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 2 2 ** mruby - An embeddable Ruby implementation 3 3 ** 4 ** Copyright (c) mruby developers 2010-20 174 ** Copyright (c) mruby developers 2010-2020 5 5 ** 6 6 ** Permission is hereby granted, free of charge, to any person obtaining … … 26 26 */ 27 27 28 /** 29 * @file mruby.h 30 */ 31 28 32 #ifndef MRUBY_H 29 33 #define MRUBY_H … … 35 39 #endif 36 40 41 #include <stdarg.h> 37 42 #include <stdint.h> 38 43 #include <stddef.h> … … 58 63 #endif 59 64 60 #if __STDC_VERSION__ >= 201112L65 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L 61 66 #define mrb_static_assert(exp, str) _Static_assert(exp, str) 62 67 #else … … 65 70 66 71 #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 67 92 68 93 #ifdef MRB_USE_FLOAT … … 71 96 #define MRB_FLOAT_EPSILON DBL_EPSILON 72 97 #endif 73 74 #include "mruby/common.h" 75 #include <mruby/value.h> 76 #include <mruby/gc.h> 77 #include <mruby/version.h> 98 #endif 78 99 79 100 /** … … 82 103 MRB_BEGIN_DECL 83 104 84 typedef uint32_t mrb_code; 85 86 /** 87 * Required arguments signature type. 105 typedef 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 88 113 */ 89 114 typedef uint32_t mrb_aspec; 90 91 115 92 116 struct mrb_irep; … … 112 136 struct RProc *proc; 113 137 mrb_value *stackent; 114 int nregs; 115 int ridx; 116 int epos; 138 uint16_t ridx; 139 uint16_t epos; 117 140 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 */ 120 143 int argc; 121 144 int acc; … … 141 164 mrb_callinfo *cibase, *ciend; 142 165 143 mrb_code **rescue;/* exception handler stack */144 int rsize;166 uint16_t *rescue; /* exception handler stack */ 167 uint16_t rsize; 145 168 struct RProc **ensure; /* ensure handler stack */ 146 int esize, eidx;169 uint16_t esize, eidx; 147 170 148 171 enum mrb_fiber_state status; … … 151 174 }; 152 175 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 */ 193 typedef mrb_value (*mrb_func_t)(struct mrb_state *mrb, mrb_value self); 194 195 #ifndef MRB_METHOD_T_STRUCT 196 typedef uintptr_t mrb_method_t; 197 #else 198 typedef 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 208 struct mrb_cache_entry { 209 struct RClass *c, *c0; 210 mrb_sym mid; 211 mrb_method_t m; 212 }; 213 #endif 214 153 215 struct mrb_jmpbuf; 154 216 155 217 typedef void (*mrb_atexit_func)(struct mrb_state*); 156 157 #define MRB_STATE_NO_REGEXP 1158 #define MRB_STATE_REGEXP 2159 218 160 219 typedef struct mrb_state { 161 220 struct mrb_jmpbuf *jmp; 162 221 163 uint32_t flags;164 222 mrb_allocf allocf; /* memory allocation function */ 165 223 void *allocf_ud; /* auxiliary data of allocf */ … … 179 237 struct RClass *array_class; 180 238 struct RClass *hash_class; 181 239 struct RClass *range_class; 240 241 #ifndef MRB_WITHOUT_FLOAT 182 242 struct RClass *float_class; 243 #endif 183 244 struct RClass *fixnum_class; 184 245 struct RClass *true_class; … … 188 249 struct RClass *kernel_module; 189 250 190 struct alloca_header *mems;191 251 mrb_gc gc; 192 252 253 #ifdef MRB_METHOD_CACHE 254 struct mrb_cache_entry cache[MRB_METHOD_CACHE_SIZE]; 255 #endif 256 193 257 mrb_sym symidx; 194 struct kh_n2s *name2sym; /* symbol hash */195 258 struct symbol_name *symtbl; /* symbol table */ 259 mrb_sym symhash[256]; 196 260 size_t symcapa; 261 #ifndef MRB_ENABLE_SYMBOLL_ALL 262 char symbuf[8]; /* buffer for small symbol names */ 263 #endif 197 264 198 265 #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); 201 268 #endif 202 269 … … 220 287 mrb_atexit_func *atexit_stack; 221 288 #endif 222 mrb_int atexit_stack_len; 289 uint16_t atexit_stack_len; 290 uint16_t ecall_nest; /* prevent infinite recursive ecall() */ 223 291 } mrb_state; 224 225 226 typedef mrb_value (*mrb_func_t)(mrb_state *mrb, mrb_value);227 292 228 293 /** … … 241 306 * } 242 307 * 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. 246 311 * @return [struct RClass *] Reference to the newly defined class. 247 312 * @see mrb_define_class_under … … 252 317 * Defines a new module. 253 318 * 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. 256 321 * @return [struct RClass *] Reference to the newly defined module. 257 322 */ 258 MRB_API struct RClass *mrb_define_module(mrb_state * , const char*);259 MRB_API mrb_value mrb_singleton_class(mrb_state *, mrb_value);323 MRB_API struct RClass *mrb_define_module(mrb_state *mrb, const char *name); 324 MRB_API mrb_value mrb_singleton_class(mrb_state *mrb, mrb_value val); 260 325 261 326 /** … … 266 331 * include A 267 332 * 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 */ 337 MRB_API void mrb_include_module(mrb_state *mrb, struct RClass *cla, struct RClass *included); 273 338 274 339 /** … … 279 344 * prepend A 280 345 * 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 */ 350 MRB_API void mrb_prepend_module(mrb_state *mrb, struct RClass *cla, struct RClass *prepended); 286 351 287 352 /** … … 292 357 * Example: 293 358 * 294 * !!!c295 359 * mrb_value example_method(mrb_state* mrb, mrb_value self) 296 360 * { … … 304 368 * } 305 369 * 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. 311 375 */ 312 376 MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec); … … 331 395 * mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE()); 332 396 * } 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 */ 403 MRB_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 */ 410 MRB_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. 344 414 * 345 415 * Example: … … 359 429 * mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE()); 360 430 * } 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_tThe 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 */ 437 MRB_API void mrb_define_module_function(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec); 368 438 369 439 /** … … 388 458 * mrb_example_gem_final(mrb_state* mrb){ 389 459 * } 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_valueThe 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 */ 465 MRB_API void mrb_define_const(mrb_state* mrb, struct RClass* cla, const char *name, mrb_value val); 396 466 397 467 /** … … 439 509 * mrb_example_gem_final(mrb_state* mrb){ 440 510 * } 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 */ 515 MRB_API void mrb_undef_method(mrb_state *mrb, struct RClass *cla, const char *name); 516 MRB_API void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym); 446 517 447 518 /** … … 478 549 * mrb_example_gem_final(mrb_state* mrb){ 479 550 * } 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 insta ce 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 */ 555 MRB_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. 488 559 * 489 560 * Example: … … 506 577 * mrb_p(mrb, obj); // => Kernel#p 507 578 * } 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 argv511 * @param [const mrb_value *]argv Array of mrb_value to initialize the object579 * @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 512 583 * @return [mrb_value] The newly initialized object 513 584 */ … … 520 591 } 521 592 522 MRB_API mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);523 524 593 /** 525 594 * Creates a new instance of Class, Class. … … 537 606 * } 538 607 * 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. 541 610 * @return [struct RClass *] Reference to the new class. 542 611 */ … … 554 623 * } 555 624 * 556 * @param [mrb_state*]mrb The current mruby state.625 * @param mrb The current mruby state. 557 626 * @return [struct RClass *] Reference to the new module. 558 627 */ … … 581 650 * } 582 651 * 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. 585 654 * @return [mrb_bool] A boolean value. 586 655 */ … … 589 658 /** 590 659 * 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. 593 662 * @return [struct RClass *] A reference to the class. 594 663 */ … … 597 666 /** 598 667 * 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. 601 670 * @return [struct RClass *] A reference to the class. 602 671 */ … … 627 696 * } 628 697 * 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. 632 701 * @return [mrb_bool] A boolean value. 633 702 */ … … 636 705 /** 637 706 * 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. 641 710 * @return [struct RClass *] A reference to the class. 642 711 */ … … 645 714 /** 646 715 * 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. 649 718 * @return [struct RClass *] A reference to the module. 650 719 */ … … 653 722 /** 654 723 * 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. 658 727 * @return [struct RClass *] A reference to the module. 659 728 */ 660 729 MRB_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 */ 731 MRB_API void mrb_notimplement(mrb_state*); 732 /* a function to be replacement of unimplemented method */ 661 733 MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value); 662 734 … … 666 738 * Equivalent to: 667 739 * 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. 670 742 * @return [mrb_value] The newly duplicated object. 671 743 */ 672 744 MRB_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);674 745 675 746 /** … … 708 779 * } 709 780 * 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. 713 784 * @return [mrb_bool] A boolean value. 714 785 */ … … 718 789 * Defines a new class under a given module 719 790 * 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 defined722 * @param [const char *]name The name of the defined class723 * @param [struct RClass *]super The new class parent791 * @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 724 795 * @return [struct RClass *] Reference to the newly defined class 725 796 * @see mrb_define_class … … 738 809 739 810 /** 740 * Fun tion takes n optional arguments811 * Function takes n optional arguments 741 812 * 742 813 * @param n … … 746 817 747 818 /** 748 * Fun tion takes n1 mandatory arguments and n2 optional arguments819 * Function takes n1 mandatory arguments and n2 optional arguments 749 820 * 750 821 * @param n1 … … 784 855 * Must be a C string composed of the following format specifiers: 785 856 * 786 * | char | Ruby type | C types | Notes 857 * | char | Ruby type | C types | Notes | 787 858 * |:----:|----------------|-------------------|----------------------------------------------------| 788 859 * | `o` | {Object} | {mrb_value} | Could be used to retrieve any type of argument | … … 791 862 * | `A` | {Array} | {mrb_value} | when `!` follows, the value may be `nil` | 792 863 * | `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` | 794 865 * | `z` | {String} | char * | `NULL` terminated string; `z!` gives `NULL` for `nil` | 795 866 * | `a` | {Array} | {mrb_value} *, {mrb_int} | Receive two arguments; `a!` gives (`NULL`,`0`) for `nil` | 796 * | `f` | {F loat}| {mrb_float} | |797 * | `i` | { Integer}| {mrb_int} | |867 * | `f` | {Fixnum}/{Float} | {mrb_float} | | 868 * | `i` | {Fixnum}/{Float} | {mrb_int} | | 798 869 * | `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 * | | | 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. | 803 876 * | `?` | 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 | 804 878 * 805 879 * @see mrb_get_args … … 808 882 809 883 /** 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 */ 927 typedef struct mrb_kwargs mrb_kwargs; 928 929 struct 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 /** 810 939 * Retrieve arguments from mrb_state. 811 940 * 812 * When applicable, implicit conversions (such as `to_str`, `to_ary`, `to_hash`) are813 * applied to received arguments.814 * Used inside a function of mrb_func_t type.815 *816 941 * @param mrb The current MRuby state. 817 * @param format [mrb_args_format]is a list of format specifiers942 * @param format is a list of format specifiers 818 943 * @param ... The passing variadic arguments must be a pointer of retrieving type. 819 944 * @return the number of arguments retrieved. 820 945 * @see mrb_args_format 946 * @see mrb_kwargs 821 947 */ 822 948 MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...); 823 949 824 static inlinemrb_sym950 MRB_INLINE mrb_sym 825 951 mrb_get_mid(mrb_state *mrb) /* get method symbol */ 826 952 { … … 828 954 } 829 955 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 */ 961 MRB_API mrb_int mrb_get_argc(mrb_state *mrb); 962 963 MRB_API mrb_value* mrb_get_argv(mrb_state *mrb); 835 964 836 965 /* `strlen` for character string literals (use with caution or `strlen` instead) … … 845 974 * Call existing ruby functions. 846 975 * 976 * Example: 977 * 847 978 * #include <stdio.h> 848 979 * #include <mruby.h> … … 861 992 * fclose(fp); 862 993 * 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 */ 1003 MRB_API mrb_value mrb_funcall(mrb_state *mrb, mrb_value val, const char *name, mrb_int argc, ...); 872 1004 /** 873 1005 * Call existing ruby functions. This is basically the type safe version of mrb_funcall. … … 891 1023 * mrb_close(mrb); 892 1024 * } 893 * @param [mrb_state*] mrb_state*The current mruby state.894 * @param [mrb_value] mrb_valueA reference to an mruby value.895 * @param [mrb_sym] mrb_sym The symbol representing the method.896 * @param [mrb_int] mrb_intThe 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. 898 1030 * @return [mrb_value] mrb_value mruby function value. 899 1031 * @see mrb_funcall 900 1032 */ 901 MRB_API mrb_value mrb_funcall_argv(mrb_state *, mrb_value, mrb_sym, mrb_int, const mrb_value*);1033 MRB_API mrb_value mrb_funcall_argv(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv); 902 1034 /** 903 1035 * Call existing ruby functions with a block. 904 1036 */ 905 MRB_API mrb_value mrb_funcall_with_block(mrb_state *, mrb_value, mrb_sym, mrb_int, const mrb_value*, mrb_value);1037 MRB_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); 906 1038 /** 907 1039 * Create a symbol 1040 * 1041 * Example: 908 1042 * 909 1043 * # Ruby style: … … 912 1046 * // C style: 913 1047 * 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 916 1051 * @return [mrb_sym] mrb_sym A symbol. 917 1052 */ 918 MRB_API mrb_sym mrb_intern_cstr(mrb_state *,const char*);1053 MRB_API mrb_sym mrb_intern_cstr(mrb_state *mrb, const char* str); 919 1054 MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t); 920 1055 MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t); … … 924 1059 MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t); 925 1060 MRB_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); 1061 MRB_API const char *mrb_sym_name(mrb_state*,mrb_sym); 1062 MRB_API const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*); 1063 MRB_API const char *mrb_sym_dump(mrb_state*,mrb_sym); 1064 MRB_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) 929 1068 930 1069 MRB_API void *mrb_malloc(mrb_state*, size_t); /* raise RuntimeError if no mem */ … … 945 1084 #define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit)) 946 1085 1086 MRB_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 947 1092 #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);1093 MRB_API char* mrb_utf8_from_locale(const char *p, int len); 1094 MRB_API char* mrb_locale_from_utf8(const char *p, int len); 950 1095 #define mrb_locale_free(p) free(p) 951 1096 #define mrb_utf8_free(p) free(p) 952 1097 #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)) 955 1100 #define mrb_locale_free(p) 956 1101 #define mrb_utf8_free(p) … … 1007 1152 MRB_API void* mrb_default_allocf(mrb_state*, void*, size_t, void*); 1008 1153 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*);1154 MRB_API mrb_value mrb_top_self(mrb_state *mrb); 1155 MRB_API mrb_value mrb_run(mrb_state *mrb, struct RProc* proc, mrb_value self); 1156 MRB_API mrb_value mrb_top_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep); 1157 MRB_API mrb_value mrb_vm_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep); 1158 MRB_API mrb_value mrb_vm_exec(mrb_state *mrb, struct RProc *proc, const mrb_code *iseq); 1014 1159 /* compatibility macros */ 1015 1160 #define mrb_toplevel_run_keep(m,p,k) mrb_top_run((m),(p),mrb_top_self(m),(k)) … … 1021 1166 MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name); 1022 1167 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);1168 MRB_API mrb_bool mrb_obj_eq(mrb_state *mrb, mrb_value a, mrb_value b); 1169 MRB_API mrb_bool mrb_obj_equal(mrb_state *mrb, mrb_value a, mrb_value b); 1025 1170 MRB_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);1171 MRB_API mrb_value mrb_convert_to_integer(mrb_state *mrb, mrb_value val, mrb_int base); 1027 1172 MRB_API mrb_value mrb_Integer(mrb_state *mrb, mrb_value val); 1173 #ifndef MRB_WITHOUT_FLOAT 1028 1174 MRB_API mrb_value mrb_Float(mrb_state *mrb, mrb_value val); 1175 #endif 1029 1176 MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj); 1030 1177 MRB_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 */ 1179 MRB_API mrb_int mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2); 1180 1181 MRB_INLINE int 1182 mrb_gc_arena_save(mrb_state *mrb) 1183 { 1184 return mrb->gc.arena_idx; 1185 } 1186 1187 MRB_INLINE void 1188 mrb_gc_arena_restore(mrb_state *mrb, int idx) 1189 { 1190 mrb->gc.arena_idx = idx; 1191 } 1031 1192 1032 1193 MRB_API void mrb_garbage_collect(mrb_state*); 1033 1194 MRB_API void mrb_full_gc(mrb_state*); 1034 1195 MRB_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);1037 1196 MRB_API void mrb_gc_mark(mrb_state*,struct RBasic*); 1038 1197 #define mrb_gc_mark_value(mrb,val) do {\ … … 1077 1236 MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...); 1078 1237 MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...); 1238 MRB_API mrb_noreturn void mrb_frozen_error(mrb_state *mrb, void *frozen_obj); 1239 MRB_API mrb_noreturn void mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max); 1079 1240 MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...); 1080 1241 MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *fmt, ...); 1081 1242 MRB_API void mrb_print_backtrace(mrb_state *mrb); 1082 1243 MRB_API void mrb_print_error(mrb_state *mrb); 1244 /* function for `raisef` formatting */ 1245 MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap); 1083 1246 1084 1247 /* macros to get typical exception objects … … 1098 1261 #define E_LOCALJUMP_ERROR (mrb_exc_get(mrb, "LocalJumpError")) 1099 1262 #define E_REGEXP_ERROR (mrb_exc_get(mrb, "RegexpError")) 1263 #define E_FROZEN_ERROR (mrb_exc_get(mrb, "FrozenError")) 1100 1264 1101 1265 #define E_NOTIMP_ERROR (mrb_exc_get(mrb, "NotImplementedError")) 1266 #ifndef MRB_WITHOUT_FLOAT 1102 1267 #define E_FLOATDOMAIN_ERROR (mrb_exc_get(mrb, "FloatDomainError")) 1268 #endif 1103 1269 1104 1270 #define E_KEY_ERROR (mrb_exc_get(mrb, "KeyError")) … … 1122 1288 MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val); 1123 1289 #define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val)) 1290 /* string type checking (contrary to the name, it doesn't convert) */ 1291 MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val); 1124 1292 MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t); 1293 1294 MRB_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 } 1125 1298 1126 1299 typedef enum call_type { … … 1131 1304 } call_type; 1132 1305 1133 MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass * klass, const char *name1, const char *name2);1306 MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b); 1134 1307 MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass); 1135 1308 MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val); … … 1142 1315 1143 1316 1144 /* 1317 /** 1145 1318 * Resume a Fiber 1146 1319 * 1147 * @mrbgemmruby-fiber1320 * Implemented in mruby-fiber 1148 1321 */ 1149 1322 MRB_API mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv); 1150 1323 1151 /* 1324 /** 1152 1325 * Yield a Fiber 1153 1326 * 1154 * @mrbgemmruby-fiber1327 * Implemented in mruby-fiber 1155 1328 */ 1156 1329 MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv); 1157 1330 1158 /* 1331 /** 1332 * Check if a Fiber is alive 1333 * 1334 * Implemented in mruby-fiber 1335 */ 1336 MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib); 1337 1338 /** 1159 1339 * FiberError reference 1160 1340 * 1161 * @mrbgemmruby-fiber1341 * Implemented in mruby-fiber 1162 1342 */ 1163 1343 #define E_FIBER_ERROR (mrb_exc_get(mrb, "FiberError")) 1344 MRB_API void mrb_stack_extend(mrb_state*, mrb_int); 1164 1345 1165 1346 /* memory pool implementation */ … … 1170 1351 MRB_API void* mrb_pool_realloc(struct mrb_pool*, void*, size_t oldlen, size_t newlen); 1171 1352 MRB_API mrb_bool mrb_pool_can_realloc(struct mrb_pool*, void*, size_t); 1353 /* temporary memory allocation, only effective while GC arena is kept */ 1172 1354 MRB_API void* mrb_alloca(mrb_state *mrb, size_t); 1173 1355 … … 1184 1366 #undef memcpy 1185 1367 #undef memset 1186 static inlinevoid*1368 static void* 1187 1369 mrbmemcpy(void *dst, const void *src, size_t n) 1188 1370 { 1189 char *d = dst;1190 const char *s = src;1371 char *d = (char*)dst; 1372 const char *s = (const char*)src; 1191 1373 while (n--) 1192 1374 *d++ = *s++; … … 1195 1377 #define memcpy(a,b,c) mrbmemcpy(a,b,c) 1196 1378 1197 static inlinevoid*1379 static void* 1198 1380 mrbmemset(void *s, int c, size_t n) 1199 1381 { 1200 char *t = s;1382 char *t = (char*)s; 1201 1383 while (n--) 1202 1384 *t++ = c;
Note:
See TracChangeset
for help on using the changeset viewer.