Header: mruby.h

Overview

MRuby C API entry point

Function Summary collapse

Define Summary

#define MRUBY_H
#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE
#define MRB_ARGS_REQ

Function requires n arguments.

#define MRB_ARGS_OPT

Funtion takes n optional arguments

#define MRB_ARGS_ARG

Funtion 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 or strlen instead) Adjacent string literals are concatenated in C/C++ in translation phase 6. If lit 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_str_new_lit
#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_SYSSTACK_ERROR
#define E_NOTIMP_ERROR
#define E_FLOATDOMAIN_ERROR
#define E_KEY_ERROR
#define mrb_int
#define E_FIBER_ERROR

FiberError reference

Required mrbgem: mruby-fiber

#define mrb_assert
#define mrb_assert_int_fit
#define mrb_static_assert

Typedef Summary

typedef mrb_code

MRuby C API entry point

typedef mrb_aspec

Required arguments signature type.

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 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 Float mrb_float
i Integer mrb_int
b boolean mrb_bool
n Symbol mrb_sym
& block mrb_value
* rest arguments mrb_value *, mrb_int Receive the rest of arguments as an array.
| 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:

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);
 }

Parameters:

  • mrb (mrb_state *)

    The current mruby state.

  • name (const char *)

    The name of the defined class.

  • super (struct RClass *)

    The new class parent.

Returns:

  • (struct RClass *)

    Reference to the newly defined class.

See Also:

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

Defines a new module.

Parameters:

  • mrb_state* (mrb_state *)

    The current mruby state.

  • char* (const char *)

    The name of the module.

Returns:

  • (struct RClass *)

    Reference to the newly defined 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. Equivalent to:

module B include A end

Parameters:

  • mrb_state* (mrb_state *)

    The current mruby state.

  • RClass* (struct RClass *)

    A reference to module or a class.

  • RClass* (struct RClass *)

    A reference to the module to be included.

void mrb_prepend_module(mrb_state* , struct RClass* , struct RClass* )

Prepends a module in another class or module.

Equivalent to: module B prepend A end

Parameters:

  • mrb_state* (mrb_state *)

    The current mruby state.

  • RClass* (struct RClass *)

    A reference to module or a class.

  • RClass* (struct RClass *)

    A reference to the module to be prepended.

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:

  • mrb (mrb_state *)

    The MRuby state reference.

  • cla (struct RClass *)

    The class pointer where the method will be defined.

  • name (const char *)

    The name of the method being defined.

  • func (mrb_func_t)

    The function pointer to the method definition.

  • aspec (mrb_aspec)

    The method parameters declaration.

void mrb_define_class_method(mrb_state * , struct RClass * , const char * , mrb_func_t , mrb_aspec )

Defines a class method.

Example:

# Ruby way
class Foo
  def Foo.bar
  end
end
// C way
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:

  • mrb_state* (mrb_state *)

    The MRuby state reference.

  • RClass* (struct RClass *)

    The class where the class method will be defined.

  • char* (const char *)

    The name of the class method being defined.

  • mrb_func_t (mrb_func_t)

    The function pointer to the class method definition.

  • mrb_aspec (mrb_aspec)

    The method parameters declaration.

void mrb_define_singleton_method(mrb_state* , struct RObject* , const char* , mrb_func_t , mrb_aspec )

void mrb_define_module_function(mrb_state* , struct RClass* , const char* , mrb_func_t , mrb_aspec )

Defines a module fuction.

Example:

  # Ruby way
  module Foo
    def Foo.bar
    end
  end
  // C way
  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:

  • mrb_state* (mrb_state *)

    The MRuby state reference.

  • RClass* (struct RClass *)

    The module where the module function will be defined.

  • char* (const char *)

    The name of the module function being defined.

  • mrb_func_t (mrb_func_t)

    The function pointer to the module function definition.

  • mrb_aspec (mrb_aspec)

    The method parameters declaration.

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

Defines a constant.

Example:

    # Ruby way
    class ExampleClass
      AGE = 22
    end
    // C way
    #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:

  • mrb_state* (mrb_state *)

    The MRuby state reference.

  • RClass* (struct RClass *)

    A class or module the constant is defined in.

  • name (const char *)

    The name of the constant being defined.

  • mrb_value (mrb_value)

    The value for the constant.

void mrb_undef_method(mrb_state* , struct RClass* , const char* )

Undefines a method.

Example:

# Ruby way

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 way
#include <stdio.h>
#include <mruby.h>

mrb_value
mrb_example_method(mrb_state *mrb){
  return mrb_str_new_cstr(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:

  • mrb_state* (mrb_state*)

    The mruby state reference.

  • RClass* (struct RClass*)

    A class the method will be undefined from.

  • constchar* (const char*)

    The name of the method to be undefined.

void mrb_undef_class_method(mrb_state* , struct RClass* , const char* )

Undefine a class method. Example:

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

ExampleClass.example_method

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

mrb_value
mrb_example_method(mrb_state *mrb){
  return mrb_str_new_cstr(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:

  • mrb_state* (mrb_state*)

    The mruby state reference.

  • RClass* (RClass*)

    A class the class method will be undefined from.

  • constchar* (constchar*)

    The name of the class method to be undefined.

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

Initialize a new object instace of c class.

Example:

# Ruby way
class ExampleClass
end

p ExampleClass # => #<ExampleClass:0x9958588>
// C way
#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:

  • mrb (mrb_state*)

    The current mruby state.

  • c (RClass*)

    Reference to the class of the new object.

  • argc (mrb_int)

    Number of arguments in argv

  • argv (const mrb_value *)

    Array of mrb_value to initialize the object

Returns:

  • (mrb_value)

    The newly initialized object

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

See Also:

mrb_value mrb_instance_new(mrb_state * mrb, mrb_value cv)

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:

  • mrb (mrb_state*)

    The current mruby state.

  • super (struct RClass *)

    The super class or parent.

Returns:

  • (struct RClass *)

    Reference to the new class.

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:

  • mrb (mrb_state*)

    The current mruby state.

Returns:

  • (struct RClass *)

    Reference to the new module.

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:

  • mrb (mrb_state*)

    The current mruby state.

  • name (const char *)

    A string representing the name of the class.

Returns:

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

Gets a class.

Parameters:

  • mrb (mrb_state*)

    The current mruby state.

  • name (const char *)

    The name of the class.

Returns:

  • (struct RClass *)

    A reference to the class.

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

Gets a child class.

Parameters:

  • mrb (mrb_state*)

    The current mruby state.

  • outer (struct RClass *)

    The name of the parent class.

  • name (const char *)

    The name of the class.

Returns:

  • (struct RClass *)

    A reference to the class.

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

Gets a module.

Parameters:

  • mrb (mrb_state*)

    The current mruby state.

  • name (const char *)

    The name of the module.

Returns:

  • (struct RClass *)

    A reference to the module.

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

Gets a module defined under another module.

Parameters:

  • mrb (mrb_state*)

    The current mruby state.

  • outer (struct RClass *)

    The name of the outer module.

  • name (const char *)

    The name of the module.

Returns:

  • (struct RClass *)

    A reference to the module.

mrb_value mrb_notimplement_m(mrb_state* , mrb_value )

mrb_value mrb_obj_dup(mrb_state * mrb, mrb_value obj)

Duplicate an object.

Equivalent to: Object#dup

Parameters:

  • mrb (mrb_state*)

    The current mruby state.

  • obj (mrb_value)

    Object to be duplicate.

Returns:

  • (mrb_value)

    The newly duplicated object.

mrb_value mrb_check_to_integer(mrb_state * mrb, mrb_value val, const char * method)

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 way
 class ExampleClass
   def example_method
   end
 end

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

 // C way
 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_cstr(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:

  • mrb (mrb_state*)

    The current mruby state.

  • c (struct RClass *)

    A reference to a class.

  • mid (mrb_sym)

    A symbol referencing a method id.

Returns:

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:

  • mrb (mrb_state*)

    The current mruby state.

  • outer (struct RClass *)

    Reference to the module under which the new class will be defined

  • name (const char *)

    The name of the defined class

  • super (struct RClass *)

    The new class parent

Returns:

  • (struct RClass *)

    Reference to the newly defined class

See Also:

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.

When applicable, implicit conversions (such as to_str, to_ary, to_hash) are applied to received arguments. Used inside a function of mrb_func_t type.

Parameters:

  • mrb

    The current MRuby state.

  • format (mrb_args_format)

    is 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_value mrb_funcall(mrb_state* , mrb_value , const char* , mrb_int , ... )

Call existing ruby functions.

 #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:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • mrb_value (mrb_value)

    A reference to an mruby value.

  • const (const char*)

    char* The name of the method.

  • mrb_int (mrb_int)

    The number of arguments the method has.

  • ... (...)

    Variadic values(not type safe!).

Returns:

  • (mrb_value)

    mrb_value mruby function value.

mrb_value mrb_funcall_argv(mrb_state* , mrb_value , mrb_sym , mrb_int , const mrb_value* )

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_cstr(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:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • mrb_value (mrb_value)

    A reference to an mruby value.

  • mrb_sym (mrb_sym)

    The symbol representing the method.

  • mrb_int (mrb_int)

    The number of arguments the method has.

  • mrb_value* (const mrb_value*)

    Pointer to the object.

Returns:

  • (mrb_value)

    mrb_value mruby function value.

See Also:

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* )

Creates a symbol from a C string.

# Ruby way:
:symbol # => :symbol

// C way:
mrb_sym mrb_symbol = mrb_intern_cstr(mrb, "symbol"); //  => :symbol

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • const (const char*)

    char* The name of the method.

Returns:

See Also:

mrb_sym mrb_intern(mrb_state* , const char* , size_t )

Creates a symbol from a C string.

# Ruby way
:symbol

// C way
void
mrb_mruby_example_gem_init(mrb_state* mrb) {
  mrb_sym mrb_symbol;
  char c_str[6] = "symbol";

  mrb_symbol = mrb_intern(mrb, "symbol", sizeof(c_str));
}

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • const (const char*)

    char* A C string.

  • size_t (size_t)

    An integer representing the size of the string.

Returns:

mrb_sym mrb_intern_static(mrb_state* , const char* , size_t )

mrb_sym mrb_intern_str(mrb_state* , mrb_value )

Creates a symbol from a String.

void
mrb_mruby_example_gem_init(mrb_state* mrb) {
  mrb_sym mrb_symbol;
  mrb_value mrb_string;

  mrb_string = mrb_str_new_cstr(mrb, "symbol"); // Returns a Ruby string from C string.
  mrb_intern_str(mrb, mrb_string); // Returns Symbol from Ruby string.
}

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • mrb_value (mrb_value)

    A Ruby value.

Returns:

mrb_value mrb_check_intern_cstr(mrb_state* , const char* )

Returns a Symbol, or nil otherwise.

void
mrb_mruby_example_gem_init(mrb_state* mrb) {
  mrb_value mrb_symbol;

  mrb_symbol = mrb_check_intern_cstr(mrb, "Symbol"); // Returns :Symbol
  mrb_p(mrb, mrb_symbol); // This is equivalent to p :Symbol
}

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • char* (const char*)

    A C string.

Returns:

mrb_value mrb_check_intern(mrb_state* , const char* , size_t )

mrb_value mrb_check_intern_str(mrb_state* , mrb_value )

Returns a Symbol, or nil otherwise.

void
mrb_mruby_example_gem_init(mrb_state* mrb) {
  mrb_value mrb_string;
  mrb_value mrb_symbol;

  mrb_string = mrb_str_new_cstr(mrb, "Symbol"); // Returns a Ruby string from C string.
  mrb_symbol = mrb_check_intern_str(mrb, mrb_string); // Returns :Symbol
  mrb_p(mrb, mrb_symbol); // This is equivalent to p :Symbol
}

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • mrb_value (mrb_value)

    A Ruby value.

Returns:

mrb_value mrb_sym2str(mrb_state* , mrb_sym )

Returns a String from a Symbol.

void
mrb_mruby_example_gem_init(mrb_state* mrb) {
  mrb_sym mrb_symbol;
  mrb_value mrb_string;
  const char* symbol_name = "symbol";

  mrb_symbol = mrb_intern(mrb, symbol_name, strlen(symbol_name)); // Returns Symbol.
  mrb_string = mrb_sym2str(mrb, mrb_symbol); // Returns symbol name as a Ruby string.
  mrb_p(mrb, mrb_string);
}

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • mrb_sym (mrb_sym)

    A Symbol.

Returns:

  • (mrb_value)

    mrb_value A Ruby string value.

void * mrb_malloc(mrb_state* , size_t )

Allocates the requested memory and returns a pointer to it. It raises RuntimeError if no memory.

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • size_t (size_t)

    The size of memory block, in bytes.

Returns:

  • (void*)

    void* Pointer to requested block of memory.

void * mrb_calloc(mrb_state* , size_t , size_t )

raise RuntimeError if no mem

void * mrb_realloc(mrb_state* , void* , size_t )

Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to mrb_malloc or mrb_calloc. It raises RuntimeError if no memory.

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • void* (void*)

    This is the pointer to a memory block previously allocated with mrb_malloc or mrb_calloc.

  • size_t (size_t)

    The size of memory block, in bytes.

Returns:

  • (void*)

    void* Pointer to requested block of memory.

void * mrb_realloc_simple(mrb_state* , void* , size_t )

Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to mrb_malloc_simple. It returns NULL if there's no memory available.

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • void* (void*)

    This is the pointer to a memory block previously allocated with mrb_malloc_simple.

  • size_t (size_t)

    The size of memory block, in bytes.

Returns:

  • (void*)

    void* Pointer to requested block of memory.

void * mrb_malloc_simple(mrb_state* , size_t )

Allocates the requested memory and returns a pointer to it. It return NULL if no memory available.

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • size_t (size_t)

    The size of memory block, in bytes.

Returns:

  • (void*)

    void* Pointer to requested block of memory.

struct RBasic * mrb_obj_alloc(mrb_state* , enum mrb_vtype, struct RClass* )

void mrb_free(mrb_state* , void* )

Deallocates the memory previously allocated by a call to mrb_calloc, mrb_malloc, mrb_realloc, mrb_malloc_simple, or mrb_realloc_simple

Parameters:

  • mrb_state* (mrb_state*)

    The current mruby state.

  • void* (void*)

    The pointer to a memory block previously allocated.

mrb_value mrb_str_new(mrb_state * mrb, const char * p, size_t len)

Returns a String from C String.

void
mrb_mruby_example_gem_init(mrb_state* mrb) {
  mrb_value mrb_string;
  char c_str[6] = "String";

  mrb_string = mrb_str_new(mrb, c_str, strlen(c_str)); // Return Ruby string value.
  mrb_p(mrb, mrb_string);
}

Parameters:

  • mrb (mrb_state*)

    The current mruby state.

  • *p (const char)

    A C string.

  • len (size_t)

    The length of string.

Returns:

mrb_value mrb_str_new_cstr(mrb_state* , const char* )

Returns a String from a C string.

void
mrb_mruby_example_gem_init(mrb_state* mrb) {
  mrb_value mrb_string;
  char c_str[6] = "String";

  mrb_string = mrb_str_new_cstr(mrb, c_str); // Return Ruby string value.
  mrb_p(mrb, mrb_string);
}

Parameters:

  • mrb (mrb_state*)

    The current mruby state.

  • *p (const char)

    A C string.

Returns:

mrb_value mrb_str_new_static(mrb_state * mrb, const char * p, size_t len)

mrb_state* mrb_open(void)

Creates new mrb_state.

Returns:

  • Pointer to the newly created mrb_state.

mrb_state* mrb_open_allocf(mrb_allocf f, void * ud)

Create new mrb_state with custom allocators.

Parameters:

  • f

    Reference to the allocation function.

  • ud

    User 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_state* mrb_open_core(mrb_allocf f, void * ud)

Create new mrb_state with just the MRuby core

Parameters:

  • f

    Reference to the allocation function. Use mrb_default_allocf for the default

  • ud

    User 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.

void mrb_close(mrb_state * mrb)

Closes and frees a mrb_state.

Parameters:

  • mrb

    Pointer to the mrb_state to be closed.

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

The default allocation function.

See Also:

  • mruby.h.mrb_allocf

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* , 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, 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)

void mrb_garbage_collect(mrb_state* )

void mrb_full_gc(mrb_state* )

void mrb_incremental_gc(mrb_state * )

int mrb_gc_arena_save(mrb_state* )

void mrb_gc_arena_restore(mrb_state* , int )

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, ... )

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_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)

void mrb_check_type(mrb_state * mrb, mrb_value x, enum mrb_vtype, t )

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

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_value mrb_fiber_resume(mrb_state * mrb, mrb_value fib, mrb_int argc, const mrb_value * argv)

Resume a Fiber

Required mrbgem: mruby-fiber

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

Yield a Fiber

Required mrbgem: mruby-fiber

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 )

void mrb_state_atexit(mrb_state * mrb, mrb_atexit_func func)

void mrb_show_version(mrb_state * mrb)

mrb_value mrb_format(mrb_state * mrb, const char * format, ... )