mruby  2.0.1
mruby is the lightweight implementation of the Ruby language
Classes | Macros | Typedefs | Enumerations | Functions
mruby.h File Reference
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include <limits.h>
#include "mrbconf.h"
#include <mruby/common.h>
#include <mruby/value.h>
#include <mruby/gc.h>
#include <mruby/version.h>
Include dependency graph for mruby.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  mrb_callinfo
 
struct  mrb_context
 
struct  mrb_method_t
 
struct  mrb_state
 

Macros

#define mrb_assert(p)   ((void)0)
 
#define mrb_assert_int_fit(t1, n, t2, max)   ((void)0)
 
#define mrb_static_assert(exp, str)   mrb_assert(exp)
 
#define FLT_EPSILON   (1.19209290e-07f)
 
#define DBL_EPSILON   ((double)2.22044604925031308085e-16L)
 
#define LDBL_EPSILON   (1.08420217248550443401e-19L)
 
#define MRB_FLOAT_EPSILON   DBL_EPSILON
 
#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE   5
 
#define MRB_METHOD_CACHE_SIZE   (1<<7)
 
#define MRB_ARGS_REQ(n)   ((mrb_aspec)((n)&0x1f) << 18)
 Function requires n arguments. More...
 
#define MRB_ARGS_OPT(n)   ((mrb_aspec)((n)&0x1f) << 13)
 Function takes n optional arguments. More...
 
#define MRB_ARGS_ARG(n1, n2)   (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2))
 Function takes n1 mandatory arguments and n2 optional arguments. More...
 
#define MRB_ARGS_REST()   ((mrb_aspec)(1 << 12))
 rest argument
 
#define MRB_ARGS_POST(n)   ((mrb_aspec)((n)&0x1f) << 7)
 required arguments after rest
 
#define MRB_ARGS_KEY(n1, n2)   ((mrb_aspec)((((n1)&0x1f) << 2) | ((n2)?(1<<1):0)))
 keyword arguments (n of keys, kdict)
 
#define MRB_ARGS_BLOCK()   ((mrb_aspec)1)
 Function takes a block argument.
 
#define MRB_ARGS_ANY()   MRB_ARGS_REST()
 Function accepts any number of arguments.
 
#define MRB_ARGS_NONE()   ((mrb_aspec)0)
 Function accepts no arguments.
 
#define mrb_strlen_lit(lit)   (sizeof(lit "") - 1)
 
#define mrb_intern_lit(mrb, lit)   mrb_intern_static(mrb, lit, mrb_strlen_lit(lit))
 
#define mrb_str_new_lit(mrb, lit)   mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
 
#define mrb_utf8_from_locale(p, l)   ((char*)(p))
 
#define mrb_locale_from_utf8(p, l)   ((char*)(p))
 
#define mrb_locale_free(p)
 
#define mrb_utf8_free(p)
 
#define mrb_toplevel_run_keep(m, p, k)   mrb_top_run((m),(p),mrb_top_self(m),(k))
 
#define mrb_toplevel_run(m, p)   mrb_toplevel_run_keep((m),(p),0)
 
#define mrb_context_run(m, p, s, k)   mrb_vm_run((m),(p),(s),(k))
 
#define mrb_gc_mark_value(mrb, val)
 
#define mrb_field_write_barrier_value(mrb, obj, val)
 
#define ISASCII(c)   ((unsigned)(c) <= 0x7f)
 
#define ISPRINT(c)   (((unsigned)(c) - 0x20) < 0x5f)
 
#define ISSPACE(c)   ((c) == ' ' || (unsigned)(c) - '\t' < 5)
 
#define ISUPPER(c)   (((unsigned)(c) - 'A') < 26)
 
#define ISLOWER(c)   (((unsigned)(c) - 'a') < 26)
 
#define ISALPHA(c)   ((((unsigned)(c) | 0x20) - 'a') < 26)
 
#define ISDIGIT(c)   (((unsigned)(c) - '0') < 10)
 
#define ISXDIGIT(c)   (ISDIGIT(c) || ((unsigned)(c) | 0x20) - 'a' < 6)
 
#define ISALNUM(c)   (ISALPHA(c) || ISDIGIT(c))
 
#define ISBLANK(c)   ((c) == ' ' || (c) == '\t')
 
#define ISCNTRL(c)   ((unsigned)(c) < 0x20 || (c) == 0x7f)
 
#define TOUPPER(c)   (ISLOWER(c) ? ((c) & 0x5f) : (c))
 
#define TOLOWER(c)   (ISUPPER(c) ? ((c) | 0x20) : (c))
 
#define E_RUNTIME_ERROR   (mrb_exc_get(mrb, "RuntimeError"))
 
#define E_TYPE_ERROR   (mrb_exc_get(mrb, "TypeError"))
 
#define E_ARGUMENT_ERROR   (mrb_exc_get(mrb, "ArgumentError"))
 
#define E_INDEX_ERROR   (mrb_exc_get(mrb, "IndexError"))
 
#define E_RANGE_ERROR   (mrb_exc_get(mrb, "RangeError"))
 
#define E_NAME_ERROR   (mrb_exc_get(mrb, "NameError"))
 
#define E_NOMETHOD_ERROR   (mrb_exc_get(mrb, "NoMethodError"))
 
#define E_SCRIPT_ERROR   (mrb_exc_get(mrb, "ScriptError"))
 
#define E_SYNTAX_ERROR   (mrb_exc_get(mrb, "SyntaxError"))
 
