Coverage Report

Created: 2026-02-14 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mruby/include/mruby.h
Line
Count
Source
1
/*
2
** mruby - An embeddable Ruby implementation
3
**
4
** Copyright (c) mruby developers 2010-
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: https://www.opensource.org/licenses/mit-license.php ]
26
*/
27
28
/**
29
 * @file mruby.h
30
 */
31
32
#ifndef MRUBY_H
33
#define MRUBY_H
34
35
#ifdef __cplusplus
36
#define __STDC_LIMIT_MACROS
37
#define __STDC_CONSTANT_MACROS
38
#define __STDC_FORMAT_MACROS
39
#endif
40
41
#include <stdarg.h>
42
#include <stdint.h>
43
#include <stddef.h>
44
#include <limits.h>
45
46
#ifdef __cplusplus
47
#ifndef UINTPTR_MAX
48
#error Must be placed `#include <mruby.h>` before `#include <stdint.h>`
49
#endif
50
#ifndef SIZE_MAX
51
#ifdef __SIZE_MAX__
52
#define SIZE_MAX __SIZE_MAX__
53
#else
54
#define SIZE_MAX std::numeric_limits<size_t>::max()
55
#endif
56
#endif
57
#endif
58
59
#ifdef _MSC_VER
60
# define __func__ __FUNCTION__
61
#endif
62
63
#ifdef MRB_DEBUG
64
#include <assert.h>
65
#define mrb_assert(p) assert(p)
66
#define mrb_assert_int_fit(t1,n,t2,max) assert((n)>=0 && ((sizeof(n)<=sizeof(t2))||(n<=(t1)(max))))
67
#else
68
223M
#define mrb_assert(p) ((void)0)
69
85.9k
#define mrb_assert_int_fit(t1,n,t2,max) ((void)0)
70
#endif
71
72
#if (defined __cplusplus && __cplusplus >= 201703L)
73
# define mrb_static_assert(...) static_assert(__VA_ARGS__)
74
# define mrb_static_assert1(exp) static_assert(exp)
75
# define mrb_static_assert2(exp, str) static_assert(exp, str)
76
#elif (defined __cplusplus && __cplusplus >= 201103L) || \
77
    (defined _MSC_VER) || \
78
    (defined __GXX_EXPERIMENTAL_CXX0X__)  /* for old G++/Clang++ */
79
# define mrb_static_assert2(exp, str) static_assert(exp, str)
80
#elif defined __STDC_VERSION__ && \
81
        ((__STDC_VERSION__ >= 201112L) || \
82
         (defined __GNUC__ && __GNUC__ * 100 + __GNUC_MINOR__ >= 406))
83
# define mrb_static_assert2(exp, str) _Static_assert(exp, str)
84
#else
85
# /* alternative implementation of static_assert() */
86
# define _mrb_static_assert_cat0(a, b) a##b
87
# define _mrb_static_assert_cat(a, b) _mrb_static_assert_cat0(a, b)
88
# ifdef __COUNTER__
89
#  define _mrb_static_assert_id(prefix) _mrb_static_assert_cat(prefix, __COUNTER__)
90
# else
91
#  define _mrb_static_assert_id(prefix) _mrb_static_assert_cat(prefix, __LINE__)
92
# endif
93
# define mrb_static_assert2(exp, str) \
94
   struct _mrb_static_assert_id(_mrb_static_assert_) { char x[(exp) ? 1 : -1]; }
95
#endif
96
97
#ifndef mrb_static_assert
98
# define mrb_static_assert1(exp) mrb_static_assert2(exp, #exp)
99
39.3k
# define mrb_static_assert_expand(...) __VA_ARGS__ /* for MSVC behaviour - https://stackoverflow.com/q/5530505 */
100
# define mrb_static_assert_selector(a, b, name, ...) name
101
/**
102
 * The `mrb_static_assert()` macro function takes one or two arguments.
103
 *
104
 *      !!!c
105
 *      mrb_static_assert(expect_condition);
106
 *      mrb_static_assert(expect_condition, error_message);
107
 */
108
# define mrb_static_assert(...) \
109
39.3k
    mrb_static_assert_expand(mrb_static_assert_selector(__VA_ARGS__, mrb_static_assert2, mrb_static_assert1, _)(__VA_ARGS__))
110
#endif
111
112
#define mrb_static_assert_powerof2(num) mrb_static_assert((num) > 0 && (num) == ((num) & -(num)), "need power of 2 for " #num)
113
114
#include "mrbconf.h"
115
116
#include <mruby/common.h>
117
#include <mruby/value.h>
118
#include <mruby/gc.h>
119
#include <mruby/version.h>
120
121
#ifndef MRB_NO_FLOAT
122
#include <math.h>
123
#include <float.h>
124
#ifndef FLT_EPSILON
125
#define FLT_EPSILON (1.19209290e-07f)
126
#endif
127
#ifndef DBL_EPSILON
128
#define DBL_EPSILON ((double)2.22044604925031308085e-16L)
129
#endif
130
#ifndef LDBL_EPSILON
131
#define LDBL_EPSILON (1.08420217248550443401e-19L)
132
#endif
133
134
#ifdef MRB_USE_FLOAT32
135
#define MRB_FLOAT_EPSILON FLT_EPSILON
136
#else
137
0
#define MRB_FLOAT_EPSILON DBL_EPSILON
138
#endif
139
#endif
140
141
/**
142
 * mruby C API entry point
143
 */
144
MRB_BEGIN_DECL
145
146
typedef uint8_t mrb_code;
147
148
/**
149
 * \class mrb_aspec
150
 *
151
 * Specifies the number of arguments a function takes
152
 *
153
 * Example: `MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1)` for a method that expects 2..3 arguments
154
 */
155
typedef uint32_t mrb_aspec;
156
157
typedef struct mrb_irep mrb_irep;
158
159
struct mrb_state;
160
161
#ifndef MRB_FIXED_STATE_ATEXIT_STACK_SIZE
162
#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5
163
#endif
164
165
typedef struct {
166
  uint8_t n:4;                  /* (15=*) c=n|nk<<4 */
167
  uint8_t nk:4;                 /* (15=*) */
168
  uint8_t cci;                  /* called from C function */
169
  uint8_t vis;                  /* 5(ZERO):1(separate module):2(method visibility) */
170
                                /* under 3-bit flags are copied to env, and after that, env takes precedence */
171
  mrb_sym mid;
172
  const struct RProc *proc;
173
  struct RProc *blk;
174
  mrb_value *stack;
175
  const mrb_code *pc;           /* current address on iseq of this proc */
176
  union {
177
    struct REnv *env;
178
    struct RClass *target_class;
179
    const void *keep_context;   /* if NULL, it means that the fiber has switched; for internal use */
180
  } u;
181
} mrb_callinfo;
182
183
enum mrb_fiber_state {
184
  MRB_FIBER_CREATED = 0,
185
  MRB_FIBER_RUNNING,
186
  MRB_FIBER_RESUMED,
187
  MRB_FIBER_SUSPENDED,
188
  MRB_FIBER_TRANSFERRED,
189
  MRB_FIBER_TERMINATED,
190
};
191
192
/* Task context status aliases */
193
#define MRB_TASK_CREATED MRB_FIBER_CREATED
194
#define MRB_TASK_STOPPED MRB_FIBER_TERMINATED
195
196
struct mrb_context {
197
  struct mrb_context *prev;
198
199
  mrb_value *stbase, *stend;              /* stack of virtual machine */
200
201
  mrb_callinfo *ci;
202
  mrb_callinfo *cibase, *ciend;
203
204
  enum mrb_fiber_state status : 4;
205
  mrb_bool vmexec : 1;
206
  struct RFiber *fib;
207
};
208
209
#ifdef MRB_METHOD_CACHE_SIZE
210
# undef MRB_NO_METHOD_CACHE
211
mrb_static_assert_powerof2(MRB_METHOD_CACHE_SIZE);
212
#else
213
/* default method cache size: 256 */
214
/* cache size needs to be power of 2 */
215
3.47G
# define MRB_METHOD_CACHE_SIZE (1<<8)
216
#endif
217
218
/**
219
 * Function pointer type for a function callable by mruby.
220
 *
221
 * The arguments to the function are stored on the mrb_state. To get them see mrb_get_args
222
 *
223
 * @param mrb The mruby state
224
 * @param self The self object
225
 * @return [mrb_value] The function's return value
226
 */
