void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg) { mrb_value mesg; mesg = mrb_str_new2(mrb, msg); mrb_exc_raise(mrb, mrb_exc_new3(mrb, c, mesg)); }
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 exc_inspect(mrb_state *mrb, mrb_value exc) { mrb_value str, mesg, file, line; mesg = mrb_attr_get(mrb, exc, mrb_intern(mrb, "mesg")); file = mrb_attr_get(mrb, exc, mrb_intern(mrb, "file")); line = mrb_attr_get(mrb, exc, mrb_intern(mrb, "line")); if (!mrb_nil_p(file) && !mrb_nil_p(line)) { str = file; mrb_str_cat2(mrb, str, ":"); mrb_str_append(mrb, str, line); mrb_str_cat2(mrb, str, ": "); if (!mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0) { mrb_str_append(mrb, str, mesg); mrb_str_cat2(mrb, str, " ("); } mrb_str_cat2(mrb, str, mrb_obj_classname(mrb, exc)); if (!mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0) { mrb_str_cat2(mrb, str, ")"); } } else { str = mrb_str_new2(mrb, mrb_obj_classname(mrb, exc)); if (!mrb_nil_p(mesg) && RSTRING_LEN(mesg) > 0) { mrb_str_cat2(mrb, str, ": "); mrb_str_append(mrb, str, mesg); } else { mrb_str_cat2(mrb, str, ": "); mrb_str_cat2(mrb, str, mrb_obj_classname(mrb, exc)); } } return str; }
static mrb_value exc_to_s(mrb_state *mrb, mrb_value exc) { mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern(mrb, "mesg")); if (mrb_nil_p(mesg)) return mrb_str_new2(mrb, mrb_obj_classname(mrb, exc)); return mesg; }
static mrb_value exc_inspect(mrb_state *mrb, mrb_value exc) { mrb_value str, klass; klass = mrb_str_new2(mrb, mrb_obj_classname(mrb, exc)); exc = mrb_obj_as_string(mrb, exc); if (RSTRING_LEN(exc) == 0) { return klass; } str = mrb_str_new2(mrb, "#<"); mrb_str_append(mrb, str, klass); mrb_str_cat2(mrb, str, ": "); mrb_str_append(mrb, str, exc); mrb_str_cat2(mrb, str, ">"); return str; }
static mrb_value exc_inspect(mrb_state *mrb, mrb_value exc) { mrb_value str; str = mrb_str_new2(mrb, mrb_obj_classname(mrb, exc)); exc = mrb_obj_as_string(mrb, exc); if (RSTRING_LEN(exc) > 0) { mrb_str_cat2(mrb, str, ": "); mrb_str_append(mrb, str, exc); } return str; }
static mrb_value mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base) { char buf[64], *b = buf + sizeof buf; unsigned long val = mrb_fixnum(x); char d = 0; if (base != 2) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid radix %d", base); } if (val >= (1 << 10)) val &= 0x3ff; if (val == 0) { return mrb_str_new2(mrb, "0"); } *--b = '\0'; do { *--b = ruby_digitmap[(int)(val % base)]; } while (val /= base); if (mrb_fixnum(x) < 0) { b = remove_sign_bits(b, base); switch (base) { case 16: d = 'f'; break; case 8: d = '7'; break; case 2: d = '1'; break; } if (d && *b != d) { *--b = d; } } return mrb_str_new2(mrb, b); }
static mrb_value mrb_env_getenv(mrb_state *mrb, mrb_value name) { if (mrb_type(name) != MRB_TT_STRING) { mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %s into String", mrb_obj_classname(mrb, name)); return mrb_nil_value(); } char *nam = mrb_string_value_ptr(mrb, name); char *env = getenv(nam); if (env == NULL) { return mrb_nil_value(); } return mrb_str_new2(mrb, env); }
mrb_value cfunc_pointer_inspect(mrb_state *mrb, mrb_value self) { struct cfunc_type_data *data = DATA_PTR(self); char cstr[256]; mrb_value str; const char* classname = mrb_obj_classname(mrb, self); if(!classname) { classname = "Unknown pointer"; } snprintf(cstr, sizeof(cstr), "<%s=%p>", classname, get_cfunc_pointer_data(data)); str = mrb_str_new2(mrb, cstr); return str; }
static mrb_value inspect_range(mrb_state *mrb, mrb_value range, mrb_value dummy, int recur) { mrb_value str, str2; struct RRange *r = mrb_range_ptr(range); if (recur) { return mrb_str_new2(mrb, r->excl ? "(... ... ...)" : "(... .. ...)"); } str = mrb_inspect(mrb, r->edges->beg); str2 = mrb_inspect(mrb, r->edges->end); str = mrb_str_dup(mrb, str); mrb_str_cat(mrb, str, "...", r->excl ? 3 : 2); mrb_str_append(mrb, str, str2); return str; }
static mrb_value inspect_struct(mrb_state *mrb, mrb_value s, mrb_value dummy, int recur) { const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s)); mrb_value members, str = mrb_str_new2(mrb, "#<struct "); mrb_value *ptr, *ptr_members; long i, len; if (cn) { mrb_str_append(mrb, str, mrb_str_new_cstr(mrb, cn)); } if (recur) { return mrb_str_cat2(mrb, str, ":...>"); } members = mrb_struct_members(mrb, s); ptr_members = RARRAY_PTR(members); ptr = RSTRUCT_PTR(s); len = RSTRUCT_LEN(s); for (i=0; i<len; i++) { mrb_value slot; mrb_sym id; if (i > 0) { mrb_str_cat2(mrb, str, ", "); } else if (cn) { mrb_str_cat2(mrb, str, " "); } slot = ptr_members[i]; id = SYM2ID(slot); if (mrb_is_local_id(id) || mrb_is_const_id(id)) { //mrb_str_append(str, mrb_id2str(id)); mrb_str_append(mrb, str, mrb_str_new_cstr(mrb, mrb_sym2name(mrb, id))); } else { mrb_str_append(mrb, str, mrb_inspect(mrb, slot)); } mrb_str_cat2(mrb, str, "="); mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i])); } mrb_str_cat2(mrb, str, ">"); return str; }
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_new2(mrb, name); ary = mrb_ary_new(mrb); va_start(ar, name); while ((mem = va_arg(ar, char*)) != 0) { mrb_sym slot = mrb_intern(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_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; }
int main(int argc, char **argv) { char ruby_code[1024] = { 0 }; char last_code_line[1024] = { 0 }; #ifndef ENABLE_READLINE int last_char; int char_index; #else char *home = NULL; #endif mrbc_context *cxt; struct mrb_parser_state *parser; mrb_state *mrb; mrb_value result; struct _args args; int n; mrb_bool code_block_open = FALSE; mrb_value MIRB_BIN; int ai; unsigned int stack_keep = 0; /* new interpreter instance */ mrb = mrb_open(); if (mrb == NULL) { fputs("Invalid mrb interpreter, exiting mirb\n", stderr); return EXIT_FAILURE; } mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0)); n = parse_args(mrb, argc, argv, &args); if (n == EXIT_FAILURE) { cleanup(mrb, &args); usage(argv[0]); return n; } print_hint(); cxt = mrbc_context_new(mrb); cxt->capture_errors = 1; cxt->lineno = 1; mrbc_filename(mrb, cxt, "(mirb)"); if (args.verbose) cxt->dump_result = 1; MIRB_BIN= mrb_str_new(mrb, argv[0], strlen(argv[0])); mrb_define_global_const(mrb, "MIRB_BIN", MIRB_BIN); #ifdef ENABLE_REQUIRE mrb_value LOAD_PATH = mrb_gv_get(mrb, mrb_intern(mrb, "$:")); if (mrb_str_cmp(mrb, MIRB_BIN, mrb_str_new2(mrb, "mirb")) != 0) { int len = strrchr(RSTRING_PTR(MIRB_BIN), '/') - RSTRING_PTR(MIRB_BIN); mrb_value extdir = mrb_str_substr(mrb, mrb_str_dup(mrb, MIRB_BIN), 0, len); mrb_str_cat2(mrb, extdir, "/../ext"); if (mrb_obj_eq(mrb, mrb_file_exist(mrb, extdir), mrb_true_value())) { mrb_ary_push(mrb, LOAD_PATH, extdir); } } #endif /* ENABLE_REQUIRE */ ai = mrb_gc_arena_save(mrb); #ifdef ENABLE_READLINE MIRB_USING_HISTORY(); home = getenv("HOME"); #ifdef _WIN32 if (!home) home = getenv("USERPROFILE"); #endif if (home) { strcpy(history_path, home); strcat(history_path, "/"); strcat(history_path, history_file_name); MIRB_READ_HISTORY(history_path); } #endif while (TRUE) { #ifndef ENABLE_READLINE print_cmdline(code_block_open); char_index = 0; while ((last_char = getchar()) != '\n') { if (last_char == EOF) break; last_code_line[char_index++] = last_char; } if (last_char == EOF) { fputs("\n", stdout); break; } last_code_line[char_index] = '\0'; #else char* line = MIRB_READLINE(code_block_open ? "* " : "> "); if (line == NULL) { printf("\n"); break; } strncpy(last_code_line, line, sizeof(last_code_line)-1); MIRB_ADD_HISTORY(line); free(line); #endif if (code_block_open) { strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); } else { if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) { break; } strcpy(ruby_code, last_code_line); } /* parse code */ parser = mrb_parser_new(mrb); parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); parser->lineno = cxt->lineno; mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); if (code_block_open) { /* no evaluation of code */ } else { if (0 < parser->nerr) { /* syntax error */ printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message); } else { /* generate bytecode */ struct RProc *proc = mrb_generate_code(mrb, parser); if (args.verbose) { mrb_codedump_all(mrb, proc); } /* pass a proc for evaulation */ /* evaluate the bytecode */ result = mrb_context_run(mrb, proc, mrb_top_self(mrb), stack_keep); stack_keep = proc->body.irep->nlocals; /* did an exception occur? */ if (mrb->exc) { p(mrb, mrb_obj_value(mrb->exc), 0); mrb->exc = 0; } else { /* no */ if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){ result = mrb_any_to_s(mrb,result); } p(mrb, result, 1); } } ruby_code[0] = '\0'; last_code_line[0] = '\0'; mrb_gc_arena_restore(mrb, ai); } mrb_parser_free(parser); cxt->lineno++; } mrbc_context_free(mrb, cxt); mrb_close(mrb); #ifdef ENABLE_READLINE MIRB_WRITE_HISTORY(history_path); #endif return 0; }
static mrb_value mrb_env_to_s(mrb_state *mrb, mrb_value self) { return mrb_str_new2(mrb, "ENV"); }