#define E_LOCALJUMP_ERROR   (mrb_exc_get(mrb, "LocalJumpError"))
 
#define E_REGEXP_ERROR   (mrb_exc_get(mrb, "RegexpError"))
 
#define E_FROZEN_ERROR   (mrb_exc_get(mrb, "FrozenError"))
 
#define E_NOTIMP_ERROR   (mrb_exc_get(mrb, "NotImplementedError"))
 
#define E_FLOATDOMAIN_ERROR   (mrb_exc_get(mrb, "FloatDomainError"))
 
#define E_KEY_ERROR   (mrb_exc_get(mrb, "KeyError"))
 
#define mrb_int(mrb, val)   mrb_fixnum(mrb_to_int(mrb, val))
 
#define E_FIBER_ERROR   (mrb_exc_get(mrb, "FiberError"))
 FiberError reference. More...
 

Typedefs

typedef uint8_t mrb_code
 MRuby C API entry point.
 
typedef uint32_t mrb_aspec
 
typedef void *(* mrb_allocf) (struct mrb_state *mrb, void *, size_t, void *ud)
 Function pointer type of custom allocator used in. More...
 
typedef mrb_value(* mrb_func_t) (struct mrb_state *mrb, mrb_value self)
 Function pointer type for a function callable by mruby. More...
 
typedef void(* mrb_atexit_func) (struct mrb_state *)
 
typedef struct mrb_state mrb_state
 
typedef const char * mrb_args_format
 Format specifiers for {mrb_get_args} function. More...
 
typedef enum call_type call_type
 
typedef struct mrb_pool mrb_pool
 

Enumerations

enum  mrb_fiber_state {
  MRB_FIBER_CREATED = 0, MRB_FIBER_RUNNING, MRB_FIBER_RESUMED, MRB_FIBER_SUSPENDED,
  MRB_FIBER_TRANSFERRED, MRB_FIBER_TERMINATED
}
 
enum  call_type { CALL_PUBLIC, CALL_FCALL, CALL_VCALL, CALL_TYPE_MAX }
 

Functions

struct RClassmrb_define_class (mrb_state *mrb, const char *name, struct RClass *super)
 Defines a new class. More...
 
struct RClassmrb_define_module (mrb_state *mrb, const char *name)
 Defines a new module. More...
 
mrb_value mrb_singleton_class (mrb_state *mrb, mrb_value val)
 
void mrb_include_module (mrb_state *mrb, struct RClass *cla, struct RClass *included)
 Include a module in another class or module. More...
 
void mrb_prepend_module (mrb_state *mrb, struct RClass *cla, struct RClass *prepended)
 Prepends a module in another class or module. More...
 
void mrb_define_method (mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec)
 Defines a global function in ruby. More...
 
void mrb_define_class_method (mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec)
 Defines a class method. More...
 
void mrb_define_singleton_method (mrb_state *mrb, struct RObject *cla, const char *name, mrb_func_t fun, mrb_aspec aspec)
 Defines a singleton method. More...
 
void mrb_define_module_function (mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec)
 Defines a module function. More...
 
void mrb_define_const (mrb_state *mrb, struct RClass *cla, const char *name, mrb_value val)
 Defines a constant. More...
 
void mrb_undef_method (mrb_state *mrb, struct RClass *cla, const char *name)
 Undefines a method. More...
 
void mrb_undef_method_id (mrb_state *, struct RClass *, mrb_sym)
 
void mrb_undef_class_method (mrb_state *mrb, struct RClass *cls, const char *name)
 Undefine a class method. More...
 
mrb_value mrb_obj_new (mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *argv)
 Initialize a new object instance of c class. More...
 
static mrb_value mrb_class_new_instance (mrb_state *mrb, mrb_int argc, const mrb_value *argv, struct RClass *c)
 
struct RClassmrb_class_new (mrb_state *mrb, struct RClass *super)
 Creates a new instance of Class, Class. More...
 
struct RClassmrb_module_new (mrb_state *mrb)
 Creates a new module, Module. More...
 
mrb_bool mrb_class_defined (mrb_state *mrb, const char *name)
 Returns an mrb_bool. More...
 
struct RClassmrb_class_get (mrb_state *mrb, const char *name)
 Gets a class. More...
 
struct RClassmrb_exc_get (mrb_state *mrb, const char *name)
 Gets a exception class. More...
 
mrb_bool mrb_class_defined_under (mrb_state *mrb, struct RClass *outer, const char *name)
 Returns an mrb_bool. More...
 
struct RClassmrb_class_get_under (mrb_state *mrb, struct RClass *outer, const char *name)
 Gets a child class. More...
 
struct RClassmrb_module_get (mrb_state *mrb, const char *name)
 Gets a module. More...
 
struct RClassmrb_module_get_under (mrb_state *mrb, struct RClass *outer, const char *name)
 Gets a module defined under another module. More...
 
void mrb_notimplement (mrb_state *)
 
mrb_value mrb_notimplement_m (mrb_state *, mrb_value)
 
mrb_value mrb_obj_dup (mrb_state *mrb, mrb_value obj)
 Duplicate an object. More...
 
mrb_bool mrb_obj_respond_to (mrb_state *mrb, struct RClass *c, mrb_sym mid)
 Returns true if obj responds to the given method. More...
 
struct RClassmrb_define_class_under (mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super)
 Defines a new class under a given module. More...
 
struct RClassmrb_define_module_under (mrb_state *mrb, struct RClass *outer, const char *name)
 