227
typedef mrb_value (*mrb_func_t)(struct mrb_state *mrb, mrb_value self);
228
229
typedef struct {
230
  uint32_t flags;                       /* compatible with mt keys in class.c */
231
232
  union {
233
    const struct RProc *proc;
234
    mrb_func_t func;
235
  } as;
236
} mrb_method_t;
237
238
#ifndef MRB_NO_METHOD_CACHE
239
struct mrb_cache_entry {
240
  struct RClass *c, *c0;
241
  /* mrb_sym mid; // mid is stored in mrb_method_t::flags */
242
  mrb_method_t m;
243
};
244
#endif
245
246
struct mrb_jmpbuf;
247
248
typedef void (*mrb_atexit_func)(struct mrb_state*);
249
250
#ifdef MRB_USE_TASK_SCHEDULER
251
struct mrb_task;
252
253
typedef struct mrb_task_state {
254
  struct mrb_task *queues[4];      /* Task queues (dormant, ready, waiting, suspended) */
255
  volatile uint32_t tick;           /* Current tick count */
256
  volatile uint32_t wakeup_tick;    /* Next wakeup tick */
257
  volatile mrb_bool switching;      /* Context switch pending flag */
258
  struct mrb_task *main_task;       /* Main task wrapper for root context */
259
  uint8_t scheduler_lock;           /* Lock counter for synchronous execution */
260
} mrb_task_state;
261
#endif
262
263
typedef struct mrb_state {
264
  struct mrb_jmpbuf *jmp;
265
266
  struct mrb_context *c;
267
  struct mrb_context *root_c;
268
  struct iv_tbl *globals;                 /* global variable table */
269
270
  struct RObject *exc;                    /* exception */
271
272
  struct RObject *top_self;
273
  struct RClass *object_class;            /* Object class */
274
  struct RClass *class_class;
275
  struct RClass *module_class;
276
  struct RClass *proc_class;
277
  struct RClass *string_class;
278
  struct RClass *array_class;
279
  struct RClass *hash_class;
280
  struct RClass *range_class;
281
282
#ifndef MRB_NO_FLOAT
283
  struct RClass *float_class;
284
#endif
285
  struct RClass *integer_class;
286
  struct RClass *true_class;
287
  struct RClass *false_class;
288
  struct RClass *nil_class;
289
  struct RClass *symbol_class;
290
  struct RClass *kernel_module;
291
292
  mrb_gc gc;
293
294
  mrb_bool bootstrapping;
295
296
#ifndef MRB_NO_METHOD_CACHE
297
  struct mrb_cache_entry cache[MRB_METHOD_CACHE_SIZE];
298
#endif
299
300
  mrb_sym symidx;
301
  const char **symtbl;
302
  size_t symcapa;
303
  struct mrb_sym_hash_table *symhash;
304
  void *sym_pool;
305
#ifndef MRB_USE_ALL_SYMBOLS
306
  char symbuf[8];                         /* buffer for small symbol names */
307
#endif
308
309
#ifdef MRB_USE_DEBUG_HOOK
310
  void (*code_fetch_hook)(struct mrb_state* mrb, const struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
311
  void (*debug_op_hook)(struct mrb_state* mrb, const struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
312
#endif
313
314
#ifdef MRB_BYTECODE_DECODE_OPTION
315
  mrb_code (*bytecode_decoder)(struct mrb_state* mrb, mrb_code code);
316
#endif
317
318
  struct RClass *eException_class;
319
  struct RClass *eStandardError_class;
320
  struct RObject *nomem_err;              /* pre-allocated NoMemoryError */
321
  struct RObject *stack_err;              /* pre-allocated SystemStackError */
322
#ifdef MRB_GC_FIXED_ARENA
323
  struct RObject *arena_err;              /* pre-allocated arena overflow error */
324
#endif
325
326
  void *ud; /* auxiliary data */
327
328
#ifdef MRB_FIXED_STATE_ATEXIT_STACK
329
  mrb_atexit_func atexit_stack[MRB_FIXED_STATE_ATEXIT_STACK_SIZE];
330
#else
331
  mrb_atexit_func *atexit_stack;
332
#endif
333
  uint16_t atexit_stack_len;
334
335
#ifdef MRB_USE_TASK_SCHEDULER
336
  mrb_task_state task;                    /* Task scheduler state */
337
#endif
338
} mrb_state;
339
340
/**
341
 * Defines a new class.
342
 *
343
 * If you're creating a gem it may look something like this:
344
 *
345
 *      !!!c
346
 *      void mrb_example_gem_init(mrb_state* mrb) {
347
 *          struct RClass *example_class;
348
 *          example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
349
 *      }
350
 *
351
 *      void mrb_example_gem_final(mrb_state* mrb) {
352
 *          //free(TheAnimals);
353
 *      }
354
 *
355
 * @param mrb The current mruby state.
356
 * @param name The name of the defined class.
357
 * @param super The new class parent.
358
 * @return [struct RClass *] Reference to the newly defined class.
359
 * @see mrb_define_class_under
360
 */
361
MRB_API struct RClass *mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super);
362
MRB_API struct RClass *mrb_define_class_id(mrb_state *mrb, mrb_sym name, struct RClass *super);
363
364
/**
365
 * Defines a new module.
366
 *
367
 * @param mrb The current mruby state.
368
 * @param name The name of the module.
369
 * @return [struct RClass *] Reference to the newly defined module.
370
 */
371
MRB_API struct RClass *mrb_define_module(mrb_state *mrb, const char *name);
372
MRB_API struct RClass *mrb_define_module_id(mrb_state *mrb, mrb_sym name);
373
374
/**
375
 * Returns the singleton class of an object.
376
 *
377
 * Raises a `TypeError` exception for immediate values.
378
 */
379
MRB_API mrb_value mrb_singleton_class(mrb_state *mrb, mrb_value val);
380
381
/**
382
 * Returns the singleton class of an object.
383
 *
384
 * Returns `NULL` for immediate values,
385
 */
386
MRB_API struct RClass *mrb_singleton_class_ptr(mrb_state *mrb, mrb_value val);
387
388
/**
389
 * Include a module in another class or module.
390
 * Equivalent to:
391
 *
392
 *   module B
393
 *     include A
394
 *   end
395
 * @param mrb The current mruby state.
396
 * @param cla A reference to module or a class.
397
 * @param included A reference to the module to be included.
398
 */
399
MRB_API void mrb_include_module(mrb_state *mrb, struct RClass *cla, struct RClass *included);
400
401
/**
402
 * Prepends a module in another class or module.
403
 *
404
 * Equivalent to:
405
 *  module B
406
 *    prepend A
407
 *  end
408
 * @param mrb The current mruby state.
409
 * @param cla A reference to module or a class.
410
 * @param prepended A reference to the module to be prepended.
411
 */
412
MRB_API void mrb_prepend_module(mrb_state *mrb, struct RClass *cla, struct RClass *prepended);
413
414
/**
415
 * Defines a global function in Ruby.
416
 *
417
 * If you're creating a gem it may look something like this
418
 *
419
 * Example:
420
 *
421
 *     mrb_value example_method(mrb_state* mrb, mrb_value self)
422
 *     {
423
 *          puts("Executing example command!");
424
 *          return self;
425
 *     }
426
 *
427
 *     void mrb_example_gem_init(mrb_state* mrb)
428
 *     {
429
 *           mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
430
 *     }
431
 *
432
 * @param mrb The mruby state reference.
433
 * @param cla The class pointer where the method will be defined.
434
 * @param name The name of the method being defined.
435
 * @param func The function pointer to the method definition.
436
 * @param aspec The method parameters declaration.
437
 */
438
439
MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
440
MRB_API void mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, mrb_aspec aspec);
441
MRB_API void mrb_define_private_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
442
MRB_API void mrb_define_private_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, mrb_aspec aspec);
443
444
/**
445
 * Defines a class method.
446
 *
447
 * Example:
448
 *
449
 *     # Ruby style
450
 *     class Foo
451
 *       def Foo.bar
452
 *       end
453
 *     end
454
 *     // C style
455
 *     mrb_value bar_method(mrb_state* mrb, mrb_value self){
456
 *       return mrb_nil_value();
457
 *     }
458
 *     void mrb_example_gem_init(mrb_state* mrb){
459
 *       struct RClass *foo;
460
 *       foo = mrb_define_class(mrb, "Foo", mrb->object_class);
461
 *       mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
462
 *     }
463
 * @param mrb The mruby state reference.
464
 * @param cla The class where the class method will be defined.
465
 * @param name The name of the class method being defined.
466
 * @param fun The function pointer to the class method definition.
467
 * @param aspec The method parameters declaration.
468
 */
469
MRB_API void mrb_define_class_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
470
MRB_API void mrb_define_class_method_id(mrb_state *mrb, struct RClass *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec);
471
472
/**
473
 * Defines a singleton method
474
 *
475
 * @see mrb_define_class_method
476
 */
