/*
** mruby - An embeddable Ruby implementation
**
** Copyright (c) mruby developers 2010-2013
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
*/
#ifndef MRUBY_H
#define MRUBY_H
#if defined(__cplusplus)
extern "C" {
#endif
#include
#include
#include "mrbconf.h"
#include "mruby/value.h"
typedef uint32_t mrb_code;
typedef uint32_t mrb_aspec;
struct mrb_state;
typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
#ifndef MRB_ARENA_SIZE
#define MRB_ARENA_SIZE 100
#endif
typedef struct {
mrb_sym mid;
struct RProc *proc;
int stackidx;
int nregs;
int argc;
mrb_code *pc; /* return address */
mrb_code *err; /* error position */
int acc;
struct RClass *target_class;
int ridx;
int eidx;
struct REnv *env;
} mrb_callinfo;
enum mrb_fiber_state {
MRB_FIBER_CREATED = 0,
MRB_FIBER_RUNNING,
MRB_FIBER_RESUMED,
MRB_FIBER_TERMINATED,
};
struct mrb_context {
struct mrb_context *prev;
mrb_value *stack; /* stack of virtual machine */
mrb_value *stbase, *stend;
mrb_callinfo *ci;
mrb_callinfo *cibase, *ciend;
mrb_code **rescue; /* exception handler stack */
int rsize;
struct RProc **ensure; /* ensure handler stack */
int esize;
enum mrb_fiber_state status;
struct RFiber *fib;
};
enum gc_state {
GC_STATE_NONE = 0,
GC_STATE_MARK,
GC_STATE_SWEEP
};
typedef struct mrb_state {
void *jmp;
mrb_allocf allocf; /* memory allocation function */
struct mrb_context *c;
struct mrb_context *root_c;
struct RObject *exc; /* exception */
struct iv_tbl *globals; /* global variable table */
struct mrb_irep **irep; /* program data array */
size_t irep_len, irep_capa;
struct RObject *top_self;
struct RClass *object_class; /* Object class */
struct RClass *class_class;
struct RClass *module_class;
struct RClass *proc_class;
struct RClass *string_class;
struct RClass *array_class;
struct RClass *hash_class;
struct RClass *float_class;
struct RClass *fixnum_class;
struct RClass *true_class;
struct RClass *false_class;
struct RClass *nil_class;
struct RClass *symbol_class;
struct RClass *kernel_module;
struct heap_page *heaps; /* heaps for GC */
struct heap_page *sweeps;
struct heap_page *free_heaps;
size_t live; /* count of live objects */
struct RBasic *arena[MRB_ARENA_SIZE]; /* GC protection array */
int arena_idx;
enum gc_state gc_state; /* state of gc */
int current_white_part; /* make white object by white_part */
struct RBasic *gray_list; /* list of gray objects to be traversed incrementally */
struct RBasic *atomic_gray_list; /* list of objects to be traversed atomically */
size_t gc_live_after_mark;
size_t gc_threshold;
int gc_interval_ratio;
int gc_step_ratio;
mrb_bool gc_disabled:1;
mrb_bool gc_full:1;
mrb_bool is_generational_gc_mode:1;
mrb_bool out_of_memory:1;
size_t majorgc_old_threshold;
struct alloca_header *mems;
mrb_sym symidx;
struct kh_n2s *name2sym; /* symbol table */
#ifdef ENABLE_DEBUG
void (*code_fetch_hook)(struct mrb_state* mrb, struct mrb_irep *irep, mrb_code *pc, mrb_value *regs);
#endif
struct RClass *eException_class;
struct RClass *eStandardError_class;
void *ud; /* auxiliary data */
} mrb_state;
typedef mrb_value (*mrb_func_t)(mrb_state *mrb, mrb_value);
struct RClass *mrb_define_class(mrb_state *, const char*, struct RClass*);
struct RClass *mrb_define_module(mrb_state *, const char*);
mrb_value mrb_singleton_class(mrb_state*, mrb_value);
void mrb_include_module(mrb_state*, struct RClass*, struct RClass*);
void mrb_define_method(mrb_state*, struct RClass*, const char*, mrb_func_t, mrb_aspec);
void mrb_define_class_method(mrb_state *, struct RClass *, const char *, mrb_func_t, mrb_aspec);
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);
void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value);
void mrb_undef_method(mrb_state*, struct RClass*, const char*);
void mrb_undef_class_method(mrb_state*, struct RClass*, const char*);
mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, int argc, mrb_value *argv);
#define mrb_class_new_instance(mrb,argc,argv,c) mrb_obj_new(mrb,c,argc,argv)
mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super);
struct RClass * mrb_module_new(mrb_state *mrb);
mrb_bool mrb_class_defined(mrb_state *mrb, const char *name);
struct RClass * mrb_class_get(mrb_state *mrb, const char *name);
struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method);
mrb_bool mrb_obj_respond_to(struct RClass* c, mrb_sym mid);
struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
/* required arguments */
#define MRB_ARGS_REQ(n) ((mrb_aspec)((n)&0x1f)