mrb_int mrb_get_args (mrb_state *mrb, mrb_args_format format,...)
 Retrieve arguments from mrb_state. More...
 
static mrb_sym mrb_get_mid (mrb_state *mrb)
 
mrb_int mrb_get_argc (mrb_state *mrb)
 Retrieve number of arguments from mrb_state. More...
 
mrb_valuemrb_get_argv (mrb_state *mrb)
 
mrb_value mrb_funcall (mrb_state *mrb, mrb_value val, const char *name, mrb_int argc,...)
 Call existing ruby functions. More...
 
mrb_value mrb_funcall_argv (mrb_state *mrb, mrb_value val, mrb_sym name_sym, mrb_int argc, const mrb_value *obj)
 Call existing ruby functions. More...
 
mrb_value mrb_funcall_with_block (mrb_state *, mrb_value, mrb_sym, mrb_int, const mrb_value *, mrb_value)
 Call existing ruby functions with a block.
 
mrb_sym mrb_intern_cstr (mrb_state *mrb, const char *str)
 Create a symbol. More...
 
mrb_sym mrb_intern (mrb_state *, const char *, size_t)
 
mrb_sym mrb_intern_static (mrb_state *, const char *, size_t)
 
mrb_sym mrb_intern_str (mrb_state *, mrb_value)
 
mrb_value mrb_check_intern_cstr (mrb_state *, const char *)
 
mrb_value mrb_check_intern (mrb_state *, const char *, size_t)
 
mrb_value mrb_check_intern_str (mrb_state *, mrb_value)
 
const char * mrb_sym2name (mrb_state *, mrb_sym)
 
const char * mrb_sym2name_len (mrb_state *, mrb_sym, mrb_int *)
 
mrb_value mrb_sym2str (mrb_state *, mrb_sym)
 
void * mrb_malloc (mrb_state *, size_t)
 
void * mrb_calloc (mrb_state *, size_t, size_t)
 
void * mrb_realloc (mrb_state *, void *, size_t)
 
void * mrb_realloc_simple (mrb_state *, void *, size_t)
 
void * mrb_malloc_simple (mrb_state *, size_t)
 
struct RBasicmrb_obj_alloc (mrb_state *, enum mrb_vtype, struct RClass *)
 
void mrb_free (mrb_state *, void *)
 
mrb_value mrb_str_new (mrb_state *mrb, const char *p, size_t len)
 
mrb_value mrb_str_new_cstr (mrb_state *, const char *)
 Turns a C string into a Ruby string value.
 
mrb_value mrb_str_new_static (mrb_state *mrb, const char *p, size_t len)
 
mrb_statemrb_open (void)
 Creates new mrb_state. More...
 
mrb_statemrb_open_allocf (mrb_allocf f, void *ud)
 Create new mrb_state with custom allocators. More...
 
mrb_statemrb_open_core (mrb_allocf f, void *ud)
 Create new mrb_state with just the MRuby core. More...
 
void mrb_close (mrb_state *mrb)
 Closes and frees a mrb_state. More...
 
void * mrb_default_allocf (mrb_state *, void *, size_t, void *)
 The default allocation function. More...
 
mrb_value mrb_top_self (mrb_state *)
 
mrb_value mrb_run (mrb_state *, struct RProc *, mrb_value)
 
mrb_value mrb_top_run (mrb_state *, struct RProc *, mrb_value, unsigned int)
 
mrb_value mrb_vm_run (mrb_state *, struct RProc *, mrb_value, unsigned int)
 
mrb_value mrb_vm_exec (mrb_state *, struct RProc *, const mrb_code *)
 
void mrb_p (mrb_state *, mrb_value)
 
mrb_int mrb_obj_id (mrb_value obj)
 
mrb_sym mrb_obj_to_sym (mrb_state *mrb, mrb_value name)
 
mrb_bool mrb_obj_eq (mrb_state *, mrb_value, mrb_value)
 
mrb_bool mrb_obj_equal (mrb_state *, mrb_value, mrb_value)
 
mrb_bool mrb_equal (mrb_state *mrb, mrb_value obj1, mrb_value obj2)
 
mrb_value mrb_convert_to_integer (mrb_state *mrb, mrb_value val, mrb_int base)
 
mrb_value mrb_Integer (mrb_state *mrb, mrb_value val)
 
mrb_value mrb_Float (mrb_state *mrb, mrb_value val)
 
mrb_value mrb_inspect (mrb_state *mrb, mrb_value obj)
 
mrb_bool mrb_eql (mrb_state *mrb, mrb_value obj1, mrb_value obj2)
 
static int mrb_gc_arena_save (mrb_state *mrb)
 
static void mrb_gc_arena_restore (mrb_state *mrb, int idx)
 
void mrb_garbage_collect (mrb_state *)
 
void mrb_full_gc (mrb_state *)
 
void mrb_incremental_gc (mrb_state *)
 
void mrb_gc_mark (mrb_state *, struct RBasic *)
 
void mrb_field_write_barrier (mrb_state *, struct RBasic *, struct RBasic *)
 
void mrb_write_barrier (mrb_state *, struct RBasic *)
 
mrb_value mrb_check_convert_type (mrb_state *mrb, mrb_value val, enum mrb_vtype type, const char *tname, const char *method)
 
mrb_value mrb_any_to_s (mrb_state *mrb, mrb_value obj)
 
const char * mrb_obj_classname (mrb_state *mrb, mrb_value obj)
 
struct RClassmrb_obj_class (mrb_state *mrb, mrb_value obj)
 
mrb_value mrb_class_path (mrb_state *mrb, struct RClass *c)
 