477
MRB_API void mrb_define_singleton_method(mrb_state *mrb, struct RObject *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
478
MRB_API void mrb_define_singleton_method_id(mrb_state *mrb, struct RObject *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec);
479
480
/**
481
 *  Defines a module function.
482
 *
483
 * Example:
484
 *
485
 *        # Ruby style
486
 *        module Foo
487
 *          def Foo.bar
488
 *          end
489
 *        end
490
 *        // C style
491
 *        mrb_value bar_method(mrb_state* mrb, mrb_value self){
492
 *          return mrb_nil_value();
493
 *        }
494
 *        void mrb_example_gem_init(mrb_state* mrb){
495
 *          struct RClass *foo;
496
 *          foo = mrb_define_module(mrb, "Foo");
497
 *          mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
498
 *        }
499
 *  @param mrb The mruby state reference.
500
 *  @param cla The module where the module function will be defined.
501
 *  @param name The name of the module function being defined.
502
 *  @param fun The function pointer to the module function definition.
503
 *  @param aspec The method parameters declaration.
504
 */
505
MRB_API void mrb_define_module_function(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
506
MRB_API void mrb_define_module_function_id(mrb_state *mrb, struct RClass *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec);
507
508
/**
509
 *  Defines a constant.
510
 *
511
 * Example:
512
 *
513
 *          # Ruby style
514
 *          class ExampleClass
515
 *            AGE = 22
516
 *          end
517
 *          // C style
518
 *          #include <stdio.h>
519
 *          #include <mruby.h>
520
 *
521
 *          void
522
 *          mrb_example_gem_init(mrb_state* mrb){
523
 *            mrb_define_const(mrb, mrb->kernel_module, "AGE", mrb_fixnum_value(22));
524
 *          }
525
 *
526
 *          mrb_value
527
 *          mrb_example_gem_final(mrb_state* mrb){
528
 *          }
529
 *  @param mrb The mruby state reference.
530
 *  @param cla A class or module the constant is defined in.
531
 *  @param name The name of the constant being defined.
532
 *  @param val The value for the constant.
533
 */
534
MRB_API void mrb_define_const(mrb_state* mrb, struct RClass* cla, const char *name, mrb_value val);
535
MRB_API void mrb_define_const_id(mrb_state* mrb, struct RClass* cla, mrb_sym name, mrb_value val);
536
537
/**
538
 * Undefines a method.
539
 *
540
 * Example:
541
 *
542
 *     # Ruby style
543
 *
544
 *     class ExampleClassA
545
 *       def example_method
546
 *         "example"
547
 *       end
548
 *     end
549
 *     ExampleClassA.new.example_method # => example
550
 *
551
 *     class ExampleClassB < ExampleClassA
552
 *       undef_method :example_method
553
 *     end
554
 *
555
 *     ExampleClassB.new.example_method # => undefined method 'example_method' for ExampleClassB (NoMethodError)
556
 *
557
 *     // C style
558
 *     #include <stdio.h>
559
 *     #include <mruby.h>
560
 *
561
 *     mrb_value
562
 *     mrb_example_method(mrb_state *mrb){
563
 *       return mrb_str_new_lit(mrb, "example");
564
 *     }
565
 *
566
 *     void
567
 *     mrb_example_gem_init(mrb_state* mrb){
568
 *       struct RClass *example_class_a;
569
 *       struct RClass *example_class_b;
570
 *       struct RClass *example_class_c;
571
 *
572
 *       example_class_a = mrb_define_class(mrb, "ExampleClassA", mrb->object_class);
573
 *       mrb_define_method(mrb, example_class_a, "example_method", mrb_example_method, MRB_ARGS_NONE());
574
 *       example_class_b = mrb_define_class(mrb, "ExampleClassB", example_class_a);
575
 *       example_class_c = mrb_define_class(mrb, "ExampleClassC", example_class_b);
576
 *       mrb_undef_method(mrb, example_class_c, "example_method");
577
 *     }
578
 *
579
 *     mrb_example_gem_final(mrb_state* mrb){
580
 *     }
581
 * @param mrb The mruby state reference.
582
 * @param cla The class the method will be undefined from.
583
 * @param name The name of the method to be undefined.
584
 */
585
MRB_API void mrb_undef_method(mrb_state *mrb, struct RClass *cla, const char *name);
586
MRB_API void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym);
587
588
/**
589
 * Undefine a class method.
590
 * Example:
591
 *
592
 *      # Ruby style
593
 *      class ExampleClass
594
 *        def self.example_method
595
 *          "example"
596
 *        end
597
 *      end
598
 *
599
 *     ExampleClass.example_method
600
 *
601
 *     // C style
602
 *     #include <stdio.h>
603
 *     #include <mruby.h>
604
 *
605
 *     mrb_value
606
 *     mrb_example_method(mrb_state *mrb){
607
 *       return mrb_str_new_lit(mrb, "example");
608
 *     }
609
 *
610
 *     void
611
 *     mrb_example_gem_init(mrb_state* mrb){
612
 *       struct RClass *example_class;
613
 *       example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
614
 *       mrb_define_class_method(mrb, example_class, "example_method", mrb_example_method, MRB_ARGS_NONE());
615
 *       mrb_undef_class_method(mrb, example_class, "example_method");
616
 *      }
617
 *
618
 *      void
619
 *      mrb_example_gem_final(mrb_state* mrb){
620
 *      }
621
 * @param mrb The mruby state reference.
622
 * @param cls A class the class method will be undefined from.
623
 * @param name The name of the class method to be undefined.
624
 */
625
MRB_API void mrb_undef_class_method(mrb_state *mrb, struct RClass *cls, const char *name);
626
MRB_API void mrb_undef_class_method_id(mrb_state *mrb, struct RClass *cls, mrb_sym name);
627
628
/**
629
 * Initialize a new object instance of c class.
630
 *
631
 * Example:
632
 *
633
 *     # Ruby style
634
 *     class ExampleClass
635
 *     end
636
 *
637
 *     p ExampleClass # => #<ExampleClass:0x9958588>
638
 *     // C style
639
 *     #include <stdio.h>
640
 *     #include <mruby.h>
641
 *
642
 *     void
643
 *     mrb_example_gem_init(mrb_state* mrb) {
644
 *       struct RClass *example_class;
645
 *       mrb_value obj;
646
 *       example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); # => class ExampleClass; end
647
 *       obj = mrb_obj_new(mrb, example_class, 0, NULL); # => ExampleClass.new
648
 *       mrb_p(mrb, obj); // => Kernel#p
649
 *      }
650
 * @param mrb The current mruby state.
651
 * @param c Reference to the class of the new object.
652
 * @param argc Number of arguments in argv
653
 * @param argv Array of mrb_value to initialize the object
654
 * @return [mrb_value] The newly initialized object
655
 */
656
MRB_API mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *argv);
657
658
/** @see mrb_obj_new */
659
MRB_INLINE mrb_value mrb_class_new_instance(mrb_state *mrb, mrb_int argc, const mrb_value *argv, struct RClass *c)
660
0
{
661
0
  return mrb_obj_new(mrb,c,argc,argv);
662
0
}
Unexecuted instantiation: mruby_fuzzer.c:mrb_class_new_instance
Unexecuted instantiation: state.c:mrb_class_new_instance
Unexecuted instantiation: debug.c:mrb_class_new_instance
Unexecuted instantiation: class.c:mrb_class_new_instance
Unexecuted instantiation: init.c:mrb_class_new_instance
Unexecuted instantiation: etc.c:mrb_class_new_instance
Unexecuted instantiation: array.c:mrb_class_new_instance
Unexecuted instantiation: version.c:mrb_class_new_instance
Unexecuted instantiation: y.tab.c:mrb_class_new_instance
Unexecuted instantiation: rational.c:mrb_class_new_instance
Unexecuted instantiation: complex.c:mrb_class_new_instance
Unexecuted instantiation: mrblib.c:mrb_class_new_instance
Unexecuted instantiation: gem_init.c:mrb_class_new_instance
Unexecuted instantiation: bigint.c:mrb_class_new_instance
Unexecuted instantiation: mempool.c:mrb_class_new_instance
Unexecuted instantiation: variable.c:mrb_class_new_instance
Unexecuted instantiation: allocf.c:mrb_class_new_instance
Unexecuted instantiation: symbol.c:mrb_class_new_instance
Unexecuted instantiation: object.c:mrb_class_new_instance
Unexecuted instantiation: numeric.c:mrb_class_new_instance
Unexecuted instantiation: gc.c:mrb_class_new_instance
Unexecuted instantiation: error.c:mrb_class_new_instance
Unexecuted instantiation: load.c:mrb_class_new_instance
Unexecuted instantiation: enum.c:mrb_class_new_instance
Unexecuted instantiation: vm.c:mrb_class_new_instance
Unexecuted instantiation: string.c:mrb_class_new_instance
Unexecuted instantiation: proc.c:mrb_class_new_instance
Unexecuted instantiation: readfloat.c:mrb_class_new_instance
Unexecuted instantiation: hash.c:mrb_class_new_instance
Unexecuted instantiation: codedump.c:mrb_class_new_instance
Unexecuted instantiation: range.c:mrb_class_new_instance
Unexecuted instantiation: kernel.c:mrb_class_new_instance
Unexecuted instantiation: readint.c:mrb_class_new_instance
Unexecuted instantiation: backtrace.c:mrb_class_new_instance
Unexecuted instantiation: codegen.c:mrb_class_new_instance
Unexecuted instantiation: set.c:mrb_class_new_instance
Unexecuted instantiation: fmt_fp.c:mrb_class_new_instance
Unexecuted instantiation: numeric_ext.c:mrb_class_new_instance
Unexecuted instantiation: hash_ext.c:mrb_class_new_instance
Unexecuted instantiation: mruby_objectspace.c:mrb_class_new_instance
Unexecuted instantiation: fiber.c:mrb_class_new_instance
Unexecuted instantiation: catch.c:mrb_class_new_instance
Unexecuted instantiation: pack.c:mrb_class_new_instance
Unexecuted instantiation: sprintf.c:mrb_class_new_instance
Unexecuted instantiation: time.c:mrb_class_new_instance
Unexecuted instantiation: struct.c:mrb_class_new_instance
Unexecuted instantiation: data.c:mrb_class_new_instance
Unexecuted instantiation: random.c:mrb_class_new_instance
Unexecuted instantiation: mruby_io_gem.c:mrb_class_new_instance
Unexecuted instantiation: socket.c:mrb_class_new_instance
Unexecuted instantiation: errno.c:mrb_class_new_instance
Unexecuted instantiation: dir.c:mrb_class_new_instance
Unexecuted instantiation: math.c:mrb_class_new_instance
Unexecuted instantiation: metaprog.c:mrb_class_new_instance
Unexecuted instantiation: method.c:mrb_class_new_instance
Unexecuted instantiation: eval.c:mrb_class_new_instance
Unexecuted instantiation: binding.c:mrb_class_new_instance
Unexecuted instantiation: proc_binding.c:mrb_class_new_instance
Unexecuted instantiation: io_hal.c:mrb_class_new_instance
Unexecuted instantiation: socket_hal.c:mrb_class_new_instance
Unexecuted instantiation: dir_hal.c:mrb_class_new_instance
Unexecuted instantiation: exception.c:mrb_class_new_instance
Unexecuted instantiation: file.c:mrb_class_new_instance
Unexecuted instantiation: file_test.c:mrb_class_new_instance
Unexecuted instantiation: io.c:mrb_class_new_instance
Unexecuted instantiation: mruby_proto_fuzzer.cpp:mrb_class_new_instance(mrb_state*, long, mrb_value const*, RClass*)
663
664
/**
665
 * Creates a new instance of Class, Class.
666
 *
667
 * Example:
668
 *
669
 *      void
670
 *      mrb_example_gem_init(mrb_state* mrb) {
671
 *        struct RClass *example_class;
672
 *
673
 *        mrb_value obj;
674
 *        example_class = mrb_class_new(mrb, mrb->object_class);
675
 *        obj = mrb_obj_new(mrb, example_class, 0, NULL); // => #<#<Class:0x9a945b8>:0x9a94588>
676
 *        mrb_p(mrb, obj); // => Kernel#p
677
 *       }
678
 *
679
 * @param mrb The current mruby state.
680
 * @param super The super class or parent.
681
 * @return [struct RClass *] Reference to the new class.
682
 */
683
MRB_API struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super);
684
685
/**
686
 * Creates a new module, Module.
687
 *
688
 * Example:
689
 *      void
690
 *      mrb_example_gem_init(mrb_state* mrb) {
691
 *        struct RClass *example_module;
692
 *
693
 *        example_module = mrb_module_new(mrb);
694
 *      }
695
 *
696
 * @param mrb The current mruby state.
697
 * @return [struct RClass *] Reference to the new module.
698
 */
