Tuple* UnMarshaller::get_tuple() { size_t count; stream >> count; Tuple* tup = Tuple::create(state, count); for(size_t i = 0; i < count; i++) { tup->put(state, i, unmarshal()); } return tup; }
Object* rbx_zsuper_send(STATE, CallFrame* call_frame, CallSite* call_site, Object* block) { Object* const recv = call_frame->self(); VariableScope* scope = call_frame->method_scope(state); assert(scope); MachineCode* v = scope->method()->machine_code(); Object* splat_obj = 0; Array* splat = 0; size_t arg_count = v->total_args; if(v->splat_position >= 0) { splat_obj = scope->get_local(state, v->splat_position); splat = try_as<Array>(splat_obj); if(splat) { arg_count += splat->size(); } else { arg_count++; } } Tuple* tup = Tuple::create_dirty(state, arg_count); for(int i = 0; i < v->total_args; i++) { tup->put(state, i, scope->get_local(state, i)); } if(splat) { for(native_int i = 0; i < splat->size(); i++) { tup->put(state, i + v->total_args, splat->get(state, i)); } } else if(splat_obj) { tup->put(state, v->total_args, splat_obj); } Arguments out_args(call_site->name(), recv, block, arg_count, 0); out_args.use_tuple(tup, arg_count); return call_site->execute(state, call_frame, out_args); }
void Metrics::update_ruby_values(STATE) { GCDependent guard(state, 0); Tuple* values = values_.get(); int index = 0; for(MetricsMap::iterator i = metrics_map_.begin(); i != metrics_map_.end(); ++i) { values->put(state, index++, Integer::from(state, (*i)->second)); } }
void test_dup_ignores_singleton_class() { Tuple* tup = Tuple::create(state, 1); tup->put(state, 0, Qtrue); // Force it to exist. tup->singleton_class(state); Tuple* tup2 = as<Tuple>(tup->duplicate(state)); TS_ASSERT(!try_as<SingletonClass>(tup2->klass_)); TS_ASSERT_DIFFERS(tup->singleton_class(state), tup2->singleton_class(state)); }
Tuple* Tuple::from(STATE, native_int fields, ...) { Tuple* tup = Tuple::create(state, fields); va_list ar; va_start(ar, fields); for(native_int i = 0; i < fields; i++) { Object *obj = va_arg(ar, Object*); // fields equals size so bounds checking is unecessary tup->put(state, i, obj); } va_end(ar); return tup; }
Object* LookupTable::store(STATE, Object* key, Object* val) { unsigned int num_entries, num_bins, bin; Object* new_ent; Tuple* cur; Tuple* entry; key_to_sym(key); num_entries = entries_->to_native(); num_bins = bins_->to_native(); if(max_density_p(num_entries, num_bins)) { redistribute(state, num_bins <<= 1); } bin = find_bin(key_hash(key), num_bins); cur = entry = try_as<Tuple>(values_->at(state, bin)); while(entry) { if(entry->at(state, 0) == key) { entry->put(state, 1, val); return val; } cur = entry; entry = try_as<Tuple>(entry->at(state, 2)); } new_ent = entry_new(state, key, val); if(cur) { cur->put(state, 2, new_ent); } else { values_->put(state, bin, new_ent); } entries(state, Fixnum::from(num_entries + 1)); return val; }
Tuple* System::vm_thread_state(STATE) { ThreadState* ts = state->thread_state(); Tuple* tuple = Tuple::create(state, 5); Symbol* reason = 0; switch(ts->raise_reason()) { case cNone: reason = state->symbol("none"); break; case cException: reason = state->symbol("exception"); break; case cReturn: reason = state->symbol("return"); break; case cBreak: reason = state->symbol("break"); break; case cExit: reason = state->symbol("exit"); break; case cCatchThrow: reason = state->symbol("catch_throw"); break; default: reason = state->symbol("unknown"); } tuple->put(state, 0, reason); tuple->put(state, 1, ts->raise_value()); tuple->put(state, 2, ts->destination_scope()); tuple->put(state, 3, ts->current_exception()); tuple->put(state, 4, ts->throw_dest()); return tuple; }
Object* Arguments::shift(STATE) { Object* first = arguments_[0]; if(argument_container_) { Tuple* tup = Tuple::create_dirty(state, total() - 1); for(uint32_t i = 1; i < total_; i++) { tup->put(state, i - 1, get_argument(i)); } use_tuple(tup, total_ - 1); } else { total_--; arguments_++; } return first; }
InstructionSequence* UnMarshaller::get_iseq() { size_t count; long op; stream >> count; InstructionSequence* iseq = InstructionSequence::create(state, count); Tuple* ops = iseq->opcodes(); for(size_t i = 0; i < count; i++) { stream >> op; ops->put(state, i, Fixnum::from(op)); } iseq->post_marshal(state); return iseq; }
inline void make_array(STATE, CallFrame* call_frame, intptr_t count) { Object* t2; Array* ary = Array::create(state, count); Tuple* tup = ary->tuple(); #ifdef RBX_ALLOC_TRACKING if(unlikely(state->vm()->allocation_tracking())) { ary->setup_allocation_site(state); } #endif int j = count - 1; for(; j >= 0; j--) { t2 = stack_pop(); tup->put(state, j, t2); } ary->total(state, Fixnum::from(count)); stack_push(ary); }
void Array::unshift(STATE, Object* val) { native_int new_size = total_->to_native() + 1; native_int lend = start_->to_native(); if(lend > 0) { tuple_->put(state, lend-1, val); start(state, Fixnum::from(lend-1)); total(state, Fixnum::from(new_size)); } else { Tuple* nt = Tuple::create(state, new_size); nt->copy_from(state, tuple_, start_, total_, Fixnum::from(1)); nt->put(state, 0, val); total(state, Fixnum::from(new_size)); start(state, Fixnum::from(0)); tuple(state, nt); } }
Array* Array::new_range(STATE, Fixnum* start, Fixnum* count) { Array* ary = state->new_object<Array>(class_object(state)); ary->total(state, count); ary->start(state, Fixnum::from(0)); native_int total = count->to_native(); if(total <= 0) { ary->tuple(state, Tuple::create(state, 0)); } else { Tuple* tup = Tuple::create(state, total); Tuple* orig = tuple_; for(native_int i = 0, j = start->to_native(); i < total; i++, j++) { tup->put(state, i, orig->at(state, j)); } ary->tuple(state, tup); } return ary; }
InstructionSequence* UnMarshaller::get_iseq() { size_t count; char data[OPCODE_LENGTH]; stream >> count; // Read off newline stream.get(); InstructionSequence* iseq = InstructionSequence::create(state, count); Tuple* ops = iseq->opcodes(); for(size_t i = 0; i < count; i++) { stream.getline(data, OPCODE_LENGTH); long op = strtol(data, NULL, 10); ops->put(state, i, Fixnum::from(op)); } iseq->post_marshal(state); return iseq; }
void Metrics::init_ruby_metrics(STATE) { LookupTable* map = LookupTable::create(state); Module* mod = as<Module>(G(rubinius)->get_const(state, state->symbol("Metrics"))); mod->set_const(state, "Map", map); Tuple* values = Tuple::create(state, metrics_map_.size()); values_.set(values); mod->set_const(state, "Values", values); int index = 0; for(MetricsMap::iterator i = metrics_map_.begin(); i != metrics_map_.end(); ++i) { values->put(state, index, Bignum::from(state, (*i)->second)); Object* key = reinterpret_cast<Object*>(state->symbol((*i)->first.c_str())); map->store(state, key, Fixnum::from(index++)); } }
Object* LookupTable::remove(STATE, Object* key) { hashval bin; Object* val; Tuple* entry; Tuple* lst; key_to_sym(key); size_t num_entries = entries_->to_native(); size_t num_bins = bins_->to_native(); if(min_density_p(num_entries, num_bins) && (num_bins >> 1) >= LOOKUPTABLE_MIN_SIZE) { redistribute(state, num_bins >>= 1); } bin = find_bin(key_hash(key), num_bins); entry = try_as<Tuple>(values_->at(state, bin)); lst = NULL; while(entry) { Object* link = entry->at(state, 2); if(entry->at(state, 0) == key) { val = entry->at(state, 1); if(lst) { lst->put(state, 2, link); } else { values_->put(state, bin, link); } entries(state, Fixnum::from(entries_->to_native() - 1)); return val; } lst = entry; entry = try_as<Tuple>(link); } return Qnil; }
void test_duplicate() { Tuple* tup = Tuple::create(state, 1); tup->put(state, 0, Qtrue); tup->set_ivar(state, state->symbol("@name"), state->symbol("foo")); Tuple* tup2 = as<Tuple>(tup->duplicate(state)); TS_ASSERT_EQUALS(tup2->at(state, 0), Qtrue); TS_ASSERT_DIFFERS(tup->id(state), tup2->id(state)); TS_ASSERT(tup->ivars_ != tup2->ivars_); TS_ASSERT_EQUALS(tup2->get_ivar(state, state->symbol("@name")), state->symbol("foo")); tup->ivars_ = as<CompactLookupTable>(tup->ivars_)->to_lookuptable(state); Tuple* tup3 = as<Tuple>(tup->duplicate(state)); TS_ASSERT(tup->ivars_ != tup2->ivars_); TS_ASSERT_EQUALS(tup3->get_ivar(state, state->symbol("@name")), state->symbol("foo")); }
static Tuple* _md_region_to_tuple(STATE, OnigRegion *region, int pos) { Tuple* tup = Tuple::create(state, region->num_regs - 1); for(int i = 1; i < region->num_regs; i++) { int beg = region->beg[i]; Tuple* sub; // If onig says the beginning is less than 0, then it's indicating // that there was no match for it. This happens with an optional // match like (a)?. We want to preserve this knowledge so we // don't add pos to it. if(beg < 0) { sub = Tuple::from(state, 2, Fixnum::from(region->beg[i]), Fixnum::from(region->end[i])); } else { sub = Tuple::from(state, 2, Fixnum::from(region->beg[i] + pos), Fixnum::from(region->end[i] + pos)); } tup->put(state, i - 1, sub); } return tup; }
inline bool zsuper(STATE, CallFrame* call_frame, intptr_t literal) { Object* block = stack_pop(); Object* const recv = call_frame->self(); VariableScope* scope = call_frame->method_scope(state); interp_assert(scope); MachineCode* mc = scope->method()->machine_code(); Object* splat_obj = 0; Array* splat = 0; size_t arg_count = mc->total_args; if(mc->splat_position >= 0) { splat_obj = scope->get_local(state, mc->splat_position); splat = try_as<Array>(splat_obj); if(splat) { arg_count += splat->size(); } else { arg_count++; } } Tuple* tup = Tuple::create(state, arg_count); native_int tup_index = 0; native_int fixed_args; if(splat) { fixed_args = mc->splat_position; } else if(mc->keywords) { fixed_args = mc->total_args - 1; } else { fixed_args = mc->total_args; } for(native_int i = 0; i < fixed_args; i++) { tup->put(state, tup_index++, scope->get_local(state, i)); } if(splat) { for(native_int i = 0; i < splat->size(); i++) { tup->put(state, tup_index++, splat->get(state, i)); } } else if(splat_obj) { tup->put(state, tup_index++, splat_obj); } if(mc->post_args) { native_int post_position = mc->splat_position + 1; for(native_int i = post_position; i < post_position + mc->post_args; i++) { tup->put(state, tup_index++, scope->get_local(state, i)); } } if(mc->keywords) { native_int placeholder_position = splat_obj ? mc->total_args : mc->total_args - 1; native_int keywords_position = placeholder_position + 1; Object* placeholder = scope->get_local(state, placeholder_position); Array* ary = Array::create(state, 2); for(native_int i = keywords_position; i <= mc->keywords_count; i++) { ary->set(state, 0, as<Symbol>(call_frame->compiled_code->local_names()->at(state, i))); ary->set(state, 1, scope->get_local(state, i)); placeholder->send(state, state->symbol("[]="), ary); } tup->put(state, tup_index++, scope->get_local(state, placeholder_position)); } CallSite* call_site = reinterpret_cast<CallSite*>(literal); Arguments new_args(call_site->name(), recv, block, arg_count, 0); new_args.use_tuple(tup, arg_count); Object* ret; Symbol* current_name = call_frame->original_name(); if(call_site->name() != current_name) { call_site->name(state, current_name); } ret = call_site->execute(state, new_args); state->vm()->checkpoint(state); CHECK_AND_PUSH(ret); }