inline static VALUE mArray_json_transfrom(VALUE self, VALUE Vstate, VALUE Vdepth) { long i, len = RARRAY_LEN(self); VALUE shift, result; long depth = NIL_P(Vdepth) ? 0 : FIX2LONG(Vdepth); VALUE delim = rb_str_new2(","); GET_STATE(Vstate); if (state->check_circular) { VALUE self_id = rb_obj_id(self); rb_hash_aset(state->seen, self_id, Qtrue); result = rb_str_buf_new(len); if (RSTRING_LEN(state->array_nl)) rb_str_append(delim, state->array_nl); shift = rb_str_times(state->indent, LONG2FIX(depth + 1)); rb_str_buf_cat2(result, "["); rb_str_buf_append(result, state->array_nl); for (i = 0; i < len; i++) { VALUE element = RARRAY_PTR(self)[i]; if (RTEST(rb_hash_aref(state->seen, rb_obj_id(element)))) { rb_raise(eCircularDatastructure, "circular data structures not supported!"); } OBJ_INFECT(result, element); if (i > 0) rb_str_buf_append(result, delim); rb_str_buf_append(result, shift); rb_str_buf_append(result, rb_funcall(element, i_to_json, 2, Vstate, LONG2FIX(depth + 1))); } if (RSTRING_LEN(state->array_nl)) { rb_str_buf_append(result, state->array_nl); rb_str_buf_append(result, rb_str_times(state->indent, LONG2FIX(depth))); } rb_str_buf_cat2(result, "]"); rb_hash_delete(state->seen, self_id); } else { result = rb_str_buf_new(len); if (RSTRING_LEN(state->array_nl)) rb_str_append(delim, state->array_nl); shift = rb_str_times(state->indent, LONG2FIX(depth + 1)); rb_str_buf_cat2(result, "["); rb_str_buf_append(result, state->array_nl); for (i = 0; i < len; i++) { VALUE element = RARRAY_PTR(self)[i]; OBJ_INFECT(result, element); if (i > 0) rb_str_buf_append(result, delim); rb_str_buf_append(result, shift); rb_str_buf_append(result, rb_funcall(element, i_to_json, 2, Vstate, LONG2FIX(depth + 1))); } rb_str_buf_append(result, state->array_nl); if (RSTRING_LEN(state->array_nl)) { rb_str_buf_append(result, rb_str_times(state->indent, LONG2FIX(depth))); } rb_str_buf_cat2(result, "]"); } return result; }
static VALUE define_final(VALUE os, SEL sel, int argc, VALUE *argv) { VALUE obj, block; rb_scan_args(argc, argv, "11", &obj, &block); if (argc == 1) { block = rb_block_proc(); } else if (!rb_respond_to(block, rb_intern("call"))) { rb_raise(rb_eArgError, "wrong type argument %s (should be callable)", rb_obj_classname(block)); } if (SPECIAL_CONST_P(obj)) { rb_raise(rb_eArgError, "immediate types are not finalizable"); } rb_vm_finalizer_t *finalizer = rb_objc_get_associative_ref((void *)obj, &finalizer_key); if (finalizer == NULL) { finalizer = (rb_vm_finalizer_t *) rb_objc_newobj(sizeof(rb_vm_finalizer_t *)); finalizer->klass = rb_cFinalizer; finalizer->objid = rb_obj_id(obj, 0); GC_WB(&finalizer->finalizers, rb_ary_new()); rb_objc_set_associative_ref((void *)obj, &finalizer_key, finalizer); rb_vm_register_finalizer(finalizer); } rb_ary_push(finalizer->finalizers, block); // For RubySpec conformance. return rb_ary_new3(2, INT2FIX(rb_safe_level()), block); }
/* * call-seq: to_json(state = nil, depth = 0) * * Returns a JSON string containing a JSON object, that is unparsed from * this Hash instance. * _state_ is a JSON::State object, that can also be used to configure the * produced JSON string output further. * _depth_ is used to find out nesting depth, to indent accordingly. */ static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self) { VALUE Vstate, Vdepth, result; long depth; rb_scan_args(argc, argv, "02", &Vstate, &Vdepth); depth = NIL_P(Vdepth) ? 0 : FIX2LONG(Vdepth); if (NIL_P(Vstate)) { long len = RHASH_SIZE(self); result = rb_str_buf_new(len); rb_str_buf_cat2(result, "{"); rb_hash_foreach(self, hash_to_json_i, result); rb_str_buf_cat2(result, "}"); } else { GET_STATE(Vstate); check_max_nesting(state, depth); if (state->check_circular) { VALUE self_id = rb_obj_id(self); if (RTEST(rb_hash_aref(state->seen, self_id))) { rb_raise(eCircularDatastructure, "circular data structures not supported!"); } rb_hash_aset(state->seen, self_id, Qtrue); result = mHash_json_transfrom(self, Vstate, LONG2FIX(depth)); rb_hash_delete(state->seen, self_id); } else { result = mHash_json_transfrom(self, Vstate, LONG2FIX(depth)); } } OBJ_INFECT(result, self); FORCE_UTF8(result); return result; }
static void free_expr_arglist(ABSTRACT_ARGLIST* arglist) { VALUE args = (VALUE)arglist; VALUE registory = rb_ary_shift(args); rb_hash_delete(registory, rb_obj_id(args)); return; }
static VALUE method_zkrb_init(int argc, VALUE* argv, VALUE self) { VALUE hostPort; VALUE options; rb_scan_args(argc, argv, "11", &hostPort, &options); if (NIL_P(options)) { options = rb_hash_new(); } else { Check_Type(options, T_HASH); } Check_Type(hostPort, T_STRING); // Look up :zkc_log_level VALUE log_level = rb_hash_aref(options, ID2SYM(rb_intern("zkc_log_level"))); if (NIL_P(log_level)) { zoo_set_debug_level(0); // no log messages } else { Check_Type(log_level, T_FIXNUM); zoo_set_debug_level((int)log_level); } VALUE data; struct zkrb_instance_data *zk_local_ctx; data = Data_Make_Struct(Zookeeper, struct zkrb_instance_data, 0, free_zkrb_instance_data, zk_local_ctx); zk_local_ctx->queue = zkrb_queue_alloc(); if (zk_local_ctx->queue == NULL) rb_raise(rb_eRuntimeError, "could not allocate zkrb queue!"); zoo_deterministic_conn_order(0); zkrb_calling_context *ctx = zkrb_calling_context_alloc(ZKRB_GLOBAL_REQ, zk_local_ctx->queue); zk_local_ctx->object_id = FIX2LONG(rb_obj_id(self)); zk_local_ctx->zh = zookeeper_init( RSTRING_PTR(hostPort), zkrb_state_callback, session_timeout_msec(self), &zk_local_ctx->myid, ctx, 0); #warning [wickman] TODO handle this properly on the Ruby side rather than C side if (!zk_local_ctx->zh) { rb_raise(rb_eRuntimeError, "error connecting to zookeeper: %d", errno); } rb_iv_set(self, "@_data", data); rb_funcall(self, rb_intern("zkc_set_running_and_notify!"), 0); error: return Qnil; }
void ruby_window_free(win_T *win) { if (win->w_ruby_ref) { rb_hash_aset(objtbl, rb_obj_id((VALUE) win->w_ruby_ref), Qnil); RDATA(win->w_ruby_ref)->data = NULL; } }
void ruby_buffer_free(buf_T *buf) { if (buf->b_ruby_ref) { rb_hash_aset(objtbl, rb_obj_id((VALUE) buf->b_ruby_ref), Qnil); RDATA(buf->b_ruby_ref)->data = NULL; } }
static int frame_edges_i(st_data_t key, st_data_t val, st_data_t arg) { VALUE edges = (VALUE)arg; intptr_t weight = (intptr_t)val; rb_hash_aset(edges, rb_obj_id((VALUE)key), INT2FIX(weight)); return ST_CONTINUE; }
VALUE ae_store_handle(VALUE ae_handle) { AE_TRACE(); VALUE ae_handle_id = rb_obj_id(ae_handle); rb_hash_aset(AE_handles, ae_handle_id, ae_handle); return ae_handle_id; }
static VALUE rb_cpShapeSetCollType(VALUE self, VALUE val) { VALUE col_type = rb_obj_id(val); rb_iv_set(self, "collType", val); SHAPE(self)->collision_type = NUM2UINT(col_type); return val; }
static VALUE rb_cpShapeSetGroup(VALUE self, VALUE val) { VALUE col_type = rb_obj_id(val); rb_iv_set(self, "group", val); SHAPE(self)->group = NUM2UINT(col_type); return val; }
static ABSTRACT_ARGLIST* init_expr_arglist(ABSTRACT_CALLER* callback_state) { VALUE self = (VALUE)callback_state; VALUE registory = rb_ivar_get(self, rb_intern("@internal_expr_args")); VALUE args = rb_ary_new(); rb_ary_push(args, registory); rb_hash_aset(registory, rb_obj_id(args), args); return (ABSTRACT_ARGLIST*)args; }
// Refer Ruby source ext/objspace/object_tracing.c::freeobj_i static void freeobj_i(VALUE tpval, void *data) { rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval); rbkit_obj_destroyed_event *event; VALUE obj = rb_tracearg_object(tparg); rbkit_logger *arg = (rbkit_logger *)data; // Delete allocation info of freed object delete_rbkit_allocation_info(tparg, obj, arg->str_table, arg->object_table); event = new_rbkit_obj_destroyed_event(FIX2ULONG(rb_obj_id(obj))); pack_event((rbkit_event_header *)event, arg->msgpacker); free(event); send_message(arg->sbuf); }
static VALUE env_references(VALUE rbenv) { rb_env_t* env; VALUE result = rb_ary_new(); GetEnvPtr(rbenv, env); if (env->env) { int i; for (i = 0; i < env->env_size; ++i) rb_ary_push(result, rb_obj_id(env->env[i])); } return result; }
static void sampling_job_handler(void *data_unused) { int start = 0; int lines[BUF_SIZE]; VALUE buff[BUF_SIZE]; VALUE rb_method_name, rb_label, rb_file, rb_singleton_method; int i, is_singleton; char *method_name, *label, *file; unsigned long line, thread_id; rbkit_cpu_sample *sample = malloc(sizeof(rbkit_cpu_sample)); int collected_size = rb_profile_frames(start, sizeof(buff) / sizeof(VALUE), buff, lines); rbkit_frame_data *frame_data = malloc(sizeof(rbkit_frame_data) * collected_size); sample->frames = frame_data; sample->frame_count = collected_size; for (i=0; i<collected_size; i++) { rb_method_name = rb_profile_frame_method_name(buff[i]); if(NIL_P(rb_method_name)) { method_name = NULL; } else { method_name = StringValueCStr(rb_method_name); } frame_data[i].method_name = method_name; rb_label = rb_profile_frame_full_label(buff[i]); label = StringValueCStr(rb_label); frame_data[i].label = label; rb_file = rb_profile_frame_absolute_path(buff[i]); if(NIL_P(rb_file)) rb_file = rb_profile_frame_path(buff[i]); file = StringValueCStr(rb_file); frame_data[i].file = file; line = FIX2ULONG(rb_profile_frame_first_lineno(buff[i])); frame_data[i].line = line; rb_singleton_method = rb_profile_frame_singleton_method_p(buff[i]); is_singleton = rb_singleton_method == Qtrue; frame_data[i].is_singleton_method = is_singleton; thread_id = FIX2ULONG(rb_obj_id(rb_thread_current())); frame_data[i].thread_id = thread_id; } queue_cpu_sample_for_sending(sample); free(frame_data); free(sample); }
// Refer Ruby source ext/objspace/object_tracing.c::newobj_i static void newobj_i(VALUE tpval, void *data) { rbkit_obj_created_event *event; rbkit_logger * arg = (rbkit_logger *)data; rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval); rbkit_allocation_info *info = new_rbkit_allocation_info(tparg, arg->str_table, arg->object_table); VALUE obj = rb_tracearg_object(tparg); VALUE klass = RBASIC_CLASS(obj); const char *class_name = NULL; if (!NIL_P(klass) && BUILTIN_TYPE(obj) != T_NONE && BUILTIN_TYPE(obj) != T_ZOMBIE && BUILTIN_TYPE(obj) != T_ICLASS) class_name = rb_class2name(klass); event = new_rbkit_obj_created_event(FIX2ULONG(rb_obj_id(obj)), class_name, info); pack_event((rbkit_event_header *)event, arg->msgpacker); free(event); send_message(arg->sbuf); }
static int frame_i(st_data_t key, st_data_t val, st_data_t arg) { VALUE frame = (VALUE)key; frame_data_t *frame_data = (frame_data_t *)val; VALUE results = (VALUE)arg; VALUE details = rb_hash_new(); VALUE name, file, edges, lines; VALUE line; rb_hash_aset(results, rb_obj_id(frame), details); name = rb_profile_frame_full_label(frame); rb_hash_aset(details, sym_name, name); file = rb_profile_frame_absolute_path(frame); if (NIL_P(file)) file = rb_profile_frame_path(frame); rb_hash_aset(details, sym_file, file); if ((line = rb_profile_frame_first_lineno(frame)) != INT2FIX(0)) rb_hash_aset(details, sym_line, line); rb_hash_aset(details, sym_total_samples, SIZET2NUM(frame_data->total_samples)); rb_hash_aset(details, sym_samples, SIZET2NUM(frame_data->caller_samples)); if (frame_data->edges) { edges = rb_hash_new(); rb_hash_aset(details, sym_edges, edges); st_foreach(frame_data->edges, frame_edges_i, (st_data_t)edges); st_free_table(frame_data->edges); frame_data->edges = NULL; } if (frame_data->lines) { lines = rb_hash_new(); rb_hash_aset(details, sym_lines, lines); st_foreach(frame_data->lines, frame_lines_i, (st_data_t)lines); st_free_table(frame_data->lines); frame_data->lines = NULL; } xfree(frame_data); return ST_DELETE; }
static void rb_call_os_finalizer2(VALUE obj, VALUE table) { long i, count; VALUE args[3]; int status, critical_save; critical_save = rb_thread_critical; rb_thread_critical = Qtrue; args[1] = rb_ary_new3(1, rb_obj_id(obj, 0)); args[2] = (VALUE)rb_safe_level(); for (i = 0, count = RARRAY_LEN(table); i < count; i++) { args[0] = RARRAY_AT(table, i); rb_protect(run_single_final, (VALUE)args, &status); } rb_thread_critical = critical_save; }
static void finalize(JSContext* js_context, JSObject* obj) { VALUE ruby_context = (VALUE)JS_GetContextPrivate(js_context); if (ruby_context) { JohnsonContext* context; JohnsonRuntime* runtime; Data_Get_Struct(ruby_context, JohnsonContext, context); VALUE ruby_runtime = (VALUE)JS_GetRuntimePrivate(JS_GetRuntime(js_context)); Data_Get_Struct(ruby_runtime, JohnsonRuntime, runtime); VALUE self = (VALUE)JS_GetInstancePrivate(context->js, obj, JS_GET_CLASS(context->js, obj), NULL); // remove the proxy OID from the id map JS_HashTableRemove(runtime->rbids, (void *)self); // free up the ruby value for GC rb_funcall(ruby_runtime, rb_intern("remove_gcthing"), 1, rb_obj_id(self)); } }
static VALUE iow_internal_object_id(VALUE self) { VALUE obj = (VALUE)DATA_PTR(self); return rb_obj_id(obj); }
static VALUE method_zkrb_init(int argc, VALUE* argv, VALUE self) { VALUE hostPort=Qnil; VALUE options=Qnil; rb_scan_args(argc, argv, "11", &hostPort, &options); if (NIL_P(options)) { options = rb_hash_new(); } else { Check_Type(options, T_HASH); } Check_Type(hostPort, T_STRING); // Look up :zkc_log_level VALUE log_level = rb_hash_aref(options, ID2SYM(rb_intern("zkc_log_level"))); if (NIL_P(log_level)) { zoo_set_debug_level(0); // no log messages } else { Check_Type(log_level, T_FIXNUM); zoo_set_debug_level(FIX2INT(log_level)); } volatile VALUE data; zkrb_instance_data_t *zk_local_ctx; data = Data_Make_Struct(CZookeeper, zkrb_instance_data_t, 0, free_zkrb_instance_data, zk_local_ctx); zk_local_ctx->queue = zkrb_queue_alloc(); if (zk_local_ctx->queue == NULL) rb_raise(rb_eRuntimeError, "could not allocate zkrb queue!"); zoo_deterministic_conn_order(0); zkrb_calling_context *ctx = zkrb_calling_context_alloc(ZKRB_GLOBAL_REQ, zk_local_ctx->queue); zk_local_ctx->object_id = FIX2LONG(rb_obj_id(self)); zk_local_ctx->zh = zookeeper_init( RSTRING_PTR(hostPort), // const char *host zkrb_state_callback, // watcher_fn session_timeout_msec(self), // recv_timeout &zk_local_ctx->myid, // cilentid_t ctx, // void *context 0); // flags zkrb_debug("method_zkrb_init, zk_local_ctx: %p, zh: %p, queue: %p, calling_ctx: %p", zk_local_ctx, zk_local_ctx->zh, zk_local_ctx->queue, ctx); if (!zk_local_ctx->zh) { rb_raise(rb_eRuntimeError, "error connecting to zookeeper: %d", errno); } zk_local_ctx->orig_pid = getpid(); rb_iv_set(self, "@_data", data); rb_funcall(self, rb_intern("zkc_set_running_and_notify!"), 0); return Qnil; }
/* * call-seq: seen?(object) * * Returns _true_, if _object_ was already seen during this generating run. */ static VALUE cState_seen_p(VALUE self, VALUE object) { GET_STATE(self); return rb_hash_aref(state->seen, rb_obj_id(object)); }
/* * call-seq: remember(object) * * Remember _object_, to find out if it was already encountered (if a cyclic * data structure is rendered). */ static VALUE cState_remember(VALUE self, VALUE object) { GET_STATE(self); return rb_hash_aset(state->seen, rb_obj_id(object), Qtrue); }
/* * call-seq: forget(object) * * Forget _object_ for this generating run. */ static VALUE cState_forget(VALUE self, VALUE object) { GET_STATE(self); return rb_hash_delete(state->seen, rb_obj_id(object)); }
static VALUE stackprof_results(int argc, VALUE *argv, VALUE self) { VALUE results, frames; if (!_stackprof.frames || _stackprof.running) return Qnil; results = rb_hash_new(); rb_hash_aset(results, sym_version, DBL2NUM(1.1)); rb_hash_aset(results, sym_mode, _stackprof.mode); rb_hash_aset(results, sym_interval, _stackprof.interval); rb_hash_aset(results, sym_samples, SIZET2NUM(_stackprof.overall_samples)); rb_hash_aset(results, sym_gc_samples, SIZET2NUM(_stackprof.during_gc)); rb_hash_aset(results, sym_missed_samples, SIZET2NUM(_stackprof.overall_signals - _stackprof.overall_samples)); frames = rb_hash_new(); rb_hash_aset(results, sym_frames, frames); st_foreach(_stackprof.frames, frame_i, (st_data_t)frames); st_free_table(_stackprof.frames); _stackprof.frames = NULL; if (_stackprof.raw && _stackprof.raw_samples_len) { size_t len, n, o; VALUE raw_samples = rb_ary_new_capa(_stackprof.raw_samples_len); for (n = 0; n < _stackprof.raw_samples_len; n++) { len = (size_t)_stackprof.raw_samples[n]; rb_ary_push(raw_samples, SIZET2NUM(len)); for (o = 0, n++; o < len; n++, o++) rb_ary_push(raw_samples, rb_obj_id(_stackprof.raw_samples[n])); rb_ary_push(raw_samples, SIZET2NUM((size_t)_stackprof.raw_samples[n])); } free(_stackprof.raw_samples); _stackprof.raw_samples = NULL; _stackprof.raw_samples_len = 0; _stackprof.raw_samples_capa = 0; _stackprof.raw_sample_index = 0; _stackprof.raw = 0; rb_hash_aset(results, sym_raw, raw_samples); } if (argc == 1) _stackprof.out = argv[0]; if (RTEST(_stackprof.out)) { VALUE file; if (RB_TYPE_P(_stackprof.out, T_STRING)) { file = rb_file_open_str(_stackprof.out, "w"); } else { file = rb_io_check_io(_stackprof.out); } rb_marshal_dump(results, file); rb_io_flush(file); _stackprof.out = Qnil; return file; } else { return results; } }
static VALUE object_spec_rb_obj_id(VALUE self, VALUE obj) { return rb_obj_id(obj); }