mrb_value mrb_convert_type (mrb_state *mrb, mrb_value val, enum mrb_vtype type, const char *tname, const char *method)
 
mrb_bool mrb_obj_is_kind_of (mrb_state *mrb, mrb_value obj, struct RClass *c)
 
mrb_value mrb_obj_inspect (mrb_state *mrb, mrb_value self)
 
mrb_value mrb_obj_clone (mrb_state *mrb, mrb_value self)
 
mrb_value mrb_exc_new (mrb_state *mrb, struct RClass *c, const char *ptr, size_t len)
 
void mrb_exc_raise (mrb_state *mrb, mrb_value exc)
 
void mrb_raise (mrb_state *mrb, struct RClass *c, const char *msg)
 
void mrb_raisef (mrb_state *mrb, struct RClass *c, const char *fmt,...)
 
void mrb_name_error (mrb_state *mrb, mrb_sym id, const char *fmt,...)
 
void mrb_frozen_error (mrb_state *mrb, void *frozen_obj)
 
void mrb_warn (mrb_state *mrb, const char *fmt,...)
 
void mrb_bug (mrb_state *mrb, const char *fmt,...)
 
void mrb_print_backtrace (mrb_state *mrb)
 
void mrb_print_error (mrb_state *mrb)
 
mrb_value mrb_vformat (mrb_state *mrb, const char *format, va_list ap)
 
mrb_value mrb_yield (mrb_state *mrb, mrb_value b, mrb_value arg)
 
mrb_value mrb_yield_argv (mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv)
 
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)
 
mrb_value mrb_yield_cont (mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const mrb_value *argv)
 
void mrb_gc_protect (mrb_state *mrb, mrb_value obj)
 
void mrb_gc_register (mrb_state *mrb, mrb_value obj)
 
void mrb_gc_unregister (mrb_state *mrb, mrb_value obj)
 
mrb_value mrb_to_int (mrb_state *mrb, mrb_value val)
 
mrb_value mrb_to_str (mrb_state *mrb, mrb_value val)
 
void mrb_check_type (mrb_state *mrb, mrb_value x, enum mrb_vtype t)
 
static void mrb_check_frozen (mrb_state *mrb, void *o)
 
void mrb_define_alias (mrb_state *mrb, struct RClass *c, const char *a, const char *b)
 
const char * mrb_class_name (mrb_state *mrb, struct RClass *klass)
 
void mrb_define_global_const (mrb_state *mrb, const char *name, mrb_value val)
 
mrb_value mrb_attr_get (mrb_state *mrb, mrb_value obj, mrb_sym id)
 
mrb_bool mrb_respond_to (mrb_state *mrb, mrb_value obj, mrb_sym mid)
 
mrb_bool mrb_obj_is_instance_of (mrb_state *mrb, mrb_value obj, struct RClass *c)
 
mrb_bool mrb_func_basic_p (mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func)
 
mrb_value mrb_fiber_resume (mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv)
 Resume a Fiber. More...
 
mrb_value mrb_fiber_yield (mrb_state *mrb, mrb_int argc, const mrb_value *argv)
 Yield a Fiber. More...
 
mrb_value mrb_fiber_alive_p (mrb_state *mrb, mrb_value fib)
 Check if a Fiber is alive. More...
 
void mrb_stack_extend (mrb_state *, mrb_int)
 
struct mrb_poolmrb_pool_open (mrb_state *)
 
void mrb_pool_close (struct mrb_pool *)
 
void * mrb_pool_alloc (struct mrb_pool *, size_t)
 
void * mrb_pool_realloc (struct mrb_pool *, void *, size_t oldlen, size_t newlen)
 
mrb_bool mrb_pool_can_realloc (struct mrb_pool *, void *, size_t)
 
void * mrb_alloca (mrb_state *mrb, size_t)
 
void mrb_state_atexit (mrb_state *mrb, mrb_atexit_func func)
 
void mrb_show_version (mrb_state *mrb)
 
void mrb_show_copyright (mrb_state *mrb)
 
mrb_value mrb_format (mrb_state *mrb, const char *format,...)
 

Macro Definition Documentation

◆ E_FIBER_ERROR

#define E_FIBER_ERROR   (mrb_exc_get(mrb, "FiberError"))

FiberError reference.

Implemented in mruby-fiber

◆ MRB_ARGS_ARG

#define MRB_ARGS_ARG (   n1,
  n2 
)    (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2))

Function takes n1 mandatory arguments and n2 optional arguments.

Parameters
n1The number of required arguments.
n2The number of optional arguments.

◆ MRB_ARGS_OPT

#define MRB_ARGS_OPT (   n)    ((mrb_aspec)((n)&0x1f) << 13)

Function takes n optional arguments.

Parameters
nThe number of optional arguments.

◆ MRB_ARGS_REQ

#define MRB_ARGS_REQ (   n)    ((mrb_aspec)((n)&0x1f) << 18)

Function requires n arguments.

Parameters
nThe number of required arguments.

◆ mrb_field_write_barrier_value

#define mrb_field_write_barrier_value (   mrb,
  obj,
  val 
)
Value:
do{\
if (!mrb_immediate_p(val)) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \
} while (0)

◆ mrb_gc_mark_value

#define mrb_gc_mark_value (   mrb,
  val 
)
Value:
do {\
if (!mrb_immediate_p(val)) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \
} while (0)

Typedef Documentation

◆ mrb_allocf

typedef void*(* mrb_allocf) (struct mrb_state *mrb, void *, size_t, void *ud)