699
MRB_API struct RClass * mrb_module_new(mrb_state *mrb);
700
701
/**
702
 * Returns an mrb_bool. True if class was defined, and false if the class was not defined.
703
 *
704
 * Example:
705
 *     void
706
 *     mrb_example_gem_init(mrb_state* mrb) {
707
 *       struct RClass *example_class;
708
 *       mrb_bool cd;
709
 *
710
 *       example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
711
 *       cd = mrb_class_defined(mrb, "ExampleClass");
712
 *
713
 *       // If mrb_class_defined returns TRUE then puts "True"
714
 *       // If mrb_class_defined returns FALSE then puts "False"
715
 *       if (cd) {
716
 *         puts("True");
717
 *       }
718
 *       else {
719
 *         puts("False");
720
 *       }
721
 *      }
722
 *
723
 * @param mrb The current mruby state.
724
 * @param name A string representing the name of the class.
725
 * @return [mrb_bool] A boolean value.
726
 */
727
MRB_API mrb_bool mrb_class_defined(mrb_state *mrb, const char *name);
728
MRB_API mrb_bool mrb_class_defined_id(mrb_state *mrb, mrb_sym name);
729
730
/**
731
 * Gets a class.
732
 * @param mrb The current mruby state.
733
 * @param name The name of the class.
734
 * @return [struct RClass *] A reference to the class.
735
*/
736
MRB_API struct RClass* mrb_class_get(mrb_state *mrb, const char *name);
737
MRB_API struct RClass* mrb_class_get_id(mrb_state *mrb, mrb_sym name);
738
739
/**
740
 * Gets a exception class.
741
 * @param mrb The current mruby state.
742
 * @param name The name of the class.
743
 * @return [struct RClass *] A reference to the class.
744
*/
745
MRB_API struct RClass* mrb_exc_get_id(mrb_state *mrb, mrb_sym name);
746
#define mrb_exc_get(mrb, name) mrb_exc_get_id(mrb, mrb_intern_cstr(mrb, name))
747
748
/**
749
 * Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined.
750
 *
751
 * Example:
752
 *     void
753
 *     mrb_example_gem_init(mrb_state* mrb) {
754
 *       struct RClass *example_outer, *example_inner;
755
 *       mrb_bool cd;
756
 *
757
 *       example_outer = mrb_define_module(mrb, "ExampleOuter");
758
 *
759
 *       example_inner = mrb_define_class_under(mrb, example_outer, "ExampleInner", mrb->object_class);
760
 *       cd = mrb_class_defined_under(mrb, example_outer, "ExampleInner");
761
 *
762
 *       // If mrb_class_defined_under returns TRUE then puts "True"
763
 *       // If mrb_class_defined_under returns FALSE then puts "False"
764
 *       if (cd) {
765
 *         puts("True");
766
 *       }
767
 *       else {
768
 *         puts("False");
769
 *       }
770
 *      }
771
 *
772
 * @param mrb The current mruby state.
773
 * @param outer The name of the outer class.
774
 * @param name A string representing the name of the inner class.
775
 * @return [mrb_bool] A boolean value.
776
 */
777
MRB_API mrb_bool mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name);
778
MRB_API mrb_bool mrb_class_defined_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
779
780
/**
781
 * Gets a child class.
782
 * @param mrb The current mruby state.
783
 * @param outer The name of the parent class.
784
 * @param name The name of the class.
785
 * @return [struct RClass *] A reference to the class.
786
*/
787
MRB_API struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
788
MRB_API struct RClass * mrb_class_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
789
790
/**
791
 * Gets a module.
792
 * @param mrb The current mruby state.
793
 * @param name The name of the module.
794
 * @return [struct RClass *] A reference to the module.
795
*/
796
MRB_API struct RClass * mrb_module_get(mrb_state *mrb, const char *name);
797
MRB_API struct RClass * mrb_module_get_id(mrb_state *mrb, mrb_sym name);
798
799
/**
800
 * Gets a module defined under another module.
801
 * @param mrb The current mruby state.
802
 * @param outer The name of the outer module.
803
 * @param name The name of the module.
804
 * @return [struct RClass *] A reference to the module.
805
*/
806
MRB_API struct RClass * mrb_module_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
807
MRB_API struct RClass * mrb_module_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
808
809
/* a function to raise NotImplementedError with current method name */
810
MRB_API void mrb_notimplement(mrb_state*);
811
/* a function to be replacement of unimplemented method */
812
MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value);
813
/* just return it self */
814
MRB_API mrb_value mrb_obj_itself(mrb_state*, mrb_value);
815
816
/**
817
 * Duplicate an object.
818
 *
819
 * Equivalent to:
820
 *   Object#dup
821
 * @param mrb The current mruby state.
822
 * @param obj Object to be duplicate.
823
 * @return [mrb_value] The newly duplicated object.
824
 */
825
MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
826
827
/**
828
 * Returns true if obj responds to the given method. If the method was defined for that
829
 * class it returns true, it returns false otherwise.
830
 *
831
 *      Example:
832
 *      # Ruby style
833
 *      class ExampleClass
834
 *        def example_method
835
 *        end
836
 *      end
837
 *
838
 *      ExampleClass.new.respond_to?(:example_method) # => true
839
 *
840
 *      // C style
841
 *      void
842
 *      mrb_example_gem_init(mrb_state* mrb) {
843
 *        struct RClass *example_class;
844
 *        mrb_sym mid;
845
 *        mrb_bool obj_resp;
846
 *
847
 *        example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
848
 *        mrb_define_method(mrb, example_class, "example_method", exampleMethod, MRB_ARGS_NONE());
849
 *        mid = mrb_intern_str(mrb, mrb_str_new_lit(mrb, "example_method" ));
850
 *        obj_resp = mrb_obj_respond_to(mrb, example_class, mid); // => TRUE (true in Ruby world)
851
 *
852
 *        // If mrb_obj_respond_to returns TRUE then puts "True"
853
 *        // If mrb_obj_respond_to returns FALSE then puts "False"
854
 *        if (obj_resp) {
855
 *          puts("True");
856
 *        }
857
 *        else {
858
 *          puts("False");
859
 *        }
860
 *      }
861
 *
862
 * @param mrb The current mruby state.
863
 * @param c A reference to a class.
864
 * @param mid A symbol referencing a method id.
865
 * @return [mrb_bool] A boolean value.
866
 */
867
MRB_API mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid);
868
869
/**
870
 * Defines a new class under a given module
871
 *
872
 * @param mrb The current mruby state.
873
 * @param outer Reference to the module under which the new class will be defined
874
 * @param name The name of the defined class
875
 * @param super The new class parent
876
 * @return [struct RClass *] Reference to the newly defined class
877
 * @see mrb_define_class
878
 */
879
MRB_API struct RClass* mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
880
MRB_API struct RClass* mrb_define_class_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name, struct RClass *super);
881
882
MRB_API struct RClass* mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
883
MRB_API struct RClass* mrb_define_module_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
884
885
/**
886
 * Function requires n arguments.
887
 *
888
 * @param n
889
 *      The number of required arguments.
890
 */
891
5.71M
#define MRB_ARGS_REQ(n)     ((mrb_aspec)((n)&0x1f) << 18)
892
893
/**
894
 * Function takes n optional arguments
895
 *
896
 * @param n
897
 *      The number of optional arguments.
898
 */
899
1.61M
#define MRB_ARGS_OPT(n)     ((mrb_aspec)((n)&0x1f) << 13)
900
901
/**
902
 * Function takes n1 mandatory arguments and n2 optional arguments
903
 *
904
 * @param n1
905
 *      The number of required arguments.
906
 * @param n2
907
 *      The number of optional arguments.
908
 */
909
511k
#define MRB_ARGS_ARG(n1,n2)   (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2))
910
911
/** rest argument */
912
1.04M
#define MRB_ARGS_REST()     ((mrb_aspec)(1 << 12))
913
914
/** required arguments after rest */
915
17.4k
#define MRB_ARGS_POST(n)    ((mrb_aspec)((n)&0x1f) << 7)
916
917
/** keyword arguments (n of keys, kdict) */
918
17.4k
#define MRB_ARGS_KEY(n1,n2) ((mrb_aspec)((((n1)&0x1f) << 2) | ((n2)?(1<<1):0)))
919
920
/**
921
 * Function takes a block argument
922
 */
923
222k
#define MRB_ARGS_BLOCK()    ((mrb_aspec)1)
924
925
/**
926
 * Function accepts any number of arguments
927
 */
928
956k
#define MRB_ARGS_ANY()      MRB_ARGS_REST()
929
930
/**
931
 * Function accepts no arguments
932
 */
933
16.4M
#define MRB_ARGS_NONE()     ((mrb_aspec)0)
934
935
/**
936
 * Format specifiers for {mrb_get_args} function
937
 *
938
 * Must be a C string composed of the following format specifiers:
939
 *
940
 * | char | Ruby type      | C types           | Notes                                              |
941
 * |:----:|----------------|-------------------|----------------------------------------------------|
942
 * | `o`  | {Object}       | {mrb_value}       | Could be used to retrieve any type of argument     |
943
 * | `C`  | {Class}/{Module} | {mrb_value}     | when `!` follows, the value may be `nil`           |
944
 * | `S`  | {String}       | {mrb_value}       | when `!` follows, the value may be `nil`           |
945
 * | `A`  | {Array}        | {mrb_value}       | when `!` follows, the value may be `nil`           |
946
 * | `H`  | {Hash}         | {mrb_value}       | when `!` follows, the value may be `nil`           |
947
 * | `s`  | {String}       | const char *, {mrb_int} | Receive two arguments; `s!` gives (`NULL`,`0`) for `nil` |
948
 * | `z`  | {String}       | const char *      | `NULL` terminated string; `z!` gives `NULL` for `nil` |
949
 * | `a`  | {Array}        | const {mrb_value} *, {mrb_int} | Receive two arguments; `a!` gives (`NULL`,`0`) for `nil` |
950
 * | `c`  | {Class}/{Module} | strcut RClass * | `c!` gives `NULL` for `nil`                        |
951
 * | `f`  | {Integer}/{Float} | {mrb_float}    |                                                    |
952
 * | `i`  | {Integer}/{Float} | {mrb_int}      |                                                    |
953
 * | `b`  | boolean        | {mrb_bool}        |                                                    |
954
 * | `n`  | {String}/{Symbol} | {mrb_sym}         |                                                    |
955
 * | `d`  | data           | void *, {mrb_data_type} const | 2nd argument will be used to check data type so it won't be modified; when `!` follows, the value may be `nil` |
956
 * | `I`  | inline struct  | void *, struct RClass | `I!` gives `NULL` for `nil`                    |
957
 * | `&`  | block          | {mrb_value}       | &! raises exception if no block given.             |
958
 * | `*`  | rest arguments | const {mrb_value} *, {mrb_int} | Receive the rest of arguments as an array; `*!` avoid copy of the stack.  |
959
 * | `\|` | optional     |                   | After this spec following specs would be optional. |
960
 * | `?`  | optional given | {mrb_bool}        | `TRUE` if preceding argument is given. Used to check optional argument is given. |
961
 * | `:`  | keyword args   | {mrb_kwargs} const | Get keyword arguments. @see mrb_kwargs |
962
 *
963
 * @see mrb_get_args
964
 *
965
 * Immediately after format specifiers it can add format modifiers:
966
 *
967
 * | char | Notes                                                                                   |
968
 * |:----:|-----------------------------------------------------------------------------------------|
969
 * | `!`  | Switch to the alternate mode; The behaviour changes depending on the format specifier   |
970
 * | `+`  | Request a not frozen object; However, except nil value                                  |
971
 */
