static mrb_value plus(mrb_state *mrb, mrb_value self) { mrb_value a, b; mrb_get_args(mrb, "oo", &a, &b); if (mrb_fixnum_p(a) && mrb_fixnum_p(b)) { mrb_int x = mrb_fixnum(a); mrb_int y = mrb_fixnum(b); return mrb_fixnum_value(x + y); } else if (mrb_string_p(a) && mrb_string_p(b)) { mrb_value s = mrb_str_plus(mrb, a, b); return s; } else { return mrb_nil_value(); } }
/* * call-seq: * struct[symbol] -> anObject * struct[fixnum] -> anObject * * Attribute Reference---Returns the value of the instance variable * named by <i>symbol</i>, or indexed (0..length-1) by * <i>fixnum</i>. Will raise <code>NameError</code> if the named * variable does not exist, or <code>IndexError</code> if the index is * out of range. * * Customer = Struct.new(:name, :address, :zip) * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) * * joe["name"] #=> "Joe Smith" * joe[:name] #=> "Joe Smith" * joe[0] #=> "Joe Smith" */ mrb_value mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx) { mrb_int i; if (mrb_string_p(idx)) { mrb_value sym = mrb_check_intern_str(mrb, idx); if (mrb_nil_p(sym)) { mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", idx); } idx = sym; } if (mrb_symbol_p(idx)) { return mrb_struct_aref_id(mrb, s, mrb_symbol(idx)); } i = mrb_fixnum(idx); if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too small for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); if (RSTRUCT_LEN(s) <= i) mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too large for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); return RSTRUCT_PTR(s)[i]; }
/* * call-seq: * str.prepend(other_str) -> str * * Prepend---Prepend the given string to <i>str</i>. * * a = "world" * a.prepend("hello ") #=> "hello world" * a #=> "hello world" */ static mrb_value mrb_str_prepend(mrb_state *mrb, mrb_value self) { struct RString *s1 = mrb_str_ptr(self), *s2, *temp_s; mrb_int len; mrb_value other, temp_str; mrb_get_args(mrb, "S", &other); mrb_str_modify(mrb, s1); if (!mrb_string_p(other)) { other = mrb_str_to_str(mrb, other); } s2 = mrb_str_ptr(other); len = RSTR_LEN(s1) + RSTR_LEN(s2); temp_str = mrb_str_new(mrb, NULL, RSTR_LEN(s1)); temp_s = mrb_str_ptr(temp_str); memcpy(RSTR_PTR(temp_s), RSTR_PTR(s1), RSTR_LEN(s1)); if (RSTRING_CAPA(self) < len) { mrb_str_resize(mrb, self, len); } memcpy(RSTR_PTR(s1), RSTR_PTR(s2), RSTR_LEN(s2)); memcpy(RSTR_PTR(s1) + RSTR_LEN(s2), RSTR_PTR(temp_s), RSTR_LEN(temp_s)); RSTR_SET_LEN(s1, len); RSTR_PTR(s1)[len] = '\0'; return self; }
mrb_value mrb_struct_aset(mrb_state *mrb, mrb_value s) { mrb_int i; mrb_value idx; mrb_value val; mrb_get_args(mrb, "oo", &idx, &val); if (mrb_string_p(idx) || mrb_symbol_p(idx)) { return mrb_struct_aset_id(mrb, s, mrb_obj_to_sym(mrb, idx), val); } i = mrb_fixnum(idx); if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too small for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } if (RSTRUCT_LEN(s) <= i) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too large for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } return RSTRUCT_PTR(s)[i] = val; }
bool rubyval_to_dictionary(mrb_state* mrb, mrb_value arg, __Dictionary** outValue, const char* funcName) { if (! mrb_hash_p(arg)) { return false; } mrb_value key_arr = mrb_hash_keys(mrb, arg); mrb_int len = mrb_ary_len(mrb, key_arr); __Dictionary* dic = __Dictionary::create(); for (mrb_int i = 0; i < len; i++) { mrb_value hk = mrb_ary_ref(mrb, key_arr, i); mrb_value hv = mrb_hash_get(mrb, arg, hk); if (mrb_string_p(hk)) { char *kstr = mrb_str_to_cstr(mrb, hk); Ref* ref = to_ref_value(mrb, hv); dic->setObject(ref, std::string(kstr)); } else if (mrb_symbol_p(hk)) { mrb_sym sym = mrb_symbol(hk); const char* kstr = mrb_sym2name(mrb, sym); Ref* ref = to_ref_value(mrb, hv); dic->setObject(ref, std::string(kstr)); } else { CCASSERT(false, "not supported key value type"); } } *outValue = dic; return true; }
bool rubyval_to_ccvaluemapintkey(mrb_state* mrb, mrb_value arg, cocos2d::ValueMapIntKey* ret, const char* funcName) { if (! mrb_hash_p(arg)) { return false; } mrb_value key_arr = mrb_hash_keys(mrb, arg); mrb_int len = mrb_ary_len(mrb, key_arr); ValueMapIntKey& dict = *ret; for (mrb_int i = 0; i < len; i++) { mrb_value hk = mrb_ary_ref(mrb, key_arr, i); mrb_value hv = mrb_hash_get(mrb, arg, hk); int int_key = 0; if (mrb_string_p(hk)) { char *kstr = mrb_str_to_cstr(mrb, hk); int_key = atoi(kstr); } else if (mrb_symbol_p(hk)) { mrb_sym sym = mrb_symbol(hk); const char* kstr = mrb_sym2name(mrb, sym); int_key = atoi(kstr); } else { return false; } Value val; if (! rubyval_to_ccvalue(mrb, hv, &val)) { return false; } dict[int_key] = val; } return true; }
mrb_value mrb_struct_aset(mrb_state *mrb, mrb_value s) { long i; mrb_value idx; mrb_value val; mrb_get_args(mrb, "oo", &idx, &val); if (mrb_string_p(idx) || mrb_symbol_p(idx)) { return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val); } i = mrb_fixnum(idx); if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", i, RSTRUCT_LEN(s)); } if (RSTRUCT_LEN(s) <= i) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", i, RSTRUCT_LEN(s)); } mrb_struct_modify(s); return RSTRUCT_PTR(s)[i] = val; }
static mrb_value mrb_addrinfo_getnameinfo(mrb_state *mrb, mrb_value self) { mrb_int flags; mrb_value ary, host, sastr, serv; int error; flags = 0; mrb_get_args(mrb, "|i", &flags); host = mrb_str_buf_new(mrb, NI_MAXHOST); serv = mrb_str_buf_new(mrb, NI_MAXSERV); sastr = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@sockaddr")); if (!mrb_string_p(sastr)) { mrb_raise(mrb, E_SOCKET_ERROR, "invalid sockaddr"); } error = getnameinfo((struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr), RSTRING_PTR(host), NI_MAXHOST, RSTRING_PTR(serv), NI_MAXSERV, flags); if (error != 0) { mrb_raisef(mrb, E_SOCKET_ERROR, "getnameinfo: %s", gai_strerror(error)); } ary = mrb_ary_new_capa(mrb, 2); mrb_str_resize(mrb, host, strlen(RSTRING_PTR(host))); mrb_ary_push(mrb, ary, host); mrb_str_resize(mrb, serv, strlen(RSTRING_PTR(serv))); mrb_ary_push(mrb, ary, serv); return ary; }
/* * call-seq: * raise * raise(string) * raise(exception [, string]) * * With no arguments, raises a <code>RuntimeError</code> * With a single +String+ argument, raises a * +RuntimeError+ with the string as a message. Otherwise, * the first parameter should be the name of an +Exception+ * class (or an object that returns an +Exception+ object when sent * an +exception+ message). The optional second parameter sets the * message associated with the exception, and the third parameter is an * array of callback information. Exceptions are caught by the * +rescue+ clause of <code>begin...end</code> blocks. * * raise "Failed to create socket" * raise ArgumentError, "No parameters", caller */ MRB_API mrb_value mrb_f_raise(mrb_state *mrb, mrb_value self) { mrb_value a[2], exc; mrb_int argc; argc = mrb_get_args(mrb, "|oo", &a[0], &a[1]); switch (argc) { case 0: mrb_raise(mrb, E_RUNTIME_ERROR, ""); break; case 1: if (mrb_string_p(a[0])) { a[1] = a[0]; argc = 2; a[0] = mrb_obj_value(E_RUNTIME_ERROR); } /* fall through */ default: exc = mrb_make_exception(mrb, argc, a); mrb_exc_raise(mrb, exc); break; } return mrb_nil_value(); /* not reached */ }
static mrb_value mrb_struct_aset(mrb_state *mrb, mrb_value s) { mrb_int i; mrb_value idx; mrb_value val; mrb_get_args(mrb, "oo", &idx, &val); if (mrb_string_p(idx)) { mrb_value sym = mrb_check_intern_str(mrb, idx); if (mrb_nil_p(sym)) { mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%S' in struct", idx); } idx = sym; } if (mrb_symbol_p(idx)) { return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val); } i = mrb_int(mrb, idx); if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too small for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } if (RSTRUCT_LEN(s) <= i) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too large for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } return RSTRUCT_PTR(s)[i] = val; }
/* * call-seq: * LCD.draw(x, y, str) # => nil * * Draw the string at specified coordinate of LCD. * * Parameters: * +x+ X-coordinate of the string left edge * +y+ Y-coordinate of the string top edge * +str+ The target for drawing. * * Returns nil. */ static mrb_value mrb_lcd_draw_string(mrb_state *mrb, mrb_value self) { mrb_value obj, str; mrb_int x, y; mrb_int fw, fh; mrb_int cw, ch; mrb_lcd_get_font_size(mrb, self, &fw, &fh); cw = EV3_LCD_WIDTH / fw; ch = EV3_LCD_HEIGHT / fh; mrb_get_args(mrb, "iio", &x, &y, &obj); if (mrb_string_p(obj)) { str = obj; } else { str = mrb_funcall(mrb, obj, "to_s", 0); } ev3_lcd_draw_string(mrb_string_value_cstr(mrb, &str), x*cw, y*ch); return mrb_nil_value(); }
static inline mrb_value mrb_hash_ht_key(mrb_state *mrb, mrb_value key) { if (mrb_string_p(key)) return mrb_str_dup(mrb, key); else return key; }
static void printstr(mrb_state *mrb, mrb_value obj, FILE *stream) { if (mrb_string_p(obj)) { fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stream); putc('\n', stream); } }
static mrb_sym get_valid_iv_sym(mrb_state *mrb, mrb_value iv_name) { mrb_sym iv_name_id; mrb_assert(mrb_symbol_p(iv_name) || mrb_string_p(iv_name)); if (mrb_string_p(iv_name)) { iv_name_id = mrb_intern_cstr(mrb, RSTRING_PTR(iv_name)); valid_iv_name(mrb, iv_name_id, RSTRING_PTR(iv_name), RSTRING_LEN(iv_name)); } else { iv_name_id = mrb_symbol(iv_name); check_iv_name(mrb, iv_name_id); } return iv_name_id; }
bool rubyval_to_std_string(mrb_state* mrb, mrb_value arg, std::string* outValue, const char* funcName) { if (mrb_string_p(arg)) { char *str = mrb_str_to_cstr(mrb, arg); *outValue = std::string(str); return true; } return false; }
static inline mrb_value mrb_hash_ht_key(mrb_state *mrb, mrb_value key) { if (mrb_string_p(key) && !RSTR_FROZEN_P(mrb_str_ptr(key))) { key = mrb_str_dup(mrb, key); RSTR_SET_FROZEN_FLAG(mrb_str_ptr(key)); } return key; }
VCL_STRING vmod_exec(VRT_CTX, struct vmod_priv *priv, VCL_STRING code) { mrb_state *mrb = (mrb_state*)priv->priv; mrb_value v = mrb_code_exec(mrb, code); if(!mrb_string_p(v)) { v = mrb_obj_as_string(mrb,v); } return RSTRING_PTR(v); }
mrb_value mrb_f_syslog_ident(mrb_state *mrb, mrb_value self) { mrb_value s; s = mrb_cv_get(mrb, self, mrb_intern_lit(mrb, "ident")); if (! mrb_string_p(s)) { if (mrb_nil_p(s)) return s; mrb_raisef(mrb, E_RUNTIME_ERROR, "class variable ident of Syslog is not a string"); } return mrb_str_new_cstr(mrb, RSTRING_PTR(s)); }
static mrb_value mrb_require_load_rb_str(mrb_state *mrb, mrb_value self) { char *path_ptr = NULL; char tmpname[] = "tmp.XXXXXXXX"; mode_t mask; FILE *tmpfp = NULL; int fd = -1, ret; mrb_irep *irep; mrb_value code, path = mrb_nil_value(); mrb_get_args(mrb, "S|S", &code, &path); if (!mrb_string_p(path)) { path = mrb_str_new_cstr(mrb, "-"); } path_ptr = mrb_str_to_cstr(mrb, path); mask = umask(077); fd = mkstemp(tmpname); if (fd == -1) { mrb_sys_fail(mrb, "can't create mkstemp() at mrb_require_load_rb_str"); } umask(mask); tmpfp = fopen(tmpname, "w+"); if (tmpfp == NULL) { mrb_sys_fail(mrb, "can't open temporay file at mrb_require_load_rb_str"); } ret = compile_rb2mrb(mrb, RSTRING_PTR(code), RSTRING_LEN(code), path_ptr, tmpfp); if (ret != MRB_DUMP_OK) { fclose(tmpfp); remove(tmpname); mrb_raisef(mrb, E_LOAD_ERROR, "can't load file -- %S", path); return mrb_nil_value(); } rewind(tmpfp); irep = mrb_read_irep_file(mrb, tmpfp); fclose(tmpfp); remove(tmpname); if (irep) { eval_load_irep(mrb, irep); } else if (mrb->exc) { // fail to load longjmp(*(jmp_buf*)mrb->jmp, 1); } else { mrb_raisef(mrb, E_LOAD_ERROR, "can't load file -- %S", path); return mrb_nil_value(); } return mrb_true_value(); }
static void p(mrb_state *mrb, mrb_value obj) { mrb_value val; val = mrb_funcall(mrb, obj, "inspect", 0); if (!mrb_string_p(val)) { val = mrb_obj_as_string(mrb, obj); } fwrite(RSTRING_PTR(val), RSTRING_LEN(val), 1, stdout); putc('\n', stdout); }
static void t_printstr(mrb_state *mrb, mrb_value obj) { char *s; int len; if (mrb_string_p(obj)) { s = RSTRING_PTR(obj); len = RSTRING_LEN(obj); fwrite(s, len, 1, stdout); } }
static sighandler_t trap_handler(mrb_state *mrb, mrb_value *cmd, int sig) { sighandler_t func = sighandler; mrb_value command; if (mrb_nil_p(*cmd)) { func = SIG_IGN; } else { command = *cmd; if (mrb_type(command) == MRB_TT_SYMBOL) { command = mrb_sym2str(mrb, mrb_symbol(command)); if (mrb_undef_p(command)) mrb_raise(mrb, E_ARGUMENT_ERROR, "bad handler"); } if (mrb_string_p(command)) { *cmd = command; switch (RSTRING_LEN(command)) { case 0: goto sig_ign; break; case 6: if (memcmp(RSTRING_PTR(command), "IGNORE", 6) == 0) { goto sig_ign; } break; case 7: if (memcmp(RSTRING_PTR(command), "SIG_IGN", 7) == 0) { sig_ign: func = SIG_IGN; *cmd = mrb_true_value(); } else if (memcmp(RSTRING_PTR(command), "SIG_DFL", 7) == 0) { sig_dfl: func = default_handler(sig); *cmd = mrb_true_value(); } else if (memcmp(RSTRING_PTR(command), "DEFAULT", 7) == 0) { goto sig_dfl; } break; case 4: if (memcmp(RSTRING_PTR(command), "EXIT", 4) == 0) { *cmd = mrb_undef_value(); } break; } } } return func; }
void ereport_exception(mrb_state *mrb) { /* TODO: add backtrace */ mrb_value s = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0); mrb->exc = NULL; if (mrb_string_p(s)) { char *err = mrb_str_to_cstr(mrb, s); ereport(ERROR, (errmsg("%s", err))); } else ereport(ERROR, (errmsg("unknown error occured"))); }
static mrb_sym mrb_sym_value(mrb_state *mrb, mrb_value val) { if (mrb_string_p(val)) { return mrb_intern_str(mrb, val); } else if(!mrb_symbol_p(val)) { mrb_value obj = mrb_funcall(mrb, val, "inspect", 0); mrb_raisef(mrb, E_TYPE_ERROR, "%s is not a symbol", mrb_string_value_ptr(mrb, obj)); } return mrb_symbol(val); }
static void printstr(mrb_state *mrb, mrb_value obj) { #ifdef ENABLE_STDIO char *s; int len; if (mrb_string_p(obj)) { s = RSTRING_PTR(obj); len = RSTRING_LEN(obj); fwrite(s, len, 1, stdout); } #endif }
void mrb_print_error(mrb_state *mrb) { #ifdef ENABLE_STDIO mrb_value s; mrb_print_backtrace(mrb); s = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0); if (mrb_string_p(s)) { fwrite(RSTRING_PTR(s), RSTRING_LEN(s), 1, stderr); putc('\n', stderr); } #endif }
static void printstr(mrb_state *mrb, mrb_value obj) { struct RString *str; char *s; int len; if (mrb_string_p(obj)) { str = mrb_str_ptr(obj); s = str->ptr; len = str->len; fwrite(s, len, 1, stdout); } }
static mrb_value exc_to_s(mrb_state *mrb, mrb_value exc) { mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg")); struct RObject *p; if (!mrb_string_p(mesg)) { return mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, exc)); } p = mrb_obj_ptr(mesg); if (!p->c) { p->c = mrb->string_class; } return mesg; }
static void printstr(mrb_state *mrb, mrb_value obj) { char *s; mrb_int len; if (mrb_string_p(obj)) { s = RSTRING_PTR(obj); len = RSTRING_LEN(obj); fwrite(s, len, 1, stdout); #ifdef _WIN32 OutputDebugString(s); #endif } }
static mrb_value get_sym_or_str_arg(mrb_state *mrb) { mrb_value sym_or_str; mrb_get_args(mrb, "o", &sym_or_str); if (mrb_symbol_p(sym_or_str) || mrb_string_p(sym_or_str)) { return sym_or_str; } else { mrb_value obj = mrb_funcall(mrb, sym_or_str, "inspect", 0); mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", obj); return mrb_nil_value(); } }