void mrb_ev3_lcd_init(mrb_state *mrb, struct RClass *ev3) { struct RClass *lcd; mrb_value lcdo; mrb_value font; mrb_value col; /* LCD class */ lcd = mrb_define_class_under(mrb, ev3, "LCD", mrb->object_class); lcdo = mrb_obj_value(lcd); font = mrb_hash_new(mrb); mrb_hash_set(mrb, font, mrb_symbol_value(mrb_intern_lit(mrb, "small")), mrb_fixnum_value(EV3_FONT_SMALL)); mrb_hash_set(mrb, font, mrb_symbol_value(mrb_intern_lit(mrb, "medium")), mrb_fixnum_value(EV3_FONT_MEDIUM)); mrb_const_set(mrb, lcdo, mrb_intern_lit(mrb, "FONT"), font); col = mrb_hash_new(mrb); mrb_hash_set(mrb, col, mrb_symbol_value(mrb_intern_lit(mrb, "white")), mrb_fixnum_value(EV3_LCD_WHITE)); mrb_hash_set(mrb, col, mrb_symbol_value(mrb_intern_lit(mrb, "black")), mrb_fixnum_value(EV3_LCD_BLACK)); mrb_const_set(mrb, lcdo, mrb_intern_lit(mrb, "COLOR"), col); mrb_const_set(mrb, lcdo, mrb_intern_lit(mrb, "WIDTH"), mrb_fixnum_value(EV3_LCD_WIDTH)); mrb_const_set(mrb, lcdo, mrb_intern_lit(mrb, "HEIGHT"), mrb_fixnum_value(EV3_LCD_HEIGHT)); mrb_mod_cv_set(mrb, lcd, mrb_intern_lit(mrb, "@@font"), mrb_fixnum_value(EV3_FONT_SMALL)); mrb_define_class_method(mrb, lcd, "font=", mrb_lcd_set_font, MRB_ARGS_REQ(1)); mrb_define_class_method(mrb, lcd, "draw", mrb_lcd_draw_string, MRB_ARGS_REQ(3)); mrb_define_method(mrb, lcd, "initialize", mrb_lcd_init, MRB_ARGS_OPT(6)); mrb_define_method(mrb, lcd, "print", mrb_lcd_print, MRB_ARGS_ANY()); mrb_define_method(mrb, lcd, "puts", mrb_lcd_puts, MRB_ARGS_ANY()); mrb_define_method(mrb, lcd, "locate", mrb_lcd_locate, MRB_ARGS_ARG(1, 1)); mrb_define_method(mrb, lcd, "clear", mrb_lcd_clear, MRB_ARGS_OPT(1)); mrb_define_method(mrb, lcd, "font", mrb_lcd_get_font, MRB_ARGS_NONE()); mrb_define_method(mrb, lcd, "left", mrb_lcd_get_left, MRB_ARGS_NONE()); mrb_define_method(mrb, lcd, "top", mrb_lcd_get_top, MRB_ARGS_NONE()); mrb_define_method(mrb, lcd, "width", mrb_lcd_get_width, MRB_ARGS_NONE()); mrb_define_method(mrb, lcd, "height", mrb_lcd_get_height, MRB_ARGS_NONE()); mrb_define_method(mrb, lcd, "cx", mrb_lcd_get_csrx, MRB_ARGS_NONE()); mrb_define_method(mrb, lcd, "cy", mrb_lcd_get_csry, MRB_ARGS_NONE()); mrb_define_method(mrb, lcd, "color", mrb_lcd_get_color, MRB_ARGS_NONE()); // mrb_undef_method(mrb, mrb->kernel_module, "__printstr__"); // mrb_define_method(mrb, mrb->kernel_module, "__printstr__", mrb_lcd_print, MRB_ARGS_ANY()); }
static mrb_value mrb_magnetic_s__read(mrb_state *mrb, mrb_value self) { mrb_int ret; mrb_value hash; BYTE abTk1[256], abTk2[256], abTk3[256]; UINT uiLen1 = 0,uiLen2 = 0,uiLen3 = 0 ; uiLen1 = sizeof(abTk1); uiLen2 = sizeof(abTk2); uiLen3 = sizeof(abTk3); ret = GEDI_MSR_Read(abTk1, &uiLen1, abTk2, &uiLen2, abTk3, &uiLen3); hash = mrb_hash_new(mrb); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "ret")), mrb_fixnum_value(ret)); if (ret == GEDI_MSR_STATUS_SUCCESS) { mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "track1")), mrb_str_new(mrb, abTk1, uiLen1)); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "track2")), mrb_str_new(mrb, abTk2, uiLen2)); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "track3")), mrb_str_new(mrb, abTk3, uiLen3)); } return hash; }
mrb_value mrb_mongo_collection_update(mrb_state *mrb, mrb_value self) { mrb_mongo_collection_data *data = DATA_PTR(self); mrb_value selector_hash, update_hash; mrb_bool upsert; bson_t *selector, *update; bson_error_t error; mongoc_update_flags_t flags; flags = MONGOC_UPDATE_MULTI_UPDATE; mrb_get_args(mrb, "HH|b", &selector_hash, &update_hash, &upsert); if (!mrb_hash_p(selector_hash)) { selector_hash = mrb_hash_new(mrb); } if (upsert) flags = flags | MONGOC_UPDATE_UPSERT; selector = bson_new(); update = bson_new(); mrb_hash_to_bson(mrb, selector_hash, selector); mrb_hash_to_bson(mrb, update_hash, update); if (!mongoc_collection_update(data->collection, flags, selector, update, NULL, &error)) { bson_destroy(selector); bson_destroy(update); mrb_raise(mrb, E_RUNTIME_ERROR, error.message); } bson_destroy(selector); bson_destroy(update); return mrb_nil_value(); }
mrb_value mrb_mongo_collection_insert(mrb_state *mrb, mrb_value self) { mrb_mongo_collection_data *data = DATA_PTR(self); mrb_value record_hash, inserted_hash; bson_t *doc; bson_oid_t oid; bson_error_t error; mrb_get_args(mrb, "H", &record_hash); doc = bson_new(); mrb_hash_to_bson(mrb, record_hash, doc); //add id if not supplied if (!bson_has_field(doc, "_id")) { bson_oid_init(&oid, NULL); bson_append_oid(doc, "_id", -1, &oid); } if (!mongoc_collection_insert(data->collection, MONGOC_INSERT_NONE, doc, NULL, &error)) { bson_destroy(doc); mrb_raise(mrb, E_RUNTIME_ERROR, error.message); } inserted_hash = mrb_hash_new(mrb); bson_to_mrb_hash(mrb, doc, inserted_hash); bson_destroy(doc); return inserted_hash; }
void mrb_ev3_button_init(mrb_state *mrb, struct RClass *ev3) { struct RClass *btn; mrb_value btno; mrb_value keys; /* Button class */ btn = mrb_define_class_under(mrb, ev3, "Button", mrb->object_class); btno = mrb_obj_value(btn); keys = mrb_hash_new(mrb); mrb_hash_set(mrb, keys, mrb_symbol_value(mrb_intern_lit(mrb, "left")), mrb_fixnum_value(LEFT_BUTTON)); mrb_hash_set(mrb, keys, mrb_symbol_value(mrb_intern_lit(mrb, "right")), mrb_fixnum_value(RIGHT_BUTTON)); mrb_hash_set(mrb, keys, mrb_symbol_value(mrb_intern_lit(mrb, "up")), mrb_fixnum_value(UP_BUTTON)); mrb_hash_set(mrb, keys, mrb_symbol_value(mrb_intern_lit(mrb, "down")), mrb_fixnum_value(DOWN_BUTTON)); mrb_hash_set(mrb, keys, mrb_symbol_value(mrb_intern_lit(mrb, "enter")), mrb_fixnum_value(ENTER_BUTTON)); mrb_hash_set(mrb, keys, mrb_symbol_value(mrb_intern_lit(mrb, "back")), mrb_fixnum_value(BACK_BUTTON)); mrb_const_set(mrb, btno, mrb_intern_lit(mrb, "KEY"), keys); mrb_define_class_method(mrb, btn, "[]", mrb_btn_get, MRB_ARGS_REQ(1)); mrb_define_method(mrb, btn, "initialize", mrb_btn_init, MRB_ARGS_REQ(1)); mrb_define_method(mrb, btn, "pressed?", mrb_btn_pressed, MRB_ARGS_NONE()); }
mrb_value mrb_io_initialize(mrb_state *mrb, mrb_value io) { struct mrb_io *fptr; mrb_int fd, flags; mrb_value mode, opt; DATA_TYPE(io) = &mrb_io_type; DATA_PTR(io) = NULL; mode = opt = mrb_nil_value(); mrb_get_args(mrb, "i|So", &fd, &mode, &opt); if (mrb_nil_p(mode)) { mode = mrb_str_new_cstr(mrb, "r"); } if (mrb_nil_p(opt)) { opt = mrb_hash_new(mrb); } flags = mrb_io_modestr_to_flags(mrb, mrb_string_value_cstr(mrb, &mode)); mrb_iv_set(mrb, io, mrb_intern(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); mrb_iv_set(mrb, io, mrb_intern(mrb, "@pos"), mrb_fixnum_value(0)); fptr = DATA_PTR(io); if (fptr == NULL) { fptr = mrb_io_alloc(mrb); } fptr->fd = fd; DATA_PTR(io) = fptr; return io; }
static mrb_value mrb_http_object_initialize(mrb_state *mrb, mrb_value self) { OBJECT_SET(mrb, self, "headers", mrb_hash_new(mrb)); OBJECT_SET(mrb, self, "body", mrb_nil_value()); OBJECT_SET(mrb, self, "method", mrb_str_new_cstr(mrb, "GET")); return self; }
static mrb_value os_count_objects(mrb_state *mrb, mrb_value self) { struct os_count_struct obj_count = { 0 }; mrb_int i; mrb_value hash; if (mrb_get_args(mrb, "|H", &hash) == 0) { hash = mrb_hash_new(mrb); } if (!mrb_test(mrb_hash_empty_p(mrb, hash))) { mrb_hash_clear(mrb, hash); } mrb_objspace_each_objects(mrb, os_count_object_type, &obj_count); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_lit(mrb, "TOTAL")), mrb_fixnum_value(obj_count.total)); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_lit(mrb, "FREE")), mrb_fixnum_value(obj_count.freed)); for (i = MRB_TT_FALSE; i < MRB_TT_MAXDEFINE; i++) { mrb_value type; switch (i) { #define COUNT_TYPE(t) case (MRB_T ## t): type = mrb_symbol_value(mrb_intern_lit(mrb, #t)); break; COUNT_TYPE(T_FALSE); COUNT_TYPE(T_FREE); COUNT_TYPE(T_TRUE); COUNT_TYPE(T_FIXNUM); COUNT_TYPE(T_SYMBOL); COUNT_TYPE(T_UNDEF); COUNT_TYPE(T_FLOAT); COUNT_TYPE(T_CPTR); COUNT_TYPE(T_OBJECT); COUNT_TYPE(T_CLASS); COUNT_TYPE(T_MODULE); COUNT_TYPE(T_ICLASS); COUNT_TYPE(T_SCLASS); COUNT_TYPE(T_PROC); COUNT_TYPE(T_ARRAY); COUNT_TYPE(T_HASH); COUNT_TYPE(T_STRING); COUNT_TYPE(T_RANGE); COUNT_TYPE(T_EXCEPTION); COUNT_TYPE(T_FILE); COUNT_TYPE(T_ENV); COUNT_TYPE(T_DATA); COUNT_TYPE(T_FIBER); #undef COUNT_TYPE default: type = mrb_fixnum_value(i); break; } if (obj_count.counts[i]) mrb_hash_set(mrb, hash, type, mrb_fixnum_value(obj_count.counts[i])); } return hash; }
/* * call-seq: * Hash(arg) -> hash * * Converts <i>arg</i> to a <code>Hash</code> by calling * <i>arg</i><code>.to_hash</code>. Returns an empty <code>Hash</code> when * <i>arg</i> is <tt>nil</tt> or <tt>[]</tt>. * * Hash([]) #=> {} * Hash(nil) #=> {} * Hash(key: :value) #=> {:key => :value} * Hash([1, 2, 3]) #=> TypeError * */ static mrb_value mrb_f_hash(mrb_state *mrb, mrb_value self) { mrb_value arg, tmp; mrb_get_args(mrb, "o", &arg); if (mrb_nil_p(arg)) { return mrb_hash_new(mrb); } tmp = mrb_check_convert_type(mrb, arg, MRB_TT_HASH, "Hash", "to_hash"); if (mrb_nil_p(tmp)) { if (mrb_array_p(arg) && RARRAY_LEN(arg) == 0) { return mrb_hash_new(mrb); } mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into Hash", mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, arg))); } return tmp; }
void mrb_mruby_rtos_toppers_gem_init(mrb_state *mrb) { struct RClass *rtos; struct RClass *tsk; mrb_value wmd; mrb_value tmo; /* RTOS module */ rtos = mrb_define_module(mrb, "RTOS"); /* RTOS api */ mrb_define_module_function(mrb, rtos, "delay", mrb_rtos_delay, MRB_ARGS_REQ(1)); mrb_define_module_function(mrb, rtos, "millis", mrb_rtos_millis, MRB_ARGS_NONE()); wmd = mrb_hash_new(mrb); mrb_hash_set(mrb, wmd, mrb_symbol_value(mrb_intern_lit(mrb, "and")), mrb_fixnum_value(TWF_ANDW)); mrb_hash_set(mrb, wmd, mrb_symbol_value(mrb_intern_lit(mrb, "or")), mrb_fixnum_value(TWF_ORW)); mrb_mod_cv_set(mrb, rtos, mrb_intern_lit(mrb, "WAITMODE"), wmd); tmo = mrb_hash_new(mrb); mrb_hash_set(mrb, tmo, mrb_symbol_value(mrb_intern_lit(mrb, "polling")), mrb_fixnum_value(TMO_POL)); mrb_hash_set(mrb, tmo, mrb_symbol_value(mrb_intern_lit(mrb, "forever")), mrb_fixnum_value(TMO_FEVR)); mrb_mod_cv_set(mrb, rtos, mrb_intern_lit(mrb, "TIMEOUT"), tmo); /* Task class */ tsk = mrb_define_class_under(mrb, rtos, "Task", mrb->object_class); mrb_define_method(mrb, tsk, "initialize", mrb_task_init, MRB_ARGS_REQ(1)); mrb_define_method(mrb, tsk, "activate", mrb_task_activate, MRB_ARGS_NONE()); mrb_define_method(mrb, tsk, "suspend", mrb_task_suspend, MRB_ARGS_NONE()); mrb_define_method(mrb, tsk, "resume", mrb_task_resume, MRB_ARGS_NONE()); mrb_define_method(mrb, tsk, "terminate", mrb_task_terminate, MRB_ARGS_NONE()); /* EventFlag class */ mrb_rtos_flag_init(mrb, rtos); /* MemoryPool, MemoryBuffer */ mrb_rtos_memory_init(mrb, rtos); /* DataQueue */ mrb_rtos_dataqueue_init(mrb, rtos); }
static mrb_value mrb_haconiwa_mrgbem_revisions(mrb_state *mrb, mrb_value self) { mrb_value ha = mrb_hash_new(mrb); for (int i = 0; GEMS[i].gemname != NULL; ++i) { mrb_hash_set(mrb, ha, mrb_str_new_cstr(mrb, GEMS[i].gemname), mrb_str_new_cstr(mrb, GEMS[i].revision)); } return ha; }
nhash * nhash_new(void) { nhash *h; h = malloc(sizeof(nhash)); h->_hash = mrb_hash_new(__LIBN_R); return h; }
nstringhash * nstringhash_new(void) { nstringhash *hash; hash = malloc(sizeof(nstringhash)); hash->_hash = mrb_hash_new(__LIBN_R); return hash; }
static mrb_value signal_list(mrb_state *mrb, mrb_value mod) { mrb_value h = mrb_hash_new(mrb); const struct signals *sigs; for (sigs = siglist; sigs->signm; sigs++) { mrb_hash_set(mrb, h, mrb_str_new_cstr(mrb, sigs->signm), mrb_fixnum_value(sigs->signo)); } return h; }
mrb_value ccvaluemap_to_rubyval(mrb_state* mrb, const cocos2d::ValueMap& inValue) { mrb_value rhash = mrb_hash_new(mrb); for (auto iter = inValue.begin(); iter != inValue.end(); ++iter) { std::string key = iter->first; const Value& obj = iter->second; mrb_value hkey = mrb_str_new_cstr(mrb, key.c_str()); mrb_value hval = ccvalue_to_rubyval(mrb, obj); mrb_hash_set(mrb, rhash, hkey, hval); } return rhash; }
// FLTK.measure(text) // Get the width and height of a string, according to the last call to FLTK.font. mrb_value mrb_fltk_measure_module_method( mrb_state *mrb, mrb_value self ) { int width = 0; int height = 0; char *text; mrb_get_args( mrb, "z", &text ); fl_measure( text, width, height ); mrb_value result = mrb_hash_new( mrb ); mrb_hash_set( mrb, result, mrb_symbol_value( mrb_intern_cstr( mrb, "width" ) ), mrb_fixnum_value( width ) ); mrb_hash_set( mrb, result, mrb_symbol_value( mrb_intern_cstr( mrb, "height" ) ), mrb_fixnum_value( height ) ); return result; }
mrb_value dictionary_to_rubyval(mrb_state* mrb, __Dictionary* dict) { mrb_value rhash = mrb_hash_new(mrb); DictElement* element = nullptr; std::string className = ""; __String* strVal = nullptr; __Dictionary* dictVal = nullptr; __Array* arrVal = nullptr; __Double* doubleVal = nullptr; __Bool* boolVal = nullptr; __Float* floatVal = nullptr; __Integer* intVal = nullptr; CCDICT_FOREACH(dict, element) { if (nullptr == element) continue; mrb_value rkey = mrb_str_new_cstr(mrb, element->getStrKey()); mrb_value rval; std::string typeName = typeid(element->getObject()).name(); auto iter = g_rubyType.find(typeName); if (g_rubyType.end() != iter) { className = iter->second; if (nullptr != dynamic_cast<Ref*>(element->getObject())) { rval = to_mrb_value(mrb, element->getObject()); } } else if((strVal = dynamic_cast<__String *>(element->getObject()))) { rval = mrb_str_new_cstr(mrb, strVal->getCString()); } else if ((dictVal = dynamic_cast<__Dictionary*>(element->getObject()))) { rval = dictionary_to_rubyval(mrb, dictVal); } else if ((arrVal = dynamic_cast<__Array*>(element->getObject()))) { rval = array_to_rubyval(mrb, arrVal); } else if ((doubleVal = dynamic_cast<__Double*>(element->getObject()))) { rval = mrb_float_value(mrb, (mrb_float)doubleVal->getValue()); } else if ((floatVal = dynamic_cast<__Float*>(element->getObject()))) { rval = mrb_float_value(mrb, (mrb_float)floatVal->getValue()); } else if ((intVal = dynamic_cast<__Integer*>(element->getObject()))) { rval = mrb_fixnum_value((mrb_int)intVal->getValue()); } else if ((boolVal = dynamic_cast<__Bool*>(element->getObject()))) { rval = mrb_bool_value((mrb_bool)boolVal->getValue()); } else { CCASSERT(false, "the type isn't suppored."); } mrb_hash_set(mrb, rhash, rkey, rval); } return rhash; }
static mrb_value ap_mrb_get_request_headers_out_hash(mrb_state *mrb, mrb_value str) { int i; mrb_value hash = mrb_hash_new(mrb); request_rec *r = ap_mrb_get_request(); const apr_array_header_t *arr = apr_table_elts(r->headers_out); apr_table_entry_t *elts = (apr_table_entry_t *)arr->elts; for (i = 0; i < arr->nelts; i++) { mrb_hash_set(mrb, hash, mrb_str_new(mrb, elts[i].key, strlen(elts[i].key)), mrb_str_new(mrb, elts[i].val, strlen(elts[i].val))); } return hash; }
static mrb_value json_value_to_mrb_value(mrb_state* mrb, JSON_Value* value) { ARENA_SAVE; switch (json_value_get_type(value)) { case JSONError: case JSONNull: return mrb_nil_value(); case JSONString: return mrb_str_new_cstr(mrb, json_value_get_string(value)); case JSONNumber: return mrb_float_value(json_value_get_number(value)); case JSONObject: { mrb_value hash = mrb_hash_new(mrb); JSON_Object* object = json_value_get_object(value); size_t count = json_object_get_count(object); int n; for (n = 0; n < count; n++) { const char* name = json_object_get_name(object, n); mrb_hash_set(mrb, hash, mrb_str_new_cstr(mrb, name), json_value_to_mrb_value(mrb, json_object_get_value(object, name))); } return hash; } case JSONArray: { mrb_value ary; ary = mrb_ary_new(mrb); JSON_Array* array = json_value_get_array(value); size_t count = json_array_get_count(array); int n; for (n = 0; n < count; n++) { JSON_Value* elem = json_array_get_value(array, n); mrb_ary_push(mrb, ary, json_value_to_mrb_value(mrb, elem)); } return ary; } case JSONBoolean: if (json_value_get_boolean(value)) { return mrb_true_value(); } return mrb_false_value(); default: mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } return mrb_nil_value(); }
mrb_value mrb_mongo_collection_query(mrb_state *mrb, mrb_value self) { mrb_value hash_query, query; mrb_value query_params[2]; mrb_get_args(mrb, "|H", &hash_query); if (!mrb_hash_p(hash_query)) { hash_query = mrb_hash_new(mrb); } struct RClass *_class_mongo = mrb_module_get(mrb, "Mongo"); struct RClass *_class_mongo_query = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_mongo), mrb_intern_lit(mrb, "Query"))); query_params[0] = self; query_params[1] = hash_query; query = mrb_obj_new(mrb, _class_mongo_query, 2, query_params); return query; }
static mrb_value Cache__shm_status(mrb_state *mrb, mrb_value self) { mrb_value hash = mrb_hash_new(mrb); local_memcache_t *lmc = get_Cache(mrb, self); if (!lmc_lock_shm_region("shm_status", lmc)) return mrb_nil_value(); lmc_mem_status_t ms = lmc_status(lmc->base, "shm_status"); if (!lmc_unlock_shm_region("shm_status", lmc)) return mrb_nil_value(); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "free_bytes")), mrb_fixnum_value(ms.total_free_mem)); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "total_bytes")), mrb_fixnum_value(ms.total_shm_size)); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "used_bytes")), mrb_fixnum_value(ms.total_shm_size - ms.total_free_mem)); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "free_chunks")), mrb_fixnum_value(ms.free_chunks)); mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "largest_chunk")), mrb_fixnum_value(ms.largest_chunk)); return hash; }
static mrb_value ap_mrb_get_env_hash(mrb_state *mrb, mrb_value str) { int i, ai; mrb_value hash = mrb_hash_new(mrb); request_rec *r = ap_mrb_get_request(); apr_table_t *e = r->subprocess_env; const apr_array_header_t *arr = apr_table_elts(e); apr_table_entry_t *elts = (apr_table_entry_t *)arr->elts; ai = mrb_gc_arena_save(mrb); for (i = 0; i < arr->nelts; i++) { mrb_hash_set(mrb , hash , mrb_str_new(mrb, elts[i].key, strlen(elts[i].key)) , mrb_str_new(mrb, elts[i].val, strlen(elts[i].val)) ); mrb_gc_arena_restore(mrb, ai); } return hash; }
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_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_http_parser_parse_request(mrb_state *mrb, mrb_value self) { mrb_value arg_data = mrb_nil_value(); mrb_value value_context; mrb_http_parser_context* context; mrb_value b = mrb_nil_value(); value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "context")); Data_Get_Struct(mrb, value_context, &http_parser_context_type, context); if (!context) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } mrb_get_args(mrb, "|&o", &b, &arg_data); if (mrb_nil_p(arg_data)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } context->proc = b; context->parser.data = context; context->was_header_value = TRUE; PARSER_SET(context, "headers", mrb_hash_new(mrb)); http_parser_init(&context->parser, HTTP_REQUEST); context->settings.on_url = parser_settings_on_url; context->settings.on_header_field = parser_settings_on_header_field; context->settings.on_header_value = parser_settings_on_header_value; context->settings.on_headers_complete = parser_settings_on_headers_complete; if (!mrb_nil_p(b)) { context->settings.on_message_complete = parser_settings_on_message_complete; } if (RSTRING_LEN(arg_data) > 0) { char* data = RSTRING_PTR(arg_data); size_t len = RSTRING_LEN(arg_data); http_parser_execute(&context->parser, &context->settings, data, len); } return mrb_nil_value(); }
void mrb_ev3_led_init(mrb_state *mrb, struct RClass *ev3) { struct RClass *led; mrb_value ledo; mrb_value col; /* LED class */ led = mrb_define_class_under(mrb, ev3, "LED", mrb->object_class); ledo = mrb_obj_value(led); col = mrb_hash_new(mrb); mrb_hash_set(mrb, col, mrb_symbol_value(mrb_intern_lit(mrb, "off")), mrb_fixnum_value(LED_OFF)); mrb_hash_set(mrb, col, mrb_symbol_value(mrb_intern_lit(mrb, "red")), mrb_fixnum_value(LED_RED)); mrb_hash_set(mrb, col, mrb_symbol_value(mrb_intern_lit(mrb, "green")), mrb_fixnum_value(LED_GREEN)); mrb_hash_set(mrb, col, mrb_symbol_value(mrb_intern_lit(mrb, "orange")), mrb_fixnum_value(LED_ORANGE)); mrb_const_set(mrb, ledo, mrb_intern_lit(mrb, "COLOR"), col); mrb_define_class_method(mrb, led, "color=", mrb_led_color, MRB_ARGS_REQ(1)); mrb_define_class_method(mrb, led, "off", mrb_led_off, MRB_ARGS_NONE()); }
mrb_value mrb_mongo_collection_drop(mrb_state *mrb, mrb_value self) { mrb_mongo_collection_data *data = DATA_PTR(self); mrb_value drop_hash; bson_t *doc; bson_error_t error; mrb_get_args(mrb, "|H", &drop_hash); if (!mrb_hash_p(drop_hash)) { drop_hash = mrb_hash_new(mrb); } doc = bson_new(); mrb_hash_to_bson(mrb, drop_hash, doc); if (!mongoc_collection_remove(data->collection, MONGOC_REMOVE_NONE, doc, NULL, &error)) { bson_destroy(doc); mrb_raise(mrb, E_RUNTIME_ERROR, error.message); } return mrb_nil_value(); }
static mrb_value mrb_http_parser_parse(mrb_state *mrb, mrb_value self) { mrb_value arg_data; mrb_value value_context; mrb_http_parser_context* context; struct RProc *b = NULL; value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "parser_context")); Data_Get_Struct(mrb, value_context, &http_parser_context_type, context); if (!context) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } mrb_get_args(mrb, "bo", &b, &arg_data); if (mrb_nil_p(arg_data)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } mrb_iv_set(mrb, self, mrb_intern(mrb, "complete_cb"), b ? mrb_obj_value(b) : mrb_nil_value()); context->parser.data = context; context->was_header_value = TRUE; CTXV_SET(context, "headers", mrb_hash_new(mrb, 32)); http_parser_init(&context->parser, HTTP_REQUEST); context->settings.on_url = parser_settings_on_url; context->settings.on_header_field = parser_settings_on_header_field; context->settings.on_header_value = parser_settings_on_header_value; context->settings.on_headers_complete = parser_settings_on_headers_complete; if (b) { context->settings.on_message_complete = parser_settings_on_message_complete; } if (RSTRING_LEN(arg_data) > 0) { http_parser_execute(&context->parser, &context->settings, (char*) RSTRING_PTR(arg_data), RSTRING_CAPA(arg_data)); } return mrb_nil_value(); }
static mrb_value mrb_env_to_hash(mrb_state *mrb, mrb_value self) { int i; mrb_value hash; int ai = mrb_gc_arena_save(mrb); hash = mrb_hash_new(mrb); for (i = 0; environ[i] != NULL; i++) { char *str = strchr(environ[i], '='); if (str != NULL) { int len = str - environ[i]; mrb_value key = mrb_str_new(mrb, environ[i], len); str++; mrb_value val = mrb_str_new(mrb, str, strlen(str)); mrb_hash_set(mrb, hash, key, val); mrb_gc_arena_restore(mrb, ai); } } return hash; }
mrb_value mrb_io_initialize(mrb_state *mrb, mrb_value io) { struct mrb_io *fptr; mrb_int fd; mrb_value mode, opt; int flags; mode = opt = mrb_nil_value(); mrb_get_args(mrb, "i|So", &fd, &mode, &opt); if (mrb_nil_p(mode)) { mode = mrb_str_new_cstr(mrb, "r"); } if (mrb_nil_p(opt)) { opt = mrb_hash_new(mrb); } flags = mrb_io_modestr_to_flags(mrb, mrb_string_value_cstr(mrb, &mode)); mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@buf"), mrb_str_new_cstr(mrb, "")); mrb_iv_set(mrb, io, mrb_intern_cstr(mrb, "@pos"), mrb_fixnum_value(0)); fptr = DATA_PTR(io); if (fptr != NULL) { fptr_finalize(mrb, fptr, 0); mrb_free(mrb, fptr); } fptr = mrb_io_alloc(mrb); DATA_TYPE(io) = &mrb_io_type; DATA_PTR(io) = fptr; fptr->fd = fd; fptr->writable = ((flags & FMODE_WRITABLE) != 0); fptr->sync = 0; return io; }