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; }
static mrb_value mrb_sdl2_ttf_font_get_glyph_metric(mrb_state *mrb, mrb_value self) { int minx, maxx, miny, maxy, advance; mrb_value character; Uint16 ch; mrb_value result; mrb_get_args(mrb, "b", &character); if (mrb_obj_is_kind_of(mrb, character, mrb_class_get_under(mrb, mrb->object_class, "String"))) { ch = *RSTRING_PTR(character); } else if (mrb_obj_is_kind_of(mrb, character, mrb_class_get_under(mrb, mrb->object_class, "Integer"))) { ch = (Uint16) character.value.i; } result = mrb_hash_new_capa(mrb, 5); if (TTF_GlyphMetrics(mrb_sdl2_font_get_ptr(mrb, self), ch, &minx, &maxx, &miny, &maxy, &advance) == -1) { mruby_sdl2_raise_error(mrb); return mrb_false_value(); } mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "minx"), mrb_fixnum_value(minx)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "maxx"), mrb_fixnum_value(maxx)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "miny"), mrb_fixnum_value(miny)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "maxy"), mrb_fixnum_value(maxy)); mrb_hash_set(mrb, result, mrb_str_new_cstr(mrb, "advance"), mrb_fixnum_value(advance)); return result; }
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; }
// 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; }
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()); }
bool convert_js_to_mrb_hash(NPP npp, const NPVariant &variant, mrb_state *mrb, mrb_value *result) { NPIdentifier *id_list = NULL; uint32_t count; NPN_Enumerate(npp, NPVARIANT_TO_OBJECT(variant), &id_list, &count); mrb_value ret_hash = mrb_hash_new_capa(mrb, count); for (uint32_t i=0; i<count; i++){ NPVariant item; NPN_GetProperty(npp, NPVARIANT_TO_OBJECT(variant), id_list[i], &item); mrb_value mrb_item; if (!convert_js_to_mrb(npp, item, mrb, &mrb_item)){ NPN_MemFree(id_list); NPN_ReleaseVariantValue(&item); return false; } NPN_ReleaseVariantValue(&item); NPUTF8 *key = NPN_UTF8FromIdentifier(id_list[i]); mrb_hash_set(mrb, ret_hash, mrb_str_new2(mrb, key), mrb_item); NPN_MemFree(key); } NPN_MemFree(id_list); *result = ret_hash; return true; }
static mrb_value mrb_ary_to_h(mrb_state *mrb, mrb_value ary) { mrb_int i; mrb_value v, hash; hash = mrb_hash_new_capa(mrb, 0); for (i = 0; i < RARRAY_LEN(ary); ++i) { v = mrb_check_array_type(mrb, RARRAY_PTR(ary)[i]); if (mrb_nil_p(v)) { mrb_raisef(mrb, E_TYPE_ERROR, "wrong element type %S at %S (expected array)", mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, RARRAY_PTR(ary)[i])), mrb_fixnum_value(i) ); } if (RARRAY_LEN(v) != 2) { mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong array length at %S (expected 2, was %S)", mrb_fixnum_value(i), mrb_fixnum_value(RARRAY_LEN(v)) ); } mrb_hash_set(mrb, hash, RARRAY_PTR(v)[0], RARRAY_PTR(v)[1]); } return hash; }
static mrb_value mrb_hash_replace(mrb_state *mrb, mrb_value hash) { mrb_value hash2, ifnone; khash_t(ht) *h2; khiter_t k; mrb_get_args(mrb, "o", &hash2); hash2 = to_hash(mrb, hash2); if (mrb_obj_equal(mrb, hash, hash2)) return hash; mrb_hash_clear(mrb, hash); h2 = RHASH_TBL(hash2); if (h2) { for (k = kh_begin(h2); k != kh_end(h2); k++) { if (kh_exist(h2, k)) mrb_hash_set(mrb, hash, kh_key(h2, k), kh_value(h2, k)); } } if (MRB_RHASH_PROCDEFAULT_P(hash2)) { RHASH(hash)->flags |= MRB_HASH_PROC_DEFAULT; ifnone = RHASH_PROCDEFAULT(hash2); } else { ifnone = RHASH_IFNONE(hash2); } mrb_iv_set(mrb, hash, mrb_intern_lit(mrb, "ifnone"), ifnone); return hash; }
static mrb_value mrb_http_object_headers_set_item(mrb_state *mrb, mrb_value self) { mrb_value key, value; mrb_get_args(mrb, "SS", &key, &value); mrb_hash_set(mrb, OBJECT_GET(mrb, self, "headers"), key, value); return mrb_nil_value(); }
/* * call-seq: * hsh[key] = value -> value * hsh.store(key, value) -> value * * Element Assignment---Associates the value given by * <i>value</i> with the key given by <i>key</i>. * <i>key</i> should not have its value changed while it is in * use as a key (a <code>String</code> passed as a key will be * duplicated and frozen). * * h = { "a" => 100, "b" => 200 } * h["a"] = 9 * h["c"] = 4 * h #=> {"a"=>9, "b"=>200, "c"=>4} * */ static mrb_value mrb_hash_aset(mrb_state *mrb, mrb_value self) { mrb_value key, val; mrb_get_args(mrb, "oo", &key, &val); mrb_hash_set(mrb, self, key, val); return val; }
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; }
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 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; }
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_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()); }
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()); }
static void post_response(struct st_h2o_mruby_http_request_context_t *ctx, int status, const h2o_http1client_header_t *headers_sorted, size_t num_headers) { mrb_state *mrb = ctx->generator->ctx->shared->mrb; int gc_arena = mrb_gc_arena_save(mrb); size_t i; mrb_value resp = mrb_ary_new_capa(mrb, 3); /* set status */ mrb_ary_set(mrb, resp, 0, mrb_fixnum_value(status)); /* set headers */ mrb_value headers_hash = mrb_hash_new_capa(mrb, (int)num_headers); for (i = 0; i < num_headers; ++i) { /* skip the headers, we determine the eos! */ if (h2o_memis(headers_sorted[i].name, headers_sorted[i].name_len, H2O_STRLIT("content-length")) || h2o_memis(headers_sorted[i].name, headers_sorted[i].name_len, H2O_STRLIT("transfer-encoding"))) continue; /* build and set the hash entry */ mrb_value k = mrb_str_new(mrb, headers_sorted[i].name, headers_sorted[i].name_len); mrb_value v = mrb_str_new(mrb, headers_sorted[i].value, headers_sorted[i].value_len); while (i + 1 < num_headers && h2o_memis(headers_sorted[i].name, headers_sorted[i].name_len, headers_sorted[i + 1].name, headers_sorted[i + 1].name_len)) { ++i; v = mrb_str_cat_lit(mrb, v, "\n"); v = mrb_str_cat(mrb, v, headers_sorted[i].value, headers_sorted[i].value_len); } mrb_hash_set(mrb, headers_hash, k, v); } mrb_ary_set(mrb, resp, 1, headers_hash); /* set input stream */ assert(mrb_nil_p(ctx->refs.input_stream)); ctx->refs.input_stream = h2o_mruby_create_data_instance( mrb, mrb_ary_entry(ctx->generator->ctx->shared->constants, H2O_MRUBY_HTTP_INPUT_STREAM_CLASS), ctx, &input_stream_type); mrb_ary_set(mrb, resp, 2, ctx->refs.input_stream); if (mrb_nil_p(ctx->receiver)) { /* is async */ mrb_funcall(mrb, ctx->refs.request, "_set_response", 1, resp); if (mrb->exc != NULL) { fprintf(stderr, "_set_response failed\n"); abort(); } } else { /* send response to the waiting receiver */ h2o_mruby_run_fiber(ctx->generator, detach_receiver(ctx), resp, NULL); } mrb_gc_arena_restore(mrb, gc_arena); }
static int parser_settings_on_headers_complete(http_parser* parser) { mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data; mrb_state* mrb = context->mrb; if(!mrb_nil_p(CTXV_GET(context, "last_header_field"))) { mrb_hash_set(mrb, CTXV_GET(context, "headers"), CTXV_GET(context, "last_header_field"), CTXV_GET(context, "last_header_value")); } return 1; }
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; }
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; }
void inputBindingInit(mrb_state *mrb) { RClass *module = mrb_define_module(mrb, "Input"); mrb_define_module_function(mrb, module, "update", inputUpdate, MRB_ARGS_NONE()); mrb_define_module_function(mrb, module, "press?", inputPress, MRB_ARGS_REQ(1)); mrb_define_module_function(mrb, module, "trigger?", inputTrigger, MRB_ARGS_REQ(1)); mrb_define_module_function(mrb, module, "repeat?", inputRepeat, MRB_ARGS_REQ(1)); mrb_define_module_function(mrb, module, "dir4", inputDir4, MRB_ARGS_NONE()); mrb_define_module_function(mrb, module, "dir8", inputDir8, MRB_ARGS_NONE()); mrb_define_module_function(mrb, module, "mouse_x", inputMouseX, MRB_ARGS_NONE()); mrb_define_module_function(mrb, module, "mouse_y", inputMouseY, MRB_ARGS_NONE()); mrb_value modVal = mrb_obj_value(module); if (rgssVer >= 3) { mrb_value symHash = mrb_hash_new_capa(mrb, buttonCodesN); for (size_t i = 0; i < buttonCodesN; ++i) { const char *str = buttonCodes[i].str; mrb_sym sym = mrb_intern_static(mrb, str, strlen(str)); mrb_value symVal = mrb_symbol_value(sym); mrb_value val = mrb_fixnum_value(buttonCodes[i].val); /* In RGSS3 all Input::XYZ constants are equal to :XYZ symbols, * to be compatible with the previous convention */ mrb_const_set(mrb, modVal, sym, symVal); mrb_hash_set(mrb, symHash, symVal, val); } mrb_iv_set(mrb, modVal, mrb_intern_lit(mrb, "buttoncodes"), symHash); getMrbData(mrb)->buttoncodeHash = symHash; } else { for (size_t i = 0; i < buttonCodesN; ++i) { const char *str = buttonCodes[i].str; mrb_sym sym = mrb_intern_static(mrb, str, strlen(str)); mrb_value val = mrb_fixnum_value(buttonCodes[i].val); mrb_const_set(mrb, modVal, sym, val); } } }
static int parser_settings_on_headers_complete(http_parser* parser) { mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data; mrb_state* mrb = context->mrb; int ai = mrb_gc_arena_save(mrb); if(!mrb_nil_p(OBJECT_GET(mrb, context->instance, "last_header_field"))) { mrb_hash_set(mrb, OBJECT_GET(mrb, context->instance, "headers"), OBJECT_GET(mrb, context->instance, "last_header_field"), OBJECT_GET(mrb, context->instance, "last_header_value")); } mrb_gc_arena_restore(mrb, ai); return 0; }
/* * call-seq: * struct.to_h -> hash * * Create a hash from member names and struct values. */ static mrb_value mrb_struct_to_h(mrb_state *mrb, mrb_value self) { mrb_value members, ret; mrb_int i; members = struct_s_members(mrb, mrb_class(mrb, self)); ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members)); for (i = 0; i < RARRAY_LEN(members); ++i) { mrb_hash_set(mrb, ret, RARRAY_PTR(members)[i], RSTRUCT_PTR(self)[i]); } return ret; }
static int parser_settings_on_headers_complete(http_parser* parser) { mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data; mrb_state* mrb = context->mrb; ARENA_SAVE; if(!mrb_nil_p(PARSER_GET(context, "last_header_field"))) { mrb_hash_set(mrb, PARSER_GET(context, "headers"), PARSER_GET(context, "last_header_field"), PARSER_GET(context, "last_header_value")); } ARENA_RESTORE; return 1; }
std::map<std::string, std::string> MRuby::invert(const std::map<std::string, std::string> &lis) const { std::map<std::string, std::string> res; mrb_value a = mrb_hash_new_capa(mMrb, lis.size()); for (auto it = lis.begin(); it != lis.end(); ++it) { mrb_hash_set(mMrb, a, mrb_str_new_cstr(mMrb, it->first.c_str()), mrb_str_new_cstr(mMrb, it->second.c_str())); } mrb_value r = mrb_funcall(mMrb, mrb_top_self(mMrb), "invert", 1, a); mrb_value keys = mrb_hash_keys(mMrb, r); for (int i = 0; i < RARRAY_LEN(keys); ++i) { mrb_value k = mrb_ary_ref(mMrb, keys, i); mrb_value v = mrb_hash_get(mMrb, r, k); res[mrb_str_to_cstr(mMrb, k)] = mrb_str_to_cstr(mMrb, v); } return res; }
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(); }
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; }
static int parser_settings_on_header_value(http_parser* parser, const char* at, size_t len) { mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data; mrb_state* mrb = context->mrb; int ai = mrb_gc_arena_save(mrb); if(!context->was_header_value) { OBJECT_SET(mrb, context->instance, "last_header_value", mrb_str_new(mrb, at, len)); context->was_header_value = TRUE; mrb_hash_set(mrb, OBJECT_GET(mrb, context->instance, "headers"), OBJECT_GET(mrb, context->instance, "last_header_field"), OBJECT_GET(mrb, context->instance, "last_header_value")); } else { mrb_str_concat(mrb, OBJECT_GET(mrb, context->instance, "last_header_value"), mrb_str_new(mrb, at, len)); } mrb_gc_arena_restore(mrb, ai); return 0; }