972
typedef const char *mrb_args_format;
973
974
/**
975
 * Get keyword arguments by `mrb_get_args()` with `:` specifier.
976
 *
977
 * `mrb_kwargs::num` indicates that the total number of keyword values.
978
 *
979
 * `mrb_kwargs::required` indicates that the specified number of keywords starting from the beginning of the `mrb_sym` array are required.
980
 *
981
 * `mrb_kwargs::table` accepts a `mrb_sym` array of C.
982
 *
983
 * `mrb_kwargs::values` is an object array of C, and the keyword argument corresponding to the `mrb_sym` array is assigned.
984
 * Note that `undef` is assigned if there is no keyword argument corresponding over `mrb_kwargs::required` to `mrb_kwargs::num`.
985
 *
986
 * `mrb_kwargs::rest` is the remaining keyword argument that can be accepted as `**rest` in Ruby.
987
 * If `NULL` is specified, `ArgumentError` is raised when there is an undefined keyword.
988
 *
989
 * Examples:
990
 *
991
 *      // def method(a: 1, b: 2)
992
 *
993
 *      mrb_int kw_num = 2;
994
 *      mrb_int kw_required = 0;
995
 *      mrb_sym kw_names[] = { mrb_intern_lit(mrb, "a"), mrb_intern_lit(mrb, "b") };
996
 *      mrb_value kw_values[kw_num];
997
 *      mrb_kwargs kwargs = { kw_num, kw_required, kw_names, kw_values, NULL };
998
 *
999
 *      mrb_get_args(mrb, ":", &kwargs);
1000
 *      if (mrb_undef_p(kw_values[0])) { kw_values[0] = mrb_fixnum_value(1); }
1001
 *      if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
1002
 *
1003
 *
1004
 *      // def method(str, x:, y: 2, z: "default string", **opts)
1005
 *
1006
 *      mrb_value str, kw_rest;
1007
 *      uint32_t kw_num = 3;
1008
 *      uint32_t kw_required = 1;
1009
 *      // Note that `#include <mruby/presym.h>` is required beforehand because `MRB_SYM()` is used.
1010
 *      // If the usage of `MRB_SYM()` is not desired, replace it with `mrb_intern_lit()`.
1011
 *      mrb_sym kw_names[] = { MRB_SYM(x), MRB_SYM(y), MRB_SYM(z) };
1012
 *      mrb_value kw_values[kw_num];
1013
 *      mrb_kwargs kwargs = { kw_num, kw_required, kw_names, kw_values, &kw_rest };
1014
 *
1015
 *      mrb_get_args(mrb, "S:", &str, &kwargs);
1016
 *      // or: mrb_get_args(mrb, ":S", &kwargs, &str);
1017
 *      if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
1018
 *      if (mrb_undef_p(kw_values[2])) { kw_values[2] = mrb_str_new_cstr(mrb, "default string"); }
1019
 */
1020
typedef struct mrb_kwargs mrb_kwargs;
1021
1022
struct mrb_kwargs
1023
{
1024
  mrb_int num;                  /* number of keyword arguments */
1025
  mrb_int required;             /* number of required keyword arguments */
1026
  const mrb_sym *table;         /* C array of symbols for keyword names */
1027
  mrb_value *values;            /* keyword argument values */
1028
  mrb_value *rest;              /* keyword rest (dict) */
1029
};
1030
1031
/**
1032
 * Retrieve arguments from mrb_state.
1033
 *
1034
 * @param mrb The current mruby state.
1035
 * @param format is a list of format specifiers
1036
 * @param ... The passing variadic arguments must be a pointer of retrieving type.
1037
 * @return the number of arguments retrieved.
1038
 * @see mrb_args_format
1039
 * @see mrb_kwargs
1040
 */
1041
MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...);
1042
1043
/**
1044
 * Array version of mrb_get_args()
1045
 *
1046
 * @param ptr Array of void*, in the same order as the varargs version.
1047
 */
1048
MRB_API mrb_int mrb_get_args_a(mrb_state *mrb, mrb_args_format format, void** ptr);
1049
1050
MRB_INLINE mrb_sym
1051
mrb_get_mid(mrb_state *mrb) /* get method symbol */
1052
0
{
1053
0
  return mrb->c->ci->mid;
1054
0
}
Unexecuted instantiation: mruby_fuzzer.c:mrb_get_mid
Unexecuted instantiation: state.c:mrb_get_mid
Unexecuted instantiation: debug.c:mrb_get_mid
Unexecuted instantiation: class.c:mrb_get_mid
Unexecuted instantiation: init.c:mrb_get_mid
Unexecuted instantiation: etc.c:mrb_get_mid
Unexecuted instantiation: array.c:mrb_get_mid
Unexecuted instantiation: version.c:mrb_get_mid
Unexecuted instantiation: y.tab.c:mrb_get_mid
Unexecuted instantiation: rational.c:mrb_get_mid
Unexecuted instantiation: complex.c:mrb_get_mid
Unexecuted instantiation: mrblib.c:mrb_get_mid
Unexecuted instantiation: gem_init.c:mrb_get_mid
Unexecuted instantiation: bigint.c:mrb_get_mid
Unexecuted instantiation: mempool.c:mrb_get_mid
Unexecuted instantiation: variable.c:mrb_get_mid
Unexecuted instantiation: allocf.c:mrb_get_mid
Unexecuted instantiation: symbol.c:mrb_get_mid
Unexecuted instantiation: object.c:mrb_get_mid
Unexecuted instantiation: numeric.c:mrb_get_mid
Unexecuted instantiation: gc.c:mrb_get_mid
Unexecuted instantiation: error.c:mrb_get_mid
Unexecuted instantiation: load.c:mrb_get_mid
Unexecuted instantiation: enum.c:mrb_get_mid
Unexecuted instantiation: vm.c:mrb_get_mid
Unexecuted instantiation: string.c:mrb_get_mid
Unexecuted instantiation: proc.c:mrb_get_mid
Unexecuted instantiation: readfloat.c:mrb_get_mid
Unexecuted instantiation: hash.c:mrb_get_mid
Unexecuted instantiation: codedump.c:mrb_get_mid
Unexecuted instantiation: range.c:mrb_get_mid
Unexecuted instantiation: kernel.c:mrb_get_mid
Unexecuted instantiation: readint.c:mrb_get_mid
Unexecuted instantiation: backtrace.c:mrb_get_mid
Unexecuted instantiation: codegen.c:mrb_get_mid
Unexecuted instantiation: set.c:mrb_get_mid
Unexecuted instantiation: fmt_fp.c:mrb_get_mid
Unexecuted instantiation: numeric_ext.c:mrb_get_mid
Unexecuted instantiation: hash_ext.c:mrb_get_mid
Unexecuted instantiation: mruby_objectspace.c:mrb_get_mid
Unexecuted instantiation: fiber.c:mrb_get_mid
Unexecuted instantiation: catch.c:mrb_get_mid
Unexecuted instantiation: pack.c:mrb_get_mid
Unexecuted instantiation: sprintf.c:mrb_get_mid
Unexecuted instantiation: time.c:mrb_get_mid
Unexecuted instantiation: struct.c:mrb_get_mid
Unexecuted instantiation: data.c:mrb_get_mid
Unexecuted instantiation: random.c:mrb_get_mid
Unexecuted instantiation: mruby_io_gem.c:mrb_get_mid
Unexecuted instantiation: socket.c:mrb_get_mid
Unexecuted instantiation: errno.c:mrb_get_mid
Unexecuted instantiation: dir.c:mrb_get_mid
Unexecuted instantiation: math.c:mrb_get_mid
Unexecuted instantiation: metaprog.c:mrb_get_mid
Unexecuted instantiation: method.c:mrb_get_mid
Unexecuted instantiation: eval.c:mrb_get_mid
Unexecuted instantiation: binding.c:mrb_get_mid
Unexecuted instantiation: proc_binding.c:mrb_get_mid
Unexecuted instantiation: io_hal.c:mrb_get_mid
Unexecuted instantiation: socket_hal.c:mrb_get_mid
Unexecuted instantiation: dir_hal.c:mrb_get_mid
Unexecuted instantiation: exception.c:mrb_get_mid
Unexecuted instantiation: file.c:mrb_get_mid
Unexecuted instantiation: file_test.c:mrb_get_mid
Unexecuted instantiation: io.c:mrb_get_mid
Unexecuted instantiation: mruby_proto_fuzzer.cpp:mrb_get_mid(mrb_state*)
1055
1056
/**
1057
 * Retrieve number of arguments from mrb_state.
1058
 *
1059
 * Correctly handles *splat arguments.
1060
 */