Function pointer type of custom allocator used in.

See also
mrb_open_allocf.

The function pointing it must behave similarly as realloc except:

  • If ptr is NULL it must allocate new space.
  • If s is NULL, ptr must be freed.

See

See also
mrb_default_allocf for the default implementation.

◆ mrb_args_format

typedef const char* mrb_args_format

Format specifiers for {mrb_get_args} function.

Must be a C string composed of the following format specifiers:

char Ruby type C types Notes
o {Object} {mrb_value} Could be used to retrieve any type of argument
C {Class}/{Module} {mrb_value}
S {String} {mrb_value} when ! follows, the value may be nil
A {Array} {mrb_value} when ! follows, the value may be nil
H {Hash} {mrb_value} when ! follows, the value may be nil
s {String} char *, {mrb_int} Receive two arguments; s! gives (NULL,0) for nil
z {String} char * NULL terminated string; z! gives NULL for nil
a {Array} {mrb_value} *, {mrb_int} Receive two arguments; a! gives (NULL,0) for nil
f {Fixnum}/{Float} {mrb_float}
i {Fixnum}/{Float} {mrb_int}
b boolean {mrb_bool}
n {String}/{Symbol} {mrb_sym}
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
I inline struct void *
& block {mrb_value} &! raises exception if no block given.
* rest arguments {mrb_value} *, {mrb_int} Receive the rest of arguments as an array; *! avoid copy of the stack.
| optional After this spec following specs would be optional.
? optional given {mrb_bool} TRUE if preceding argument is given. Used to check optional argument is given.
See also
mrb_get_args

◆ mrb_func_t

typedef mrb_value(* mrb_func_t) (struct mrb_state *mrb, mrb_value self)

Function pointer type for a function callable by mruby.

The arguments to the function are stored on the mrb_state. To get them see mrb_get_args

Parameters
mrbThe mruby state
selfThe self object
Returns
[mrb_value] The function's return value

Function Documentation

◆ mrb_class_defined()

mrb_bool mrb_class_defined ( mrb_state mrb,
const char *  name 
)

Returns an mrb_bool.

True if class was defined, and false if the class was not defined.

Example: void mrb_example_gem_init(mrb_state* mrb) { struct RClass *example_class; mrb_bool cd;

example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); cd = mrb_class_defined(mrb, "ExampleClass");

// If mrb_class_defined returns 1 then puts "True" // If mrb_class_defined returns 0 then puts "False" if (cd == 1){ puts("True"); } else { puts("False"); } }

Parameters
mrbThe current mruby state.
nameA string representing the name of the class.
Returns
[mrb_bool] A boolean value.

◆ mrb_class_defined_under()

mrb_bool mrb_class_defined_under ( mrb_state mrb,
struct RClass outer,
const char *  name 
)

Returns an mrb_bool.

True if inner class was defined, and false if the inner class was not defined.

