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 ssize_t mrb_wslay_event_recv_callback(wslay_event_context_ptr ctx, uint8_t *buf, size_t len, int flags, void *user_data) { mrb_wslay_user_data *data = (mrb_wslay_user_data *) user_data; mrb_state*mrb = data->mrb; int ai = mrb_gc_arena_save(mrb); struct mrb_jmpbuf *prev_jmp = mrb->jmp; struct mrb_jmpbuf c_jmp; mrb_int ret = -1; MRB_TRY(&c_jmp) { mrb->jmp = &c_jmp; mrb_value argv[2]; argv[0] = mrb_cptr_value(mrb, buf); argv[1] = mrb_fixnum_value(len); errno = 0; mrb_assert(mrb_type(data->recv_callback) == MRB_TT_PROC); mrb_value buf_obj = mrb_yield_argv(mrb, data->recv_callback, NELEMS(argv), argv); if (mrb_fixnum_p(buf_obj)) { ret = mrb_fixnum(buf_obj); } else { buf_obj = mrb_str_to_str(mrb, buf_obj); ret = RSTRING_LEN(buf_obj); if (ret < 0||ret > len) { mrb_raise(mrb, E_RANGE_ERROR, "returned buf doesn't fit"); } if (ret > 0) { memmove(buf, (uint8_t *) RSTRING_PTR(buf_obj), ret); } } mrb->jmp = prev_jmp; } MRB_CATCH(&c_jmp) { mrb->jmp = prev_jmp; if (mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EAGAIN"))|| mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EWOULDBLOCK"))) { mrb->exc = NULL; wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK); } else { wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE); } } MRB_END_EXC(&c_jmp); mrb_gc_arena_restore(mrb, ai); return ret; }
static mrb_value mrb_sdl2_ttf_font_initialize(mrb_state *mrb, mrb_value self) { TTF_Font *font = NULL; mrb_sdl2_ttf_font_data_t *data = (mrb_sdl2_ttf_font_data_t*)DATA_PTR(self); if (NULL == data) { data = (mrb_sdl2_ttf_font_data_t*)mrb_malloc(mrb, sizeof(mrb_sdl2_ttf_font_data_t)); if (NULL == data) { mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory."); } data->font = NULL; } if (2 == mrb->c->ci->argc) { SDL_RWops *rwops; mrb_value object; mrb_int size; mrb_get_args(mrb, "oi", &object, &size); if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mrb->object_class, "String"))) { font = TTF_OpenFont(RSTRING_PTR(object), size); } else if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mod_SDL2, "RWops"))) { rwops = mrb_sdl2_rwops_get_ptr(mrb, object); font = TTF_OpenFontRW(rwops, 0, size); } } else if (3 == mrb->c->ci->argc) { SDL_RWops *rwops; mrb_value object; mrb_int size, index; mrb_get_args(mrb, "oii", &object, &size, &index); if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mrb->object_class, "String"))) { font = TTF_OpenFontIndex(RSTRING_PTR(object), size, index); } else if (mrb_obj_is_kind_of(mrb, object, mrb_class_get_under(mrb, mod_SDL2, "RWops"))) { rwops = mrb_sdl2_rwops_get_ptr(mrb, object); font = TTF_OpenFontIndexRW(rwops, 0, size, index); } } else { mrb_free(mrb, data); mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments."); } if (NULL == font) { mrb_free(mrb, data); mruby_sdl2_raise_error(mrb); } data->font = font; DATA_PTR(self) = data; DATA_TYPE(self) = &mrb_sdl2_ttf_font_data_type; return self; }
static mrb_value mrb_sdl2_ttf_font_get_glyph_index(mrb_state *mrb, mrb_value self) { mrb_value character; Uint16 ch; 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; } return mrb_fixnum_value(TTF_GlyphIsProvided(mrb_sdl2_font_get_ptr(mrb, self), ch)); }
static mrb_value mrb_sdl2_mixer_music_initialize(mrb_state *mrb, mrb_value self) { Mix_Music *music = NULL; mrb_sdl2_mixer_music_data_t *data = (mrb_sdl2_mixer_music_data_t*)DATA_PTR(self); if (NULL == data) { data = (mrb_sdl2_mixer_music_data_t*)mrb_malloc(mrb, sizeof(mrb_sdl2_mixer_music_data_t)); if (NULL == data) { mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory."); } data->music = NULL; } if (1 == mrb->c->ci->argc) { mrb_value obj; SDL_RWops *rwops; mrb_get_args(mrb, "o", &obj); if (mrb_obj_is_kind_of(mrb, obj, mrb_class_get_under(mrb, mrb->object_class, "String"))) { music = Mix_LoadMUS(RSTRING_PTR(obj)); } else if (mrb_obj_is_kind_of(mrb, obj, mrb_class_get_under(mrb, mod_SDL2, "RWops"))) { rwops = mrb_sdl2_rwops_get_ptr(mrb, obj); music = Mix_LoadMUS_RW(rwops, 0); } else { mrb_free(mrb, data); mrb_raise(mrb, E_RUNTIME_ERROR, "unexpected type."); } } else if (2 == mrb->c->ci->argc) { mrb_value rw; mrb_int type; SDL_RWops *rwops; mrb_get_args(mrb, "oi", &rw, &type); rwops = mrb_sdl2_rwops_get_ptr(mrb, rw); music = Mix_LoadMUSType_RW(rwops, (Mix_MusicType) type, 0); } else { mrb_free(mrb, data); mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments."); } if (NULL == music) { mrb_free(mrb, data); mruby_sdl2_raise_error(mrb); } data->music = music; DATA_PTR(self) = data; DATA_TYPE(self) = &mrb_sdl2_mixer_music_data_type; return self; }
void grn_mrb_index_column_init(grn_ctx *ctx) { grn_mrb_data *data = &(ctx->impl->mrb); mrb_state *mrb = data->state; struct RClass *module = data->module; struct RClass *column_class; struct RClass *klass; column_class = mrb_class_get_under(mrb, module, "Column"); klass = mrb_define_class_under(mrb, module, "IndexColumn", column_class); MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); mrb_define_method(mrb, klass, "initialize", mrb_grn_index_column_initialize, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "lexicon", mrb_grn_index_column_get_lexicon, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "estimate_size_for_term_id", mrb_grn_index_column_estimate_size_for_term_id, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "estimate_size_for_query", mrb_grn_index_column_estimate_size_for_query, MRB_ARGS_ARG(1, 1)); mrb_define_method(mrb, klass, "estimate_size_for_lexicon_cursor", mrb_grn_index_column_estimate_size_for_lexicon_cursor, MRB_ARGS_REQ(1)); }
mrb_value mrb_redis_connect(mrb_state *mrb, mrb_value self) { mrb_value host, port; mrb_int timeout = 1; struct timeval timeout_struct = { timeout, 0 }; redisContext *rc = (redisContext *)DATA_PTR(self); if (rc) { mrb_free(mrb, rc); } DATA_TYPE(self) = &redisContext_type; DATA_PTR(self) = NULL; if(mrb_get_args(mrb, "oo|i", &host, &port, &timeout) == 3) { timeout_struct.tv_sec = timeout; } rc = redisConnectWithTimeout(mrb_str_to_cstr(mrb, host), mrb_fixnum(port), timeout_struct); if (rc->err) { struct RClass *redis = mrb_class_get(mrb, "Redis"); struct RClass *connectionError = mrb_class_get_under(mrb, redis, "ConnectionError"); mrb_raise(mrb, connectionError, "redis connection failed."); } DATA_PTR(self) = rc; return self; }
void ruby_register_cocos2dx_experimental_video_VideoPlayer(mrb_state* mrb, struct RClass* rmodule) { std::string typeName = typeid(cocos2d::experimental::ui::VideoPlayer).name(); g_rubyType[typeName] = "CCExp::VideoPlayer"; g_rubyDataType[typeName] = &ruby_cocos2dx_Ref_type; struct RClass* p_rmodule = mrb->object_class; p_rmodule = mrb_module_get_under(mrb, p_rmodule, "CCUI"); struct RClass* p_rclass = mrb_class_get_under(mrb, p_rmodule, "Widget"); struct RClass *rclass = mrb_define_class_under(mrb, rmodule, "VideoPlayer", p_rclass); MRB_SET_INSTANCE_TT(rclass, MRB_TT_DATA); mrb_define_method(mrb, rclass, "get_file_name", ruby_cocos2dx_experimental_video_VideoPlayer_getFileName, ARGS_NONE()); mrb_define_method(mrb, rclass, "get_url", ruby_cocos2dx_experimental_video_VideoPlayer_getURL, ARGS_NONE()); mrb_define_method(mrb, rclass, "play", ruby_cocos2dx_experimental_video_VideoPlayer_play, ARGS_NONE()); mrb_define_method(mrb, rclass, "pause", ruby_cocos2dx_experimental_video_VideoPlayer_pause, ARGS_NONE()); mrb_define_method(mrb, rclass, "set_keep_aspect_ratio_enabled", ruby_cocos2dx_experimental_video_VideoPlayer_setKeepAspectRatioEnabled, ARGS_REQ(1)); mrb_define_method(mrb, rclass, "resume", ruby_cocos2dx_experimental_video_VideoPlayer_resume, ARGS_NONE()); mrb_define_method(mrb, rclass, "stop", ruby_cocos2dx_experimental_video_VideoPlayer_stop, ARGS_NONE()); mrb_define_method(mrb, rclass, "set_full_screen_enabled", ruby_cocos2dx_experimental_video_VideoPlayer_setFullScreenEnabled, ARGS_REQ(1)); mrb_define_method(mrb, rclass, "set_file_name", ruby_cocos2dx_experimental_video_VideoPlayer_setFileName, ARGS_REQ(1)); mrb_define_method(mrb, rclass, "set_url", ruby_cocos2dx_experimental_video_VideoPlayer_setURL, ARGS_REQ(1)); mrb_define_method(mrb, rclass, "keep_aspect_ratio_enabled?", ruby_cocos2dx_experimental_video_VideoPlayer_isKeepAspectRatioEnabled, ARGS_NONE()); mrb_define_method(mrb, rclass, "on_play_event", ruby_cocos2dx_experimental_video_VideoPlayer_onPlayEvent, ARGS_REQ(1)); mrb_define_method(mrb, rclass, "full_screen_enabled?", ruby_cocos2dx_experimental_video_VideoPlayer_isFullScreenEnabled, ARGS_NONE()); mrb_define_method(mrb, rclass, "playing?", ruby_cocos2dx_experimental_video_VideoPlayer_isPlaying, ARGS_NONE()); mrb_define_method(mrb, rclass, "seek_to", ruby_cocos2dx_experimental_video_VideoPlayer_seekTo, ARGS_REQ(1)); mrb_define_class_method(mrb, rclass, "create", ruby_cocos2dx_experimental_video_VideoPlayer_create_static, ARGS_NONE()); struct RClass* renum_EventType = mrb_define_module_under(mrb, rclass, "EventType"); ruby_cocos2dx_experimental_video_VideoPlayer_EventType_enum_init(mrb, renum_EventType); mrb_define_class_method(mrb, renum_EventType, "PLAYING", ruby_cocos2dx_experimental_video_constant_get, ARGS_NONE()); mrb_define_class_method(mrb, renum_EventType, "PAUSED", ruby_cocos2dx_experimental_video_constant_get, ARGS_NONE()); mrb_define_class_method(mrb, renum_EventType, "STOPPED", ruby_cocos2dx_experimental_video_constant_get, ARGS_NONE()); mrb_define_class_method(mrb, renum_EventType, "COMPLETED", ruby_cocos2dx_experimental_video_constant_get, ARGS_NONE()); }
static grn_rc grn_plugin_call_register_mrb(grn_ctx *ctx, grn_id id, grn_plugin *plugin) { grn_mrb_data *data = &(ctx->impl->mrb); mrb_state *mrb = data->state; struct RClass *module = data->module; struct RClass *plugin_loader_class; int arena_index; { int added; grn_hash_add(ctx, ctx->impl->mrb.registered_plugins, &id, sizeof(grn_id), NULL, &added); if (!added) { return ctx->rc; } } arena_index = mrb_gc_arena_save(mrb); plugin_loader_class = mrb_class_get_under(mrb, module, "PluginLoader"); mrb_funcall(mrb, mrb_obj_value(plugin_loader_class), "load_file", 1, mrb_str_new_cstr(mrb, ctx->impl->plugin_path)); mrb_gc_arena_restore(mrb, arena_index); return ctx->rc; }
static mrb_value object_find_index(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_obj *object; mrb_value mrb_operator; grn_obj *index; int n_indexes; int section_id; mrb_get_args(mrb, "o", &mrb_operator); object = DATA_PTR(self); n_indexes = grn_column_index(ctx, object, mrb_fixnum(mrb_operator), &index, 1, §ion_id); if (n_indexes == 0) { return mrb_nil_value(); } else { grn_mrb_data *data; struct RClass *klass; mrb_value args[2]; data = &(ctx->impl->mrb); klass = mrb_class_get_under(mrb, data->module, "IndexInfo"); args[0] = grn_mrb_value_from_grn_obj(mrb, index); args[1] = mrb_fixnum_value(section_id); return mrb_obj_new(mrb, klass, 2, args); } }
static mrb_value indexable_find_index(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_obj *object; mrb_value mrb_operator; grn_operator operator; grn_index_datum index_datum; int n_index_data; mrb_get_args(mrb, "o", &mrb_operator); object = DATA_PTR(self); operator = grn_mrb_value_to_operator(mrb, mrb_operator); n_index_data = grn_column_find_index_data(ctx, object, operator, &index_datum, 1); if (n_index_data == 0) { return mrb_nil_value(); } else { grn_mrb_data *data; struct RClass *klass; mrb_value args[2]; data = &(ctx->impl->mrb); klass = mrb_class_get_under(mrb, data->module, "IndexInfo"); args[0] = grn_mrb_value_from_grn_obj(mrb, index_datum.index); args[1] = mrb_fixnum_value(index_datum.section); return mrb_obj_new(mrb, klass, 2, args); } }
static ssize_t mrb_wslay_event_send_callback(wslay_event_context_ptr ctx, const uint8_t *buf, size_t len, int flags, void *user_data) { mrb_wslay_user_data *data = (mrb_wslay_user_data *) user_data; mrb_state* mrb = data->mrb; int ai = mrb_gc_arena_save(mrb); struct mrb_jmpbuf *prev_jmp = mrb->jmp; struct mrb_jmpbuf c_jmp; mrb_int ret = -1; MRB_TRY(&c_jmp) { data->mrb->jmp = &c_jmp; errno = 0; mrb_value buf_obj = mrb_str_new_static(mrb, (const char *) buf, len); mrb_assert(mrb_type(data->send_callback) == MRB_TT_PROC); mrb_value sent = mrb_yield(mrb, data->send_callback, buf_obj); ret = mrb_int(mrb, sent); mrb_assert(ret >= 0&&ret <= len); mrb->jmp = prev_jmp; } MRB_CATCH(&c_jmp) { mrb->jmp = prev_jmp; if (mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EAGAIN"))|| mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EWOULDBLOCK"))) { mrb->exc = NULL; wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK); } else { wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE); } } MRB_END_EXC(&c_jmp); mrb_gc_arena_restore(mrb, ai); return ret; }
static void domain_error(mrb_state *mrb, const char *func) { struct RClass *math = mrb_module_get(mrb, "Math"); struct RClass *domainerror = mrb_class_get_under(mrb, math, "DomainError"); mrb_value str = mrb_str_new_cstr(mrb, func); mrb_raisef(mrb, domainerror, "Numerical argument is out of domain - %S", str); }
struct RClass* winapi_hook_class(mrb_state* mrb) { return mrb_class_get_under( mrb, winapi_class(mrb), "Hook"); }
static mrb_value mrb_queue_create_instance(mrb_state *mrb) { struct RClass *dispatch_class, *queue_class; dispatch_class = mrb_module_get(mrb, "Dispatch"); queue_class = mrb_class_get_under(mrb, dispatch_class, "Queue"); return mrb_obj_value(mrb_obj_alloc(mrb, MRB_TT_OBJECT, queue_class)); }
static mrb_value mrb_grn_scan_info_new(mrb_state *mrb, scan_info *scan_info) { grn_ctx *ctx = (grn_ctx *)mrb->ud; struct RClass *module = ctx->impl->mrb.module; struct RClass *klass; mrb_value mrb_scan_info; mrb_scan_info = mrb_cptr_value(mrb, scan_info); klass = mrb_class_get_under(mrb, module, "ScanInfo"); return mrb_obj_new(mrb, klass, 1, &mrb_scan_info); }
static mrb_value mrb_grn_expr_code_new(mrb_state *mrb, grn_expr_code *code) { grn_ctx *ctx = (grn_ctx *)mrb->ud; struct RClass *module = ctx->impl->mrb.module; struct RClass *klass; mrb_value mrb_code; mrb_code = mrb_cptr_value(mrb, code); klass = mrb_class_get_under(mrb, module, "ExpressionCode"); return mrb_obj_new(mrb, klass, 1, &mrb_code); }
void mrb_fltk_input_class_init( mrb_state *mrb ) { ARENA_SAVE; struct RClass *mrb_fltk_module = mrb_module_get( mrb, "FLTK" ); struct RClass *mrb_fltk_widget_class = mrb_class_get_under( mrb, mrb_fltk_module, "Widget" ); DEFINE_CLASS( input, Input, mrb_fltk_widget_class ); DEFINE_INSTANCE_METHOD( input, initialize, ARGS_ANY() ); DEFINE_INSTANCE_METHOD_ACCESSOR( input, value ); ARENA_RESTORE; }
void mrb_bc_table_init(mrb_state* mrb) { struct RClass *plt, *emv, *table; plt = mrb_class_get(mrb, "EMVPlatform"); emv = mrb_class_get_under(mrb, plt, "EMV"); table = mrb_define_class_under(mrb, emv, "Table", mrb->object_class); mrb_define_class_method(mrb , table , "init" , mrb_emv_table_s_init , MRB_ARGS_REQ(1)); mrb_define_class_method(mrb , table , "rec" , mrb_emv_table_s_rec , MRB_ARGS_REQ(1)); mrb_define_class_method(mrb , table , "fin" , mrb_emv_table_s_fin , MRB_ARGS_NONE()); mrb_define_class_method(mrb , table , "change" , mrb_emv_table_s_change , MRB_ARGS_REQ(1)); }
static mrb_value mrb_uv_thread_self(mrb_state *mrb, mrb_value self) { mrb_uv_thread *ctx; ctx = (mrb_uv_thread*)mrb_malloc(mrb, sizeof(mrb_uv_thread)); ctx->instance = mrb_nil_value(); ctx->mrb = mrb; ctx->thread = uv_thread_self(); return mrb_obj_value(Data_Wrap_Struct( mrb, mrb_class_get_under(mrb, mrb_module_get(mrb, "UV"), "Thread"), &mrb_uv_thread_type, ctx)); }
static mrb_value mrb_sdl2_video_surface_get_color_mod(mrb_state *mrb, mrb_value self) { uint8_t r, g, b; SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); if (0 != SDL_GetSurfaceColorMod(s, &r, &g, &b)) { mruby_sdl2_raise_error(mrb); } mrb_value rgb[3] = { mrb_fixnum_value(r), mrb_fixnum_value(g), mrb_fixnum_value(b) }; return mrb_obj_new(mrb, mrb_class_get_under(mrb, mod_SDL2, "RGB"), 3, rgb); }
void grn_mrb_variable_size_column_init(grn_ctx *ctx) { grn_mrb_data *data = &(ctx->impl->mrb); mrb_state *mrb = data->state; struct RClass *module = data->module; struct RClass *column_class; struct RClass *klass; column_class = mrb_class_get_under(mrb, module, "Column"); klass = mrb_define_class_under(mrb, module, "VariableSizeColumn", column_class); MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); mrb_define_method(mrb, klass, "initialize", mrb_grn_variable_size_column_initialize, MRB_ARGS_REQ(1)); }
mrb_value mrb_uv_dlopen(mrb_state *mrb, char const *name) { mrb_value ret = mrb_obj_value(mrb_obj_alloc(mrb, MRB_TT_DATA, mrb_class_get_under(mrb, mrb_module_get(mrb, "UV"), "DL"))); uv_lib_t *lib = (uv_lib_t*)mrb_malloc(mrb, sizeof(uv_lib_t)); int err; DATA_TYPE(ret) = &dl_type; DATA_PTR(ret) = lib; err = uv_dlopen(name, lib); if (err == -1) { mrb_raise(mrb, E_UV_ERROR, uv_dlerror(lib)); } return ret; }
static mrb_value mrb_sdl2_video_surface_set_color_mod(mrb_state *mrb, mrb_value self) { mrb_value color; SDL_Surface *s = mrb_sdl2_video_surface_get_ptr(mrb, self); mrb_get_args(mrb, "o", &color); if (!mrb_obj_is_kind_of(mrb, color, mrb_class_get_under(mrb, mod_SDL2, "RGB"))) { mrb_raise(mrb, E_TYPE_ERROR, "given 1st argument is unexpected type (expected RGB)."); } uint8_t const r = mrb_fixnum(mrb_iv_get(mrb, color, mrb_intern(mrb, "@r", 2))); uint8_t const g = mrb_fixnum(mrb_iv_get(mrb, color, mrb_intern(mrb, "@g", 2))); uint8_t const b = mrb_fixnum(mrb_iv_get(mrb, color, mrb_intern(mrb, "@b", 2))); if (0 != SDL_SetSurfaceColorMod(s, r, g, b)) { mruby_sdl2_raise_error(mrb); } return self; }
static mrb_value indexable_indexes(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_obj *object; grn_index_datum index_datum; grn_index_datum *index_data; int i, n_index_data; mrb_value mrb_indexes; object = DATA_PTR(self); n_index_data = grn_column_get_all_index_data(ctx, object, &index_datum, 1); if (n_index_data == 0) { return mrb_ary_new(mrb); } if (n_index_data == 1) { index_data = &index_datum; } else { index_data = GRN_MALLOCN(grn_index_datum, n_index_data); n_index_data = grn_column_get_all_index_data(ctx, object, index_data, n_index_data); } mrb_indexes = mrb_ary_new_capa(mrb, n_index_data); for (i = 0; i < n_index_data; i++) { grn_mrb_data *data; struct RClass *klass; mrb_value args[2]; data = &(ctx->impl->mrb); klass = mrb_class_get_under(mrb, data->module, "IndexInfo"); args[0] = grn_mrb_value_from_grn_obj(mrb, index_data[i].index); args[1] = mrb_fixnum_value(index_data[i].section); mrb_ary_push(mrb, mrb_indexes, mrb_obj_new(mrb, klass, 2, args)); } if (index_data != &index_datum) { GRN_FREE(index_data); } return mrb_indexes; }
static void io_set_process_status(mrb_state *mrb, pid_t pid, int status) { struct RClass *c_process, *c_status; mrb_value v; c_status = NULL; if (mrb_class_defined(mrb, "Process")) { c_process = mrb_module_get(mrb, "Process"); if (mrb_const_defined(mrb, mrb_obj_value(c_process), mrb_intern_cstr(mrb, "Status"))) { c_status = mrb_class_get_under(mrb, c_process, "Status"); } } if (c_status != NULL) { v = mrb_funcall(mrb, mrb_obj_value(c_status), "new", 2, mrb_fixnum_value(pid), mrb_fixnum_value(status)); } else { v = mrb_fixnum_value(WEXITSTATUS(status)); } mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$?"), v); }
static void mrb_wslay_event_on_msg_recv_callback(wslay_event_context_ptr ctx, const struct wslay_event_on_msg_recv_arg *arg, void *user_data) { mrb_wslay_user_data *data = (mrb_wslay_user_data *) user_data; mrb_state *mrb = data->mrb; int ai = mrb_gc_arena_save(mrb); struct mrb_jmpbuf *prev_jmp = mrb->jmp; struct mrb_jmpbuf c_jmp; MRB_TRY(&c_jmp) { mrb->jmp = &c_jmp; mrb_value argv[4]; argv[0] = mrb_fixnum_value(arg->rsv); argv[1] = MRB_GET_OPCODE(mrb_fixnum_value(arg->opcode)); argv[2] = mrb_str_new(mrb, (const char *) arg->msg, arg->msg_length); argv[3] = MRB_GET_STATUSCODE(mrb_fixnum_value(arg->status_code)); mrb_value on_msg_recv_arg = mrb_obj_new(mrb, mrb_class_get_under(mrb, mrb_module_get_under(mrb, mrb_module_get(mrb, "Wslay"), "Event"), "OnMsgRecvArg"), NELEMS(argv), argv); mrb_assert(mrb_type(data->on_msg_recv_callback) == MRB_TT_PROC); mrb_yield(mrb, data->on_msg_recv_callback, on_msg_recv_arg); mrb_gc_arena_restore(mrb, ai); mrb->jmp = prev_jmp; } MRB_CATCH(&c_jmp) { mrb->jmp = prev_jmp; wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE); mrb_gc_arena_restore(mrb, ai); MRB_THROW(mrb->jmp); } MRB_END_EXC(&c_jmp); }
struct RClass * mrb_class_get(mrb_state *mrb, const char *name) { return mrb_class_get_under(mrb, mrb->object_class, name); }
void grn_mrb_value_to_raw_data(mrb_state *mrb, const char *context, mrb_value mrb_value_, grn_id domain_id, grn_mrb_value_to_raw_data_buffer *buffer, void **raw_value, unsigned int *raw_value_size) { grn_ctx *ctx = (grn_ctx *)mrb->ud; enum mrb_vtype mrb_value_type; grn_bool try_cast = GRN_FALSE; grn_obj *from_bulk = NULL; if (mrb_nil_p(mrb_value_)) { *raw_value = NULL; *raw_value_size = 0; return; } mrb_value_type = mrb_type(mrb_value_); switch (mrb_value_type) { case MRB_TT_STRING : switch (domain_id) { case GRN_DB_SHORT_TEXT : case GRN_DB_TEXT : case GRN_DB_LONG_TEXT : *raw_value = RSTRING_PTR(mrb_value_); *raw_value_size = RSTRING_LEN(mrb_value_); break; default : try_cast = GRN_TRUE; break; } break; default : { struct RClass *klass; grn_mrb_data *data = &(ctx->impl->mrb); klass = mrb_class(mrb, mrb_value_); if (domain_id == GRN_DB_TIME && klass == data->builtin.time_class) { mrb_value mrb_sec; mrb_value mrb_usec; mrb_sec = mrb_funcall(mrb, mrb_value_, "to_i", 0); mrb_usec = mrb_funcall(mrb, mrb_value_, "usec", 0); buffer->value.time_value = GRN_TIME_PACK(mrb_fixnum(mrb_sec), mrb_fixnum(mrb_usec)); *raw_value = &(buffer->value.time_value); *raw_value_size = sizeof(buffer->value.time_value); } else { try_cast = GRN_TRUE; if (mrb_value_type == MRB_TT_DATA && klass == mrb_class_get_under(mrb, data->module, "Bulk")) { from_bulk = DATA_PTR(mrb_value_); } } } break; } if (!try_cast) { return; } if (!from_bulk) { from_bulk = &(buffer->from); grn_mrb_value_to_bulk(mrb, mrb_value_, from_bulk); } if (!grn_mrb_bulk_cast(mrb, from_bulk, &(buffer->to), domain_id)) { grn_obj *domain; char domain_name[GRN_TABLE_MAX_KEY_SIZE]; int domain_name_size; domain = grn_ctx_at(ctx, domain_id); domain_name_size = grn_obj_name(ctx, domain, domain_name, GRN_TABLE_MAX_KEY_SIZE); mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S: failed to convert to %S: %S", mrb_str_new_static(mrb, context, strlen(context)), mrb_str_new_static(mrb, domain_name, domain_name_size), mrb_funcall(mrb, mrb_value_, "inspect", 0)); } *raw_value = GRN_BULK_HEAD(&(buffer->to)); *raw_value_size = GRN_BULK_VSIZE(&(buffer->to)); }
struct RClass * grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_mrb_data *data; struct RClass *klass = NULL; data = &(ctx->impl->mrb); switch (object->header.type) { case GRN_BULK : klass = mrb_class_get_under(mrb, data->module, "Bulk"); break; case GRN_ACCESSOR : klass = mrb_class_get_under(mrb, data->module, "Accessor"); break; case GRN_COLUMN_FIX_SIZE : klass = mrb_class_get_under(mrb, data->module, "FixedSizeColumn"); break; case GRN_COLUMN_VAR_SIZE : klass = mrb_class_get_under(mrb, data->module, "VariableSizeColumn"); break; case GRN_COLUMN_INDEX : klass = mrb_class_get_under(mrb, data->module, "IndexColumn"); break; case GRN_TYPE : klass = mrb_class_get_under(mrb, data->module, "Type"); break; case GRN_PROC : klass = mrb_class_get_under(mrb, data->module, "Procedure"); break; case GRN_EXPR : klass = mrb_class_get_under(mrb, data->module, "Expression"); break; case GRN_TABLE_NO_KEY : klass = mrb_class_get_under(mrb, data->module, "Array"); break; case GRN_TABLE_HASH_KEY : klass = mrb_class_get_under(mrb, data->module, "HashTable"); break; case GRN_TABLE_PAT_KEY : klass = mrb_class_get_under(mrb, data->module, "PatriciaTrie"); break; case GRN_TABLE_DAT_KEY : klass = mrb_class_get_under(mrb, data->module, "DoubleArrayTrie"); break; case GRN_DB : klass = mrb_class_get_under(mrb, data->module, "Database"); break; case GRN_VOID : klass = mrb_class_get_under(mrb, data->module, "Void"); break; default : break; } if (!klass) { #define BUFFER_SIZE 1024 char buffer[BUFFER_SIZE]; grn_snprintf(buffer, BUFFER_SIZE, BUFFER_SIZE, "can't find class for object type: %#x", object->header.type); mrb_raise(mrb, E_ARGUMENT_ERROR, buffer); #undef BUFFER_SIZE } return klass; }