1061
MRB_API mrb_int mrb_get_argc(mrb_state *mrb);
1062
1063
/**
1064
 * Retrieve an array of arguments from mrb_state.
1065
 *
1066
 * Correctly handles *splat arguments.
1067
 */
1068
MRB_API const mrb_value *mrb_get_argv(mrb_state *mrb);
1069
1070
/**
1071
 * Retrieve the first and only argument from mrb_state.
1072
 * Raises ArgumentError unless the number of arguments is exactly one.
1073
 *
1074
 * Correctly handles *splat arguments.
1075
 */
1076
MRB_API mrb_value mrb_get_arg1(mrb_state *mrb);
1077
1078
/**
1079
 * Check if a block argument is given from mrb_state.
1080
 */
1081
MRB_API mrb_bool mrb_block_given_p(mrb_state *mrb);
1082
1083
/* `strlen` for character string literals (use with caution or `strlen` instead)
1084
    Adjacent string literals are concatenated in C/C++ in translation phase 6.
1085
    If `lit` is not one, the compiler will report a syntax error:
1086
     MSVC: "error C2143: syntax error : missing ')' before 'string'"
1087
     GCC:  "error: expected ')' before string constant"
1088
*/
1089
917k
#define mrb_strlen_lit(lit) (sizeof(lit "") - 1)
1090
1091
/**
1092
 * Call existing Ruby functions.
1093
 *
1094
 * Example:
1095
 *
1096
 *      #include <stdio.h>
1097
 *      #include <mruby.h>
1098
 *      #include <mruby/compile.h>
1099
 *
1100
 *      int
1101
 *      main()
1102
 *      {
1103
 *        mrb_int i = 99;
1104
 *        mrb_state *mrb = mrb_open();
1105
 *
1106
 *        if (!mrb) { }
1107
 *        FILE *fp = fopen("test.rb","r");
1108
 *        mrb_value obj = mrb_load_file(mrb,fp);
1109
 *        mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
1110
 *        mrb_funcall_id(mrb, obj, MRB_SYM(method_name), 1, mrb_fixnum_value(i));
1111
 *        fclose(fp);
1112
 *        mrb_close(mrb);
1113
 *      }
1114
 *
1115
 * @param mrb The current mruby state.
1116
 * @param val A reference to an mruby value.
1117
 * @param name The name of the method.
1118
 * @param argc The number of arguments the method has.
1119
 * @param ... Variadic values(not type safe!).
1120
 * @return [mrb_value] mruby function value.
1121
 */
1122
MRB_API mrb_value mrb_funcall(mrb_state *mrb, mrb_value val, const char *name, mrb_int argc, ...);
1123
MRB_API mrb_value mrb_funcall_id(mrb_state *mrb, mrb_value val, mrb_sym mid, mrb_int argc, ...);
1124
/**
1125
 * Call existing Ruby functions. This is basically the type safe version of mrb_funcall.
1126
 *
1127
 *      #include <stdio.h>
1128
 *      #include <mruby.h>
1129
 *      #include <mruby/compile.h>
1130
 *      int
1131
 *      main()
1132
 *      {
1133
 *        mrb_state *mrb = mrb_open();
1134
 *        mrb_value obj = mrb_fixnum_value(1);
1135
 *
1136
 *        if (!mrb) { }
1137
 *
1138
 *        FILE *fp = fopen("test.rb","r");
1139
 *        mrb_value obj = mrb_load_file(mrb,fp);
1140
 *        mrb_funcall_argv(mrb, obj, MRB_SYM(method_name), 1, &obj); // Calling Ruby function from test.rb.
1141
 *        fclose(fp);
1142
 *        mrb_close(mrb);
1143
 *       }
1144
 * @param mrb The current mruby state.
1145
 * @param val A reference to an mruby value.
1146
 * @param name_sym The symbol representing the method.
1147
 * @param argc The number of arguments the method has.
1148
 * @param obj Pointer to the object.
1149
 * @return [mrb_value] mrb_value mruby function value.
1150
 * @see mrb_funcall
1151
 */
1152
MRB_API mrb_value mrb_funcall_argv(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv);
1153
/**
1154
 * Call existing Ruby functions with a block.
1155
 */
1156
MRB_API mrb_value mrb_funcall_with_block(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv, mrb_value block);
1157
/**
1158
 * Create a symbol from C string. But usually it's better to
1159
 * use MRB_SYM, MRB_OPSYM, MRB_CVSYM, MRB_IVSYM, MRB_GVSYM,
1160
 * MRB_SYM_B, MRB_SYM_Q, MRB_SYM_E macros.
1161
 *
1162
 * Example:
1163
 *
1164
 *     # Ruby style:
1165
 *     :pizza # => :pizza
1166
 *
1167
 *     // C style:
1168
 *     mrb_sym sym1 = mrb_intern_lit(mrb, "pizza"); //  => :pizza
1169
 *     mrb_sym sym2 = MRB_SYM(pizza);               //  => :pizza
1170
 *     mrb_sym sym3 = MRB_SYM_Q(pizza);             //  => :pizza?
1171
 *
1172
 * @param mrb The current mruby state.
1173
 * @param str The string to be symbolized
1174
 * @return [mrb_sym] mrb_sym A symbol.
1175
 */
1176
MRB_API mrb_sym mrb_intern_cstr(mrb_state *mrb, const char* str);
1177
MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t);
1178
MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t);
1179
144k
#define mrb_intern_lit(mrb, lit) mrb_intern_static(mrb, (lit ""), mrb_strlen_lit(lit))
1180
MRB_API mrb_sym mrb_intern_str(mrb_state*,mrb_value);
1181
/* mrb_intern_check series functions returns 0 if the symbol is not defined */
1182
MRB_API mrb_sym mrb_intern_check_cstr(mrb_state*,const char*);
1183
MRB_API mrb_sym mrb_intern_check(mrb_state*,const char*,size_t);
1184
MRB_API mrb_sym mrb_intern_check_str(mrb_state*,mrb_value);
1185
/* mrb_check_intern series functions returns nil if the symbol is not defined */
1186
/* otherwise returns mrb_value */
1187
MRB_API mrb_value mrb_check_intern_cstr(mrb_state*,const char*);
1188
MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t);
1189
MRB_API mrb_value mrb_check_intern_str(mrb_state*,mrb_value);
1190
MRB_API const char *mrb_sym_name(mrb_state*,mrb_sym);
1191
MRB_API const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*);
1192
MRB_API const char *mrb_sym_dump(mrb_state*,mrb_sym);
1193
MRB_API mrb_value mrb_sym_str(mrb_state*,mrb_sym);
1194
#define mrb_sym2name(mrb,sym) mrb_sym_name(mrb,sym)
1195
#define mrb_sym2name_len(mrb,sym,len) mrb_sym_name_len(mrb,sym,len)
1196
#define mrb_sym2str(mrb,sym) mrb_sym_str(mrb,sym)
1197
1198
MRB_API void *mrb_malloc(mrb_state*, size_t);         /* raise RuntimeError if no mem */
1199
MRB_API void *mrb_calloc(mrb_state*, size_t, size_t); /* ditto */
1200
MRB_API void *mrb_realloc(mrb_state*, void*, size_t); /* ditto */
1201
MRB_API void *mrb_realloc_simple(mrb_state*, void*, size_t); /* return NULL if no memory available */
1202
MRB_API void *mrb_malloc_simple(mrb_state*, size_t);  /* return NULL if no memory available */
1203
MRB_API struct RBasic *mrb_obj_alloc(mrb_state*, enum mrb_vtype, struct RClass*);
1204
MRB_API void mrb_free(mrb_state*, void*);
1205
1206
/**
1207
 * Allocates a Ruby object that matches the constant literal defined in
1208
 * `enum mrb_vtype` and returns a pointer to the corresponding C type.
1209
 *
1210
 * @param mrb   The current mruby state
1211
 * @param tt    The constant literal of `enum mrb_vtype`
1212
 * @param klass A Class object
1213
 * @return      Reference to the newly created object
1214
 */
1215
15.9M
#define MRB_OBJ_ALLOC(mrb, tt, klass) ((MRB_VTYPE_TYPEOF(tt)*)mrb_obj_alloc(mrb, tt, klass))
1216
1217
MRB_API mrb_value mrb_str_new(mrb_state *mrb, const char *p, mrb_int len);
1218
1219
/**
1220
 * Turns a C string into a Ruby string value.
1221
 */
1222
MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*);
1223
MRB_API mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, mrb_int len);
1224
425k
#define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
1225
1226
MRB_API mrb_value mrb_obj_freeze(mrb_state*, mrb_value);
1227
19.1k
#define mrb_str_new_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new(mrb,p,len))
1228
#define mrb_str_new_cstr_frozen(mrb,p) mrb_obj_freeze(mrb,mrb_str_new_cstr(mrb,p))
1229
0
#define mrb_str_new_static_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new_static(mrb,p,len))
1230
2.75k
#define mrb_str_new_lit_frozen(mrb,lit) mrb_obj_freeze(mrb,mrb_str_new_lit(mrb,lit))
1231
1232
#ifdef _WIN32
1233
MRB_API char* mrb_utf8_from_locale(const char *p, int len);
1234
MRB_API char* mrb_locale_from_utf8(const char *p, int len);
1235
#define mrb_locale_free(p) free(p)
1236
#define mrb_utf8_free(p) free(p)
1237
#else
1238
0
#define mrb_utf8_from_locale(p, l) ((char*)(p))
1239
2.57k
#define mrb_locale_from_utf8(p, l) ((char*)(p))
1240
#define mrb_locale_free(p)
1241
#define mrb_utf8_free(p)
1242
#endif
1243
1244
/**
1245
 * Creates new mrb_state.
1246
 *
1247
 * @return
1248
 *      Pointer to the newly created mrb_state.
1249
 */
