1 /*
2 ** mruby - An embeddable Ruby implementation
3 **
4 ** Copyright (c) mruby developers 2010-2019
5 **
6 ** Permission is hereby granted, free of charge, to any person obtaining
7 ** a copy of this software and associated documentation files (the
8 ** "Software"), to deal in the Software without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Software, and to
11 ** permit persons to whom the Software is furnished to do so, subject to
12 ** the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be
15 ** included in all copies or substantial portions of the Software.
16 **
17 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 **
25 ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
26 */
27 
28 #ifndef MRUBY_H
29 #define MRUBY_H
30 
31 #ifdef __cplusplus
32 #define __STDC_LIMIT_MACROS
33 #define __STDC_CONSTANT_MACROS
34 #define __STDC_FORMAT_MACROS
35 #endif
36 
37 #include <stdarg.h>
38 #include <stdint.h>
39 #include <stddef.h>
40 #include <limits.h>
41 
42 #ifdef __cplusplus
43 #ifndef SIZE_MAX
44 #ifdef __SIZE_MAX__
45 #define SIZE_MAX __SIZE_MAX__
46 #else
47 #define SIZE_MAX std::numeric_limits<size_t>::max()
48 #endif
49 #endif
50 #endif
51 
52 #ifdef MRB_DEBUG
53 #include <assert.h>
54 #define mrb_assert(p) assert(p)
55 #define mrb_assert_int_fit(t1,n,t2,max) assert((n)>=0 && ((sizeof(n)<=sizeof(t2))||(n<=(t1)(max))))
56 #else
57 #define mrb_assert(p) ((void)0)
58 #define mrb_assert_int_fit(t1,n,t2,max) ((void)0)
59 #endif
60 
61 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
62 #define mrb_static_assert(exp, str) _Static_assert(exp, str)
63 #else
64 #define mrb_static_assert(exp, str) mrb_assert(exp)
65 #endif
66 
67 #include "mrbconf.h"
68 
69 #ifndef MRB_WITHOUT_FLOAT
70 #ifndef FLT_EPSILON
71 #define FLT_EPSILON (1.19209290e-07f)
72 #endif
73 #ifndef DBL_EPSILON
74 #define DBL_EPSILON ((double)2.22044604925031308085e-16L)
75 #endif
76 #ifndef LDBL_EPSILON
77 #define LDBL_EPSILON (1.08420217248550443401e-19L)
78 #endif
79 
80 #ifdef MRB_USE_FLOAT
81 #define MRB_FLOAT_EPSILON FLT_EPSILON
82 #else
83 #define MRB_FLOAT_EPSILON DBL_EPSILON
84 #endif
85 #endif
86 
87 #include "mruby/common.h"
88 #include <mruby/value.h>
89 #include <mruby/gc.h>
90 #include <mruby/version.h>
91 
92 /**
93  * MRuby C API entry point
94  */
95 MRB_BEGIN_DECL
96 
97 typedef uint8_t mrb_code;
98 
99 /**
100  * Required arguments signature type.
101  */
102 typedef uint32_t mrb_aspec;
103 
104 
105 struct mrb_irep;
106 struct mrb_state;
107 
108 /**
109  * Function pointer type of custom allocator used in @see mrb_open_allocf.
110  *
111  * The function pointing it must behave similarly as realloc except:
112  * - If ptr is NULL it must allocate new space.
113  * - If s is NULL, ptr must be freed.
114  *
115  * See @see mrb_default_allocf for the default implementation.
116  */
117 typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
118 
119 #ifndef MRB_FIXED_STATE_ATEXIT_STACK_SIZE
120 #define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5
121 #endif
122 
123 typedef struct {
124   mrb_sym mid;
125   struct RProc *proc;
126   mrb_value *stackent;
127   uint16_t ridx;
128   uint16_t epos;
129   struct REnv *env;
130   mrb_code *pc;                 /* return address */
131   mrb_code *err;                /* error position */
132   int argc;
133   int acc;
134   struct RClass *target_class;
135 } mrb_callinfo;
136 
137 enum mrb_fiber_state {
138   MRB_FIBER_CREATED = 0,
139   MRB_FIBER_RUNNING,
140   MRB_FIBER_RESUMED,
141   MRB_FIBER_SUSPENDED,
142   MRB_FIBER_TRANSFERRED,
143   MRB_FIBER_TERMINATED,
144 };
145 
146 struct mrb_context {
147   struct mrb_context *prev;
148 
149   mrb_value *stack;                       /* stack of virtual machine */
150   mrb_value *stbase, *stend;
151 
152   mrb_callinfo *ci;
153   mrb_callinfo *cibase, *ciend;
154 
155   uint16_t *rescue;                       /* exception handler stack */
156   uint16_t rsize;
157   struct RProc **ensure;                  /* ensure handler stack */
158   uint16_t esize, eidx;
159 
160   enum mrb_fiber_state status;
161   mrb_bool vmexec;
162   struct RFiber *fib;
163 };
164 
165 #ifdef MRB_METHOD_CACHE_SIZE
166 # define MRB_METHOD_CACHE
167 #else
168 /* default method cache size: 128 */
169 /* cache size needs to be power of 2 */
170 # define MRB_METHOD_CACHE_SIZE (1<<7)
171 #endif
172 
173 typedef mrb_value (*mrb_func_t)(struct mrb_state *mrb, mrb_value);
174 
175 #ifdef MRB_METHOD_TABLE_INLINE
176 typedef uintptr_t mrb_method_t;
177 #else
178 typedef struct {
179   mrb_bool func_p;
180   union {
181     struct RProc *proc;
182     mrb_func_t func;
183   };
184 } mrb_method_t;
185 #endif
186 
187 #ifdef MRB_METHOD_CACHE
188 struct mrb_cache_entry {
189   struct RClass *c, *c0;
190   mrb_sym mid;
191   mrb_method_t m;
192 };
193 #endif
194 
195 struct mrb_jmpbuf;
196 
197 typedef void (*mrb_atexit_func)(struct mrb_state*);
198 
199 #define MRB_STATE_NO_REGEXP 1
200 #define MRB_STATE_REGEXP    2
201 
202 typedef struct mrb_state {
203   struct mrb_jmpbuf *jmp;
204 
205   uint32_t flags;
206   mrb_allocf allocf;                      /* memory allocation function */
207   void *allocf_ud;                        /* auxiliary data of allocf */
208 
209   struct mrb_context *c;
210   struct mrb_context *root_c;
211   struct iv_tbl *globals;                 /* global variable table */
212 
213   struct RObject *exc;                    /* exception */
214 
215   struct RObject *top_self;
216   struct RClass *object_class;            /* Object class */
217   struct RClass *class_class;
218   struct RClass *module_class;
219   struct RClass *proc_class;
220   struct RClass *string_class;
221   struct RClass *array_class;
222   struct RClass *hash_class;
223   struct RClass *range_class;
224 
225 #ifndef MRB_WITHOUT_FLOAT
226   struct RClass *float_class;
227 #endif
228   struct RClass *fixnum_class;
229   struct RClass *true_class;
230   struct RClass *false_class;
231   struct RClass *nil_class;
232   struct RClass *symbol_class;
233   struct RClass *kernel_module;
234 
235   struct alloca_header *mems;
236   mrb_gc gc;
237 
238 #ifdef MRB_METHOD_CACHE
239   struct mrb_cache_entry cache[MRB_METHOD_CACHE_SIZE];
240 #endif
241 
242   mrb_sym symidx;
243   struct symbol_name *symtbl;   /* symbol table */
244   mrb_sym symhash[256];
245   size_t symcapa;
246 #ifndef MRB_ENABLE_SYMBOLL_ALL
247   char symbuf[8];               /* buffer for small symbol names */
248 #endif
249 
250 #ifdef MRB_ENABLE_DEBUG_HOOK
251   void (*code_fetch_hook)(struct mrb_state* mrb, struct mrb_irep *irep, mrb_code *pc, mrb_value *regs);
252   void (*debug_op_hook)(struct mrb_state* mrb, struct mrb_irep *irep, mrb_code *pc, mrb_value *regs);
253 #endif
254 
255 #ifdef MRB_BYTECODE_DECODE_OPTION
256   mrb_code (*bytecode_decoder)(struct mrb_state* mrb, mrb_code code);
257 #endif
258 
259   struct RClass *eException_class;
260   struct RClass *eStandardError_class;
261   struct RObject *nomem_err;              /* pre-allocated NoMemoryError */
262   struct RObject *stack_err;              /* pre-allocated SysStackError */
263 #ifdef MRB_GC_FIXED_ARENA
264   struct RObject *arena_err;              /* pre-allocated arena overfow error */
265 #endif
266 
267   void *ud; /* auxiliary data */
268 
269 #ifdef MRB_FIXED_STATE_ATEXIT_STACK
270   mrb_atexit_func atexit_stack[MRB_FIXED_STATE_ATEXIT_STACK_SIZE];
271 #else
272   mrb_atexit_func *atexit_stack;
273 #endif
274   uint16_t atexit_stack_len;
275   uint16_t ecall_nest;                    /* prevent infinite recursive ecall() */
276 } mrb_state;
277 
278 /**
279  * Defines a new class.
280  *
281  * If you're creating a gem it may look something like this:
282  *
283  *      !!!c
284  *      void mrb_example_gem_init(mrb_state* mrb) {
285  *          struct RClass *example_class;
286  *          example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
287  *      }
288  *
289  *      void mrb_example_gem_final(mrb_state* mrb) {
290  *          //free(TheAnimals);
291  *      }
292  *
293  * @param [mrb_state *] mrb The current mruby state.
294  * @param [const char *] name The name of the defined class.
295  * @param [struct RClass *] super The new class parent.
296  * @return [struct RClass *] Reference to the newly defined class.
297  * @see mrb_define_class_under
298  */
299 MRB_API struct RClass *mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super);
300 
301 /**
302  * Defines a new module.
303  *
304  * @param [mrb_state *] mrb_state* The current mruby state.
305  * @param [const char *] char* The name of the module.
306  * @return [struct RClass *] Reference to the newly defined module.
307  */
308 MRB_API struct RClass *mrb_define_module(mrb_state *, const char*);
309 MRB_API mrb_value mrb_singleton_class(mrb_state*, mrb_value);
310 
311 /**
312  * Include a module in another class or module.
313  * Equivalent to:
314  *
315  *   module B
316  *     include A
317  *   end
318  * @param [mrb_state *] mrb_state* The current mruby state.
319  * @param [struct RClass *] RClass* A reference to module or a class.
320  * @param [struct RClass *] RClass* A reference to the module to be included.
321  */
322 MRB_API void mrb_include_module(mrb_state*, struct RClass*, struct RClass*);
323 
324 /**
325  * Prepends a module in another class or module.
326  *
327  * Equivalent to:
328  *  module B
329  *    prepend A
330  *  end
331  * @param [mrb_state *] mrb_state* The current mruby state.
332  * @param [struct RClass *] RClass* A reference to module or a class.
333  * @param [struct RClass *] RClass* A reference to the module to be prepended.
334  */
335 MRB_API void mrb_prepend_module(mrb_state*, struct RClass*, struct RClass*);
336 
337 /**
338  * Defines a global function in ruby.
339  *
340  * If you're creating a gem it may look something like this
341  *
342  * Example:
343  *
344  *     !!!c
345  *     mrb_value example_method(mrb_state* mrb, mrb_value self)
346  *     {
347  *          puts("Executing example command!");
348  *          return self;
349  *     }
350  *
351  *     void mrb_example_gem_init(mrb_state* mrb)
352  *     {
353  *           mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
354  *     }
355  *
356  * @param [mrb_state *] mrb The MRuby state reference.
357  * @param [struct RClass *] cla The class pointer where the method will be defined.
358  * @param [const char *] name The name of the method being defined.
359  * @param [mrb_func_t] func The function pointer to the method definition.
360  * @param [mrb_aspec] aspec The method parameters declaration.
361  */
362 MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
363 
364 /**
365  * Defines a class method.
366  *
367  * Example:
368  *
369  *     # Ruby style
370  *     class Foo
371  *       def Foo.bar
372  *       end
373  *     end
374  *     // C style
375  *     mrb_value bar_method(mrb_state* mrb, mrb_value self){
376  *       return mrb_nil_value();
377  *     }
378  *     void mrb_example_gem_init(mrb_state* mrb){
379  *       struct RClass *foo;
380  *       foo = mrb_define_class(mrb, "Foo", mrb->object_class);
381  *       mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
382  *     }
383  * @param [mrb_state *] mrb_state* The MRuby state reference.
384  * @param [struct RClass *] RClass* The class where the class method will be defined.
385  * @param [const char *] char* The name of the class method being defined.
386  * @param [mrb_func_t] mrb_func_t The function pointer to the class method definition.
387  * @param [mrb_aspec] mrb_aspec The method parameters declaration.
388  */
389 MRB_API void mrb_define_class_method(mrb_state *, struct RClass *, const char *, mrb_func_t, mrb_aspec);
390 MRB_API void mrb_define_singleton_method(mrb_state*, struct RObject*, const char*, mrb_func_t, mrb_aspec);
391 
392 /**
393  *  Defines a module function.
394  *
395  * Example:
396  *
397  *        # Ruby style
398  *        module Foo
399  *          def Foo.bar
400  *          end
401  *        end
402  *        // C style
403  *        mrb_value bar_method(mrb_state* mrb, mrb_value self){
404  *          return mrb_nil_value();
405  *        }
406  *        void mrb_example_gem_init(mrb_state* mrb){
407  *          struct RClass *foo;
408  *          foo = mrb_define_module(mrb, "Foo");
409  *          mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
410  *        }
411  *  @param [mrb_state *] mrb_state* The MRuby state reference.
412  *  @param [struct RClass *] RClass* The module where the module function will be defined.
413  *  @param [const char *] char* The name of the module function being defined.
414  *  @param [mrb_func_t] mrb_func_t The function pointer to the module function definition.
415  *  @param [mrb_aspec] mrb_aspec The method parameters declaration.
416  */
417 MRB_API void mrb_define_module_function(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec);
418 
419 /**
420  *  Defines a constant.
421  *
422  * Example:
423  *
424  *          # Ruby style
425  *          class ExampleClass
426  *            AGE = 22
427  *          end
428  *          // C style
429  *          #include <stdio.h>
430  *          #include <mruby.h>
431  *
432  *          void
433  *          mrb_example_gem_init(mrb_state* mrb){
434  *            mrb_define_const(mrb, mrb->kernel_module, "AGE", mrb_fixnum_value(22));
435  *          }
436  *
437  *          mrb_value
438  *          mrb_example_gem_final(mrb_state* mrb){
439  *          }
440  *  @param [mrb_state *] mrb_state* The MRuby state reference.
441  *  @param [struct RClass *] RClass* A class or module the constant is defined in.
442  *  @param [const char *] name The name of the constant being defined.
443  *  @param [mrb_value] mrb_value The value for the constant.
444  */
445 MRB_API void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value);
446 
447 /**
448  * Undefines a method.
449  *
450  * Example:
451  *
452  *     # Ruby style
453  *
454  *     class ExampleClassA
455  *       def example_method
456  *         "example"
457  *       end
458  *     end
459  *     ExampleClassA.new.example_method # => example
460  *
461  *     class ExampleClassB < ExampleClassA
462  *       undef_method :example_method
463  *     end
464  *
465  *     ExampleClassB.new.example_method # => undefined method 'example_method' for ExampleClassB (NoMethodError)
466  *
467  *     // C style
468  *     #include <stdio.h>
469  *     #include <mruby.h>
470  *
471  *     mrb_value
472  *     mrb_example_method(mrb_state *mrb){
473  *       return mrb_str_new_lit(mrb, "example");
474  *     }
475  *
476  *     void
477  *     mrb_example_gem_init(mrb_state* mrb){
478  *       struct RClass *example_class_a;
479  *       struct RClass *example_class_b;
480  *       struct RClass *example_class_c;
481  *
482  *       example_class_a = mrb_define_class(mrb, "ExampleClassA", mrb->object_class);
483  *       mrb_define_method(mrb, example_class_a, "example_method", mrb_example_method, MRB_ARGS_NONE());
484  *       example_class_b = mrb_define_class(mrb, "ExampleClassB", example_class_a);
485  *       example_class_c = mrb_define_class(mrb, "ExampleClassC", example_class_b);
486  *       mrb_undef_method(mrb, example_class_c, "example_method");
487  *     }
488  *
489  *     mrb_example_gem_final(mrb_state* mrb){
490  *     }
491  * @param [mrb_state*] mrb_state* The mruby state reference.
492  * @param [struct RClass*] RClass* A class the method will be undefined from.
493  * @param [const char*] const char* The name of the method to be undefined.
494  */
495 MRB_API void mrb_undef_method(mrb_state*, struct RClass*, const char*);
496 MRB_API void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym);
497 
498 /**
499  * Undefine a class method.
500  * Example:
501  *
502  *      # Ruby style
503  *      class ExampleClass
504  *        def self.example_method
505  *          "example"
506  *        end
507  *      end
508  *
509  *     ExampleClass.example_method
510  *
511  *     // C style
512  *     #include <stdio.h>
513  *     #include <mruby.h>
514  *
515  *     mrb_value
516  *     mrb_example_method(mrb_state *mrb){
517  *       return mrb_str_new_lit(mrb, "example");
518  *     }
519  *
520  *     void
521  *     mrb_example_gem_init(mrb_state* mrb){
522  *       struct RClass *example_class;
523  *       example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
524  *       mrb_define_class_method(mrb, example_class, "example_method", mrb_example_method, MRB_ARGS_NONE());
525  *       mrb_undef_class_method(mrb, example_class, "example_method");
526  *      }
527  *
528  *      void
529  *      mrb_example_gem_final(mrb_state* mrb){
530  *      }
531  * @param [mrb_state*] mrb_state* The mruby state reference.
532  * @param [RClass*] RClass* A class the class method will be undefined from.
533  * @param [const char*] const char* The name of the class method to be undefined.
534  */
535 MRB_API void mrb_undef_class_method(mrb_state*, struct RClass*, const char*);
536 
537 /**
538  * Initialize a new object instance of c class.
539  *
540  * Example:
541  *
542  *     # Ruby style
543  *     class ExampleClass
544  *     end
545  *
546  *     p ExampleClass # => #<ExampleClass:0x9958588>
547  *     // C style
548  *     #include <stdio.h>
549  *     #include <mruby.h>
550  *
551  *     void
552  *     mrb_example_gem_init(mrb_state* mrb) {
553  *       struct RClass *example_class;
554  *       mrb_value obj;
555  *       example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); # => class ExampleClass; end
556  *       obj = mrb_obj_new(mrb, example_class, 0, NULL); # => ExampleClass.new
557  *       mrb_p(mrb, obj); // => Kernel#p
558  *      }
559  * @param [mrb_state*] mrb The current mruby state.
560  * @param [RClass*] c Reference to the class of the new object.
561  * @param [mrb_int] argc Number of arguments in argv
562  * @param [const mrb_value *] argv Array of mrb_value to initialize the object
563  * @return [mrb_value] The newly initialized object
564  */
565 MRB_API mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *argv);
566 
567 /** @see mrb_obj_new */
mrb_class_new_instance(mrb_state * mrb,mrb_int argc,const mrb_value * argv,struct RClass * c)568 MRB_INLINE mrb_value mrb_class_new_instance(mrb_state *mrb, mrb_int argc, const mrb_value *argv, struct RClass *c)
569 {
570   return mrb_obj_new(mrb,c,argc,argv);
571 }
572 
573 MRB_API mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
574 
575 /**
576  * Creates a new instance of Class, Class.
577  *
578  * Example:
579  *
580  *      void
581  *      mrb_example_gem_init(mrb_state* mrb) {
582  *        struct RClass *example_class;
583  *
584  *        mrb_value obj;
585  *        example_class = mrb_class_new(mrb, mrb->object_class);
586  *        obj = mrb_obj_new(mrb, example_class, 0, NULL); // => #<#<Class:0x9a945b8>:0x9a94588>
587  *        mrb_p(mrb, obj); // => Kernel#p
588  *       }
589  *
590  * @param [mrb_state*] mrb The current mruby state.
591  * @param [struct RClass *] super The super class or parent.
592  * @return [struct RClass *] Reference to the new class.
593  */
594 MRB_API struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super);
595 
596 /**
597  * Creates a new module, Module.
598  *
599  * Example:
600  *      void
601  *      mrb_example_gem_init(mrb_state* mrb) {
602  *        struct RClass *example_module;
603  *
604  *        example_module = mrb_module_new(mrb);
605  *      }
606  *
607  * @param [mrb_state*] mrb The current mruby state.
608  * @return [struct RClass *] Reference to the new module.
609  */
610 MRB_API struct RClass * mrb_module_new(mrb_state *mrb);
611 
612 /**
613  * Returns an mrb_bool. True if class was defined, and false if the class was not defined.
614  *
615  * Example:
616  *     void
617  *     mrb_example_gem_init(mrb_state* mrb) {
618  *       struct RClass *example_class;
619  *       mrb_bool cd;
620  *
621  *       example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
622  *       cd = mrb_class_defined(mrb, "ExampleClass");
623  *
624  *       // If mrb_class_defined returns 1 then puts "True"
625  *       // If mrb_class_defined returns 0 then puts "False"
626  *       if (cd == 1){
627  *         puts("True");
628  *       }
629  *       else {
630  *         puts("False");
631  *       }
632  *      }
633  *
634  * @param [mrb_state*] mrb The current mruby state.
635  * @param [const char *] name A string representing the name of the class.
636  * @return [mrb_bool] A boolean value.
637  */
638 MRB_API mrb_bool mrb_class_defined(mrb_state *mrb, const char *name);
639 
640 /**
641  * Gets a class.
642  * @param [mrb_state*] mrb The current mruby state.
643  * @param [const char *] name The name of the class.
644  * @return [struct RClass *] A reference to the class.
645 */
646 MRB_API struct RClass * mrb_class_get(mrb_state *mrb, const char *name);
647 
648 /**
649  * Gets a exception class.
650  * @param [mrb_state*] mrb The current mruby state.
651  * @param [const char *] name The name of the class.
652  * @return [struct RClass *] A reference to the class.
653 */
654 MRB_API struct RClass * mrb_exc_get(mrb_state *mrb, const char *name);
655 
656 /**
657  * Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined.
658  *
659  * Example:
660  *     void
661  *     mrb_example_gem_init(mrb_state* mrb) {
662  *       struct RClass *example_outer, *example_inner;
663  *       mrb_bool cd;
664  *
665  *       example_outer = mrb_define_module(mrb, "ExampleOuter");
666  *
667  *       example_inner = mrb_define_class_under(mrb, example_outer, "ExampleInner", mrb->object_class);
668  *       cd = mrb_class_defined_under(mrb, example_outer, "ExampleInner");
669  *
670  *       // If mrb_class_defined_under returns 1 then puts "True"
671  *       // If mrb_class_defined_under returns 0 then puts "False"
672  *       if (cd == 1){
673  *         puts("True");
674  *       }
675  *       else {
676  *         puts("False");
677  *       }
678  *      }
679  *
680  * @param [mrb_state*] mrb The current mruby state.
681  * @param [struct RClass *] outer The name of the outer class.
682  * @param [const char *] name A string representing the name of the inner class.
683  * @return [mrb_bool] A boolean value.
684  */
685 MRB_API mrb_bool mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name);
686 
687 /**
688  * Gets a child class.
689  * @param [mrb_state*] mrb The current mruby state.
690  * @param [struct RClass *] outer The name of the parent class.
691  * @param [const char *] name The name of the class.
692  * @return [struct RClass *] A reference to the class.
693 */
694 MRB_API struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
695 
696 /**
697  * Gets a module.
698  * @param [mrb_state*] mrb The current mruby state.
699  * @param [const char *] name The name of the module.
700  * @return [struct RClass *] A reference to the module.
701 */
702 MRB_API struct RClass * mrb_module_get(mrb_state *mrb, const char *name);
703 
704 /**
705  * Gets a module defined under another module.
706  * @param [mrb_state*] mrb The current mruby state.
707  * @param [struct RClass *] outer The name of the outer module.
708  * @param [const char *] name The name of the module.
709  * @return [struct RClass *] A reference to the module.
710 */
711 MRB_API struct RClass * mrb_module_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
712 /* a function to raise NotImplementedError with current method name */
713 MRB_API void mrb_notimplement(mrb_state*);
714 /* a function to be replacement of unimplemented method */
715 MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value);
716 
717 /**
718  * Duplicate an object.
719  *
720  * Equivalent to:
721  *   Object#dup
722  * @param [mrb_state*] mrb The current mruby state.
723  * @param [mrb_value] obj Object to be duplicate.
724  * @return [mrb_value] The newly duplicated object.
725  */
726 MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
727 
728 /**
729  * Returns true if obj responds to the given method. If the method was defined for that
730  * class it returns true, it returns false otherwise.
731  *
732  *      Example:
733  *      # Ruby style
734  *      class ExampleClass
735  *        def example_method
736  *        end
737  *      end
738  *
739  *      ExampleClass.new.respond_to?(:example_method) # => true
740  *
741  *      // C style
742  *      void
743  *      mrb_example_gem_init(mrb_state* mrb) {
744  *        struct RClass *example_class;
745  *        mrb_sym mid;
746  *        mrb_bool obj_resp;
747  *
748  *        example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
749  *        mrb_define_method(mrb, example_class, "example_method", exampleMethod, MRB_ARGS_NONE());
750  *        mid = mrb_intern_str(mrb, mrb_str_new_lit(mrb, "example_method" ));
751  *        obj_resp = mrb_obj_respond_to(mrb, example_class, mid); // => 1(true in Ruby world)
752  *
753  *        // If mrb_obj_respond_to returns 1 then puts "True"
754  *        // If mrb_obj_respond_to returns 0 then puts "False"
755  *        if (obj_resp == 1) {
756  *          puts("True");
757  *        }
758  *        else if (obj_resp == 0) {
759  *          puts("False");
760  *        }
761  *      }
762  *
763  * @param [mrb_state*] mrb The current mruby state.
764  * @param [struct RClass *] c A reference to a class.
765  * @param [mrb_sym] mid A symbol referencing a method id.
766  * @return [mrb_bool] A boolean value.
767  */
768 MRB_API mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid);
769 
770 /**
771  * Defines a new class under a given module
772  *
773  * @param [mrb_state*] mrb The current mruby state.
774  * @param [struct RClass *] outer Reference to the module under which the new class will be defined
775  * @param [const char *] name The name of the defined class
776  * @param [struct RClass *] super The new class parent
777  * @return [struct RClass *] Reference to the newly defined class
778  * @see mrb_define_class
779  */
780 MRB_API struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
781 
782 MRB_API struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
783 
784 /**
785  * Function requires n arguments.
786  *
787  * @param n
788  *      The number of required arguments.
789  */
790 #define MRB_ARGS_REQ(n)     ((mrb_aspec)((n)&0x1f) << 18)
791 
792 /**
793  * Function takes n optional arguments
794  *
795  * @param n
796  *      The number of optional arguments.
797  */
798 #define MRB_ARGS_OPT(n)     ((mrb_aspec)((n)&0x1f) << 13)
799 
800 /**
801  * Function takes n1 mandatory arguments and n2 optional arguments
802  *
803  * @param n1
804  *      The number of required arguments.
805  * @param n2
806  *      The number of optional arguments.
807  */
808 #define MRB_ARGS_ARG(n1,n2)   (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2))
809 
810 /** rest argument */
811 #define MRB_ARGS_REST()     ((mrb_aspec)(1 << 12))
812 
813 /** required arguments after rest */
814 #define MRB_ARGS_POST(n)    ((mrb_aspec)((n)&0x1f) << 7)
815 
816 /** keyword arguments (n of keys, kdict) */
817 #define MRB_ARGS_KEY(n1,n2) ((mrb_aspec)((((n1)&0x1f) << 2) | ((n2)?(1<<1):0)))
818 
819 /**
820  * Function takes a block argument
821  */
822 #define MRB_ARGS_BLOCK()    ((mrb_aspec)1)
823 
824 /**
825  * Function accepts any number of arguments
826  */
827 #define MRB_ARGS_ANY()      MRB_ARGS_REST()
828 
829 /**
830  * Function accepts no arguments
831  */
832 #define MRB_ARGS_NONE()     ((mrb_aspec)0)
833 
834 /**
835  * Format specifiers for {mrb_get_args} function
836  *
837  * Must be a C string composed of the following format specifiers:
838  *
839  * | char | Ruby type      | C types           | Notes                                              |
840  * |:----:|----------------|-------------------|----------------------------------------------------|
841  * | `o`  | {Object}       | {mrb_value}       | Could be used to retrieve any type of argument     |
842  * | `C`  | {Class}/{Module} | {mrb_value}     |                                                    |
843  * | `S`  | {String}       | {mrb_value}       | when `!` follows, the value may be `nil`           |
844  * | `A`  | {Array}        | {mrb_value}       | when `!` follows, the value may be `nil`           |
845  * | `H`  | {Hash}         | {mrb_value}       | when `!` follows, the value may be `nil`           |
846  * | `s`  | {String}       | char *, {mrb_int} | Receive two arguments; `s!` gives (`NULL`,`0`) for `nil`       |
847  * | `z`  | {String}       | char *            | `NULL` terminated string; `z!` gives `NULL` for `nil`           |
848  * | `a`  | {Array}        | {mrb_value} *, {mrb_int} | Receive two arguments; `a!` gives (`NULL`,`0`) for `nil` |
849  * | `f`  | {Float}        | {mrb_float}       |                                                    |
850  * | `i`  | {Integer}      | {mrb_int}         |                                                    |
851  * | `b`  | boolean        | {mrb_bool}        |                                                    |
852  * | `n`  | {Symbol}       | {mrb_sym}         |                                                    |
853  * | `&`  | block          | {mrb_value}       | &! raises exception if no block given.             |
854  * | `*`  | rest arguments | {mrb_value} *, {mrb_int} | Receive the rest of arguments as an array; *! avoid copy of the stack.  |
855  * | &vert; | optional     |                   | After this spec following specs would be optional. |
856  * | `?`  | optional given | {mrb_bool}        | `TRUE` if preceding argument is given. Used to check optional argument is given. |
857  *
858  * @see mrb_get_args
859  */
860 typedef const char *mrb_args_format;
861 
862 /**
863  * Retrieve arguments from mrb_state.
864  *
865  * @param mrb The current MRuby state.
866  * @param format [mrb_args_format] is a list of format specifiers
867  * @param ... The passing variadic arguments must be a pointer of retrieving type.
868  * @return the number of arguments retrieved.
869  * @see mrb_args_format
870  */
871 MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...);
872 
873 static inline mrb_sym
mrb_get_mid(mrb_state * mrb)874 mrb_get_mid(mrb_state *mrb) /* get method symbol */
875 {
876   return mrb->c->ci->mid;
877 }
878 
879 /**
880  * Retrieve number of arguments from mrb_state.
881  *
882  * Correctly handles *splat arguments.
883  */
884 MRB_API mrb_int mrb_get_argc(mrb_state *mrb);
885 
886 MRB_API mrb_value* mrb_get_argv(mrb_state *mrb);
887 
888 /* `strlen` for character string literals (use with caution or `strlen` instead)
889     Adjacent string literals are concatenated in C/C++ in translation phase 6.
890     If `lit` is not one, the compiler will report a syntax error:
891      MSVC: "error C2143: syntax error : missing ')' before 'string'"
892      GCC:  "error: expected ')' before string constant"
893 */
894 #define mrb_strlen_lit(lit) (sizeof(lit "") - 1)
895 
896 /**
897  * Call existing ruby functions.
898  *
899  *      #include <stdio.h>
900  *      #include <mruby.h>
901  *      #include "mruby/compile.h"
902  *
903  *      int
904  *      main()
905  *      {
906  *        mrb_int i = 99;
907  *        mrb_state *mrb = mrb_open();
908  *
909  *        if (!mrb) { }
910  *        FILE *fp = fopen("test.rb","r");
911  *        mrb_value obj = mrb_load_file(mrb,fp);
912  *        mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
913  *        fclose(fp);
914  *        mrb_close(mrb);
915  *       }
916  * @param [mrb_state*] mrb_state* The current mruby state.
917  * @param [mrb_value] mrb_value A reference to an mruby value.
918  * @param [const char*] const char* The name of the method.
919  * @param [mrb_int] mrb_int The number of arguments the method has.
920  * @param [...] ... Variadic values(not type safe!).
921  * @return [mrb_value] mrb_value mruby function value.
922  */
923 MRB_API mrb_value mrb_funcall(mrb_state*, mrb_value, const char*, mrb_int,...);
924 /**
925  * Call existing ruby functions. This is basically the type safe version of mrb_funcall.
926  *
927  *      #include <stdio.h>
928  *      #include <mruby.h>
929  *      #include "mruby/compile.h"
930  *      int
931  *      main()
932  *      {
933  *        mrb_int i = 99;
934  *        mrb_state *mrb = mrb_open();
935  *
936  *        if (!mrb) { }
937  *        mrb_sym m_sym = mrb_intern_lit(mrb, "method_name"); // Symbol for method.
938  *
939  *        FILE *fp = fopen("test.rb","r");
940  *        mrb_value obj = mrb_load_file(mrb,fp);
941  *        mrb_funcall_argv(mrb, obj, m_sym, 1, &obj); // Calling ruby function from test.rb.
942  *        fclose(fp);
943  *        mrb_close(mrb);
944  *       }
945  * @param [mrb_state*] mrb_state* The current mruby state.
946  * @param [mrb_value] mrb_value A reference to an mruby value.
947  * @param [mrb_sym] mrb_sym The symbol representing the method.
948  * @param [mrb_int] mrb_int The number of arguments the method has.
949  * @param [const mrb_value*] mrb_value* Pointer to the object.
950  * @return [mrb_value] mrb_value mruby function value.
951  * @see mrb_funcall
952  */
953 MRB_API mrb_value mrb_funcall_argv(mrb_state*, mrb_value, mrb_sym, mrb_int, const mrb_value*);
954 /**
955  * Call existing ruby functions with a block.
956  */
957 MRB_API mrb_value mrb_funcall_with_block(mrb_state*, mrb_value, mrb_sym, mrb_int, const mrb_value*, mrb_value);
958 /**
959  * Create a symbol
960  *
961  *     # Ruby style:
962  *     :pizza # => :pizza
963  *
964  *     // C style:
965  *     mrb_sym m_sym = mrb_intern_lit(mrb, "pizza"); //  => :pizza
966  * @param [mrb_state*] mrb_state* The current mruby state.
967  * @param [const char*] const char* The name of the method.
968  * @return [mrb_sym] mrb_sym A symbol.
969  */
970 MRB_API mrb_sym mrb_intern_cstr(mrb_state*,const char*);
971 MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t);
972 MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t);
973 #define mrb_intern_lit(mrb, lit) mrb_intern_static(mrb, lit, mrb_strlen_lit(lit))
974 MRB_API mrb_sym mrb_intern_str(mrb_state*,mrb_value);
975 MRB_API mrb_value mrb_check_intern_cstr(mrb_state*,const char*);
976 MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t);
977 MRB_API mrb_value mrb_check_intern_str(mrb_state*,mrb_value);
978 MRB_API const char *mrb_sym2name(mrb_state*,mrb_sym);
979 MRB_API const char *mrb_sym2name_len(mrb_state*,mrb_sym,mrb_int*);
980 MRB_API mrb_value mrb_sym2str(mrb_state*,mrb_sym);
981 
982 MRB_API void *mrb_malloc(mrb_state*, size_t);         /* raise RuntimeError if no mem */
983 MRB_API void *mrb_calloc(mrb_state*, size_t, size_t); /* ditto */
984 MRB_API void *mrb_realloc(mrb_state*, void*, size_t); /* ditto */
985 MRB_API void *mrb_realloc_simple(mrb_state*, void*, size_t); /* return NULL if no memory available */
986 MRB_API void *mrb_malloc_simple(mrb_state*, size_t);  /* return NULL if no memory available */
987 MRB_API struct RBasic *mrb_obj_alloc(mrb_state*, enum mrb_vtype, struct RClass*);
988 MRB_API void mrb_free(mrb_state*, void*);
989 
990 MRB_API mrb_value mrb_str_new(mrb_state *mrb, const char *p, size_t len);
991 
992 /**
993  * Turns a C string into a Ruby string value.
994  */
995 MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*);
996 MRB_API mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, size_t len);
997 #define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
998 
999 #ifdef _WIN32
1000 MRB_API char* mrb_utf8_from_locale(const char *p, int len);
1001 MRB_API char* mrb_locale_from_utf8(const char *p, int len);
1002 #define mrb_locale_free(p) free(p)
1003 #define mrb_utf8_free(p) free(p)
1004 #else
1005 #define mrb_utf8_from_locale(p, l) ((char*)(p))
1006 #define mrb_locale_from_utf8(p, l) ((char*)(p))
1007 #define mrb_locale_free(p)
1008 #define mrb_utf8_free(p)
1009 #endif
1010 
1011 /**
1012  * Creates new mrb_state.
1013  *
1014  * @return
1015  *      Pointer to the newly created mrb_state.
1016  */
1017 MRB_API mrb_state* mrb_open(void);
1018 
1019 /**
1020  * Create new mrb_state with custom allocators.
1021  *
1022  * @param f
1023  *      Reference to the allocation function.
1024  * @param ud
1025  *      User data will be passed to custom allocator f.
1026  *      If user data isn't required just pass NULL.
1027  * @return
1028  *      Pointer to the newly created mrb_state.
1029  */
1030 MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud);
1031 
1032 /**
1033  * Create new mrb_state with just the MRuby core
1034  *
1035  * @param f
1036  *      Reference to the allocation function.
1037  *      Use mrb_default_allocf for the default
1038  * @param ud
1039  *      User data will be passed to custom allocator f.
1040  *      If user data isn't required just pass NULL.
1041  * @return
1042  *      Pointer to the newly created mrb_state.
1043  */
1044 MRB_API mrb_state* mrb_open_core(mrb_allocf f, void *ud);
1045 
1046 /**
1047  * Closes and frees a mrb_state.
1048  *
1049  * @param mrb
1050  *      Pointer to the mrb_state to be closed.
1051  */
1052 MRB_API void mrb_close(mrb_state *mrb);
1053 
1054 /**
1055  * The default allocation function.
1056  *
1057  * @see mrb_allocf
1058  */
1059 MRB_API void* mrb_default_allocf(mrb_state*, void*, size_t, void*);
1060 
1061 MRB_API mrb_value mrb_top_self(mrb_state *);
1062 MRB_API mrb_value mrb_run(mrb_state*, struct RProc*, mrb_value);
1063 MRB_API mrb_value mrb_top_run(mrb_state*, struct RProc*, mrb_value, unsigned int);
1064 MRB_API mrb_value mrb_vm_run(mrb_state*, struct RProc*, mrb_value, unsigned int);
1065 MRB_API mrb_value mrb_vm_exec(mrb_state*, struct RProc*, mrb_code*);
1066 /* compatibility macros */
1067 #define mrb_toplevel_run_keep(m,p,k) mrb_top_run((m),(p),mrb_top_self(m),(k))
1068 #define mrb_toplevel_run(m,p) mrb_toplevel_run_keep((m),(p),0)
1069 #define mrb_context_run(m,p,s,k) mrb_vm_run((m),(p),(s),(k))
1070 
1071 MRB_API void mrb_p(mrb_state*, mrb_value);
1072 MRB_API mrb_int mrb_obj_id(mrb_value obj);
1073 MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name);
1074 
1075 MRB_API mrb_bool mrb_obj_eq(mrb_state*, mrb_value, mrb_value);
1076 MRB_API mrb_bool mrb_obj_equal(mrb_state*, mrb_value, mrb_value);
1077 MRB_API mrb_bool mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
1078 MRB_API mrb_value mrb_convert_to_integer(mrb_state *mrb, mrb_value val, mrb_int base);
1079 MRB_API mrb_value mrb_Integer(mrb_state *mrb, mrb_value val);
1080 #ifndef MRB_WITHOUT_FLOAT
1081 MRB_API mrb_value mrb_Float(mrb_state *mrb, mrb_value val);
1082 #endif
1083 MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj);
1084 MRB_API mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
1085 
1086 static inline int mrb_gc_arena_save(mrb_state*);
1087 static inline void mrb_gc_arena_restore(mrb_state*,int);
1088 
1089 static inline int
mrb_gc_arena_save(mrb_state * mrb)1090 mrb_gc_arena_save(mrb_state *mrb)
1091 {
1092   return mrb->gc.arena_idx;
1093 }
1094 
1095 static inline void
mrb_gc_arena_restore(mrb_state * mrb,int idx)1096 mrb_gc_arena_restore(mrb_state *mrb, int idx)
1097 {
1098   mrb->gc.arena_idx = idx;
1099 }
1100 
1101 MRB_API void mrb_garbage_collect(mrb_state*);
1102 MRB_API void mrb_full_gc(mrb_state*);
1103 MRB_API void mrb_incremental_gc(mrb_state *);
1104 MRB_API void mrb_gc_mark(mrb_state*,struct RBasic*);
1105 #define mrb_gc_mark_value(mrb,val) do {\
1106   if (!mrb_immediate_p(val)) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \
1107 } while (0)
1108 MRB_API void mrb_field_write_barrier(mrb_state *, struct RBasic*, struct RBasic*);
1109 #define mrb_field_write_barrier_value(mrb, obj, val) do{\
1110   if (!mrb_immediate_p(val)) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \
1111 } while (0)
1112 MRB_API void mrb_write_barrier(mrb_state *, struct RBasic*);
1113 
1114 MRB_API mrb_value mrb_check_convert_type(mrb_state *mrb, mrb_value val, enum mrb_vtype type, const char *tname, const char *method);
1115 MRB_API mrb_value mrb_any_to_s(mrb_state *mrb, mrb_value obj);
1116 MRB_API const char * mrb_obj_classname(mrb_state *mrb, mrb_value obj);
1117 MRB_API struct RClass* mrb_obj_class(mrb_state *mrb, mrb_value obj);
1118 MRB_API mrb_value mrb_class_path(mrb_state *mrb, struct RClass *c);
1119 MRB_API mrb_value mrb_convert_type(mrb_state *mrb, mrb_value val, enum mrb_vtype type, const char *tname, const char *method);
1120 MRB_API mrb_bool mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c);
1121 MRB_API mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value self);
1122 MRB_API mrb_value mrb_obj_clone(mrb_state *mrb, mrb_value self);
1123 
1124 #ifndef ISPRINT
1125 #define ISASCII(c) ((unsigned)(c) <= 0x7f)
1126 #define ISPRINT(c) (((unsigned)(c) - 0x20) < 0x5f)
1127 #define ISSPACE(c) ((c) == ' ' || (unsigned)(c) - '\t' < 5)
1128 #define ISUPPER(c) (((unsigned)(c) - 'A') < 26)
1129 #define ISLOWER(c) (((unsigned)(c) - 'a') < 26)
1130 #define ISALPHA(c) ((((unsigned)(c) | 0x20) - 'a') < 26)
1131 #define ISDIGIT(c) (((unsigned)(c) - '0') < 10)
1132 #define ISXDIGIT(c) (ISDIGIT(c) || ((unsigned)(c) | 0x20) - 'a' < 6)
1133 #define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
1134 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
1135 #define ISCNTRL(c) ((unsigned)(c) < 0x20 || (c) == 0x7f)
1136 #define TOUPPER(c) (ISLOWER(c) ? ((c) & 0x5f) : (c))
1137 #define TOLOWER(c) (ISUPPER(c) ? ((c) | 0x20) : (c))
1138 #endif
1139 
1140 MRB_API mrb_value mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, size_t len);
1141 MRB_API mrb_noreturn void mrb_exc_raise(mrb_state *mrb, mrb_value exc);
1142 
1143 MRB_API mrb_noreturn void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg);
1144 MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...);
1145 MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...);
1146 MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...);
1147 MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *fmt, ...);
1148 MRB_API void mrb_print_backtrace(mrb_state *mrb);
1149 MRB_API void mrb_print_error(mrb_state *mrb);
1150 /* function for `raisef` formatting */
1151 MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap);
1152 
1153 /* macros to get typical exception objects
1154    note:
1155    + those E_* macros requires mrb_state* variable named mrb.
1156    + exception objects obtained from those macros are local to mrb
1157 */
1158 #define E_RUNTIME_ERROR             (mrb_exc_get(mrb, "RuntimeError"))
1159 #define E_TYPE_ERROR                (mrb_exc_get(mrb, "TypeError"))
1160 #define E_ARGUMENT_ERROR            (mrb_exc_get(mrb, "ArgumentError"))
1161 #define E_INDEX_ERROR               (mrb_exc_get(mrb, "IndexError"))
1162 #define E_RANGE_ERROR               (mrb_exc_get(mrb, "RangeError"))
1163 #define E_NAME_ERROR                (mrb_exc_get(mrb, "NameError"))
1164 #define E_NOMETHOD_ERROR            (mrb_exc_get(mrb, "NoMethodError"))
1165 #define E_SCRIPT_ERROR              (mrb_exc_get(mrb, "ScriptError"))
1166 #define E_SYNTAX_ERROR              (mrb_exc_get(mrb, "SyntaxError"))
1167 #define E_LOCALJUMP_ERROR           (mrb_exc_get(mrb, "LocalJumpError"))
1168 #define E_REGEXP_ERROR              (mrb_exc_get(mrb, "RegexpError"))
1169 #define E_FROZEN_ERROR              (mrb_exc_get(mrb, "FrozenError"))
1170 
1171 #define E_NOTIMP_ERROR              (mrb_exc_get(mrb, "NotImplementedError"))
1172 #ifndef MRB_WITHOUT_FLOAT
1173 #define E_FLOATDOMAIN_ERROR         (mrb_exc_get(mrb, "FloatDomainError"))
1174 #endif
1175 
1176 #define E_KEY_ERROR                 (mrb_exc_get(mrb, "KeyError"))
1177 
1178 MRB_API mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg);
1179 MRB_API mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv);
1180 MRB_API mrb_value mrb_yield_with_class(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv, mrb_value self, struct RClass *c);
1181 
1182 /* continue execution to the proc */
1183 /* this function should always be called as the last function of a method */
1184 /* e.g. return mrb_yield_cont(mrb, proc, self, argc, argv); */
1185 mrb_value mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const mrb_value *argv);
1186 
1187 /* mrb_gc_protect() leaves the object in the arena */
1188 MRB_API void mrb_gc_protect(mrb_state *mrb, mrb_value obj);
1189 /* mrb_gc_register() keeps the object from GC. */
1190 MRB_API void mrb_gc_register(mrb_state *mrb, mrb_value obj);
1191 /* mrb_gc_unregister() removes the object from GC root. */
1192 MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj);
1193 
1194 MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
1195 #define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val))
1196 MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val);
1197 MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
1198 
1199 typedef enum call_type {
1200   CALL_PUBLIC,
1201   CALL_FCALL,
1202   CALL_VCALL,
1203   CALL_TYPE_MAX
1204 } call_type;
1205 
1206 MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b);
1207 MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass);
1208 MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val);
1209 
1210 MRB_API mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id);
1211 
1212 MRB_API mrb_bool mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid);
1213 MRB_API mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c);
1214 MRB_API mrb_bool mrb_func_basic_p(mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func);
1215 
1216 
1217 /*
1218  * Resume a Fiber
1219  *
1220  * @mrbgem mruby-fiber
1221  */
1222 MRB_API mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv);
1223 
1224 /*
1225  * Yield a Fiber
1226  *
1227  * @mrbgem mruby-fiber
1228  */
1229 MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv);
1230 
1231 /*
1232  * Check if a Fiber is alive
1233  *
1234  * @mrbgem mruby-fiber
1235  */
1236 MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib);
1237 
1238 /*
1239  * FiberError reference
1240  *
1241  * @mrbgem mruby-fiber
1242  */
1243 #define E_FIBER_ERROR (mrb_exc_get(mrb, "FiberError"))
1244 MRB_API void mrb_stack_extend(mrb_state*, mrb_int);
1245 
1246 /* memory pool implementation */
1247 typedef struct mrb_pool mrb_pool;
1248 MRB_API struct mrb_pool* mrb_pool_open(mrb_state*);
1249 MRB_API void mrb_pool_close(struct mrb_pool*);
1250 MRB_API void* mrb_pool_alloc(struct mrb_pool*, size_t);
1251 MRB_API void* mrb_pool_realloc(struct mrb_pool*, void*, size_t oldlen, size_t newlen);
1252 MRB_API mrb_bool mrb_pool_can_realloc(struct mrb_pool*, void*, size_t);
1253 MRB_API void* mrb_alloca(mrb_state *mrb, size_t);
1254 
1255 MRB_API void mrb_state_atexit(mrb_state *mrb, mrb_atexit_func func);
1256 
1257 MRB_API void mrb_show_version(mrb_state *mrb);
1258 MRB_API void mrb_show_copyright(mrb_state *mrb);
1259 
1260 MRB_API mrb_value mrb_format(mrb_state *mrb, const char *format, ...);
1261 
1262 #if 0
1263 /* memcpy and memset does not work with gdb reverse-next on my box */
1264 /* use naive memcpy and memset instead */
1265 #undef memcpy
1266 #undef memset
1267 static void*
1268 mrbmemcpy(void *dst, const void *src, size_t n)
1269 {
1270   char *d = (char*)dst;
1271   const char *s = (const char*)src;
1272   while (n--)
1273     *d++ = *s++;
1274   return d;
1275 }
1276 #define memcpy(a,b,c) mrbmemcpy(a,b,c)
1277 
1278 static void*
1279 mrbmemset(void *s, int c, size_t n)
1280 {
1281   char *t = (char*)s;
1282   while (n--)
1283     *t++ = c;
1284   return s;
1285 }
1286 #define memset(a,b,c) mrbmemset(a,b,c)
1287 #endif
1288 
1289 MRB_END_DECL
1290 
1291 #endif  /* MRUBY_H */
1292