Example: void mrb_example_gem_init(mrb_state* mrb) { struct RClass *example_outer, *example_inner; mrb_bool cd;

example_outer = mrb_define_module(mrb, "ExampleOuter");

example_inner = mrb_define_class_under(mrb, example_outer, "ExampleInner", mrb->object_class); cd = mrb_class_defined_under(mrb, example_outer, "ExampleInner");

// If mrb_class_defined_under returns 1 then puts "True" // If mrb_class_defined_under returns 0 then puts "False" if (cd == 1){ puts("True"); } else { puts("False"); } }

Parameters
mrbThe current mruby state.
outerThe name of the outer class.
nameA string representing the name of the inner class.
Returns
[mrb_bool] A boolean value.

◆ mrb_class_get()

struct RClass* mrb_class_get ( mrb_state mrb,
const char *  name 
)

Gets a class.

Parameters
mrbThe current mruby state.
nameThe name of the class.
Returns
[struct RClass *] A reference to the class.

◆ mrb_class_get_under()

struct RClass* mrb_class_get_under ( mrb_state mrb,
struct RClass outer,
const char *  name 
)

Gets a child class.

Parameters
mrbThe current mruby state.
outerThe name of the parent class.
nameThe name of the class.
Returns
[struct RClass *] A reference to the class.

◆ mrb_class_new()

struct RClass* mrb_class_new ( mrb_state mrb,
struct RClass super 
)

Creates a new instance of Class, Class.

Example:

 void
 mrb_example_gem_init(mrb_state* mrb) {
   struct RClass *example_class;

   mrb_value obj;
   example_class = mrb_class_new(mrb, mrb->object_class);
   obj = mrb_obj_new(mrb, example_class, 0, NULL); // => #<#<Class:0x9a945b8>:0x9a94588>
   mrb_p(mrb, obj); // => Kernel#p
  }
Parameters
mrbThe current mruby state.
superThe super class or parent.
Returns
[struct RClass *] Reference to the new class.

Creates a new class.

Parameters
supera class from which the new class derives.
Exceptions
TypeErrorsuper is not inheritable.
TypeErrorsuper is the Class class.

◆ mrb_class_new_instance()

static mrb_value mrb_class_new_instance ( mrb_state mrb,
mrb_int  argc,
const mrb_value argv,
struct RClass c 
)
inlinestatic
See also
mrb_obj_new

◆ mrb_close()

void mrb_close ( mrb_state mrb)

Closes and frees a mrb_state.

Parameters
mrbPointer to the mrb_state to be closed.

◆ mrb_default_allocf()

void* mrb_default_allocf ( mrb_state ,
void *  ,
size_t  ,
void *   
)

The default allocation function.

See also
mrb_allocf

◆ mrb_define_alias()

void mrb_define_alias ( mrb_state mrb,
struct RClass klass,
const char *  name1,
const char *  name2 
)

Defines an alias of a method.

Parameters
mrbthe mruby state
klassthe class which the original method belongs to
name1a new name for the method
name2the original name of the method

◆ mrb_define_class()

struct RClass* mrb_define_class ( mrb_state mrb,
const char *  name,
struct RClass super 
)

Defines a new class.

If you're creating a gem it may look something like this:

 !!!c
 void mrb_example_gem_init(mrb_state* mrb) {
     struct RClass *example_class;
     example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
 }

 void mrb_example_gem_final(mrb_state* mrb) {
     //free(TheAnimals);
 }
Parameters
mrbThe current mruby state.
nameThe name of the defined class.
superThe new class parent.
Returns
[struct RClass *] Reference to the newly defined class.
See also
mrb_define_class_under

◆ mrb_define_class_method()

void mrb_define_class_method ( mrb_state mrb,
struct RClass cla,
const char *  name,
mrb_func_t  fun,
mrb_aspec  aspec 
)

Defines a class method.

Example:

# Ruby style
class Foo
  def Foo.bar
  end
end
// C style
mrb_value bar_method(mrb_state* mrb, mrb_value self){
  return mrb_nil_value();
}
void mrb_example_gem_init(mrb_state* mrb){
  struct RClass *foo;
  foo = mrb_define_class(mrb, "Foo", mrb->object_class);
  mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
}
Parameters
mrbThe MRuby state reference.
claThe class where the class method will be defined.
nameThe name of the class method being defined.
funThe function pointer to the class method definition.
aspecThe method parameters declaration.

◆ mrb_define_class_under()

struct RClass* mrb_define_class_under ( mrb_state mrb,
struct RClass outer,
const char *  name,
struct RClass super 
)

Defines a new class under a given module.

Parameters
mrbThe current mruby state.
outerReference to the module under which the new class will be defined
nameThe name of the defined class
superThe new class parent
Returns
[struct RClass *] Reference to the newly defined class
See also
mrb_define_class

Defines a class under the namespace of outer.

Parameters
outera class which contains the new class.
namename of the new class
supera class from which the new class will derive. NULL means Object class.
Returns
the created class
Exceptions
TypeErrorif the constant name name is already taken but the constant is not a Class.
NameErrorif the class is already defined but the class can not be reopened because its superclass is not super.
Postcondition
top-level constant named name refers the returned class.
Note
if a class named name is already defined and its superclass is super, the function just returns the defined class.

◆ mrb_define_const()

void mrb_define_const ( mrb_state mrb,
struct RClass cla,
const char *  name,
mrb_value  val 
)

Defines a constant.

Example:

     # Ruby style
     class ExampleClass
       AGE = 22
     end
     // C style
     #include <stdio.h>
     #include <mruby.h>

     void
     mrb_example_gem_init(mrb_state* mrb){
       mrb_define_const(mrb, mrb->kernel_module, "AGE", mrb_fixnum_value(22));
     }

     mrb_value
     mrb_example_gem_final(mrb_state* mrb){
     }
Parameters
mrbThe MRuby state reference.
claA class or module the constant is defined in.
nameThe name of the constant being defined.
valThe value for the constant.

◆ mrb_define_method()

void mrb_define_method ( mrb_state mrb,
struct RClass cla,
const char *  name,
mrb_func_t  func,
mrb_aspec  aspec 
)

Defines a global function in ruby.

If you're creating a gem it may look something like this

Example:

mrb_value example_method(mrb_state* mrb, mrb_value self)
{
     puts("Executing example command!");
     return self;
}

void mrb_example_gem_init(mrb_state* mrb)
{
      mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
}
Parameters
mrbThe MRuby state reference.
claThe class pointer where the method will be defined.
nameThe name of the method being defined.
funcThe function pointer to the method definition.
aspecThe method parameters declaration.

◆ mrb_define_module()

struct RClass* mrb_define_module ( mrb_state mrb,
const char *  name 
)

Defines a new module.

Parameters
mrbThe current mruby state.
nameThe name of the module.
Returns
[struct RClass *] Reference to the newly defined module.

◆ mrb_define_module_function()

void mrb_define_module_function ( mrb_state mrb,
struct RClass cla,
const char *  name,
mrb_func_t  fun,
mrb_aspec  aspec 
)

Defines a module function.

Example:

   # Ruby style
   module Foo
     def Foo.bar
     end
   end
   // C style
   mrb_value bar_method(mrb_state* mrb, mrb_value self){
     return mrb_nil_value();
   }
   void mrb_example_gem_init(mrb_state* mrb){
     struct RClass *foo;
     foo = mrb_define_module(mrb, "Foo");
     mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
   }
Parameters
mrbThe MRuby state reference.
claThe module where the module function will be defined.
nameThe name of the module function being defined.
funThe function pointer to the module function definition.
aspecThe method parameters declaration.

◆ mrb_define_singleton_method()

void mrb_define_singleton_method ( mrb_state mrb,
struct RObject cla,
const char *  name,
mrb_func_t  fun,
mrb_aspec  aspec 
)

Defines a singleton method.

See also
mrb_define_class_method

◆ mrb_exc_get()

struct RClass* mrb_exc_get ( mrb_state mrb,
const char *  name 
)

Gets a exception class.

Parameters
mrbThe current mruby state.
nameThe name of the class.
Returns
[struct RClass *] A reference to the class.

◆ mrb_fiber_alive_p()

mrb_value mrb_fiber_alive_p ( mrb_state mrb,
mrb_value  fib 
)

Check if a Fiber is alive.

Implemented in mruby-fiber

◆ mrb_fiber_resume()

mrb_value mrb_fiber_resume ( mrb_state mrb,
mrb_value  fib,
mrb_int  argc,
const mrb_value argv 
)

Resume a Fiber.

Implemented in mruby-fiber

◆ mrb_fiber_yield()

mrb_value mrb_fiber_yield ( mrb_state mrb,
mrb_int  argc,
const mrb_value argv 
)

Yield a Fiber.

Implemented in mruby-fiber

◆ mrb_funcall()

mrb_value mrb_funcall ( mrb_state mrb,
mrb_value  val,
const char *  name,
mrb_int  argc,
  ... 
)

Call existing ruby functions.

Example:

 #include <stdio.h>
 #include <mruby.h>
 #include "mruby/compile.h"

 int
 main()
 {
   mrb_int i = 99;
   mrb_state *mrb = mrb_open();

   if (!mrb) { }
   FILE *fp = fopen("test.rb","r");
   mrb_value obj = mrb_load_file(mrb,fp);
   mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
   fclose(fp);
   mrb_close(mrb);
 }
Parameters
mrbThe current mruby state.
valA reference to an mruby value.
nameThe name of the method.
argcThe number of arguments the method has.
...Variadic values(not type safe!).
Returns
[mrb_value] mruby function value.

◆ mrb_funcall_argv()

mrb_value mrb_funcall_argv ( mrb_state mrb,
mrb_value  val,
mrb_sym  name_sym,
mrb_int  argc,
const mrb_value obj 
)

Call existing ruby functions.

This is basically the type safe version of mrb_funcall.

#include <stdio.h>
#include <mruby.h>
#include "mruby/compile.h"
int
main()
{
  mrb_int i = 99;
  mrb_state *mrb = mrb_open();

  if (!mrb) { }
  mrb_sym m_sym = mrb_intern_lit(mrb, "method_name"); // Symbol for method.

  FILE *fp = fopen("test.rb","r");
  mrb_value obj = mrb_load_file(mrb,fp);
  mrb_funcall_argv(mrb, obj, m_sym, 1, &obj); // Calling ruby function from test.rb.
  fclose(fp);
  mrb_close(mrb);
 }
Parameters
mrbThe current mruby state.
valA reference to an mruby value.
name_symThe symbol representing the method.
argcThe number of arguments the method has.
objPointer to the object.
Returns
[mrb_value] mrb_value mruby function value.
See also
mrb_funcall

◆ mrb_get_argc()

mrb_int mrb_get_argc ( mrb_state mrb)

Retrieve number of arguments from mrb_state.

Correctly handles *splat arguments.

◆ mrb_get_args()

mrb_int mrb_get_args ( mrb_state mrb,
mrb_args_format  format,
  ... 
)

Retrieve arguments from mrb_state.

Parameters
mrbThe current MRuby state.
formatis a list of format specifiers
...The passing variadic arguments must be a pointer of retrieving type.
Returns
the number of arguments retrieved.
See also
mrb_args_format

◆ mrb_include_module()

void mrb_include_module ( mrb_state mrb,
struct RClass cla,
struct RClass included 
)

Include a module in another class or module.

Equivalent to:

module B include A end

Parameters
mrbThe current mruby state.
claA reference to module or a class.
includedA reference to the module to be included.

◆ mrb_intern_cstr()

mrb_sym mrb_intern_cstr ( mrb_state mrb,
const char *  str 
)

Create a symbol.

Example:

# Ruby style:
:pizza # => :pizza

// C style:
mrb_sym m_sym = mrb_intern_lit(mrb, "pizza"); //  => :pizza
Parameters
mrbThe current mruby state.
strThe string to be symbolized
Returns
[mrb_sym] mrb_sym A symbol.

◆ mrb_module_get()

struct RClass* mrb_module_get ( mrb_state mrb,
const char *  name 
)

Gets a module.

Parameters
mrbThe current mruby state.
nameThe name of the module.
Returns
[struct RClass *] A reference to the module.

◆ mrb_module_get_under()

struct RClass* mrb_module_get_under ( mrb_state mrb,
struct RClass outer,
const char *  name 
)

Gets a module defined under another module.

Parameters
mrbThe current mruby state.
outerThe name of the outer module.
nameThe name of the module.
Returns
[struct RClass *] A reference to the module.

◆ mrb_module_new()

struct RClass* mrb_module_new ( mrb_state mrb)

Creates a new module, Module.

Example: void mrb_example_gem_init(mrb_state* mrb) { struct RClass *example_module;

example_module = mrb_module_new(mrb); }

Parameters
mrbThe current mruby state.
Returns
[struct RClass *] Reference to the new module.

Creates a new module.

◆ mrb_obj_dup()

mrb_value mrb_obj_dup ( mrb_state mrb,
mrb_value  obj 
)

Duplicate an object.

Equivalent to: Object::dup

Parameters
mrbThe current mruby state.
objObject to be duplicate.
Returns
[mrb_value] The newly duplicated object.

◆ mrb_obj_new()

mrb_value mrb_obj_new ( mrb_state mrb,
struct RClass c,
mrb_int  argc,
const mrb_value argv 
)

Initialize a new object instance of c class.

Example:

# Ruby style
class ExampleClass
end

p ExampleClass # => #<ExampleClass:0x9958588>
// C style
#include <stdio.h>
#include <mruby.h>

void
mrb_example_gem_init(mrb_state* mrb) {
  struct RClass *example_class;
  mrb_value obj;
  example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); # => class ExampleClass; end
  obj = mrb_obj_new(mrb, example_class, 0, NULL); # => ExampleClass.new
  mrb_p(mrb, obj); // => Kernel#p
 }
