Skip to content

Instantly share code, notes, and snippets.

@take-cheeze
Last active April 18, 2022 22:19
Show Gist options
  • Save take-cheeze/9171332 to your computer and use it in GitHub Desktop.
Save take-cheeze/9171332 to your computer and use it in GitHub Desktop.

mruby.h

Basic header of mruby. It includes mrbconf.h, mruby/value.h, mruby/version.h internally.

mrb_state management

mrb_open

mrb_state*
mrb_open(void);

Creates new mrb_state.

mrb_allocf

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

Function pointer type of custom allocator used in mrb_open_allocf.

Requirement

The function pointing it must behave similarly as realloc. If ptr is NULL it must allocate new space. If s is NULL, ptr must be freed.

mrb_open_allocf

mrb_state*
mrb_open_allocf(mrb_allocf f, void *ud);

Create new mrb_state with custom allocator. ud will be passed to custom allocator f. If user data isn't required just pass NULL.

Reqiurement

Function pointer f must satisfy requirements of its type.

mrb_close

void
mrb_close(mrb_state *mrb);

Frees mrb_state.

Requirement

mrb must be same as return value of mrb_open or mrb_open_allocf.

Method

mrb_get_args

int
mrb_get_args(mrb_state *mrb, const char *format, ...);

Retrieve arguments from mrb_state. Use it inside a function pointed by mrb_func_t. It returns number of function retrieved. format is a list of following format specifier:

char mruby type retrieve types note
o Object mrb_value Could be used to retreive any type of argument
C Class/Module mrb_value
S String mrb_value
A Array mrb_value
H Hash mrb_value
s String mrb_value
z String mrb_value
a Array mrb_value*, mrb_int
f Float mrb_float
i Interger mrb_int
b Boolean mrb_bool
n Symbol mrb_sym
& Block mrb_value
* Array mrb_value*, int Receive the rest of arguments as an array. Unlike *operator Array object won't be created.
'|'

The passing values must be pointer of retreive types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment