static mrb_value mrb_skkdic_dict_data(mrb_state *mrb, mrb_value self) { mrb_value path, block, argv[4], table; char line[2048], *cstr, *p; int okuri_pos = 0; FILE *f; int i = 0; mrb_get_args(mrb, "&S", &block, &path); f = fopen(RSTRING_PTR(path), "r"); if (f == NULL) { puts("file open error\n"); } else { while(fgets(line, sizeof(line), f) != NULL) { int ai = mrb_gc_arena_save(mrb); if (strcmp(line, ";; okuri-ari entries.\n") == 0) { okuri_pos++; continue; } if (strcmp(line, ";; okuri-nasi entries.\n") == 0) { okuri_pos++; continue; } if (line[0] == ';') { continue; } cstr = strtok(line, " "); argv[1] = mrb_str_new_cstr(mrb, cstr); cstr = strtok(NULL, " "); p = strtok(cstr, "/"); table = mrb_ary_new(mrb); while (p != NULL) { int ai2 = mrb_gc_arena_save(mrb); mrb_ary_push(mrb, table, mrb_str_new_cstr(mrb, p)); p = strtok(NULL, "/"); mrb_gc_arena_restore(mrb, ai2); } mrb_ary_pop(mrb, table); argv[2] = table; if (okuri_pos == 1) { // okuri_ari argv[0] = mrb_fixnum_value(0); mrb_yield_argv(mrb, block, 3, argv); } else if(okuri_pos == 2) { // okuri_nasi argv[0] = mrb_fixnum_value(1); mrb_yield_argv(mrb, block, 3, argv); } mrb_gc_arena_restore(mrb, ai); } } return self; }
static void* mrb_thread_func(void* data) { mrb_thread_context* context = (mrb_thread_context*) data; mrb_state* mrb = context->mrb; context->result = mrb_yield_argv(mrb, mrb_obj_value(context->proc), context->argc, context->argv); return NULL; }
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_mutex_synchronize(mrb_state* mrb, mrb_value self) { mrb_value proc = mrb_nil_value(); mrb_get_args(mrb, "&", &proc); if (!mrb_nil_p(proc)) { mrb_mutex_lock(mrb, self); mrb_yield_argv(mrb, proc, 0, NULL); mrb_mutex_unlock(mrb, self); } return mrb_nil_value(); }
static void _uv_thread_proc(void *arg) { mrb_uv_thread* context = (mrb_uv_thread*) arg; mrb_state* mrb = context->mrb; mrb_value proc, thread_arg; if (!mrb) return; proc = mrb_iv_get(mrb, context->instance, mrb_intern_lit(mrb, "thread_proc")); thread_arg = mrb_iv_get(mrb, context->instance, mrb_intern_lit(mrb, "thread_arg")); if (!mrb_nil_p(proc)) { mrb_value args[1]; args[0] = thread_arg; mrb_yield_argv(mrb, proc, 1, args); } }
// FLTK::Group#begin static mrb_value mrb_fltk_group_begin_method( mrb_state *mrb, mrb_value self ) { CONTEXT_SETUP( widget ); mrb_value block = mrb_nil_value(); mrb_get_args( mrb, "&", &block ); if( !mrb_nil_p( block ) ) { mrb_value args[1]; args[0] = self; ( (Fl_Group *)context->fl_instance )->begin(); mrb_yield_argv( mrb, block, 1, args ); ( (Fl_Group *)context->fl_instance )->end(); } else { ( (Fl_Group *)context->fl_instance )->begin(); } return mrb_nil_value(); }
/* * call-seq: * string.lines -> array of string * * Returns strings per line; * * a = "abc\ndef" * a.lines #=> ["abc\n", "def"] */ static mrb_value mrb_str_lines(mrb_state *mrb, mrb_value self) { mrb_value result; mrb_value blk; int ai; mrb_int len; mrb_value arg; char *p = RSTRING_PTR(self), *t; char *e = p + RSTRING_LEN(self); mrb_get_args(mrb, "&", &blk); result = mrb_ary_new(mrb); if (!mrb_nil_p(blk)) { while (p < e) { t = p; while (p < e && *p != '\n') p++; if (*p == '\n') p++; len = (mrb_int) (p - t); arg = mrb_str_new(mrb, t, len); mrb_yield_argv(mrb, blk, 1, &arg); } return self; } while (p < e) { ai = mrb_gc_arena_save(mrb); t = p; while (p < e && *p != '\n') p++; if (*p == '\n') p++; len = (mrb_int) (p - t); mrb_ary_push(mrb, result, mrb_str_new(mrb, t, len)); mrb_gc_arena_restore(mrb, ai); } return result; }
static int parser_settings_on_message_complete(http_parser* parser) { mrb_value c; mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data; mrb_http_parser_context *new_context; mrb_state* mrb = context->mrb; mrb_value args[1]; c = mrb_class_new_instance(mrb, 0, NULL, _class_http_request); new_context = (mrb_http_parser_context*) malloc(sizeof(mrb_http_parser_context)); memcpy(new_context, context, sizeof(mrb_http_parser_context)); mrb_iv_set(mrb, c, mrb_intern(mrb, "context"), mrb_obj_value( Data_Wrap_Struct(mrb, mrb->object_class, &http_parser_context_type, (void*) new_context))); args[0] = c; mrb_yield_argv(context->mrb, context->proc, 1, args); PARSER_SET(context, "headers", mrb_nil_value()); PARSER_SET(context, "last_header_field", mrb_nil_value()); PARSER_SET(context, "last_header_value", mrb_nil_value()); PARSER_SET(context, "buf", mrb_nil_value()); return 0; }
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; }
static void _uv_once_cb() { mrb_assert(!mrb_nil_p(once_info.block)); mrb_yield_argv(once_info.mrb, once_info.block, 0, NULL); }
static mrb_value _http_parser_parse(mrb_state *mrb, mrb_value self, int type) { mrb_value arg_data = mrb_nil_value(); mrb_value value_context; mrb_http_parser_context* context; mrb_value b = mrb_nil_value(); struct RClass* _class_http; struct RClass* clazz; char* data; size_t len; char* eol; size_t done; 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->parser.data = context; _class_http = mrb_class_get(mrb, "HTTP"); if (type == HTTP_REQUEST) { clazz = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_http), mrb_intern(mrb, "Request"))); context->instance = mrb_class_new_instance(mrb, 0, NULL, clazz); } else { clazz = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_http), mrb_intern(mrb, "Response"))); context->instance = mrb_class_new_instance(mrb, 0, NULL, clazz); } context->was_header_value = TRUE; http_parser_init(&context->parser, type); context->type = type; 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; context->settings.on_body = parser_settings_on_body; context->settings.on_message_complete = parser_settings_on_message_complete; data = RSTRING_PTR(arg_data); len = RSTRING_LEN(arg_data); eol = strpbrk(data, "\r\n"); if (eol) { } RETRY: if (len > 10 && (!strncmp(data+9, "200 Connection established\r\n", 28) || !strncmp(data+9, "100 Continue\r\n", 14) || *(data+9) == '3')) { char* next = strstr(data, "\r\n\r\n"); if (next) { len -= (next + 4 - data); data = next + 4; goto RETRY; } } done = http_parser_execute(&context->parser, &context->settings, data, len); if (done < len) { OBJECT_SET(mrb, context->instance, "body", mrb_str_new(mrb, data + done, len - done)); } if (!mrb_nil_p(b)) { mrb_value args[1]; args[0] = context->instance; mrb_yield_argv(mrb, b, 1, args); return mrb_nil_value(); } return context->instance; }
static mrb_value mrb_sqlite3_database_execute(mrb_state *mrb, mrb_value self) { int argc = 0; mrb_value* argv = NULL; mrb_value b = mrb_nil_value(); mrb_value value_context; mrb_sqlite3_database* db = NULL; mrb_value fields; int i, r, count; sqlite3_stmt* stmt = NULL; mrb_value args[2]; mrb_value query; mrb_get_args(mrb, "&S*", &b, &query, &argv, &argc); value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "context")); db = NULL; Data_Get_Struct(mrb, value_context, &mrb_sqlite3_database_type, db); if (!db) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } r = sqlite3_prepare_v2(db->db, RSTRING_PTR(query), RSTRING_LEN(query), &stmt, NULL); if (r != SQLITE_OK) { if (stmt) { sqlite3_finalize(stmt); sqlite3_reset(stmt); } mrb_raise(mrb, E_RUNTIME_ERROR, sqlite3_errmsg(db->db)); } if (!stmt) { return mrb_nil_value(); } if (argc > 0) { const char* error = bind_values(mrb, db->db, stmt, argc, argv); if (error) { mrb_raise(mrb, E_ARGUMENT_ERROR, error); } } fields = mrb_ary_new(mrb); count = sqlite3_column_count(stmt); for (i = 0; i < count; i++) { const char* name = sqlite3_column_name(stmt, i); mrb_ary_push(mrb, fields, mrb_str_new_cstr(mrb, name)); } if (mrb_nil_p(b)) { struct RClass* _class_sqlite3; struct RClass* _class_sqlite3_resultset; mrb_value c; mrb_sqlite3_resultset* rs = (mrb_sqlite3_resultset*) malloc(sizeof(mrb_sqlite3_resultset)); if (!rs) { mrb_raise(mrb, E_RUNTIME_ERROR, "can't memory alloc"); } memset(rs, 0, sizeof(mrb_sqlite3_resultset)); rs->mrb = mrb; rs->stmt = stmt; _class_sqlite3 = mrb_class_get(mrb, "SQLite3"); _class_sqlite3_resultset = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_sqlite3), mrb_intern(mrb, "ResultSet"))); c = mrb_class_new_instance(mrb, 0, NULL, _class_sqlite3_resultset); mrb_iv_set(mrb, c, mrb_intern(mrb, "context"), mrb_obj_value( Data_Wrap_Struct(mrb, mrb->object_class, &mrb_sqlite3_resultset_type, (void*) rs))); mrb_iv_set(mrb, c, mrb_intern(mrb, "fields"), fields); mrb_iv_set(mrb, c, mrb_intern(mrb, "db"), self); mrb_iv_set(mrb, c, mrb_intern(mrb, "eof"), mrb_false_value()); return c; } while ((r = sqlite3_step(stmt)) == SQLITE_ROW) { int ai = mrb_gc_arena_save(mrb); args[0] = row_to_value(mrb, stmt); args[1] = fields; mrb_yield_argv(mrb, b, 2, args); mrb_gc_arena_restore(mrb, ai); } sqlite3_finalize(stmt); if (r != SQLITE_OK && r != SQLITE_DONE) { mrb_raise(mrb, E_RUNTIME_ERROR, sqlite3_errmsg(db->db)); } return mrb_nil_value(); }
static mrb_value protect_cb(mrb_state *mrb, mrb_value b) { return mrb_yield_argv(mrb, b, 0, NULL); }