Header: mruby.h
Overview
MRuby C API entry point
Function Summary collapse
-
struct RClass * mrb_define_class(mrb_state *, const char *, struct RClass *)
Defines a new class.
-
struct RClass * mrb_define_module(mrb_state *, const char *)
Defines a new module.
-
mrb_value mrb_singleton_class(mrb_state *, mrb_value)
-
void mrb_include_module(mrb_state *, struct RClass *, struct RClass *)
Include a module in another class or module.
-
void mrb_prepend_module(mrb_state *, struct RClass *, struct RClass *)
Prepends a module in another class or module.
-
void mrb_define_method(mrb_state *, struct RClass *, const char *, mrb_func_t, mrb_aspec)
Defines a global function in ruby.
-
void mrb_define_class_method(mrb_state *, struct RClass *, const char *, mrb_func_t, mrb_aspec)
Defines a class method.
-
void mrb_define_singleton_method(mrb_state *, struct RObject *, const char *, mrb_func_t, mrb_aspec)
Defines a singleton method.
-
void mrb_define_module_function(mrb_state *, struct RClass *, const char *, mrb_func_t, mrb_aspec)
Defines a module function.
-
void mrb_define_const(mrb_state*, struct RClass*, const char *, mrb_value)
Defines a constant.
-
void mrb_undef_method(mrb_state *, struct RClass *, const char *)
Undefines a method.
-
void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym)
-
void mrb_undef_class_method(mrb_state *, struct RClass *, const char *)
Undefine a class method.
-
mrb_value mrb_obj_new(mrb_state *, struct RClass *, mrb_int, const mrb_value *)
Initialize a new object instance of c class.
-
mrb_value mrb_class_new_instance(mrb_state *, mrb_int, const mrb_value *, struct RClass *)
-
struct RClass * mrb_class_new(mrb_state *, struct RClass *)
Creates a new instance of Class, Class.
-
struct RClass * mrb_module_new(mrb_state *)
Creates a new module, Module.
-
mrb_bool mrb_class_defined(mrb_state *, const char *)
Returns an mrb_bool.
-
struct RClass * mrb_class_get(mrb_state *, const char *)
Gets a class.
-
struct RClass * mrb_exc_get(mrb_state *, const char *)
Gets a exception class.
-
mrb_bool mrb_class_defined_under(mrb_state *, struct RClass *, const char *)
Returns an mrb_bool.
-
struct RClass * mrb_class_get_under(mrb_state *, struct RClass *, const char *)
Gets a child class.
-
struct RClass * mrb_module_get(mrb_state *, const char *)
Gets a module.
-
struct RClass * mrb_module_get_under(mrb_state *, struct RClass *, const char *)
Gets a module defined under another module.
-
void mrb_notimplement(mrb_state*)
a function to raise NotImplementedError with current method name.
-
mrb_value mrb_notimplement_m(mrb_state*, mrb_value)
a function to be replacement of unimplemented method.
-
mrb_value mrb_obj_dup(mrb_state *, mrb_value)
Duplicate an object.
-
mrb_bool mrb_obj_respond_to(mrb_state *, struct RClass*, mrb_sym)
Returns true if obj responds to the given method.
-
struct RClass * mrb_define_class_under(mrb_state *, struct RClass *, const char *, struct RClass *)
Defines a new class under a given module.
-
struct RClass * mrb_define_module_under(mrb_state *, struct RClass *, const char *)
-
mrb_int mrb_get_args(mrb_state *, mrb_args_format, ...)
Retrieve arguments from mrb_state.
-
mrb_sym mrb_get_mid(mrb_state *)
get method symbol.
-
mrb_int mrb_get_argc(mrb_state *)
Retrieve number of arguments from mrb_state.
-
mrb_value* mrb_get_argv(mrb_state *)
-
mrb_value mrb_funcall(mrb_state *, mrb_value, const char *, mrb_int, ...)
Call existing ruby functions.
-
mrb_value mrb_funcall_argv(mrb_state *, mrb_value, mrb_sym, mrb_int, const mrb_value *)
Call existing ruby functions.
-
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 *, const char*)
Create a symbol.
-
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)
-
mrb_value mrb_sym_str(mrb_state*, mrb_sym)
-
void * mrb_malloc(mrb_state*, size_t)
raise RuntimeError if no mem.
-
void * mrb_calloc(mrb_state*, size_t, size_t)
ditto.
-
void * mrb_realloc(mrb_state*, void*, size_t)
ditto.
-
void * mrb_realloc_simple(mrb_state*, void*, size_t)
return NULL if no memory available.
-
void * mrb_malloc_simple(mrb_state*, size_t)
return NULL if no memory available.
-
struct RBasic * mrb_obj_alloc(mrb_state*, enum, struct RClass*)
return NULL if no memory available.
-
void mrb_free(mrb_state*, void*)
-
mrb_value mrb_str_new(mrb_state *, const char *, size_t)
-
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 *, const char *, size_t)
-
mrb_value mrb_obj_freeze(mrb_state*, mrb_value)
-
mrb_state* mrb_open(void)
Creates new mrb_state.
-
mrb_state* mrb_open_allocf(mrb_allocf, void *)
Create new mrb_state with custom allocators.
-
mrb_state* mrb_open_core(mrb_allocf, void *)
Create new mrb_state with just the MRuby core.
-
void mrb_close(mrb_state *)
Closes and frees a mrb_state.
-
void* mrb_default_allocf(mrb_state*, void*, size_t, void*)
The default allocation function.
-
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, stack_keep)
-
mrb_value mrb_vm_run(mrb_state *, struct RProc *, mrb_value, unsigned, stack_keep)
-
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)
-
mrb_sym mrb_obj_to_sym(mrb_state *, mrb_value)
-
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_value, mrb_value)
-
mrb_value mrb_convert_to_integer(mrb_state *, mrb_value, mrb_int)
-
mrb_value mrb_Integer(mrb_state *, mrb_value)
-
mrb_value mrb_Float(mrb_state *, mrb_value)
-
mrb_value mrb_inspect(mrb_state *, mrb_value)
-
mrb_bool mrb_eql(mrb_state *, mrb_value, mrb_value)
-
mrb_int mrb_cmp(mrb_state *, mrb_value, mrb_value)
mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error.
-
int mrb_gc_arena_save(mrb_state *)
-
void mrb_gc_arena_restore(mrb_state *, int)
-
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_value, enum, type, const char *, const char *)
-
mrb_value mrb_any_to_s(mrb_state *, mrb_value)
-
struct RClass* mrb_obj_class(mrb_state *, mrb_value)
-
mrb_value mrb_class_path(mrb_state *, struct RClass *)
-
mrb_value mrb_convert_type(mrb_state *, mrb_value, enum, type, const char *, const char *)
-
mrb_bool mrb_obj_is_kind_of(mrb_state *, mrb_value, struct RClass *)
-
mrb_value mrb_obj_inspect(mrb_state *, mrb_value)
-
mrb_value mrb_obj_clone(mrb_state *, mrb_value)
-
mrb_value mrb_exc_new(mrb_state *, struct RClass *, const char *, size_t)
-
mrb_noreturn mrb_exc_raise(mrb_state *, mrb_value)
-
mrb_noreturn mrb_raise(mrb_state *, struct RClass *, const char *)
-
mrb_noreturn mrb_raisef(mrb_state *, struct RClass *, const char *, ...)
-
mrb_noreturn mrb_name_error(mrb_state *, mrb_sym, const char *, ...)
-
mrb_noreturn mrb_frozen_error(mrb_state *, void *)
-
void mrb_warn(mrb_state *, const char *, ...)
-
mrb_noreturn mrb_bug(mrb_state *, const char *, ...)
-
void mrb_print_backtrace(mrb_state *)
-
void mrb_print_error(mrb_state *)
-
mrb_value mrb_vformat(mrb_state *, const char *, va_list)
function for
raisef
formatting. -
mrb_value mrb_yield(mrb_state *, mrb_value, mrb_value)
-
mrb_value mrb_yield_argv(mrb_state *, mrb_value, mrb_int, const mrb_value *)
-
mrb_value mrb_yield_with_class(mrb_state *, mrb_value, mrb_int, const mrb_value *, mrb_value, struct RClass *)
-
void mrb_gc_protect(mrb_state *, mrb_value)
mrb_gc_protect() leaves the object in the arena.
-
void mrb_gc_register(mrb_state *, mrb_value)
mrb_gc_register() keeps the object from GC.
-
void mrb_gc_unregister(mrb_state *, mrb_value)
mrb_gc_unregister() removes the object from GC root.
-
mrb_value mrb_to_int(mrb_state *, mrb_value)
-
mrb_value mrb_to_str(mrb_state *, mrb_value)
string type checking (contrary to the name, it doesn’t convert).
-
void mrb_check_type(mrb_state *, mrb_value, enum, t)
-
void mrb_check_frozen(mrb_state *, void *)
-
void mrb_define_alias(mrb_state *, struct RClass *, const char *, const char *)
-
void mrb_define_global_const(mrb_state *, const char *, mrb_value)
-
mrb_value mrb_attr_get(mrb_state *, mrb_value, mrb_sym)
-
mrb_bool mrb_respond_to(mrb_state *, mrb_value, mrb_sym)
-
mrb_bool mrb_obj_is_instance_of(mrb_state *, mrb_value, struct RClass*)
-
mrb_bool mrb_func_basic_p(mrb_state *, mrb_value, mrb_sym, mrb_func_t)
-
mrb_value mrb_fiber_resume(mrb_state *, mrb_value, mrb_int, const mrb_value *)
Resume a Fiber.
-
mrb_value mrb_fiber_yield(mrb_state *, mrb_int, const mrb_value *)
Yield a Fiber.
-
mrb_value mrb_fiber_alive_p(mrb_state *, mrb_value)
Check if a Fiber is alive.
-
void mrb_stack_extend(mrb_state*, mrb_int)
-
struct mrb_pool* mrb_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, size_t)
-
mrb_bool mrb_pool_can_realloc(struct mrb_pool*, void*, size_t)
-
void* mrb_alloca(mrb_state *, size_t)
temporary memory allocation, only effective while GC arena is kept.
-
void mrb_state_atexit(mrb_state *, mrb_atexit_func)
-
void mrb_show_version(mrb_state *)
-
void mrb_show_copyright(mrb_state *)
-
mrb_value mrb_format(mrb_state *, const char *, ...)
Define Summary
- #define MRUBY_H
- #define __STDC_LIMIT_MACROS
- #define __STDC_CONSTANT_MACROS
- #define __STDC_FORMAT_MACROS
- #define SIZE_MAX
- #define mrb_assert
- #define mrb_assert_int_fit
- #define mrb_static_assert
- #define FLT_EPSILON
- #define DBL_EPSILON
- #define LDBL_EPSILON
- #define MRB_FLOAT_EPSILON
- #define MRB_FIXED_STATE_ATEXIT_STACK_SIZE
- #define MRB_METHOD_CACHE
- #define MRB_METHOD_CACHE_SIZE
cache size needs to be power of 2
- #define MRB_ARGS_REQ
Function requires n arguments.
- #define MRB_ARGS_OPT
Function takes n optional arguments
- #define MRB_ARGS_ARG
Function takes n1 mandatory arguments and n2 optional arguments
- #define MRB_ARGS_REST
rest argument
- #define MRB_ARGS_POST
required arguments after rest
- #define MRB_ARGS_KEY
keyword arguments (n of keys, kdict)
- #define MRB_ARGS_BLOCK
Function takes a block argument
- #define MRB_ARGS_ANY
Function accepts any number of arguments
- #define MRB_ARGS_NONE
Function accepts no arguments
- #define mrb_strlen_lit
strlen
for character string literals (use with caution orstrlen
instead) Adjacent string literals are concatenated in C/C++ in translation phase 6. Iflit
is not one, the compiler will report a syntax error: MSVC: “error C2143: syntax error : missing ‘)’ before ‘string’” GCC: “error: expected ‘)’ before string constant” - #define mrb_intern_lit
- #define mrb_sym2name
- #define mrb_sym2name_len
- #define mrb_sym2str
- #define mrb_str_new_lit
- #define mrb_str_new_frozen
- #define mrb_str_new_cstr_frozen
- #define mrb_str_new_static_frozen
- #define mrb_str_new_lit_frozen
- #define mrb_locale_free
- #define mrb_utf8_free
- #define mrb_utf8_from_locale
- #define mrb_locale_from_utf8
- #define mrb_toplevel_run_keep
compatibility macros
- #define mrb_toplevel_run
- #define mrb_context_run
- #define mrb_gc_mark_value
- #define mrb_field_write_barrier_value
- #define ISASCII
- #define ISPRINT
- #define ISSPACE
- #define ISLOWER
- #define ISALPHA
- #define ISDIGIT
- #define ISXDIGIT
- #define ISALNUM
- #define ISBLANK
- #define TOUPPER
- #define TOLOWER
- #define E_RUNTIME_ERROR
macros to get typical exception objects note: + those E_* macros requires mrb_state* variable named mrb. + exception objects obtained from those macros are local to mrb
- #define E_TYPE_ERROR
- #define E_ARGUMENT_ERROR
- #define E_INDEX_ERROR
- #define E_RANGE_ERROR
- #define E_NAME_ERROR
- #define E_NOMETHOD_ERROR
- #define E_SCRIPT_ERROR
- #define E_SYNTAX_ERROR
- #define E_LOCALJUMP_ERROR
- #define E_REGEXP_ERROR
- #define E_FROZEN_ERROR
- #define E_NOTIMP_ERROR
- #define E_FLOATDOMAIN_ERROR
- #define E_KEY_ERROR
- #define mrb_int
- #define E_FIBER_ERROR
FiberError reference
Implemented in mruby-fiber
- #define memcpy
- #define memset
Typedef Summary
- typedef mrb_code
MRuby C API entry point
- typedef mrb_aspec
\class mrb_aspec
Specifies the number of arguments a function takes
Example:
MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1)
for a method that expects 2..3 arguments - typedef mrb_method_t
- typedef 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 benil
A
Array mrb_value when !
follows, the value may benil
H
Hash mrb_value when !
follows, the value may benil
s
String char *, mrb_int Receive two arguments; s!
gives (NULL
,0
) fornil
z
String char * NULL
terminated string;z!
givesNULL
fornil
a
Array mrb_value *, mrb_int Receive two arguments; a!
gives (NULL
,0
) fornil
f
Fixnum/Float mrb_float i
Fixnum/Float mrb_int b
boolean mrb_bool n
String/Symbol mrb_sym d
data void *, mruby.h.mrb_data_type const 2nd argument will be used to check data type so it won’t be modified; when !
follows, the value may benil
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.:
keyword args mrb_kwargs const Get keyword arguments. @see mrb_kwargs - typedef mrb_kwargs
Get keyword arguments by
mrb_get_args()
with:
specifier.mrb_kwargs::num
indicates that the number of keyword values.mrb_kwargs::values
is an object array, and the keyword argument corresponding to the string array is assigned. Note thatundef
is assigned if there is no keyword argument corresponding tomrb_kwargs::optional
.mrb_kwargs::table
accepts a string array.mrb_kwargs::required
indicates that the specified number of keywords starting from the beginning of the string array are required.mrb_kwargs::rest
is the remaining keyword argument that can be accepted as**rest
in Ruby. IfNULL
is specified,ArgumentError
is raised when there is an undefined keyword.Examples:
// def method(a: 1, b: 2) uint32_t kw_num = 2; const char *kw_names[kw_num] = { "a", "b" }; uint32_t kw_required = 0; mrb_value kw_values[kw_num]; const mrb_kwargs kwargs = { kw_num, kw_values, kw_names, kw_required, NULL }; mrb_get_args(mrb, ":", &kwargs); if (mrb_undef_p(kw_values[0])) { kw_values[0] = mrb_fixnum_value(1); } if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); } // def method(str, x:, y: 2, z: "default string", **opts) mrb_value str, kw_rest; uint32_t kw_num = 3; const char *kw_names[kw_num] = { "x", "y", "z" }; uint32_t kw_required = 1; mrb_value kw_values[kw_num]; const mrb_kwargs kwargs = { kw_num, kw_values, kw_names, kw_required, &kw_rest }; mrb_get_args(mrb, "S:", &str, &kwargs); // or: mrb_get_args(mrb, ":S", &kwargs, &str); if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); } if (mrb_undef_p(kw_values[2])) { kw_values[2] = mrb_str_new_cstr(mrb, "default string"); }
- typedef mrb_pool
memory pool implementation
Function Details
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:
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);
}
struct RClass * mrb_define_module(mrb_state * mrb, const char * name)
Defines a new module.
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. Equivalent to:
module B include A end
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
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());
}
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());
}
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
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());
}
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){
}
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){
}
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. 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){
}
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
}
mrb_value mrb_class_new_instance(mrb_state * mrb, mrb_int argc, const mrb_value * argv, struct RClass * c)
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
}
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);
}
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");
}
}
struct RClass * mrb_class_get(mrb_state * mrb, const char * name)
Gets a class.
struct RClass * mrb_exc_get(mrb_state * mrb, const char * name)
Gets a exception class.
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");
}
}
struct RClass * mrb_class_get_under(mrb_state * mrb, struct RClass * outer, const char * name)
Gets a child class.
struct RClass * mrb_module_get(mrb_state * mrb, const char * name)
Gets a module.
struct RClass * mrb_module_get_under(mrb_state * mrb, struct RClass * outer, const char * name)
Gets a module defined under another module.
void mrb_notimplement(mrb_state* )
a function to raise NotImplementedError with current method name
mrb_value mrb_notimplement_m(mrb_state* , mrb_value )
a function to be replacement of unimplemented method
mrb_value mrb_obj_dup(mrb_state * mrb, mrb_value obj)
Duplicate an object.
Equivalent to: Object#dup
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");
}
}
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
struct RClass * mrb_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.
mrb_sym mrb_get_mid(mrb_state * mrb)
get method symbol
mrb_int mrb_get_argc(mrb_state * mrb)
Retrieve number of arguments from mrb_state.
Correctly handles *splat arguments.
mrb_value* mrb_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.
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);
}
mrb_value mrb_funcall_argv(mrb_state * mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value * argv)
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);
}
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)
Call existing ruby functions with a block.
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
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 )
mrb_value mrb_sym_str(mrb_state* , mrb_sym )
void * mrb_malloc(mrb_state* , size_t )
raise RuntimeError if no mem
void * mrb_calloc(mrb_state* , size_t , size_t )
ditto
void * mrb_realloc(mrb_state* , void* , size_t )
ditto
void * mrb_realloc_simple(mrb_state* , void* , size_t )
return NULL if no memory available
void * mrb_malloc_simple(mrb_state* , size_t )
return NULL if no memory available
struct RBasic * mrb_obj_alloc(mrb_state* , enum mrb_vtype, struct RClass* )
return NULL if no memory available
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_value mrb_obj_freeze(mrb_state* , mrb_value )
mrb_state* mrb_open(void)
Creates new mrb_state.
mrb_state* mrb_open_allocf(mrb_allocf f, void * ud)
Create new mrb_state with custom allocators.
mrb_state* mrb_open_core(mrb_allocf f, void * ud)
Create new mrb_state with just the MRuby core
void mrb_close(mrb_state * mrb)
Closes and frees a mrb_state.
void* mrb_default_allocf(mrb_state* , void* , size_t , void* )
The default allocation function.
mrb_value mrb_top_self(mrb_state * mrb)
mrb_value mrb_run(mrb_state * mrb, struct RProc* proc, mrb_value self)
mrb_value mrb_top_run(mrb_state * mrb, struct RProc * proc, mrb_value self, unsigned int, stack_keep )
mrb_value mrb_vm_run(mrb_state * mrb, struct RProc * proc, mrb_value self, unsigned int, stack_keep )
mrb_value mrb_vm_exec(mrb_state * mrb, struct RProc * proc, const mrb_code * iseq)
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, mrb_value a, mrb_value b)
mrb_bool mrb_obj_equal(mrb_state * mrb, mrb_value a, mrb_value b)
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)
mrb_int mrb_cmp(mrb_state * mrb, mrb_value obj1, mrb_value obj2)
mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error
int mrb_gc_arena_save(mrb_state * mrb)
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)
struct RClass* mrb_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)
mrb_noreturn mrb_exc_raise(mrb_state * mrb, mrb_value exc)
mrb_noreturn mrb_raise(mrb_state * mrb, struct RClass * c, const char * msg)
mrb_noreturn mrb_raisef(mrb_state * mrb, struct RClass * c, const char * fmt, ... )
mrb_noreturn mrb_name_error(mrb_state * mrb, mrb_sym id, const char * fmt, ... )
mrb_noreturn mrb_frozen_error(mrb_state * mrb, void * frozen_obj)
void mrb_warn(mrb_state * mrb, const char * fmt, ... )
mrb_noreturn 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)
function for raisef
formatting
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)
void mrb_gc_protect(mrb_state * mrb, mrb_value obj)
mrb_gc_protect() leaves the object in the arena
void mrb_gc_register(mrb_state * mrb, mrb_value obj)
mrb_gc_register() keeps the object from GC.
void mrb_gc_unregister(mrb_state * mrb, mrb_value obj)
mrb_gc_unregister() removes the object from GC root.
mrb_value mrb_to_int(mrb_state * mrb, mrb_value val)
mrb_value mrb_to_str(mrb_state * mrb, mrb_value val)
string type checking (contrary to the name, it doesn’t convert)
void mrb_check_type(mrb_state * mrb, mrb_value x, enum mrb_vtype, t )
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)
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
Implemented in mruby-fiber
mrb_value mrb_fiber_yield(mrb_state * mrb, mrb_int argc, const mrb_value * argv)
Yield a Fiber
Implemented in mruby-fiber
mrb_value mrb_fiber_alive_p(mrb_state * mrb, mrb_value fib)
Check if a Fiber is alive
Implemented in mruby-fiber
void mrb_stack_extend(mrb_state* , mrb_int )
struct mrb_pool* mrb_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 )
temporary memory allocation, only effective while GC arena is kept