1250
MRB_API mrb_state* mrb_open(void);
1251
1252
/**
1253
 * Create new mrb_state with just the mruby core
1254
 *
1255
 * @param f
1256
 *      Reference to the allocation function.
1257
 *      Use mrb_basic_alloc_func for the default
1258
 * @param ud
1259
 *      User data will be passed to custom allocator f.
1260
 *      If user data isn't required just pass NULL.
1261
 * @return
1262
 *      Pointer to the newly created mrb_state.
1263
 */
1264
MRB_API mrb_state* mrb_open_core(void);
1265
1266
/**
1267
 * Closes and frees a mrb_state.
1268
 *
1269
 * @param mrb
1270
 *      Pointer to the mrb_state to be closed.
1271
 */
1272
MRB_API void mrb_close(mrb_state *mrb);
1273
#ifndef MRB_NO_METHOD_CACHE
1274
MRB_API void mrb_method_cache_clear(mrb_state *mrb);
1275
#else
1276
#define mrb_method_cache_clear(mrb) ((void)0)
1277
#endif
1278
1279
/**
1280
 * Check if mrb_open() failed
1281
 *
1282
 * @param mrb
1283
 *      Pointer returned from mrb_open() or mrb_open_core().
1284
 * @return
1285
 *      Non-zero if initialization failed, 0 if succeeded.
1286
 * @note
1287
 *      mrb_open() may return non-NULL even on failure (with mrb->exc set).
1288
 *      Use this macro to check for failure:
1289
 *      @code
1290
 *      mrb_state *mrb = mrb_open();
1291
 *      if (MRB_OPEN_FAILURE(mrb)) {
1292
 *        if (mrb) {
1293
 *          // Inspect mrb->exc for error details
1294
 *          mrb_close(mrb);
1295
 *        }
1296
 *        return EXIT_FAILURE;
1297
 *      }
1298
 *      @endcode
1299
 */
1300
#define MRB_OPEN_FAILURE(mrb) (!(mrb) || (mrb)->exc)
1301
1302
/**
1303
 * Check if mrb_open() succeeded
1304
 *
1305
 * @param mrb
1306
 *      Pointer returned from mrb_open() or mrb_open_core().
1307
 * @return
1308
 *      Non-zero if initialization succeeded, 0 if failed.
1309
 */
1310
#define MRB_OPEN_SUCCESS(mrb) (!MRB_OPEN_FAILURE(mrb))
1311
1312
/**
1313
 * The memory allocation function. You can redefine this function for your own allocator.
1314
 *
1315
 */
1316
MRB_API void* mrb_basic_alloc_func(void*, size_t);
1317
1318
MRB_API mrb_value mrb_top_self(mrb_state *mrb);
1319
1320
/**
1321
 * Enter the mruby VM and execute the proc.
1322
 *
1323
 * @param mrb
1324
 *      The current mruby state.
1325
 * @param proc
1326
 *      An object containing `irep`.
1327
 *      If supplied an object containing anything other than `irep`, it will probably crash.
1328
 * @param self
1329
 *      `self` on the execution context of `proc`.
1330
 * @param stack_keep
1331
 *      Specifies the number of values to hold from the stack top.
1332
 *      Values on the stack outside this range will be initialized to `nil`.
1333
 *
1334
 * @note
1335
 *      When called from a C function defined as a method, the current stack is destroyed.
1336
 *      If you want to use arguments obtained by `mrb_get_args()` or other methods after `mrb_top_run()`,
1337
 *      you must protect them by `mrb_gc_protect()` or other ways before this function.
1338
 *      Or consider using `mrb_yield()` family functions.
1339
 */
1340
MRB_API mrb_value mrb_top_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep);
1341
1342
MRB_API mrb_value mrb_vm_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep);
1343
MRB_API mrb_value mrb_vm_exec(mrb_state *mrb, const struct RProc *proc, const mrb_code *iseq);
1344
/* compatibility macros */
1345
#define mrb_toplevel_run_keep(m,p,k) mrb_top_run((m),(p),mrb_top_self(m),(k))
1346
#define mrb_toplevel_run(m,p) mrb_toplevel_run_keep((m),(p),0)
1347
#define mrb_context_run(m,p,s,k) mrb_vm_run((m),(p),(s),(k))
1348
1349
MRB_API void mrb_p(mrb_state*, mrb_value);
1350
MRB_API mrb_int mrb_obj_id(mrb_value obj);
1351
MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name);
1352
1353
MRB_API mrb_bool mrb_obj_eq(mrb_state *mrb, mrb_value a, mrb_value b);
1354
MRB_API mrb_bool mrb_obj_equal(mrb_state *mrb, mrb_value a, mrb_value b);
1355
MRB_API mrb_bool mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
1356
#ifndef MRB_NO_FLOAT
1357
MRB_API mrb_value mrb_ensure_float_type(mrb_state *mrb, mrb_value val);
1358
90.1k
#define mrb_as_float(mrb, x) mrb_float(mrb_ensure_float_type(mrb, x))
1359
/* obsolete: use mrb_ensure_float_type() instead */
1360
#define mrb_to_float(mrb, val) mrb_ensure_float_type(mrb, val)
1361
#endif
1362
MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj);
1363
MRB_API mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
1364
/* mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error */
1365
MRB_API mrb_int mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
1366
1367
/* recursion detection */
1368
MRB_API mrb_bool mrb_recursive_method_p(mrb_state *mrb, mrb_sym mid, mrb_value obj1, mrb_value obj2);
1369
MRB_API mrb_bool mrb_recursive_func_p(mrb_state *mrb, mrb_sym mid, mrb_value obj1, mrb_value obj2);
1370
1371
#define MRB_RECURSIVE_P(mrb, mid, obj1, obj2) \
1372
  mrb_recursive_method_p(mrb, mid, obj1, obj2)
1373
1374
#define MRB_RECURSIVE_UNARY_P(mrb, mid, obj) \
1375
39.9k
  mrb_recursive_method_p(mrb, mid, obj, mrb_nil_value())
1376
1377
#define MRB_RECURSIVE_BINARY_P(mrb, mid, obj1, obj2) \
1378
  mrb_recursive_method_p(mrb, mid, obj1, obj2)
1379
1380
#define MRB_RECURSIVE_FUNC_P(mrb, mid, obj) \
1381
  mrb_recursive_func_p(mrb, mid, obj, mrb_nil_value())
1382
1383
#define MRB_RECURSIVE_BINARY_FUNC_P(mrb, mid, obj1, obj2) \
1384
276k
  mrb_recursive_func_p(mrb, mid, obj1, obj2)
1385
1386
16.4M
#define mrb_gc_arena_save(mrb) ((mrb)->gc.arena_idx)
1387
29.5M
#define mrb_gc_arena_restore(mrb, idx) ((mrb)->gc.arena_idx = (idx))
1388
1389
MRB_API void mrb_garbage_collect(mrb_state*);
1390
MRB_API void mrb_full_gc(mrb_state*);
1391
MRB_API void mrb_incremental_gc(mrb_state*);
1392
MRB_API void mrb_gc_mark(mrb_state*,struct RBasic*);
1393
17.7M
#define mrb_gc_mark_value(mrb,val) do {\
1394
17.7M
  if (!mrb_immediate_p(val)) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \
1395
17.7M
} while (0)
1396
MRB_API void mrb_field_write_barrier(mrb_state*, struct RBasic*, struct RBasic*);
1397
10.2M
#define mrb_field_write_barrier_value(mrb, obj, val) do{\
1398
10.2M
  if (!mrb_immediate_p(val)) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \
