static int iv_i(mrb_state *mrb, mrb_sym sym, mrb_value v, void *p) { mrb_value ary; const char* s; mrb_int len; ary = *(mrb_value*)p; s = mrb_sym2name_len(mrb, sym, &len); if (len > 1 && s[0] == '@' && s[1] != '@') { mrb_ary_push(mrb, ary, mrb_symbol_value(sym)); } return 0; }
static mrb_value mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) { mrb_value members, ary; mrb_value *p, *pend; members = mrb_struct_s_members(mrb, klass); ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members)); p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members); while (p < pend) { mrb_ary_push(mrb, ary, *p); p++; } return ary; }
static mrb_value mrb_hash_values(mrb_state *mrb, mrb_value hash) { khash_t(ht) *h = RHASH_TBL(hash); khiter_t k; mrb_value ary = mrb_ary_new_capa(mrb, kh_size(h)); if (!h) return ary; for (k = kh_begin(h); k != kh_end(h); k++) { if (kh_exist(h, k)){ mrb_value v = kh_value(h,k); mrb_ary_push(mrb, ary, v); } } return ary; }
static mrb_value mrb_mod_included_modules(mrb_state *mrb, mrb_value self) { mrb_value result; struct RClass *c = mrb_class_ptr(self); result = mrb_ary_new(mrb); while (c) { if (c->tt == MRB_TT_ICLASS) { mrb_ary_push(mrb, result, mrb_obj_value(c->c)); } c = c->super; } return result; }
mrb_value docopt_value_to_mrb_value(const docopt::value& value, mrb_state *mrb) { if (value.isString()) { return mrb_str_new_cstr(mrb, value.asString().c_str()); } else if (value.isBool()) { return mrb_bool_value(value.asBool() ? 1 : 0); } else if (value.isLong()) { return mrb_fixnum_value(value.asLong()); } else if (value.isStringList()) { mrb_value res = mrb_ary_new(mrb); for(const auto& str: value.asStringList()){ mrb_ary_push(mrb, res, mrb_str_new_cstr(mrb, str.c_str())); } return res; } return mrb_nil_value(); }
static mrb_value mrb_grn_expression_codes(mrb_state *mrb, mrb_value self) { grn_expr *expr; mrb_value mrb_codes; int i; expr = DATA_PTR(self); mrb_codes = mrb_ary_new_capa(mrb, expr->codes_curr); for (i = 0; i < expr->codes_curr; i++) { grn_expr_code *code = expr->codes + i; mrb_ary_push(mrb, mrb_codes, mrb_grn_expr_code_new(mrb, code)); } return mrb_codes; }
static mrb_value hash_values_at(mrb_state *mrb, mrb_value hash) { mrb_value *argv, result; mrb_int argc, i; int ai; mrb_get_args(mrb, "*!", &argv, &argc); result = mrb_ary_new_capa(mrb, argc); ai = mrb_gc_arena_save(mrb); for (i = 0; i < argc; i++) { mrb_ary_push(mrb, result, mrb_hash_get(mrb, hash, argv[i])); mrb_gc_arena_restore(mrb, ai); } return result; }
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(); }
//convert rbObj in RVector (assumed to be possible!!!) SEXP mrbArray2RVector(mrb_value rbobj) { SEXP ans; mrb_value arr,elt,tmp; //char *name; int n,i; if(!mrb_obj_is_kind_of(mrb,rbobj,mrb->array_class)) { if(!(mrb_obj_is_kind_of(mrb,rbobj,mrb->fixnum_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->float_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->string_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->true_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->false_class))) return R_NilValue; n=1; arr = mrb_ary_new_capa(mrb,1); mrb_ary_push(mrb,arr,rbobj); } else { arr=rbobj; n=RARRAY_LEN(rbobj); } //Rprintf("n=%d\n",n); elt=mrb_ary_entry(arr,0); if(mrb_type(elt)==MRB_TT_FLOAT) { PROTECT(ans=allocVector(REALSXP,n)); for(i=0;i<n;i++) { REAL(ans)[i]=mrb_float(mrb_ary_entry(arr,i)); } } else if(mrb_type(elt)==MRB_TT_FIXNUM) { PROTECT(ans=allocVector(INTSXP,n)); for(i=0;i<n;i++) { INTEGER(ans)[i]=mrb_int(mrb,mrb_ary_entry(arr,i)); } } else if(mrb_type(elt)==MRB_TT_TRUE || mrb_type(elt)==MRB_TT_FALSE) { PROTECT(ans=allocVector(LGLSXP,n)); for(i=0;i<n;i++) { LOGICAL(ans)[i]=(mrb_type(mrb_ary_entry(arr,i))==MRB_TT_FALSE ? FALSE : TRUE); } } else if(mrb_type(elt)==MRB_TT_STRING) { PROTECT(ans=allocVector(STRSXP,n)); for(i=0;i<n;i++) { tmp=mrb_ary_entry(arr,i); SET_STRING_ELT(ans,i,mkChar(mrb_string_value_ptr(mrb,tmp))); } } else ans=R_NilValue; UNPROTECT(1); return ans; }
/* * call-seq: * global_variables -> array * * Returns an array of the names of global variables. * * global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr] */ mrb_value mrb_f_global_variables(mrb_state *mrb, mrb_value self) { iv_tbl *t = mrb->globals; mrb_value ary = mrb_ary_new(mrb); size_t i; char buf[3]; iv_foreach(mrb, t, gv_i, &ary); buf[0] = '$'; buf[2] = 0; for (i = 1; i <= 9; ++i) { buf[1] = (char)(i + '0'); mrb_ary_push(mrb, ary, mrb_symbol_value(mrb_intern(mrb, buf, 2))); } return ary; }
ngx_int_t ngx_http_mruby_run_body_filter(ngx_http_request_t *r, ngx_http_mruby_state_t *state, ngx_http_mruby_code_t *code, ngx_flag_t cached) { ngx_http_mruby_ctx_t *ctx; mrb_value ARGV, mrb_result; size_t body_length; ctx = ngx_http_get_module_ctx(r, ngx_http_mruby_module); ngx_http_mruby_push_request(r); state->ai = mrb_gc_arena_save(state->mrb); ARGV = mrb_ary_new_capa(state->mrb, 1); mrb_ary_push(state->mrb, ARGV, mrb_str_new(state->mrb, (char *)ctx->filter_ctx.body, ctx->filter_ctx.body_length)); mrb_define_global_const(state->mrb, "ARGV", ARGV); mrb_result = mrb_run(state->mrb, code->proc, mrb_top_self(state->mrb)); if (state->mrb->exc) { ngx_http_mruby_raise_error(state, code, r); mrb_gc_arena_restore(state->mrb, state->ai); //mrb_gc_protect(state->mrb, ctx->table); if (!cached) { ngx_http_mruby_irep_clean(state, code); } return NGX_ERROR; } mrb_result = mrb_obj_as_string(state->mrb, mrb_result); body_length = RSTRING_LEN(mrb_result); if (body_length != 0) { ctx->filter_ctx.body = (u_char *)RSTRING_PTR(mrb_result); ctx->filter_ctx.body_length = body_length; } mrb_gc_arena_restore(state->mrb, state->ai); //mrb_gc_protect(state->mrb, ctx->table); if (!cached) { ngx_http_mruby_irep_clean(state, code); } return NGX_OK; }
mrb_value mrb_env_keys(mrb_state *mrb, mrb_value self) { int i; mrb_value ary; ary = mrb_ary_new(mrb); for (i = 0; environ[i] != NULL; i++) { char *str = strchr(environ[i], '='); if (str != NULL) { int len = str - environ[i]; mrb_ary_push(mrb, ary, mrb_str_new(mrb, environ[i], len)); } } return ary; }
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; }
mrb_value mrb_env_values(mrb_state *mrb, mrb_value self) { int i; mrb_value ary; ary = mrb_ary_new(mrb); for (i = 0; environ[i] != NULL; i++) { char *str = strchr(environ[i], '='); if (str) { str++; int len = strlen(str); mrb_ary_push(mrb, ary, mrb_str_new(mrb, str, len)); } } return ary; }
static mrb_value mrb_hash_values(mrb_state *mrb, mrb_value hash) { khash_t(ht) *h = RHASH_TBL(hash); khiter_t k; mrb_value ary = mrb_ary_new(mrb); if (!h) return ary; for (k = kh_begin(h); k != kh_end(h); k++) { if (kh_exist(h, k)){ mrb_value v = kh_value(h,k); if ( !mrb_special_const_p(v) ) v = mrb_obj_dup(mrb, v); mrb_ary_push(mrb, ary, v); } } return ary; }
ngx_int_t ngx_http_mruby_run_args(ngx_http_request_t *r, ngx_http_mruby_state_t *state, ngx_http_mruby_code_t *code, ngx_flag_t cached, ngx_http_variable_value_t *args, size_t nargs, ngx_str_t *result) { ngx_uint_t i; mrb_value ARGV, mrb_result; state->ai = mrb_gc_arena_save(state->mrb); ARGV = mrb_ary_new_capa(state->mrb, nargs); for (i = 0; i < nargs; i++) { mrb_ary_push(state->mrb, ARGV, mrb_str_new(state->mrb, (char *)args[i].data, args[i].len)); } mrb_define_global_const(state->mrb, "ARGV", ARGV); ngx_http_mruby_push_request(r); mrb_result = mrb_run(state->mrb, code->proc, mrb_top_self(state->mrb)); if (state->mrb->exc) { ngx_http_mruby_raise_error(state, code, r); mrb_gc_arena_restore(state->mrb, state->ai); //mrb_gc_protect(state->mrb, ctx->table); if (!cached) { ngx_http_mruby_irep_clean(state, code); } return NGX_ERROR; } mrb_result = mrb_obj_as_string(state->mrb, mrb_result); result->data = (u_char *)RSTRING_PTR(mrb_result); result->len = RSTRING_LEN(mrb_result); mrb_gc_arena_restore(state->mrb, state->ai); //mrb_gc_protect(state->mrb, ctx->table); if (!cached) { ngx_http_mruby_irep_clean(state, code); } return NGX_OK; }
static mrb_value indexable_index_ids(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_obj *object; grn_hook_entry entry; int i; int n_indexes; mrb_value mrb_index_ids; grn_obj hook_data; object = DATA_PTR(self); if (grn_obj_is_key_accessor(ctx, object)) { object = grn_ctx_at(ctx, object->header.domain); } if (grn_obj_is_table(ctx, object)) { entry = GRN_HOOK_INSERT; } else if (grn_obj_is_column(ctx, object)) { entry = GRN_HOOK_SET; } else { return mrb_ary_new(mrb); } n_indexes = grn_obj_get_nhooks(ctx, object, entry); mrb_index_ids = mrb_ary_new_capa(mrb, n_indexes); GRN_TEXT_INIT(&hook_data, 0); for (i = 0; i < n_indexes; i++) { GRN_BULK_REWIND(&hook_data); grn_obj_get_hook(ctx, object, entry, i, &hook_data); if (GRN_BULK_VSIZE(&hook_data) == sizeof(grn_obj_default_set_value_hook_data)) { grn_obj_default_set_value_hook_data *data; data = (grn_obj_default_set_value_hook_data *)GRN_TEXT_VALUE(&hook_data); mrb_ary_push(mrb, mrb_index_ids, mrb_fixnum_value(data->target)); } } return mrb_index_ids; }
static mrb_value inspect_ary(mrb_state *mrb, mrb_value ary, mrb_value list) { mrb_int i; mrb_value s, arystr; char head[] = { '[' }; char sep[] = { ',', ' ' }; char tail[] = { ']' }; /* check recursive */ for(i=0; i<RARRAY_LEN(list); i++) { if (mrb_obj_equal(mrb, ary, RARRAY_PTR(list)[i])) { return mrb_str_new(mrb, "[...]", 5); } } mrb_ary_push(mrb, list, ary); arystr = mrb_str_buf_new(mrb, 64); mrb_str_buf_cat(mrb, arystr, head, sizeof(head)); for(i=0; i<RARRAY_LEN(ary); i++) { int ai = mrb_gc_arena_save(mrb); if (i > 0) { mrb_str_buf_cat(mrb, arystr, sep, sizeof(sep)); } if (mrb_array_p(RARRAY_PTR(ary)[i])) { s = inspect_ary(mrb, RARRAY_PTR(ary)[i], list); } else { s = mrb_inspect(mrb, RARRAY_PTR(ary)[i]); } mrb_str_buf_cat(mrb, arystr, RSTRING_PTR(s), RSTRING_LEN(s)); mrb_gc_arena_restore(mrb, ai); } mrb_str_buf_cat(mrb, arystr, tail, sizeof(tail)); mrb_ary_pop(mrb, list); return arystr; }
mrb_value mrb_struct_define(mrb_state *mrb, const char *name, ...) { va_list ar; mrb_value nm, ary; char *mem; if (!name) nm = mrb_nil_value(); else nm = mrb_str_new_cstr(mrb, name); ary = mrb_ary_new(mrb); va_start(ar, name); while ((mem = va_arg(ar, char*)) != 0) { mrb_sym slot = mrb_intern_cstr(mrb, mem); mrb_ary_push(mrb, ary, mrb_symbol_value(slot)); } va_end(ar); return make_struct(mrb, nm, ary, struct_class(mrb)); }
mrb_value mrb_redis_keys(mrb_state *mrb, mrb_value self) { mrb_value pattern, array = mrb_nil_value(); redisContext *rc = DATA_PTR(self); mrb_get_args(mrb, "o", &pattern); redisReply *rr = redisCommand(rc, "KEYS %s", mrb_str_to_cstr(mrb, pattern)); if (rr->type == REDIS_REPLY_ARRAY) { if (rr->elements > 0) { int i; array = mrb_ary_new(mrb); for (i = 0; i < rr->elements; i++) { mrb_ary_push(mrb, array, mrb_str_new_cstr(mrb, rr->element[i]->str)); } } } freeReplyObject(rr); return array; }
static mrb_value bs_locate_handle_buffer(mrb_state *mrb, mrb_value self) { mrb_value guid; mrb_value ret; UINTN size; EFI_HANDLE *phandles; mrb_get_args(mrb, "o", &guid); if (mrb_nil_p(guid)){ /* AllHandles */ EFI_STATUS status; status = gBS->LocateHandleBuffer(AllHandles, NULL, NULL, &size, &phandles); if (EFI_ERROR(status)){ return mrb_nil_value(); } }else{ /* ByProtocol */ EFI_STATUS status; EFI_GUID efi_guid; mrb_uefi_guid_get_guid(mrb, guid, &efi_guid); status = gBS->LocateHandleBuffer(ByProtocol, &efi_guid, NULL, &size, &phandles); if (EFI_ERROR(status)){ return mrb_nil_value(); } } ret = mrb_ary_new_capa(mrb, (int)size); { UINTN i; for (i=0; i<size; i++){ int arena = mrb_gc_arena_save(mrb); mrb_ary_push(mrb, ret, mrb_uefi_handle_make(mrb, phandles[i])); mrb_gc_arena_restore(mrb, arena); } } gBS->FreePool(phandles); return ret; }
int main(int argc, char *argv[]) { mrb_state *mrb = mrb_open(); mrb_value ARGV = mrb_ary_new_capa(mrb, argc); int i; int return_value; #ifdef HACONIWA_SECURE_RUN if(check_match_owner(argc, argv) < 0) { mrb_raise(mrb, E_RUNTIME_ERROR, "haconiwa runner and hacofile's owner would not be matched: This run prohibited on secure-run build"); exit(2); } #endif if(setuid(geteuid()) < 0) { mrb_sys_fail(mrb, "setuid"); exit(2); } if(setgid(getegid()) < 0) { mrb_sys_fail(mrb, "setgid"); exit(2); } for (i = 0; i < argc; i++) { mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i])); } mrb_define_global_const(mrb, "ARGV", ARGV); // call __main__(ARGV) mrb_funcall(mrb, mrb_top_self(mrb), "__main__", 1, ARGV); return_value = EXIT_SUCCESS; if (mrb->exc) { mrb_print_error(mrb); return_value = EXIT_FAILURE; } mrb_close(mrb); return return_value; }
mrb_value mrbcc_load_so(mrb_state *mrb, mrb_value self, const char *filename) { void *handle; mrb_value (*entry_point)(mrb_state*, mrb_value); mrb_value ary; char *error; handle = dlopen(filename, RTLD_LAZY); if (!handle) { fprintf (stderr, "%s\n", dlerror()); return mrb_nil_value(); } dlerror(); /* Clear any existing error */ entry_point = dlsym(handle, "mrbb_exec_entry_point"); if ((error = dlerror()) != NULL) { fprintf (stderr, "%s\n", error); return mrb_nil_value(); } ary = mrb_iv_get(mrb, mrb_obj_value(mrb->kernel_module), mrb_intern(mrb, "@loaded_compiled_mrb_handles")); mrb_ary_push(mrb, ary, mrb_fixnum_value((mrb_int) handle)); // TODO warning return (*entry_point)(mrb, self); }
mrb_value mrb_redis_smembers(mrb_state *mrb, mrb_value self) { int i; mrb_value array, key; redisContext *rc = DATA_PTR(self); mrb_get_args(mrb, "o", &key); redisReply *rr = redisCommand(rc, "SMEMBERS %s", mrb_str_to_cstr(mrb, key)); if (rr->type == REDIS_REPLY_ARRAY) { array = mrb_ary_new(mrb); for (i = 0; i < rr->elements; i++) { mrb_ary_push(mrb, array, mrb_str_new(mrb, rr->element[i]->str, rr->element[i]->len)); } } else { freeReplyObject(rr); return mrb_nil_value(); } freeReplyObject(rr); return array; }
int rbIsRVector(mrb_value rbobj) { mrb_value rbobj2; int i,n; if(!mrb_obj_is_kind_of(mrb,rbobj,mrb->array_class)) { if(!(mrb_obj_is_kind_of(mrb,rbobj,mrb->fixnum_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->float_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->string_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->true_class) || mrb_obj_is_kind_of(mrb,rbobj,mrb->false_class))) return 0; rbobj2=mrb_ary_new_capa(mrb,1); mrb_ary_push(mrb,rbobj2,rbobj); rbobj=rbobj2; //Seems that the 3 previous lines could be replaced by: // return 1; } n=RARRAY_LEN(rbobj); for(i=0;i<n;i++) { rbobj2=mrb_ary_entry(rbobj,i); if(!(mrb_obj_is_kind_of(mrb,rbobj2,mrb->fixnum_class) || mrb_obj_is_kind_of(mrb,rbobj2,mrb->float_class) || mrb_obj_is_kind_of(mrb,rbobj2,mrb->string_class) || mrb_obj_is_kind_of(mrb,rbobj2,mrb->true_class) || mrb_obj_is_kind_of(mrb,rbobj2,mrb->false_class))) { return 0; } } return 1; }
/* * call-seq: * obj.instance_variables -> array * * Returns an array of instance variable names for the receiver. Note * that simply defining an accessor does not create the corresponding * instance variable. * * class Fred * attr_accessor :a1 * def initialize * @iv = 3 * end * end * Fred.new.instance_variables #=> [:@iv] */ mrb_value mrb_obj_instance_variables(mrb_state *mrb, mrb_value self) { mrb_value ary; kh_iv_t *h = RCLASS_IV_TBL(self); int i; const char* p; ary = mrb_ary_new(mrb); if (h) { for (i=0;i<kh_end(h);i++) { if (kh_exist(h, i)) { p = mrb_sym2name(mrb, kh_key(h,i)); if (*p == '@') { if (mrb_type(kh_value(h, i)) != MRB_TT_UNDEF) mrb_ary_push(mrb, ary, mrb_str_new_cstr(mrb, p)); } } } } return ary; }
mrb_value mrb_redis_hkeys(mrb_state *mrb, mrb_value self) { mrb_value key, array = mrb_nil_value(); redisContext *rc = DATA_PTR(self); mrb_get_args(mrb, "o", &key); const char *argv[] = {"HKEYS", RSTRING_PTR(key)}; size_t lens[] = {5, RSTRING_LEN(key)}; redisReply *rr = redisCommandArgv(rc, 2, argv, lens); if (rr->type == REDIS_REPLY_ARRAY) { if (rr->elements > 0) { int i; array = mrb_ary_new(mrb); for (i = 0; i < rr->elements; i++) { mrb_ary_push(mrb, array, mrb_str_new(mrb, rr->element[i]->str, rr->element[i]->len)); } } } freeReplyObject(rr); return array; }
ngx_int_t ngx_mrb_run_body_filter(ngx_http_request_t *r, ngx_mrb_state_t *state, ngx_mrb_code_t *code, ngx_flag_t cached, ngx_http_mruby_ctx_t *ctx) { mrb_value ARGV, mrb_result; ARGV = mrb_ary_new_capa(state->mrb, 1); mrb_ary_push(state->mrb, ARGV, mrb_str_new(state->mrb, (char *)ctx->body, ctx->body_length)); mrb_define_global_const(state->mrb, "ARGV", ARGV); mrb_result = mrb_run(state->mrb, mrb_proc_new(state->mrb, state->mrb->irep[code->n]), mrb_top_self(state->mrb)); if (state->mrb->exc) { if (code->code_type == NGX_MRB_CODE_TYPE_FILE) { ngx_mrb_raise_file_error(state->mrb, mrb_obj_value(state->mrb->exc), r, code->code.file); } else { ngx_mrb_raise_error(state->mrb, mrb_obj_value(state->mrb->exc), r); } mrb_gc_arena_restore(state->mrb, state->ai); if (!cached) { ngx_mrb_irep_clean(r, state, code); ngx_mrb_state_clean(r, state); } return NGX_ERROR; } if (mrb_type(mrb_result) != MRB_TT_STRING) { mrb_result = mrb_funcall(state->mrb, mrb_result, "to_s", 0, NULL); } ctx->body = (u_char *)RSTRING_PTR(mrb_result); ctx->body_length = ngx_strlen(ctx->body); mrb_gc_arena_restore(state->mrb, state->ai); if (!cached) { ngx_mrb_irep_clean(r, state, code); ngx_mrb_state_clean(r, state); } return NGX_OK; }
mrb_value mrb_redis_lrange(mrb_state *mrb, mrb_value self) { int i; mrb_value list, array; mrb_int arg1, arg2; mrb_get_args(mrb, "oii", &list, &arg1, &arg2); redisContext *rc = mrb_redis_get_context(mrb, self); redisReply *rr = redisCommand(rc,"LRANGE %s %d %d", RSTRING_PTR(list), arg1, arg2); if (rr->type == REDIS_REPLY_ARRAY) { array = mrb_ary_new(mrb); for (i = 0; i < rr->elements; i++) { mrb_ary_push(mrb, array, mrb_str_new2(mrb, rr->element[i]->str)); } } else { freeReplyObject(rr); return mrb_nil_value(); } freeReplyObject(rr); return array; }
static mrb_value mrb_uv_key_set(mrb_state *mrb, mrb_value self) { uv_key_t *key; void *p; mrb_value new_val; mrb_value ary; mrb_get_args(mrb, "o", &new_val); if (mrb_type(new_val) < MRB_TT_HAS_BASIC) { mrb_raisef(mrb, E_TYPE_ERROR, "cannot store value without basic: %S", new_val); } key = (uv_key_t*)mrb_uv_get_ptr(mrb, self, &mrb_uv_key_type); p = uv_key_get(key); ary = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "values")); mrb_assert(mrb_array_p(ary)); if (p) { /* remove value */ int i, dst; for (i = 0, dst = 0; i < RARRAY_LEN(ary); ++i) { mrb_value v = RARRAY_PTR(ary)[i]; if (mrb_ptr(v) != p) { mrb_ary_ptr(ary)->ptr[dst++] = v; } } RARRAY_LEN(ary) = dst; } uv_key_set(key, mrb_ptr(new_val)); mrb_ary_push(mrb, ary, new_val); /* protect from GC */ return new_val; }