void mrb_init_array(mrb_state *mrb) { struct RClass *a; a = mrb->array_class = mrb_define_class(mrb, "Array", mrb->object_class); MRB_SET_INSTANCE_TT(a, MRB_TT_ARRAY); mrb_include_module(mrb, a, mrb_class_get(mrb, "Enumerable")); mrb_define_class_method(mrb, a, "[]", mrb_ary_s_create, ARGS_ANY()); /* 15.2.12.4.1 */ mrb_define_method(mrb, a, "*", mrb_ary_times, ARGS_REQ(1)); /* 15.2.12.5.1 */ mrb_define_method(mrb, a, "+", mrb_ary_plus, ARGS_REQ(1)); /* 15.2.12.5.2 */ mrb_define_method(mrb, a, "<<", mrb_ary_push_m, ARGS_REQ(1)); /* 15.2.12.5.3 */ mrb_define_method(mrb, a, "[]", mrb_ary_aget, ARGS_ANY()); /* 15.2.12.5.4 */ mrb_define_method(mrb, a, "[]=", mrb_ary_aset, ARGS_ANY()); /* 15.2.12.5.5 */ mrb_define_method(mrb, a, "clear", mrb_ary_clear, ARGS_NONE()); /* 15.2.12.5.6 */ mrb_define_method(mrb, a, "concat", mrb_ary_concat_m, ARGS_REQ(1)); /* 15.2.12.5.8 */ mrb_define_method(mrb, a, "delete_at", mrb_ary_delete_at, ARGS_REQ(1)); /* 15.2.12.5.9 */ mrb_define_method(mrb, a, "empty?", mrb_ary_empty_p, ARGS_NONE()); /* 15.2.12.5.12 */ mrb_define_method(mrb, a, "first", mrb_ary_first, ARGS_OPT(1)); /* 15.2.12.5.13 */ mrb_define_method(mrb, a, "index", mrb_ary_index_m, ARGS_REQ(1)); /* 15.2.12.5.14 */ mrb_define_method(mrb, a, "initialize_copy", mrb_ary_replace_m, ARGS_REQ(1)); /* 15.2.12.5.16 */ mrb_define_method(mrb, a, "join", mrb_ary_join_m, ARGS_ANY()); /* 15.2.12.5.17 */ mrb_define_method(mrb, a, "last", mrb_ary_last, ARGS_ANY()); /* 15.2.12.5.18 */ mrb_define_method(mrb, a, "length", mrb_ary_size, ARGS_NONE()); /* 15.2.12.5.19 */ mrb_define_method(mrb, a, "pop", mrb_ary_pop, ARGS_NONE()); /* 15.2.12.5.21 */ mrb_define_method(mrb, a, "push", mrb_ary_push_m, ARGS_ANY()); /* 15.2.12.5.22 */ mrb_define_method(mrb, a, "replace", mrb_ary_replace_m, ARGS_REQ(1)); /* 15.2.12.5.23 */ mrb_define_method(mrb, a, "reverse", mrb_ary_reverse, ARGS_NONE()); /* 15.2.12.5.24 */ mrb_define_method(mrb, a, "reverse!", mrb_ary_reverse_bang, ARGS_NONE()); /* 15.2.12.5.25 */ mrb_define_method(mrb, a, "rindex", mrb_ary_rindex_m, ARGS_REQ(1)); /* 15.2.12.5.26 */ mrb_define_method(mrb, a, "shift", mrb_ary_shift, ARGS_NONE()); /* 15.2.12.5.27 */ mrb_define_method(mrb, a, "size", mrb_ary_size, ARGS_NONE()); /* 15.2.12.5.28 */ mrb_define_method(mrb, a, "slice", mrb_ary_aget, ARGS_ANY()); /* 15.2.12.5.29 */ mrb_define_method(mrb, a, "unshift", mrb_ary_unshift_m, ARGS_ANY()); /* 15.2.12.5.30 */ mrb_define_method(mrb, a, "inspect", mrb_ary_inspect, ARGS_NONE()); /* 15.2.12.5.31 (x) */ mrb_define_alias(mrb, a, "to_s", "inspect"); /* 15.2.12.5.32 (x) */ mrb_define_method(mrb, a, "==", mrb_ary_equal, ARGS_REQ(1)); /* 15.2.12.5.33 (x) */ mrb_define_method(mrb, a, "eql?", mrb_ary_eql, ARGS_REQ(1)); /* 15.2.12.5.34 (x) */ mrb_define_method(mrb, a, "<=>", mrb_ary_cmp, ARGS_REQ(1)); /* 15.2.12.5.36 (x) */ }
static mrb_value mrb_basicsocket_getsockopt(mrb_state *mrb, mrb_value self) { char opt[8]; int s; mrb_int family, level, optname; mrb_value c, data; socklen_t optlen; mrb_get_args(mrb, "ii", &level, &optname); s = socket_fd(mrb, self); optlen = sizeof(opt); if (getsockopt(s, level, optname, opt, &optlen) == -1) mrb_sys_fail(mrb, "getsockopt"); c = mrb_const_get(mrb, mrb_obj_value(mrb_class_get(mrb, "Socket")), mrb_intern_lit(mrb, "Option")); family = socket_family(s); data = mrb_str_new(mrb, opt, optlen); return mrb_funcall(mrb, c, "new", 4, mrb_fixnum_value(family), mrb_fixnum_value(level), mrb_fixnum_value(optname), data); }
int main(void) { mrb_state *mrb = mrb_open(); struct RClass* cv = mrb_class_get(mrb, "String"); mrb_value str = mrb_obj_new(mrb, cv, 0, 0); mrb_value head = mrb_str_new(mrb,"p 'hello",8); mrb_value tail = mrb_str_new(mrb," world'",7); str = mrb_funcall_argv(mrb, str, mrb_intern2(mrb, "+", 1), 1, &head); str = mrb_funcall_argv(mrb, str, mrb_intern2(mrb, "+", 1), 1, &tail); printf("str#size is %d \n", mrb_funcall(mrb, str, "size", 0, 0)); printf("str is '%s' \n", RSTRING_PTR(str)); mrb_load_string(mrb, RSTRING_PTR(str)); return 0; }
void mrb_ev3_serial_init(mrb_state *mrb, struct RClass *ev3) { struct RClass *file = mrb_class_get(mrb, "File"); struct RClass *ser; mrb_value sero; mrb_value port; /* SerialPort class */ ser = mrb_define_class_under(mrb, ev3, "SerialPort", file); sero = mrb_obj_value(ser); port = mrb_hash_new(mrb); mrb_hash_set(mrb, port, mrb_symbol_value(mrb_intern_lit(mrb, "default")), mrb_fixnum_value(EV3_SERIAL_DEFAULT)); mrb_hash_set(mrb, port, mrb_symbol_value(mrb_intern_lit(mrb, "uart")), mrb_fixnum_value(EV3_SERIAL_UART)); mrb_hash_set(mrb, port, mrb_symbol_value(mrb_intern_lit(mrb, "bt")), mrb_fixnum_value(EV3_SERIAL_BT)); mrb_const_set(mrb, sero, mrb_intern_lit(mrb, "PORT"), port); mrb_define_method(mrb, ser, "initialize", mrb_serial_init, MRB_ARGS_REQ(1)); }
void mrb_print_init(mrb_state* mrb) { struct RClass *platform; struct RClass *print; platform = mrb_class_get(mrb, "Platform"); print = mrb_define_class_under(mrb, platform, "Printer", mrb->object_class); mrb_define_class_method(mrb , print , "_open" , mrb_platform_printer_s__open , MRB_ARGS_NONE()); mrb_define_class_method(mrb , print , "_reset" , mrb_platform_printer_s__reset , MRB_ARGS_NONE()); mrb_define_class_method(mrb , print , "_close" , mrb_platform_printer_s__close , MRB_ARGS_NONE()); mrb_define_class_method(mrb , print , "_font=" , mrb_platform_printer_s__font , MRB_ARGS_REQ(1)); mrb_define_class_method(mrb , print , "_level=" , mrb_platform_printer_s__level , MRB_ARGS_REQ(1)); mrb_define_class_method(mrb , print , "_size" , mrb_platform_printer_s__size , MRB_ARGS_REQ(4)); mrb_define_class_method(mrb , print , "_feed" , mrb_platform_printer_s__feed , MRB_ARGS_REQ(1)); mrb_define_class_method(mrb , print , "_print" , mrb_platform_printer_s__print , MRB_ARGS_REQ(1)); mrb_define_class_method(mrb , print , "_print_bmp" , mrb_platform_printer_s__print_bmp , MRB_ARGS_REQ(1)); mrb_define_class_method(mrb , print , "_check" , mrb_platform_printer_s__check , MRB_ARGS_NONE()); }
MRB_API void mrb_sys_fail(mrb_state *mrb, const char *mesg) { struct RClass *sce; mrb_int no; no = (mrb_int)errno; if (mrb_class_defined(mrb, "SystemCallError")) { sce = mrb_class_get(mrb, "SystemCallError"); if (mesg != NULL) { mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 2, mrb_fixnum_value(no), mrb_str_new_cstr(mrb, mesg)); } else { mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 1, mrb_fixnum_value(no)); } } else { mrb_raise(mrb, E_RUNTIME_ERROR, mesg); } }
void planeBindingInit(mrb_state *mrb) { RClass *klass = mrb_define_class(mrb, "Plane", mrb_class_get(mrb,"Object")); disposableBindingInit<Plane> (mrb, klass); mrb_define_method(mrb, klass, "initialize", planeInitialize, MRB_ARGS_OPT(1)); INIT_PROP_BIND( Plane, Bitmap, "bitmap" ); INIT_PROP_BIND( Plane, OX, "ox" ); INIT_PROP_BIND( Plane, OY, "oy" ); INIT_PROP_BIND( Plane, Z, "z" ); INIT_PROP_BIND( Plane, ZoomX, "zoom_x" ); INIT_PROP_BIND( Plane, ZoomY, "zoom_y" ); INIT_PROP_BIND( Plane, Opacity, "opacity" ); INIT_PROP_BIND( Plane, BlendType, "blend_type" ); INIT_PROP_BIND( Plane, Color, "color" ); INIT_PROP_BIND( Plane, Tone, "tone" ); mrb_define_method(mrb, klass, "inspect", inspectObject, MRB_ARGS_NONE()); }
void mrb_mruby_errno_gem_init(mrb_state *mrb) { struct RClass *e, *eno, *sce, *ste; mrb_value h, noerror; ste = mrb_class_get(mrb, "StandardError"); sce = mrb_define_class(mrb, "SystemCallError", ste); mrb_define_class_method(mrb, sce, "_sys_fail", mrb_sce_sys_fail, MRB_ARGS_REQ(1)); mrb_define_method(mrb, sce, "errno", mrb_sce_errno, MRB_ARGS_NONE()); mrb_define_method(mrb, sce, "to_s", mrb_sce_to_s, MRB_ARGS_NONE()); mrb_define_method(mrb, sce, "initialize", mrb_sce_init, MRB_ARGS_ARG(1, 1)); eno = mrb_define_module(mrb, "Errno"); h = mrb_hash_new(mrb); mrb_define_const(mrb, eno, "Errno2class", h); e = mrb_define_class_under(mrb, eno, "NOERROR", sce); mrb_define_const(mrb, e, "Errno", mrb_fixnum_value(0)); mrb_define_method(mrb, e, "initialize", mrb_exxx_init, MRB_ARGS_OPT(1)); //mrb_define_method(mrb, e, "===", mrb_exxx_cmp, MRB_ARGS_REQ(1)); noerror = mrb_obj_value(e); #define itsdefined(SYM) \ do { \ int ai = mrb_gc_arena_save(mrb); \ e = mrb_define_class_under(mrb, eno, #SYM, sce); \ mrb_define_const(mrb, e, "Errno", mrb_fixnum_value(SYM)); \ mrb_define_method(mrb, e, "initialize", mrb_exxx_init, MRB_ARGS_OPT(1)); \ mrb_hash_set(mrb, h, mrb_fixnum_value(SYM), mrb_obj_value(e)); \ mrb_gc_arena_restore(mrb, ai); \ } while (0) #define itsnotdefined(SYM) \ do { \ mrb_define_const(mrb, eno, #SYM, noerror); \ } while (0) #include "known_errors_def.cstub" }
static mrb_value mrb_module_instance_method(mrb_state *mrb, mrb_value self) { struct RClass *owner; struct RProc *proc; struct RObject *ume; mrb_sym name; mrb_get_args(mrb, "n", &name); mrb_search_method_owner(mrb, mrb_class_ptr(self), self, name, &owner, &proc, TRUE); ume = method_object_alloc(mrb, mrb_class_get(mrb, "UnboundMethod")); mrb_obj_iv_set(mrb, ume, mrb_intern_lit(mrb, "@owner"), mrb_obj_value(owner)); mrb_obj_iv_set(mrb, ume, mrb_intern_lit(mrb, "@recv"), mrb_nil_value()); mrb_obj_iv_set(mrb, ume, mrb_intern_lit(mrb, "@name"), mrb_symbol_value(name)); mrb_obj_iv_set(mrb, ume, mrb_intern_lit(mrb, "proc"), proc ? mrb_obj_value(proc) : mrb_nil_value()); mrb_obj_iv_set(mrb, ume, mrb_intern_lit(mrb, "@klass"), self); return mrb_obj_value(ume); }
static mrb_value mrb_kernel_method(mrb_state *mrb, mrb_value self) { struct RClass *owner; struct RProc *proc; struct RObject *me; mrb_sym name; mrb_get_args(mrb, "n", &name); mrb_search_method_owner(mrb, mrb_class(mrb, self), self, name, &owner, &proc, FALSE); me = method_object_alloc(mrb, mrb_class_get(mrb, "Method")); mrb_obj_iv_set(mrb, me, mrb_intern_lit(mrb, "@owner"), mrb_obj_value(owner)); mrb_obj_iv_set(mrb, me, mrb_intern_lit(mrb, "@recv"), self); mrb_obj_iv_set(mrb, me, mrb_intern_lit(mrb, "@name"), mrb_symbol_value(name)); mrb_obj_iv_set(mrb, me, mrb_intern_lit(mrb, "proc"), proc ? mrb_obj_value(proc) : mrb_nil_value()); mrb_obj_iv_set(mrb, me, mrb_intern_lit(mrb, "@klass"), mrb_obj_value(mrb_class(mrb, self))); return mrb_obj_value(me); }
void mrb_init_hash(mrb_state *mrb) { struct RClass *h; h = mrb->hash_class = mrb_define_class(mrb, "Hash", mrb->object_class); MRB_SET_INSTANCE_TT(h, MRB_TT_HASH); mrb_include_module(mrb, h, mrb_class_get(mrb, "Enumerable")); mrb_define_method(mrb, h, "==", mrb_hash_equal, MRB_ARGS_REQ(1)); /* 15.2.13.4.1 */ mrb_define_method(mrb, h, "[]", mrb_hash_aget, MRB_ARGS_REQ(1)); /* 15.2.13.4.2 */ mrb_define_method(mrb, h, "[]=", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.3 */ mrb_define_method(mrb, h, "clear", mrb_hash_clear, MRB_ARGS_NONE()); /* 15.2.13.4.4 */ mrb_define_method(mrb, h, "default", mrb_hash_default, MRB_ARGS_ANY()); /* 15.2.13.4.5 */ mrb_define_method(mrb, h, "default=", mrb_hash_set_default, MRB_ARGS_REQ(1)); /* 15.2.13.4.6 */ mrb_define_method(mrb, h, "default_proc", mrb_hash_default_proc,MRB_ARGS_NONE()); /* 15.2.13.4.7 */ mrb_define_method(mrb, h, "default_proc=", mrb_hash_set_default_proc,MRB_ARGS_REQ(1)); /* 15.2.13.4.7 */ mrb_define_method(mrb, h, "__delete", mrb_hash_delete, MRB_ARGS_REQ(1)); /* core of 15.2.13.4.8 */ mrb_define_method(mrb, h, "empty?", mrb_hash_empty_p, MRB_ARGS_NONE()); /* 15.2.13.4.12 */ mrb_define_method(mrb, h, "has_key?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.13 */ mrb_define_method(mrb, h, "has_value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.14 */ mrb_define_method(mrb, h, "include?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.15 */ mrb_define_method(mrb, h, "__init_core", mrb_hash_init_core, MRB_ARGS_ANY()); /* core of 15.2.13.4.16 */ mrb_define_method(mrb, h, "initialize_copy", mrb_hash_replace, MRB_ARGS_REQ(1)); /* 15.2.13.4.17 */ mrb_define_method(mrb, h, "key?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.18 */ mrb_define_method(mrb, h, "keys", mrb_hash_keys, MRB_ARGS_NONE()); /* 15.2.13.4.19 */ mrb_define_method(mrb, h, "length", mrb_hash_size_m, MRB_ARGS_NONE()); /* 15.2.13.4.20 */ mrb_define_method(mrb, h, "member?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.21 */ mrb_define_method(mrb, h, "replace", mrb_hash_replace, MRB_ARGS_REQ(1)); /* 15.2.13.4.23 */ mrb_define_method(mrb, h, "shift", mrb_hash_shift, MRB_ARGS_NONE()); /* 15.2.13.4.24 */ mrb_define_method(mrb, h, "size", mrb_hash_size_m, MRB_ARGS_NONE()); /* 15.2.13.4.25 */ mrb_define_method(mrb, h, "store", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.26 */ mrb_define_method(mrb, h, "value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.27 */ mrb_define_method(mrb, h, "values", mrb_hash_values, MRB_ARGS_NONE()); /* 15.2.13.4.28 */ mrb_define_method(mrb, h, "to_hash", mrb_hash_to_hash, MRB_ARGS_NONE()); /* 15.2.13.4.29 (x)*/ mrb_define_method(mrb, h, "inspect", mrb_hash_inspect, MRB_ARGS_NONE()); /* 15.2.13.4.30 (x)*/ mrb_define_alias(mrb, h, "to_s", "inspect"); /* 15.2.13.4.31 (x)*/ mrb_define_method(mrb, h, "eql?", mrb_hash_eql, MRB_ARGS_REQ(1)); /* 15.2.13.4.32 (x)*/ }
void mrb_init_file(mrb_state *mrb) { struct RClass *io, *file, *cnst; io = mrb_class_get(mrb, "IO"); file = mrb_define_class(mrb, "File", io); MRB_SET_INSTANCE_TT(file, MRB_TT_DATA); mrb_define_class_method(mrb, file, "umask", mrb_file_s_umask, MRB_ARGS_REQ(1)); mrb_define_class_method(mrb, file, "delete", mrb_file_s_unlink, MRB_ARGS_ANY()); mrb_define_class_method(mrb, file, "unlink", mrb_file_s_unlink, MRB_ARGS_ANY()); mrb_define_class_method(mrb, file, "rename", mrb_file_s_rename, MRB_ARGS_REQ(2)); mrb_define_class_method(mrb, file, "symlink", mrb_file_s_symlink, MRB_ARGS_REQ(2)); mrb_define_class_method(mrb, file, "chmod", mrb_file_s_chmod, MRB_ARGS_REQ(1) | MRB_ARGS_REST()); mrb_define_class_method(mrb, file, "readlink", mrb_file_s_readlink, MRB_ARGS_REQ(1)); mrb_define_class_method(mrb, file, "dirname", mrb_file_dirname, MRB_ARGS_REQ(1)); mrb_define_class_method(mrb, file, "basename", mrb_file_basename, MRB_ARGS_REQ(1)); mrb_define_class_method(mrb, file, "realpath", mrb_file_realpath, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); mrb_define_class_method(mrb, file, "_getwd", mrb_file__getwd, MRB_ARGS_NONE()); mrb_define_class_method(mrb, file, "_gethome", mrb_file__gethome, MRB_ARGS_OPT(1)); mrb_define_method(mrb, file, "flock", mrb_file_flock, MRB_ARGS_REQ(1)); mrb_define_method(mrb, file, "mtime", mrb_file_mtime, MRB_ARGS_NONE()); cnst = mrb_define_module_under(mrb, file, "Constants"); mrb_define_const(mrb, cnst, "LOCK_SH", mrb_fixnum_value(LOCK_SH)); mrb_define_const(mrb, cnst, "LOCK_EX", mrb_fixnum_value(LOCK_EX)); mrb_define_const(mrb, cnst, "LOCK_UN", mrb_fixnum_value(LOCK_UN)); mrb_define_const(mrb, cnst, "LOCK_NB", mrb_fixnum_value(LOCK_NB)); mrb_define_const(mrb, cnst, "SEPARATOR", mrb_str_new_cstr(mrb, FILE_SEPARATOR)); mrb_define_const(mrb, cnst, "PATH_SEPARATOR", mrb_str_new_cstr(mrb, PATH_SEPARATOR)); #if defined(_WIN32) || defined(_WIN64) mrb_define_const(mrb, cnst, "ALT_SEPARATOR", mrb_str_new_cstr(mrb, FILE_ALT_SEPARATOR)); #else mrb_define_const(mrb, cnst, "ALT_SEPARATOR", mrb_nil_value()); #endif mrb_define_const(mrb, cnst, "NULL", mrb_str_new_cstr(mrb, NULL_FILE)); }
static mrb_value mrb_module_instance_method(mrb_state *mrb, mrb_value self) { struct RClass *unbound_method = mrb_class_get(mrb, "UnboundMethod"); struct RClass *owner; struct RProc *proc; mrb_value id; mrb_value ume; mrb_value argv[4]; mrb_get_args(mrb, "o", &id); mrb_search_method_owner(mrb, mrb_class_ptr(self), self, id, &owner, &proc); argv[0] = self; argv[1] = mrb_obj_value(owner); argv[2] = id; argv[3] = mrb_obj_value(proc); ume = mrb_obj_new(mrb, unbound_method, 4, argv); return ume; }
MRB_API mrb_value mrb_sample_buffer_slice(mrb_state *mrb, mrb_value sample_buffer, mrb_int len, mrb_int offset) { mrb_value buf; mrb_sample_buffer *b = 0, *other = 0; if (len <= 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "Length must be positive"); if (offset < 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "Offset can not be negative"); other = mrb_sample_buffer_check(mrb, sample_buffer); if (len > other->len - offset) mrb_raise(mrb, E_ARGUMENT_ERROR, "Slice must be completely inside the buffer"); buf = mrb_obj_value(mrb_obj_alloc(mrb, MRB_TT_DATA, mrb_class_get(mrb, "SampleBuffer"))); b = mrb_sample_buffer_alloc(mrb); b->shared = other->shared; mrb_shared_sample_buffer_ref(mrb, other->shared); b->len = len; b->offset = offset; mrb_data_init(buf, b, &mrb_sample_buffer_type); return buf; }
//---------------------------------------------------------- void ScriptEngine::setup() { string dataRoot = dirname(mScriptPath); // set texture & script root ofSetDataPathRoot(dataRoot); // open mrb open(); // load builtin library mrb_load_irep(mMrb, BuiltIn); mConsoleModule = mrb_class_get(mMrb, "Console"); // bind Bind::All(mMrb); // load user script load(); // call setup funcallIf("setup"); }
MRB_API mrb_value mrb_sample_buffer_new_zero(mrb_state *mrb, mrb_int len, mrb_int offset) { mrb_value buf; mrb_sample_buffer *b = 0; mrb_int i; if (len <= 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "Length must be positive"); if (offset < 0) mrb_raise(mrb, E_ARGUMENT_ERROR, "Offset can not be negative"); buf = mrb_obj_value(mrb_obj_alloc(mrb, MRB_TT_DATA, mrb_class_get(mrb, "SampleBuffer"))); b = mrb_sample_buffer_alloc(mrb); b->shared = mrb_shared_sample_buffer_alloc(mrb, len); b->shared->offset = offset; b->len = len; b->offset = 0; for (i = 0; i < len; ++i) { b->shared->buffer[i] = 0; } mrb_data_init(buf, b, &mrb_sample_buffer_type); return buf; }
static mrb_value mrb_kernel_method(mrb_state *mrb, mrb_value self) { struct RClass *method = mrb_class_get(mrb, "Method"); struct RClass *owner; struct RProc *proc; mrb_value id; mrb_value me; mrb_value argv[5]; mrb_get_args(mrb, "o", &id); mrb_search_method_owner(mrb, mrb_class(mrb, self), self, id, &owner, &proc); argv[0] = mrb_obj_value(mrb_obj_class(mrb, self)); argv[1] = mrb_obj_value(owner); argv[2] = self; argv[3] = id; argv[4] = mrb_obj_value(proc); me = mrb_obj_new(mrb, method, 5, argv); return me; }
void grn_ctx_impl_mrb_init(grn_ctx *ctx) { if (!grn_ctx_impl_mrb_mruby_enabled) { ctx->impl->mrb.state = NULL; ctx->impl->mrb.base_directory[0] = '\0'; ctx->impl->mrb.module = NULL; ctx->impl->mrb.object_class = NULL; ctx->impl->mrb.checked_procs = NULL; ctx->impl->mrb.registered_plugins = NULL; ctx->impl->mrb.builtin.time_class = NULL; ctx->impl->mrb.groonga.operator_class = NULL; } else { mrb_state *mrb; mrb = mrb_open_allocf(grn_ctx_impl_mrb_allocf, ctx); ctx->impl->mrb.state = mrb; ctx->impl->mrb.base_directory[0] = '\0'; grn_ctx_impl_mrb_init_bindings(ctx); if (ctx->impl->mrb.state->exc) { mrb_value reason; reason = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0); ERR(GRN_UNKNOWN_ERROR, "failed to initialize mruby: %.*s", RSTRING_LEN(reason), RSTRING_PTR(reason)); mrb_close(ctx->impl->mrb.state); ctx->impl->mrb.state = NULL; } else { ctx->impl->mrb.checked_procs = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); ctx->impl->mrb.registered_plugins = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from)); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to)); ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time"); } } }
//utility to make instance of Blinker static mrb_value make_blinker_obj(int pin, int interval_ms) { RClass *blinker_class = mrb_class_get(g_mrb, "Blinker"); if (g_mrb->exc){ p(g_mrb, mrb_obj_value(g_mrb->exc)); Serial2.println("failed to get Blinker class"); g_mrb->exc = 0; return mrb_nil_value(); } mrb_value args[2]; args[0] = mrb_fixnum_value(pin); args[1] = mrb_fixnum_value(interval_ms); mrb_value blinker_obj = mrb_class_new_instance(g_mrb, 2, args, blinker_class); if (g_mrb->exc){ p(g_mrb, mrb_obj_value(g_mrb->exc)); Serial2.println("failed to create new Blinker instance"); g_mrb->exc = 0; return mrb_nil_value(); } return blinker_obj; }
static size_t memfwrite_callback(char* ptr, size_t size, size_t nmemb, void* stream) { MEMFILE* mf = (MEMFILE*) stream; int block = size * nmemb; mrb_value args[2]; mrb_state* mrb = mf->mrb; int ai = mrb_gc_arena_save(mrb); \ if (mf->data && mrb_nil_p(mf->header)) { mrb_value str = mrb_str_new(mrb, mf->data, mf->size); struct RClass* _class_http = mrb_class_get(mrb, "HTTP"); struct RClass* _class_http_parser = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_http), mrb_intern_cstr(mrb, "Parser"))); mrb_value parser = mrb_obj_new(mrb, _class_http_parser, 0, NULL); args[0] = str; mf->header = mrb_funcall_argv(mrb, parser, mrb_intern_cstr(mrb, "parse_response"), 1, args); } args[0] = mf->header; args[1] = mrb_str_new(mrb, ptr, block); mrb_gc_arena_restore(mrb, ai); mrb_yield_argv(mrb, mf->proc, 2, args); return block; }
void grn_ctx_impl_mrb_init(grn_ctx *ctx) { char grn_mruby_enabled[GRN_ENV_BUFFER_SIZE]; grn_getenv("GRN_MRUBY_ENABLED", grn_mruby_enabled, GRN_ENV_BUFFER_SIZE); if (grn_mruby_enabled[0] && strcmp(grn_mruby_enabled, "no") == 0) { ctx->impl->mrb.state = NULL; ctx->impl->mrb.base_directory[0] = '\0'; ctx->impl->mrb.module = NULL; ctx->impl->mrb.object_class = NULL; ctx->impl->mrb.checked_procs = NULL; ctx->impl->mrb.registered_plugins = NULL; ctx->impl->mrb.builtin.time_class = NULL; ctx->impl->mrb.groonga.operator_class = NULL; } else { mrb_state *mrb; mrb = mrb_open(); ctx->impl->mrb.state = mrb; ctx->impl->mrb.base_directory[0] = '\0'; grn_ctx_impl_mrb_init_bindings(ctx); /* TODO: Implement better error handling on init. */ if (ctx->impl->mrb.state->exc) { mrb_print_error(mrb); } ctx->impl->mrb.checked_procs = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); ctx->impl->mrb.registered_plugins = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from)); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to)); ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time"); } }
void mrb_init_range(mrb_state *mrb) { struct RClass *r; r = mrb->range_class = mrb_define_class(mrb, "Range", mrb->object_class); mrb_include_module(mrb, r, mrb_class_get(mrb, "Enumerable")); mrb_define_method(mrb, r, "begin", mrb_range_beg, ARGS_NONE()); /* 15.2.14.4.3 */ mrb_define_method(mrb, r, "end", mrb_range_end, ARGS_NONE()); /* 15.2.14.4.5 */ mrb_define_method(mrb, r, "==", mrb_range_eq, ARGS_REQ(1)); /* 15.2.14.4.1 */ mrb_define_method(mrb, r, "===", mrb_range_include, ARGS_REQ(1)); /* 15.2.14.4.2 */ mrb_define_method(mrb, r, "each", mrb_range_each, ARGS_NONE()); /* 15.2.14.4.4 */ mrb_define_method(mrb, r, "exclude_end?", mrb_range_excl, ARGS_NONE()); /* 15.2.14.4.6 */ mrb_define_method(mrb, r, "first", mrb_range_beg, ARGS_NONE()); /* 15.2.14.4.7 */ mrb_define_method(mrb, r, "include?", mrb_range_include, ARGS_REQ(1)); /* 15.2.14.4.8 */ mrb_define_method(mrb, r, "initialize", mrb_range_initialize, ARGS_REQ(4)); /* 15.2.14.4.9 */ mrb_define_method(mrb, r, "last", mrb_range_end, ARGS_NONE()); /* 15.2.14.4.10 */ mrb_define_method(mrb, r, "member?", mrb_range_include, ARGS_REQ(1)); /* 15.2.14.4.11 */ mrb_define_method(mrb, r, "to_s", range_to_s, ARGS_NONE()); /* 15.2.14.4.12(x) */ mrb_define_method(mrb, r, "inspect", range_inspect, ARGS_NONE()); /* 15.2.14.4.13(x) */ mrb_define_method(mrb, r, "eql?", range_eql, ARGS_REQ(1)); /* 15.2.14.4.14(x) */ mrb_define_method(mrb, r, "initialize_copy", range_initialize_copy, ARGS_REQ(1)); /* 15.2.14.4.15(x) */ }
static mrb_value get_random(mrb_state *mrb) { return mrb_const_get(mrb, mrb_obj_value(mrb_class_get(mrb, "Random")), mrb_intern_lit(mrb, "DEFAULT")); }
/* ------------------------------------------------------------------------*/ void mrb_init_numeric(mrb_state *mrb) { struct RClass *numeric, *integer, *fixnum, *fl; /* Numeric Class */ numeric = mrb_define_class(mrb, "Numeric", mrb->object_class); mrb_include_module(mrb, numeric, mrb_class_get(mrb, "Comparable")); mrb_define_method(mrb, numeric, "+@", num_uplus, ARGS_REQ(1)); /* 15.2.7.4.1 */ mrb_define_method(mrb, numeric, "-@", num_uminus, ARGS_REQ(1)); /* 15.2.7.4.2 */ mrb_define_method(mrb, numeric, "**", num_pow, ARGS_REQ(1)); mrb_define_method(mrb, numeric, "/", num_div, ARGS_REQ(1)); /* 15.2.8.3.4 */ mrb_define_method(mrb, numeric, "quo", num_div, ARGS_REQ(1)); /* 15.2.7.4.5 (x) */ mrb_define_method(mrb, numeric, "abs", num_abs, ARGS_NONE()); /* 15.2.7.4.3 */ mrb_define_method(mrb, numeric, "<=>", num_cmp, ARGS_REQ(1)); /* 15.2.9.3.6 */ /* Integer Class */ integer = mrb_define_class(mrb, "Integer", numeric); fixnum = mrb->fixnum_class = mrb_define_class(mrb, "Fixnum", integer); mrb_undef_class_method(mrb, fixnum, "new"); mrb_define_method(mrb, fixnum, "+", fix_plus, ARGS_REQ(1)); /* 15.2.8.3.1 */ mrb_define_method(mrb, fixnum, "-", fix_minus, ARGS_REQ(1)); /* 15.2.8.3.2 */ mrb_define_method(mrb, fixnum, "-@", fix_uminus, ARGS_REQ(1)); /* 15.2.7.4.2 */ mrb_define_method(mrb, fixnum, "*", fix_mul, ARGS_REQ(1)); /* 15.2.8.3.3 */ mrb_define_method(mrb, fixnum, "%", fix_mod, ARGS_REQ(1)); /* 15.2.8.3.5 */ mrb_define_method(mrb, fixnum, "==", fix_equal, ARGS_REQ(1)); /* 15.2.8.3.7 */ mrb_define_method(mrb, fixnum, "~", fix_rev, ARGS_NONE()); /* 15.2.8.3.8 */ mrb_define_method(mrb, fixnum, "&", fix_and, ARGS_REQ(1)); /* 15.2.8.3.9 */ mrb_define_method(mrb, fixnum, "|", fix_or, ARGS_REQ(1)); /* 15.2.8.3.10 */ mrb_define_method(mrb, fixnum, "^", fix_xor, ARGS_REQ(1)); /* 15.2.8.3.11 */ mrb_define_method(mrb, fixnum, "<<", fix_lshift, ARGS_REQ(1)); /* 15.2.8.3.12 */ mrb_define_method(mrb, fixnum, ">>", fix_rshift, ARGS_REQ(1)); /* 15.2.8.3.13 */ mrb_define_method(mrb, fixnum, "ceil", int_to_i, ARGS_NONE()); /* 15.2.8.3.14 */ mrb_define_method(mrb, fixnum, "eql?", num_eql, ARGS_REQ(1)); /* 15.2.8.3.16 */ mrb_define_method(mrb, fixnum, "floor", num_floor, ARGS_NONE()); /* 15.2.8.3.17 */ mrb_define_method(mrb, fixnum, "hash", flo_hash, ARGS_NONE()); /* 15.2.8.3.18 */ mrb_define_method(mrb, fixnum, "next", int_succ, ARGS_NONE()); /* 15.2.8.3.19 */ mrb_define_method(mrb, fixnum, "round", num_round, ARGS_ANY()); /* 15.2.8.3.20 */ mrb_define_method(mrb, fixnum, "succ", fix_succ, ARGS_NONE()); /* 15.2.8.3.21 */ mrb_define_method(mrb, fixnum, "to_f", fix_to_f, ARGS_NONE()); /* 15.2.8.3.23 */ mrb_define_method(mrb, fixnum, "to_i", int_to_i, ARGS_NONE()); /* 15.2.8.3.24 */ mrb_define_method(mrb, fixnum, "to_s", fix_to_s, ARGS_NONE()); /* 15.2.8.3.25 */ mrb_define_method(mrb, fixnum, "inspect", fix_to_s, ARGS_NONE()); mrb_define_method(mrb, fixnum, "truncate", int_to_i, ARGS_NONE()); /* 15.2.8.3.26 */ mrb_define_method(mrb, fixnum, "divmod", fix_divmod, ARGS_REQ(1)); /* 15.2.8.3.30 (x) */ /* Float Class */ fl = mrb->float_class = mrb_define_class(mrb, "Float", numeric); mrb_undef_class_method(mrb, fl, "new"); mrb_define_method(mrb, fl, "+", flo_plus, ARGS_REQ(1)); /* 15.2.9.3.1 */ mrb_define_method(mrb, fl, "-", flo_minus, ARGS_REQ(1)); /* 15.2.9.3.2 */ mrb_define_method(mrb, fl, "*", flo_mul, ARGS_REQ(1)); /* 15.2.9.3.3 */ mrb_define_method(mrb, fl, "%", flo_mod, ARGS_REQ(1)); /* 15.2.9.3.5 */ mrb_define_method(mrb, fl, "==", flo_eq, ARGS_REQ(1)); /* 15.2.9.3.7 */ mrb_define_method(mrb, fl, "ceil", flo_ceil, ARGS_NONE()); /* 15.2.9.3.8 */ mrb_define_method(mrb, fl, "finite?", flo_finite_p, ARGS_NONE()); /* 15.2.9.3.9 */ mrb_define_method(mrb, fl, "floor", flo_floor, ARGS_NONE()); /* 15.2.9.3.10 */ mrb_define_method(mrb, fl, "infinite?", flo_infinite_p, ARGS_NONE()); /* 15.2.9.3.11 */ mrb_define_method(mrb, fl, "round", flo_round, ARGS_ANY()); /* 15.2.9.3.12 */ mrb_define_method(mrb, fl, "to_f", flo_to_f, ARGS_NONE()); /* 15.2.9.3.13 */ mrb_define_method(mrb, fl, "to_i", flo_truncate, ARGS_NONE()); /* 15.2.9.3.14 */ mrb_define_method(mrb, fl, "truncate", flo_truncate, ARGS_NONE()); /* 15.2.9.3.15 */ mrb_define_method(mrb, fl, "to_s", flo_to_s, ARGS_NONE()); /* 15.2.9.3.16(x) */ mrb_define_method(mrb, fl, "inspect", flo_to_s, ARGS_NONE()); }
static mrb_value mrb_groonga_init(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = mrb->ud; mrb_undef_class_method(mrb, ctx->impl->mrb.module, "init"); mrb_define_class(mrb, "LoadError", mrb_class_get(mrb, "ScriptError")); mrb_define_method(mrb, mrb->kernel_module, "load", mrb_kernel_load, MRB_ARGS_REQ(1)); { mrb_value load_path; const char *plugins_dir; const char *system_ruby_scripts_dir; load_path = mrb_ary_new(mrb); plugins_dir = grn_plugin_get_system_plugins_dir(); mrb_ary_push(mrb, load_path, mrb_str_new_cstr(mrb, plugins_dir)); system_ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx); mrb_ary_push(mrb, load_path, mrb_str_new_cstr(mrb, system_ruby_scripts_dir)); mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$LOAD_PATH"), load_path); } grn_mrb_load(ctx, "require.rb"); grn_mrb_load(ctx, "initialize/pre.rb"); grn_mrb_converter_init(ctx); grn_mrb_error_init(ctx); grn_mrb_id_init(ctx); grn_mrb_operator_init(ctx); grn_mrb_command_version_init(ctx); grn_mrb_ctx_init(ctx); grn_mrb_logger_init(ctx); grn_mrb_query_logger_init(ctx); grn_mrb_void_init(ctx); grn_mrb_bulk_init(ctx); grn_mrb_cache_init(ctx); grn_mrb_object_init(ctx); grn_mrb_object_flags_init(ctx); grn_mrb_database_init(ctx); grn_mrb_table_init(ctx); grn_mrb_array_init(ctx); grn_mrb_hash_table_init(ctx); grn_mrb_patricia_trie_init(ctx); grn_mrb_double_array_trie_init(ctx); grn_mrb_table_group_flags_init(ctx); grn_mrb_table_group_result_init(ctx); grn_mrb_table_sort_flags_init(ctx); grn_mrb_table_sort_key_init(ctx); grn_mrb_record_init(ctx); grn_mrb_column_init(ctx); grn_mrb_fixed_size_column_init(ctx); grn_mrb_variable_size_column_init(ctx); grn_mrb_index_column_init(ctx); grn_mrb_index_cursor_init(ctx); grn_mrb_type_init(ctx); grn_mrb_expr_init(ctx); grn_mrb_accessor_init(ctx); grn_mrb_procedure_init(ctx); grn_mrb_command_init(ctx); grn_mrb_command_input_init(ctx); grn_mrb_table_cursor_init(ctx); grn_mrb_table_cursor_flags_init(ctx); grn_mrb_content_type_init(ctx); grn_mrb_writer_init(ctx); grn_mrb_config_init(ctx); grn_mrb_load(ctx, "initialize/post.rb"); return mrb_nil_value(); }
// Create the MRuby object for this virtual mrb_value Create_MRuby_Object(mrb_state* p_state) { return mrb_obj_value(Data_Wrap_Struct(p_state, mrb_class_get(p_state, "Enemy"), &Scripting::rtTSC_Scriptable, this)); }
mrb_value mrb_directfb_input_device_value(mrb_state* mrb, IDirectFBInputDevice* input_device) { struct RClass* class_directfb = mrb_class_get(mrb, "DirectFB"); struct RClass* c = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(class_directfb), mrb_intern_lit(mrb, "InputDevice"))); return mrb_directfb_input_device_wrap(mrb, c, input_device); }
static struct RClass * mrb_module_get(mrb_state *mrb, const char *name) { return mrb_class_get(mrb, name); }
static struct RClass * struct_class(mrb_state *mrb) { return mrb_class_get(mrb, "Struct"); }
mrb_value Camera::init_mrb_variables(mrb_state* mrb) { auto cameraInstance = mrb_camera_wrap(mrb, mrb_class_get(mrb, "Camera"), new mrb_camera(this)); return cameraInstance; }