Parameters
mrbThe current mruby state.
cReference to the class of the new object.
argcNumber of arguments in argv
argvArray of mrb_value to initialize the object
Returns
[mrb_value] The newly initialized object

◆ mrb_obj_respond_to()

mrb_bool mrb_obj_respond_to ( mrb_state mrb,
struct RClass c,
mrb_sym  mid 
)

Returns true if obj responds to the given method.

If the method was defined for that class it returns true, it returns false otherwise.

 Example:
 # Ruby style
 class ExampleClass
   def example_method
   end
 end

 ExampleClass.new.respond_to?(:example_method) # => true

 // C style
 void
 mrb_example_gem_init(mrb_state* mrb) {
   struct RClass *example_class;
   mrb_sym mid;
   mrb_bool obj_resp;

   example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
   mrb_define_method(mrb, example_class, "example_method", exampleMethod, MRB_ARGS_NONE());
   mid = mrb_intern_str(mrb, mrb_str_new_lit(mrb, "example_method" ));
   obj_resp = mrb_obj_respond_to(mrb, example_class, mid); // => 1(true in Ruby world)

   // If mrb_obj_respond_to returns 1 then puts "True"
   // If mrb_obj_respond_to returns 0 then puts "False"
   if (obj_resp == 1) {
     puts("True");
   }
   else if (obj_resp == 0) {
     puts("False");
   }
 }
Parameters
mrbThe current mruby state.
cA reference to a class.
midA symbol referencing a method id.
Returns
[mrb_bool] A boolean value.

◆ mrb_open()

mrb_state* mrb_open ( void  )

Creates new mrb_state.

Returns
Pointer to the newly created mrb_state.

◆ mrb_open_allocf()

mrb_state* mrb_open_allocf ( mrb_allocf  f,
void *  ud 
)

Create new mrb_state with custom allocators.

Parameters
fReference to the allocation function.
udUser data will be passed to custom allocator f. If user data isn't required just pass NULL.
Returns
Pointer to the newly created mrb_state.

◆ mrb_open_core()

mrb_state* mrb_open_core ( mrb_allocf  f,
void *  ud 
)

Create new mrb_state with just the MRuby core.

Parameters
fReference to the allocation function. Use mrb_default_allocf for the default
udUser data will be passed to custom allocator f. If user data isn't required just pass NULL.
Returns
Pointer to the newly created mrb_state.

◆ mrb_prepend_module()

void mrb_prepend_module ( mrb_state mrb,
struct RClass cla,
struct RClass prepended 
)

Prepends a module in another class or module.

Equivalent to: module B prepend A end

Parameters
mrbThe current mruby state.
claA reference to module or a class.
prependedA reference to the module to be prepended.

◆ mrb_undef_class_method()

void mrb_undef_class_method ( mrb_state mrb,
struct RClass cls,
const char *  name 
)

Undefine a class method.

Example:

 # Ruby style
 class ExampleClass
   def self.example_method
     "example"
   end
 end

ExampleClass.example_method

// C style
#include <stdio.h>
#include <mruby.h>

mrb_value
mrb_example_method(mrb_state *mrb){
  return mrb_str_new_lit(mrb, "example");
}

void
mrb_example_gem_init(mrb_state* mrb){
  struct RClass *example_class;
  example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
  mrb_define_class_method(mrb, example_class, "example_method", mrb_example_method, MRB_ARGS_NONE());
  mrb_undef_class_method(mrb, example_class, "example_method");
 }

 void
 mrb_example_gem_final(mrb_state* mrb){
 }
Parameters
mrbThe mruby state reference.
clsA class the class method will be undefined from.
nameThe name of the class method to be undefined.

◆ mrb_undef_method()

void mrb_undef_method ( mrb_state mrb,
struct RClass cla,
const char *  name 
)

Undefines a method.

Example:

# Ruby style

class ExampleClassA
  def example_method
    "example"
  end
end
ExampleClassA.new.example_method # => example

class ExampleClassB < ExampleClassA
  undef_method :example_method
end

ExampleClassB.new.example_method # => undefined method 'example_method' for ExampleClassB (NoMethodError)

// C style
#include <stdio.h>
#include <mruby.h>

mrb_value
mrb_example_method(mrb_state *mrb){
  return mrb_str_new_lit(mrb, "example");
}

void
mrb_example_gem_init(mrb_state* mrb){
  struct RClass *example_class_a;
  struct RClass *example_class_b;
  struct RClass *example_class_c;

  example_class_a = mrb_define_class(mrb, "ExampleClassA", mrb->object_class);
  mrb_define_method(mrb, example_class_a, "example_method", mrb_example_method, MRB_ARGS_NONE());
  example_class_b = mrb_define_class(mrb, "ExampleClassB", example_class_a);
  example_class_c = mrb_define_class(mrb, "ExampleClassC", example_class_b);
  mrb_undef_method(mrb, example_class_c, "example_method");
}

mrb_example_gem_final(mrb_state* mrb){
}
Parameters
mrbThe mruby state reference.
claThe class the method will be undefined from.
nameThe name of the method to be undefined.