source: EcnlProtoTool/trunk/mruby-1.3.0/include/mruby.h@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

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