1399
10.2M
} while (0)
1400
MRB_API void mrb_write_barrier(mrb_state *, struct RBasic*);
1401
1402
MRB_API mrb_value mrb_type_convert(mrb_state *mrb, mrb_value val, enum mrb_vtype type, mrb_sym method);
1403
#define mrb_convert_type(mrb, val, type, tname, method) mrb_type_convert(mrb, val, type, mrb_intern_lit(mrb, method))
1404
MRB_API mrb_value mrb_type_convert_check(mrb_state *mrb, mrb_value val, enum mrb_vtype type, mrb_sym method);
1405
#define mrb_check_convert_type(mrb, val, type, tname, method) mrb_type_convert_check(mrb, val, type, mrb_intern_lit(mrb, method))
1406
1407
MRB_API mrb_value mrb_any_to_s(mrb_state *mrb, mrb_value obj);
1408
MRB_API const char * mrb_obj_classname(mrb_state *mrb, mrb_value obj);
1409
MRB_API struct RClass* mrb_obj_class(mrb_state *mrb, mrb_value obj);
1410
MRB_API mrb_value mrb_class_path(mrb_state *mrb, struct RClass *c);
1411
MRB_API mrb_bool mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c);
1412
MRB_API mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value self);
1413
MRB_API mrb_value mrb_obj_clone(mrb_state *mrb, mrb_value self);
1414
1415
#ifndef ISPRINT
1416
3.97M
#define ISASCII(c) ((unsigned)(c) <= 0x7f)
1417
597k
#define ISPRINT(c) (((unsigned)(c) - 0x20) < 0x5f)
1418
5.71M
#define ISSPACE(c) ((c) == ' ' || (unsigned)(c) - '\t' < 5)
1419
2.58M
#define ISUPPER(c) (((unsigned)(c) - 'A') < 26)
1420
1.91M
#define ISLOWER(c) (((unsigned)(c) - 'a') < 26)
1421
23.5M
#define ISALPHA(c) ((((unsigned)(c) | 0x20) - 'a') < 26)
1422
4.50M
#define ISDIGIT(c) (((unsigned)(c) - '0') < 10)
1423
230
#define ISXDIGIT(c) (ISDIGIT(c) || ((unsigned)(c) | 0x20) - 'a' < 6)
1424
20.8M
#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
1425
#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
1426
#define ISCNTRL(c) ((unsigned)(c) < 0x20 || (c) == 0x7f)
1427
0
#define TOUPPER(c) (ISLOWER(c) ? ((c) & 0x5f) : (c))
1428
0
#define TOLOWER(c) (ISUPPER(c) ? ((c) | 0x20) : (c))
1429
#endif
1430
1431
MRB_API mrb_value mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, mrb_int len);
1432
MRB_API mrb_noreturn void mrb_exc_raise(mrb_state *mrb, mrb_value exc);
1433
1434
MRB_API mrb_noreturn void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg);
1435
MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...);
1436
MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...);
1437
MRB_API mrb_noreturn void mrb_frozen_error(mrb_state *mrb, void *frozen_obj);
1438
MRB_API mrb_noreturn void mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max);
1439
MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...);
1440
MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *mesg);
1441
MRB_API void mrb_print_backtrace(mrb_state *mrb);
1442
MRB_API void mrb_print_error(mrb_state *mrb);
1443
/* function for `raisef` formatting */
1444
MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap);
1445
1446
/* macros to get typical exception objects
1447
   note:
1448
   + those E_* macros requires mrb_state* variable named mrb.
1449
   + exception objects obtained from those macros are local to mrb
1450
*/
1451
390
#define MRB_ERROR_SYM(sym) mrb_intern_lit(mrb, #sym)
1452
106k
#define E_EXCEPTION          mrb->eException_class
1453
170k
#define E_STANDARD_ERROR     mrb->eStandardError_class
1454
610
#define E_RUNTIME_ERROR      mrb_exc_get_id(mrb, MRB_ERROR_SYM(RuntimeError))
1455
610
#define E_TYPE_ERROR         mrb_exc_get_id(mrb, MRB_ERROR_SYM(TypeError))
1456
157
#define E_ZERODIV_ERROR      mrb_exc_get_id(mrb, MRB_ERROR_SYM(ZeroDivisionError))
1457
1.19k
#define E_ARGUMENT_ERROR     mrb_exc_get_id(mrb, MRB_ERROR_SYM(ArgumentError))
1458
222
#define E_INDEX_ERROR        mrb_exc_get_id(mrb, MRB_ERROR_SYM(IndexError))
1459
395
#define E_RANGE_ERROR        mrb_exc_get_id(mrb, MRB_ERROR_SYM(RangeError))
1460
1.80k
#define E_NAME_ERROR         mrb_exc_get_id(mrb, MRB_ERROR_SYM(NameError))
1461
13.2k
#define E_NOMETHOD_ERROR     mrb_exc_get_id(mrb, MRB_ERROR_SYM(NoMethodError))
1462
0
#define E_SCRIPT_ERROR       mrb_exc_get_id(mrb, MRB_ERROR_SYM(ScriptError))
1463
4
#define E_SYNTAX_ERROR       mrb_exc_get_id(mrb, MRB_ERROR_SYM(SyntaxError))
1464
3
#define E_LOCALJUMP_ERROR    mrb_exc_get_id(mrb, MRB_ERROR_SYM(LocalJumpError))
1465
#define E_REGEXP_ERROR       mrb_exc_get_id(mrb, MRB_ERROR_SYM(RegexpError))
1466
0
#define E_FROZEN_ERROR       mrb_exc_get_id(mrb, MRB_ERROR_SYM(FrozenError))
1467
0
#define E_NOTIMP_ERROR       mrb_exc_get_id(mrb, MRB_ERROR_SYM(NotImplementedError))
1468
0
#define E_KEY_ERROR          mrb_exc_get_id(mrb, MRB_ERROR_SYM(KeyError))
1469
#ifndef MRB_NO_FLOAT
1470
0
# define E_FLOATDOMAIN_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FloatDomainError))
1471
#endif
1472
1473
MRB_API mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg);
1474
MRB_API mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv);
1475
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);
1476
1477
/* continue execution to the proc */
1478
/* this function should always be called as the last function of a method */
1479
/* e.g. return mrb_yield_cont(mrb, proc, self, argc, argv); */
1480
mrb_value mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const mrb_value *argv);
1481
1482
/* mrb_gc_protect() leaves the object in the arena */
1483
MRB_API void mrb_gc_protect(mrb_state *mrb, mrb_value obj);
1484
/* mrb_gc_register() keeps the object from GC. */
1485
MRB_API void mrb_gc_register(mrb_state *mrb, mrb_value obj);
1486
/* mrb_gc_unregister() removes the object from GC root. */
1487
MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj);
1488
1489
/* type conversion/check functions */
1490
MRB_API mrb_value mrb_ensure_array_type(mrb_state *mrb, mrb_value self);
1491
MRB_API mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
1492
MRB_API mrb_value mrb_ensure_hash_type(mrb_state *mrb, mrb_value hash);
1493
MRB_API mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value hash);
1494
MRB_API mrb_value mrb_ensure_string_type(mrb_state *mrb, mrb_value str);
1495
MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
1496
/* obsolete: use mrb_ensure_string_type() instead */
1497
#define mrb_string_type(mrb, str) mrb_ensure_string_type(mrb,str)
1498
#define mrb_to_str(mrb, str) mrb_ensure_string_type(mrb,str)
1499
/* obsolete: use mrb_obj_as_string() instead */
1500
#define mrb_str_to_str(mrb, str) mrb_obj_as_string(mrb, str)
1501
/* check if val is an integer (including Bigint) */
1502
MRB_API mrb_value mrb_ensure_integer_type(mrb_state *mrb, mrb_value val);
1503
/* check if val fit in mrb_int */
1504
MRB_API mrb_value mrb_ensure_int_type(mrb_state *mrb, mrb_value val);
1505
1.09M
#define mrb_as_int(mrb, val) mrb_integer(mrb_ensure_int_type(mrb, val))
1506
/* obsolete: use mrb_ensure_int_type() instead */
1507
#define mrb_to_integer(mrb, val) mrb_ensure_int_type(mrb, val)
1508
#define mrb_to_int(mrb, val) mrb_ensure_int_type(mrb, val)
1509
1510
/* string type checking (contrary to the name, it doesn't convert) */
1511
MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
1512
MRB_API void mrb_check_frozen(mrb_state *mrb, void *);
1513
MRB_API void mrb_check_frozen_value(mrb_state *mrb, mrb_value v);
1514
MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b);
1515
MRB_API void mrb_define_alias_id(mrb_state *mrb, struct RClass *c, mrb_sym a, mrb_sym b);
1516
MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass);
1517
MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val);
1518
1519
MRB_API mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id);
1520
1521
MRB_API mrb_bool mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid);
1522
MRB_API mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, const struct RClass* c);
1523
MRB_API mrb_bool mrb_func_basic_p(mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func);
1524
1525
/* obsolete function(s); will be removed */
1526
0
#define mrb_int(mrb, val) mrb_as_int(mrb, val)
1527
1528
/**
1529
 * Create a new Fiber from proc object
1530
 *
1531
 * Implemented in mruby-fiber
1532
 */
1533
MRB_API mrb_value mrb_fiber_new(mrb_state *mrb, const struct RProc *proc);
1534
1535
/**
1536
 * Resume a Fiber
1537
 *
1538
 * Implemented in mruby-fiber
1539
 *
1540
 * Switches to the specified fiber and executes. Like the `Fiber#resume` method.
1541
 */
1542
MRB_API mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv);
1543
1544
/**
1545
 * Yield a Fiber
1546
 *
1547
 * Implemented in mruby-fiber
1548
 *
1549
 * Passes control to the caller fiber of the running fiber. Like the `Fiber.yield` method.
1550
 *
1551
 * @note This function is only available from inside a function defined as a method by,
1552
 *       for example, `mrb_define_method()`.
1553
 *       Also, the work following `mrb_fiber_yield()` cannot be performed,
1554
 *       and the return value of `mrb_fiber_yield()` must be returned as is.
1555
 *
1556
 *           return mrb_fiber_yield(mrb, argc, argv);
1557
 */
1558
MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv);
1559
1560
/**
1561
 * Check if a Fiber is alive
1562
 *
1563
 * Implemented in mruby-fiber
1564
 */
1565
MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib);
1566
1567
/**
1568
 * FiberError reference
1569
 *
1570
 * Implemented in mruby-fiber
1571
 */
1572
0
#define E_FIBER_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FiberError))
1573
MRB_API void mrb_stack_extend(mrb_state*, mrb_int);
1574
1575
/* temporary memory allocation, only effective while GC arena is kept */
1576
MRB_API void* mrb_temp_alloc(mrb_state *mrb, size_t);
1577
1.14k
#define mrb_alloca(mrb, size) mrb_temp_alloc(mrb, size) /* for compatibility */
1578
1579
MRB_API void mrb_state_atexit(mrb_state *mrb, mrb_atexit_func func);
1580
1581
MRB_API void mrb_show_version(mrb_state *mrb);
1582
MRB_API void mrb_show_copyright(mrb_state *mrb);
1583
1584
MRB_API mrb_value mrb_format(mrb_state *mrb, const char *format, ...);
1585
1586
#ifdef MRB_PRESYM_SCANNING
1587
# include <mruby/presym/scanning.h>
1588
#endif
1589
1590
#if 0
1591
/* memcpy and memset does not work with gdb reverse-next on my box */
1592
/* use naive memcpy and memset instead */
1593
#undef memcpy
1594
#undef memset
1595
static void*
1596
mrbmemcpy(void *dst, const void *src, size_t n)
1597
{
1598
  char *d = (char*)dst;
1599
  const char *s = (const char*)src;
1600
  while (n--)
1601
    *d++ = *s++;
1602
  return d;
1603
}
1604
#define memcpy(a,b,c) mrbmemcpy(a,b,c)
1605
1606
static void*
1607
mrbmemset(void *s, int c, size_t n)
1608
{
1609
  char *t = (char*)s;
1610
  while (n--)
1611
    *t++ = c;
1612
  return s;
1613
}
1614
#define memset(a,b,c) mrbmemset(a,b,c)
1615
#endif
1616
1617
19.4M
#define mrb_int_hash_func(mrb,key) (uint32_t)((key)^((key)<<2)^((key)>>2))
1618
1619
128k
#define MRB_UNIQNAME(name)         MRB_UNIQNAME_1(name, __LINE__)
1620
128k
#define MRB_UNIQNAME_1(name, line) MRB_UNIQNAME_2(name, line)
1621
42.7k
#define MRB_UNIQNAME_2(name, line) name##line
1622
1623
MRB_END_DECL
1624
1625
#endif  /* MRUBY_H */