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