static VALUE float_to_r(VALUE self) { VALUE a = float_decode(self); return f_mul(RARRAY_AT(a, 0), f_expt(INT2FIX(FLT_RADIX), RARRAY_AT(a, 1))); }
static VALUE string_to_r(VALUE self) { VALUE s = f_gsub(self, underscores_pat, an_underscore); VALUE a = string_to_r_internal(s); if (!NIL_P(RARRAY_AT(a, 0))) return RARRAY_AT(a, 0); return rb_rational_new1(INT2FIX(0)); }
static VALUE string_to_r_strict(VALUE self) { VALUE a = string_to_r_internal(self); if (NIL_P(RARRAY_AT(a, 0)) || RSTRING_LEN(RARRAY_AT(a, 1)) > 0) { VALUE s = f_inspect(self); rb_raise(rb_eArgError, "invalid value for Rational: %s", StringValuePtr(s)); } return RARRAY_AT(a, 0); }
static VALUE nurat_marshal_load(VALUE self, VALUE a) { get_dat1(self); dat->num = RARRAY_AT(a, 0); dat->den = RARRAY_AT(a, 1); if (f_zero_p(dat->den)) rb_raise(rb_eZeroDivError, "devided by zero"); return self; }
static VALUE rb_env_clear_imp(VALUE rcv, SEL sel) { VALUE keys = env_keys(Qnil, 0); /* rb_secure(4); */ for (long i = 0, count = RARRAY_LEN(keys); i < count; i++) { VALUE val = rb_f_getenv(Qnil, 0, RARRAY_AT(keys, i)); if (!NIL_P(val)) { env_delete(Qnil, RARRAY_AT(keys, i)); } } return envtbl; }
static VALUE rhash_create(VALUE klass, SEL sel, int argc, VALUE *argv) { if (argc == 1) { VALUE tmp = rhash_try_convert(Qnil, 0, argv[0]); if (!NIL_P(tmp)) { VALUE hash = rhash_alloc(klass, 0); if (IS_RHASH(tmp)) { GC_WB(&RHASH(hash)->tbl, st_copy(RHASH(tmp)->tbl)); } else { VALUE keys = rb_hash_keys(tmp); for (long i = 0, count = RARRAY_LEN(keys); i < count; i++) { VALUE key = RARRAY_AT(keys, i); VALUE val = rb_hash_lookup(tmp, key); rhash_aset(hash, 0, key, val); } } return hash; } tmp = rb_check_array_type(argv[0]); if (!NIL_P(tmp)) { VALUE hash = rhash_alloc(klass, 0); for (int i = 0; i < RARRAY_LEN(tmp); ++i) { VALUE v = rb_check_array_type(RARRAY_AT(tmp, i)); if (NIL_P(v)) { continue; } const long len = RARRAY_LEN(v); if (len < 1 || 2 < len) { continue; } rhash_aset(hash, 0, RARRAY_AT(v, 0), RARRAY_AT(v, 1)); } return hash; } } if (argc % 2 != 0) { rb_raise(rb_eArgError, "odd number of arguments for Hash"); } VALUE hash = rhash_alloc(klass, 0); for (int i = 0; i < argc; i += 2) { rb_hash_aset(hash, argv[i], argv[i + 1]); } return hash; }
static VALUE format_message(VALUE exc) { CFMutableStringRef result = CFStringCreateMutable(NULL, 0); VALUE message = rb_vm_call(exc, sel_registerName("message"), 0, NULL); VALUE bt = rb_vm_call(exc, sel_registerName("backtrace"), 0, NULL); message = rb_check_string_type(message); const char *msg = message == Qnil ? "" : RSTRING_PTR(message); const long count = (bt != Qnil ? RARRAY_LEN(bt) : 0); if (count > 0) { for (long i = 0; i < count; i++) { const char *bte = RSTRING_PTR(RARRAY_AT(bt, i)); if (i == 0) { CFStringAppendFormat(result, NULL, CFSTR("%s: %s (%s)\n"), bte, msg, rb_class2name(*(VALUE *)exc)); } else { CFStringAppendFormat(result, NULL, CFSTR("\tfrom %s\n"), bte); } } } else { CFStringAppendFormat(result, NULL, CFSTR("%s (%s)\n"), msg, rb_class2name(*(VALUE *)exc)); } CFMakeCollectable(result); return (VALUE)result; }
void rb_extend_object(VALUE obj, VALUE module) { VALUE klass; if (TYPE(obj) == T_CLASS && RCLASS_RUBY(obj)) { VALUE sklass = rb_make_singleton_class(RCLASS_SUPER(obj)); RCLASS_SET_SUPER(obj, sklass); klass = *(VALUE *)sklass; } else { klass = rb_singleton_class(obj); } rb_include_module(klass, module); VALUE m = module; do { VALUE ary = rb_attr_get(m, idIncludedModules); if (ary != Qnil) { for (int i = 0, count = RARRAY_LEN(ary); i < count; i++) { VALUE mod = RARRAY_AT(ary, i); rb_extend_object(obj, mod); } } m = RCLASS_SUPER(m); } while (m == 0 || RCLASS_SINGLETON(m)); }
static VALUE rb_mod_append_features(VALUE module, SEL sel, VALUE include) { VALUE orig = include; switch (TYPE(include)) { case T_CLASS: case T_MODULE: break; default: Check_Type(include, T_CLASS); break; } if (RCLASS_RUBY(include)) { VALUE sinclude = rb_make_singleton_class(RCLASS_SUPER(include)); RCLASS_SET_SUPER(include, sinclude); include = sinclude; } rb_include_module2(include, orig, module, true, true); VALUE m = module; do { VALUE ary = rb_attr_get(m, idIncludedModules); if (ary != Qnil) { for (int i = 0, count = RARRAY_LEN(ary); i < count; i++) { VALUE mod = RARRAY_AT(ary, i); rb_mod_append_features(mod, sel, include); } } m = RCLASS_SUPER(m); } while (m == 0 || RCLASS_SINGLETON(m)); return module; }
void rb_vm_bugreport(void) { VALUE bt; if (GET_THREAD()->vm) { int i; SDR(); bt = rb_make_backtrace(); if (TYPE(bt) == T_ARRAY) for (i = 0; i < RARRAY_LEN(bt); i++) { dp(RARRAY_AT(bt, i)); } } #if HAVE_BACKTRACE #include <execinfo.h> #define MAX_NATIVE_TRACE 1024 { static void *trace[MAX_NATIVE_TRACE]; int n = backtrace(trace, MAX_NATIVE_TRACE); int i; fprintf(stderr, "-- backtrace of native function call (Use addr2line) --\n"); for (i=0; i<n; i++) { fprintf(stderr, "%p\n", trace[i]); } fprintf(stderr, "-------------------------------------------------------\n"); } #endif }
static VALUE rb_struct_set(VALUE obj, SEL sel, VALUE val) { VALUE members, slot; long i; // foo=: -> foo char buf[100]; const size_t s = strlcpy(buf, sel_getName(sel), sizeof buf); buf[s - 2] = '\0'; ID field = rb_intern(buf); members = rb_struct_members(obj); rb_struct_modify(obj); for (i=0; i<RARRAY_LEN(members); i++) { slot = RARRAY_AT(members, i); if (SYM2ID(slot) == field) { GC_WB(&RSTRUCT_PTR(obj)[i], val); return val; } } rb_name_error(rb_frame_this_func(), "`%s' is not a struct member", rb_id2name(field)); return Qnil; /* not reached */ }
static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv) { VALUE proc, passed, arity; proc = RARRAY_AT(args, 0); passed = RARRAY_AT(args, 1); arity = RARRAY_AT(args, 2); passed = rb_ary_plus(passed, rb_ary_new4(argc, argv)); rb_ary_freeze(passed); if(RARRAY_LEN(passed) < FIX2INT(arity)) { arity = make_curry_proc(proc, passed, arity); return arity; } arity = rb_proc_call(proc, passed); return arity; }
void rb_backtrace(void) { VALUE ary = rb_vm_backtrace(0); for (long i = 0, count = RARRAY_LEN(ary); i < count; i++) { printf("\tfrom %s\n", RSTRING_PTR(RARRAY_AT(ary, i))); } }
PRIMITIVE VALUE vm_masgn_get_elem_before_splat(VALUE ary, int offset) { if (offset < RARRAY_LEN(ary)) { return RARRAY_AT(ary, offset); } return Qnil; }
PRIMITIVE VALUE vm_masgn_get_elem_after_splat(VALUE ary, int before_splat_count, int after_splat_count, int offset) { const int len = RARRAY_LEN(ary); if (len < before_splat_count + after_splat_count) { offset += before_splat_count; if (offset < len) { return RARRAY_AT(ary, offset); } } else { offset += len - after_splat_count; return RARRAY_AT(ary, offset); } return Qnil; }
static css_node_t * node_get_child(void *context, int i) { RubyFrame frame; VALUE ary = ((struct ruby_css_node *)context)->children; assert(i >= 0 && i < RARRAY_LEN(ary)); return NODE(RARRAY_AT(ary, i))->node; }
VALUE rb_get_load_path(void) { VALUE load_path = rb_vm_load_path(); VALUE ary = rb_ary_new(); for (long i = 0, count = RARRAY_LEN(load_path); i < count; i++) { rb_ary_push(ary, rb_file_expand_path(RARRAY_AT(load_path, i), Qnil)); } return ary; }
static void * imp_rhash_keyenum_nextObject(void *rcv, SEL sel) { rb_hash_keyenum_t *ke = (rb_hash_keyenum_t *)rcv; if (ke->pos == RARRAY_LEN(ke->keys)) { return NULL; } VALUE key = RARRAY_AT(ke->keys, ke->pos); ke->pos++; return (void *)RB2OC(key); }
static VALUE env_each_value(VALUE ehash, SEL sel) { RETURN_ENUMERATOR(ehash, 0, 0); VALUE values = env_values(Qnil, 0); /* rb_secure(4); */ for (long i = 0, count = RARRAY_LEN(values); i < count; i++) { rb_yield(RARRAY_AT(values, i)); RETURN_IF_BROKEN(); } return ehash; }
void rb_thread_unlock_all_mutexes(rb_vm_thread_t *thread) { if (thread->mutexes != Qnil) { int i, count = RARRAY_LEN(thread->mutexes); for (i = 0; i < count; i++) { rb_mutex_unlock0(RARRAY_AT(thread->mutexes, i), false, false); } rb_ary_clear(thread->mutexes); } }
static VALUE make_struct(VALUE name, VALUE members, VALUE klass) { VALUE nstr; ID id; long i, len; OBJ_FREEZE(members); if (NIL_P(name)) { nstr = rb_class_new(klass); #if !WITH_OBJC rb_make_metaclass(nstr, RBASIC(klass)->klass); #endif rb_class_inherited(klass, nstr); } else { /* old style: should we warn? */ name = rb_str_to_str(name); id = rb_to_id(name); if (!rb_is_const_id(id)) { rb_name_error(id, "identifier %s needs to be constant", StringValuePtr(name)); } if (rb_const_defined_at(klass, id)) { rb_warn("redefining constant Struct::%s", StringValuePtr(name)); rb_mod_remove_const(klass, ID2SYM(id)); } nstr = rb_define_class_under(klass, rb_id2name(id), klass); } rb_ivar_set(nstr, id_members, members); rb_objc_define_method(*(VALUE *)nstr, "alloc", struct_alloc, 0); rb_objc_define_method(*(VALUE *)nstr, "new", rb_class_new_instance_imp, -1); rb_objc_define_method(*(VALUE *)nstr, "[]", rb_class_new_instance_imp, -1); rb_objc_define_method(*(VALUE *)nstr, "members", rb_struct_s_members_m, 0); len = RARRAY_LEN(members); for (i=0; i< len; i++) { ID id = SYM2ID(RARRAY_AT(members, i)); if (rb_is_local_id(id) || rb_is_const_id(id)) { long j = i; /* Needed for block data reference. */ /* Struct attribute reader */ rb_objc_define_method(nstr, rb_id2name(id), pl_imp_implementationWithBlock(^(VALUE obj) { return RSTRUCT_PTR(obj)[j]; }), 0); /* Struct attribute writer */ rb_objc_define_method(nstr, rb_id2name(rb_id_attrset(id)), pl_imp_implementationWithBlock(^(VALUE obj, VALUE val) { VALUE *ptr = RSTRUCT_PTR(obj); rb_struct_modify(obj); GC_WB(&ptr[i], val); return val; }), 1);
static void rb_mod_included_modules_nosuper(VALUE mod, VALUE ary) { VALUE inc_mods = rb_attr_get(mod, idIncludedModules); if (inc_mods != Qnil) { int i, count = RARRAY_LEN(inc_mods); for (i = 0; i < count; i++) { VALUE imod = RARRAY_AT(inc_mods, i); rb_ary_push(ary, imod); } } }
static VALUE make_struct(VALUE name, VALUE members, VALUE klass) { VALUE nstr; ID id; long i, count; OBJ_FREEZE(members); if (NIL_P(name)) { nstr = rb_class_new(klass); #if !WITH_OBJC rb_make_metaclass(nstr, RBASIC(klass)->klass); #endif rb_class_inherited(klass, nstr); } else { /* old style: should we warn? */ name = rb_str_to_str(name); id = rb_to_id(name); if (!rb_is_const_id(id)) { rb_name_error(id, "identifier %s needs to be constant", StringValuePtr(name)); } if (rb_const_defined_at(klass, id)) { rb_warn("redefining constant Struct::%s", StringValuePtr(name)); rb_mod_remove_const(klass, ID2SYM(id)); } nstr = rb_define_class_under(klass, rb_id2name(id), klass); } rb_iv_set(nstr, "__size__", LONG2NUM(RARRAY_LEN(members))); rb_iv_set(nstr, "__members__", members); rb_objc_define_method(*(VALUE *)nstr, "alloc", struct_alloc, 0); rb_objc_define_method(*(VALUE *)nstr, "new", rb_class_new_instance_imp, -1); rb_objc_define_method(*(VALUE *)nstr, "[]", rb_class_new_instance_imp, -1); rb_objc_define_method(*(VALUE *)nstr, "members", rb_struct_s_members_m, 0); for (i = 0, count = RARRAY_LEN(members); i < count; i++) { ID id = SYM2ID(RARRAY_AT(members, i)); if (rb_is_local_id(id) || rb_is_const_id(id)) { if (i < N_REF_FUNC) { rb_objc_define_method(nstr, rb_id2name(id), ref_func[i], 0); } else { rb_objc_define_method(nstr, rb_id2name(id), rb_struct_ref, 0); } rb_objc_define_method(nstr, rb_id2name(rb_id_attrset(id)), rb_struct_set, 1); } } return nstr; }
static VALUE rhash_delete_if(VALUE hash, SEL sel) { RETURN_ENUMERATOR(hash, 0, 0); rhash_modify(hash); VALUE ary = rb_ary_new(); rhash_foreach(hash, delete_if_i, ary); RETURN_IF_BROKEN(); for (int i = 0, count = RARRAY_LEN(ary); i < count; i++) { VALUE key = RARRAY_AT(ary, i); rhash_delete_key(hash, key); } return hash; }
void rb_exec_end_proc(void) { while (true) { const int count = RARRAY_LEN(at_exit_procs); if (count > 0) { VALUE proc = RARRAY_AT(at_exit_procs, count - 1); rb_ary_delete_at(at_exit_procs, count - 1); rb_rescue(rb_end_proc_call_try, proc, rb_end_proc_call_catch, 0); continue; } break; } }
static VALUE env_each_pair(VALUE ehash, SEL sel) { RETURN_ENUMERATOR(ehash, 0, 0); rb_secure(4); VALUE ary = rb_ary_new(); char **env = GET_ENVIRON(); while (*env != NULL) { const char *s = strchr(*env, '='); if (s != NULL) { rb_ary_push(ary, env_str_new(*env, s - *env)); rb_ary_push(ary, env_str_new2(s + 1)); } env++; } for (long i = 0, count = RARRAY_LEN(ary); i < count; i += 2) { rb_yield(rb_assoc_new(RARRAY_AT(ary, i), RARRAY_AT(ary, i + 1))); RETURN_IF_BROKEN(); } return ehash; }
static css_dim_t node_measure(void *context, float width, float height) { RubyFrame frame; VALUE node = ((struct ruby_css_node *)context)->obj; static SEL measure_sel = 0; if (measure_sel == 0) { #if CC_TARGET_OS_IPHONE || CC_TARGET_OS_APPLETV measure_sel = rb_selector("measure:"); #else measure_sel = rb_selector("measure"); #endif } VALUE args[] = { DBL2NUM(width), DBL2NUM(height) }; VALUE ary = rb_send(node, measure_sel, 2, args); css_dim_t dim; dim.dimensions[0] = NUM2DBL(RARRAY_AT(ary, 0)); dim.dimensions[1] = NUM2DBL(RARRAY_AT(ary, 1)); return dim; }
vm_resolve_args(VALUE **pargv, size_t argv_size, int *pargc, VALUE *args) { unsigned int i, argc = *pargc, real_argc = 0, j = 0; VALUE *argv = *pargv; bool splat_arg_follows = false; for (i = 0; i < argc; i++) { VALUE arg = args[j++]; if (arg == SPLAT_ARG_FOLLOWS) { splat_arg_follows = true; i--; } else { if (splat_arg_follows) { VALUE ary = rb_check_convert_type(arg, T_ARRAY, "Array", "to_a"); if (NIL_P(ary)) { ary = rb_ary_new4(1, &arg); } int count = RARRAY_LEN(ary); if (real_argc + count >= argv_size) { const size_t new_argv_size = real_argc + count + 100; VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * new_argv_size); memcpy(new_argv, argv, sizeof(VALUE) * argv_size); argv = new_argv; argv_size = new_argv_size; } int j; for (j = 0; j < count; j++) { argv[real_argc++] = RARRAY_AT(ary, j); } splat_arg_follows = false; } else { if (real_argc >= argv_size) { const size_t new_argv_size = real_argc + 100; VALUE *new_argv = (VALUE *)xmalloc_ptrs(sizeof(VALUE) * new_argv_size); memcpy(new_argv, argv, sizeof(VALUE) * argv_size); argv = new_argv; argv_size = new_argv_size; } argv[real_argc++] = arg; } } } *pargv = argv; *pargc = real_argc; }
static VALUE env_replace(VALUE env, SEL sel, VALUE hash) { VALUE keys = env_keys(Qnil, 0); /* rb_secure(4); */ if (env == hash) { return env; } hash = to_hash(hash); rb_hash_foreach(hash, env_replace_i, keys); for (long i = 0, count = RARRAY_LEN(keys); i < count; i++) { env_delete(env, RARRAY_AT(keys, i)); } return env; }
VALUE rb_mod_ancestors(VALUE mod) { // This method should return a new array without classes that should be // ignored. VALUE ary = rb_mod_ancestors_nocopy(mod); VALUE filtered = rb_ary_new(); for (int i = 0, count = RARRAY_LEN(ary); i < count; i++) { VALUE p = RARRAY_AT(ary, i); if (!rb_class_hidden(p)) { rb_ary_push(filtered, p); } } return filtered; }