예제 #1
0
static VALUE profiler_m_profile(int argc, VALUE *argv, VALUE self)
{
  rb_iseq_t *iseq;
  VALUE minfo, obj, mid, singleton_p = Qfalse;

  rb_scan_args(argc, argv, "21", &obj, &mid, &singleton_p);

  if (rb_obj_class(mid) != rb_cSymbol) {
    rb_raise(rb_eArgError, "second argument should be symbol");
  }

  iseq = iseq_find(obj, mid, singleton_p);
  minfo = rb_funcall(cMethodInfo, rb_intern("new"), 4, iseq->self, obj, mid, singleton_p);
  iseq_inject(iseq);
  profiler_linetrace(0);
  rb_funcall(cMethodInfo, rb_intern("register"), 1, minfo);

  return Qnil;
}
예제 #2
0
extern VALUE
context_create(VALUE thread)
{
  debug_context_t *context = ALLOC(debug_context_t);

  context->last_file         = Qnil;
  context->last_line         = Qnil;
  context->flags             = 0;
  context->calced_stack_size = real_stack_size();
  context->thnum             = ++thnum_max;
  context->thread            = thread;
  reset_stepping_stop_points(context);
  context->stop_reason       = CTX_STOP_NONE;
  context->backtrace         = Qnil;

  if (rb_obj_class(thread) == cDebugThread) CTX_FL_SET(context, CTX_FL_IGNORE);

  return Data_Wrap_Struct(cContext, context_mark, 0, context);
}
예제 #3
0
파일: struct.c 프로젝트: Chatto/VGdesk
static VALUE
recursive_hash(VALUE s, VALUE dummy, int recur)
{
    long i, len;
    st_index_t h;
    VALUE n, *ptr;

    h = rb_hash_start(rb_hash(rb_obj_class(s)));
    if (!recur) {
	ptr = RSTRUCT_PTR(s);
	len = RSTRUCT_LEN(s);
	for (i = 0; i < len; i++) {
	    n = rb_hash(ptr[i]);
	    h = rb_hash_uint(h, NUM2LONG(n));
	}
    }
    h = rb_hash_end(h);
    return INT2FIX(h);
}
예제 #4
0
static VALUE
ossl_cipher_init(int argc, VALUE *argv, VALUE self, int mode)
{
    EVP_CIPHER_CTX *ctx;
    unsigned char key[EVP_MAX_KEY_LENGTH], *p_key = NULL;
    unsigned char iv[EVP_MAX_IV_LENGTH], *p_iv = NULL;
    VALUE pass, init_v;

    if(rb_scan_args(argc, argv, "02", &pass, &init_v) > 0){
	/*
	 * oops. this code mistakes salt for IV.
	 * We deprecated the arguments for this method, but we decided
	 * keeping this behaviour for backward compatibility.
	 */
	VALUE cname  = rb_class_path(rb_obj_class(self));
	rb_warn("arguments for %"PRIsVALUE"#encrypt and %"PRIsVALUE"#decrypt were deprecated; "
                "use %"PRIsVALUE"#pkcs5_keyivgen to derive key and IV",
                cname, cname, cname);
	StringValue(pass);
	GetCipher(self, ctx);
	if (NIL_P(init_v)) memcpy(iv, "OpenSSL for Ruby rulez!", sizeof(iv));
	else{
	    StringValue(init_v);
	    if (EVP_MAX_IV_LENGTH > RSTRING_LEN(init_v)) {
		memset(iv, 0, EVP_MAX_IV_LENGTH);
		memcpy(iv, RSTRING_PTR(init_v), RSTRING_LEN(init_v));
	    }
	    else memcpy(iv, RSTRING_PTR(init_v), sizeof(iv));
	}
	EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
		       (unsigned char *)RSTRING_PTR(pass), RSTRING_LENINT(pass), 1, key, NULL);
	p_key = key;
	p_iv = iv;
    }
    else {
	GetCipher(self, ctx);
    }
    if (EVP_CipherInit_ex(ctx, NULL, NULL, p_key, p_iv, mode) != 1) {
	ossl_raise(eCipherError, NULL);
    }

    return self;
}
예제 #5
0
파일: digest.c 프로젝트: DashYang/sim
/* :nodoc: */
static VALUE
rb_digest_base_finish(VALUE self)
{
    rb_digest_metadata_t *algo;
    void *pctx;
    VALUE str;

    algo = get_digest_base_metadata(rb_obj_class(self));

    TypedData_Get_Struct(self, void, &digest_type, pctx);

    str = rb_str_new(0, algo->digest_len);
    algo->finish_func(pctx, (unsigned char *)RSTRING_PTR(str));

    /* avoid potential coredump caused by use of a finished context */
    algo_init(algo, pctx);

    return str;
}
예제 #6
0
static VALUE
rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self)
{
    VALUE klass = rb_obj_class(self);
    long i, n;

    rb_struct_modify(self);
    n = num_members(klass);
    if (n < argc) {
        rb_raise(rb_eArgError, "struct size differs");
    }
    for (i=0; i<argc; i++) {
        RSTRUCT_SET(self, i, argv[i]);
    }
    if (n > argc) {
        rb_mem_clear((VALUE *)RSTRUCT_CONST_PTR(self)+argc, n-argc);
    }
    return Qnil;
}
예제 #7
0
파일: context.c 프로젝트: guilherme/byebug
extern VALUE
context_create(VALUE thread)
{
  debug_context_t *context = ALLOC(debug_context_t);

  context->flags = 0;
  context->thnum = ++thnum_max;
  context->thread = thread;
  reset_stepping_stop_points(context);
  context->stop_reason = CTX_STOP_NONE;

  rb_debug_inspector_open(context_backtrace_set, (void *)context);
  context->calced_stack_size = dc_stack_size(context) + 1;

  if (rb_obj_class(thread) == cDebugThread)
    CTX_FL_SET(context, CTX_FL_IGNORE);

  return Data_Wrap_Struct(cContext, context_mark, 0, context);
}
예제 #8
0
파일: rbgtk.c 프로젝트: benolee/ruby-gnome2
void
rbgtk_initialize_gtkobject(VALUE obj, GtkObject *gtkobj)
{
    gtkobj = g_object_ref(gtkobj);
    gtk_object_sink(gtkobj);
    G_INITIALIZE(obj, gtkobj);

    if (GTK_IS_WINDOW(gtkobj) || GTK_IS_MENU_SHELL(gtkobj)) {
        VALUE klass;
        klass = rb_obj_class(obj);
        if (rb_ivar_defined(klass, id__windows__) == Qfalse) {
            rb_ivar_set(klass, id__windows__, rb_hash_new());
        }
        rb_hash_aset(rb_ivar_get(klass, id__windows__), obj, Qnil);
        g_signal_connect_after(gtkobj, "destroy",
                               G_CALLBACK(remove_from_windows),
                               (gpointer)obj);
    }
}
예제 #9
0
파일: error.c 프로젝트: 1nueve/MacRuby
static VALUE
syserr_initialize(VALUE self, SEL sel, int argc, VALUE *argv)
{
#if !defined(_WIN32) && !defined(__VMS)
    char *strerror();
#endif
    const char *err;
    VALUE mesg, error;
    VALUE klass = rb_obj_class(self);

    if (klass == rb_eSystemCallError) {
	rb_scan_args(argc, argv, "11", &mesg, &error);
	if (argc == 1 && FIXNUM_P(mesg)) {
	    error = mesg; mesg = Qnil;
	}
	if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &klass)) {
	    /* change class */
	    if (TYPE(self) != T_OBJECT) { /* insurance to avoid type crash */
		rb_raise(rb_eTypeError, "invalid instance type");
	    }
	    RBASIC(self)->klass = klass;
	}
    }
    else {
	rb_scan_args(argc, argv, "01", &mesg);
	error = rb_const_get(klass, rb_intern("Errno"));
    }
    if (!NIL_P(error)) err = strerror(NUM2INT(error));
    else err = "unknown error";
    if (!NIL_P(mesg)) {
	VALUE str = mesg;

	StringValue(str);
	mesg = rb_sprintf("%s - %.*s", err,
			  (int)RSTRING_LEN(str), RSTRING_PTR(str));
    }
    else {
	mesg = rb_str_new2(err);
    }
    rb_vm_call_super(self, selInitialize2, 1, &mesg);
    rb_iv_set(self, "errno", error);
    return self;
}
static VALUE
ossl_cipher_init(int argc, VALUE *argv, VALUE self, int mode)
{
    EVP_CIPHER_CTX *ctx;
    unsigned char key[EVP_MAX_KEY_LENGTH], *p_key = NULL;
    unsigned char iv[EVP_MAX_IV_LENGTH], *p_iv = NULL;
    VALUE pass, init_v;

    if(rb_scan_args(argc, argv, "02", &pass, &init_v) > 0){
	/*
	 * oops. this code mistakes salt for IV.
	 * We deprecated the arguments for this method, but we decided
	 * keeping this behaviour for backward compatibility.
	 */
	StringValue(pass);
	GetCipher(self, ctx);
	if (NIL_P(init_v)) memcpy(iv, "OpenSSL for Ruby rulez!", sizeof(iv));
	else{
	    char *cname  = rb_class2name(rb_obj_class(self));
	    rb_warning("key derivation by %s#encrypt is deprecated; "
		       "use %s::pkcs5_keyivgen instead", cname, cname);
	    StringValue(init_v);
	    if (EVP_MAX_IV_LENGTH > RSTRING(init_v)->len) {
		memset(iv, 0, EVP_MAX_IV_LENGTH);
		memcpy(iv, RSTRING(init_v)->ptr, RSTRING(init_v)->len);
	    }
	    else memcpy(iv, RSTRING(init_v)->ptr, sizeof(iv));
	}
	EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
		       RSTRING(pass)->ptr, RSTRING(pass)->len, 1, key, NULL);
	p_key = key;
	p_iv = iv;
    }
    else {
	GetCipher(self, ctx);
    }
    if (EVP_CipherInit_ex(ctx, NULL, NULL, p_key, p_iv, mode) != 1) {
	ossl_raise(eCipherError, NULL);
    }

    return self;
}
예제 #11
0
파일: struct.c 프로젝트: AdamDotCom/my-rvm
static VALUE
inspect_struct(VALUE s, VALUE dummy, int recur)
{
    VALUE cname = rb_class_name(rb_obj_class(s));
    VALUE members, str = rb_str_new2("#<struct ");
    long i;
    char first = RSTRING_PTR(cname)[0];

    if (recur || first != '#') {
	rb_str_append(str, cname);
    }
    if (recur) {
	return rb_str_cat2(str, ":...>");
    }

    members = rb_struct_members(s);
    for (i=0; i<RSTRUCT_LEN(s); i++) {
	VALUE slot;
	ID id;

	if (i > 0) {
	    rb_str_cat2(str, ", ");
	}
	else if (first != '#') {
	    rb_str_cat2(str, " ");
	}
	slot = RARRAY_PTR(members)[i];
	id = SYM2ID(slot);
	if (rb_is_local_id(id) || rb_is_const_id(id)) {
	    rb_str_append(str, rb_id2str(id));
	}
	else {
	    rb_str_append(str, rb_inspect(slot));
	}
	rb_str_cat2(str, "=");
	rb_str_append(str, rb_inspect(RSTRUCT_PTR(s)[i]));
    }
    rb_str_cat2(str, ">");
    OBJ_INFECT(str, s);

    return str;
}
예제 #12
0
파일: vm_backtrace.c 프로젝트: sho-h/ruby
VALUE
rb_profile_frame_classpath(VALUE frame)
{
    VALUE klass = rb_iseq_klass(frame2iseq(frame));

    if (klass && !NIL_P(klass)) {
	if (RB_TYPE_P(klass, T_ICLASS)) {
	    klass = RBASIC(klass)->klass;
	}
	else if (FL_TEST(klass, FL_SINGLETON)) {
	    klass = rb_ivar_get(klass, id__attached__);
	    if (!RB_TYPE_P(klass, T_CLASS))
		return rb_sprintf("#<%s:%p>", rb_class2name(rb_obj_class(klass)), (void*)klass);
	}
	return rb_class_path(klass);
    }
    else {
	return Qnil;
    }
}
예제 #13
0
/* :nodoc: */
static VALUE ra_buffer_init_copy(VALUE copy, VALUE buf) {
    if (copy == buf) return copy;

    // Checks
    rb_check_frozen(copy);
    if (!rb_obj_is_instance_of(buf, rb_obj_class(copy))) {
        rb_raise(rb_eTypeError, "wrong argument class");
    }

    RA_BUFFER *copy_struct, *buf_struct;
    Data_Get_Struct(copy, RA_BUFFER, copy_struct);
    Data_Get_Struct(buf, RA_BUFFER, buf_struct);

    // Clone data
    memcpy(copy_struct, buf_struct, sizeof(RA_BUFFER));
    long size = ra_buffer_alloc_data(copy_struct);
    memcpy(copy_struct->data, buf_struct->data, size);

    return copy;
}
예제 #14
0
static VALUE
rb_struct_initialize(VALUE self, VALUE values)
{
    VALUE klass = rb_obj_class(self);
    VALUE size;
    long n;

    rb_struct_modify(self);
    size = rb_struct_iv_get(klass, "__size__");
    n = FIX2LONG(size);
    if (n < RARRAY(values)->len) {
	rb_raise(rb_eArgError, "struct size differs");
    }
    MEMCPY(RSTRUCT(self)->ptr, RARRAY(values)->ptr, VALUE, RARRAY(values)->len);
    if (n > RARRAY(values)->len) {
	rb_mem_clear(RSTRUCT(self)->ptr+RARRAY(values)->len,
		     n-RARRAY(values)->len);
    }
    return Qnil;
}
예제 #15
0
static VALUE
inspect_struct(VALUE s, VALUE dummy, int recur)
{
    const char *cname = rb_class2name(rb_obj_class(s));
    VALUE str, members;
    long i;

    if (recur) {
	return rb_sprintf("#<struct %s:...>", cname);
    }

    members = rb_struct_members(s);
    if (cname[0] == '#') {
	str = rb_str_new2("#<struct ");
    }
    else {
	str = rb_sprintf("#<struct %s ", cname);
    }
    for (i=0; i<RSTRUCT_LEN(s); i++) {
	VALUE slot;
	ID id;

	if (i > 0) {
	    rb_str_cat2(str, ", ");
	}
	slot = RARRAY_AT(members, i);
	id = SYM2ID(slot);
	if (rb_is_local_id(id) || rb_is_const_id(id)) {
	    rb_str_buf_append(str, rb_id2str(id));
	}
	else {
	    rb_str_buf_append(str, rb_inspect(slot));
	}
	rb_str_cat2(str, "=");
	rb_str_buf_append(str, rb_inspect(RSTRUCT_PTR(s)[i]));
    }
    rb_str_cat2(str, ">");
    OBJ_INFECT(str, s);

    return str;
}
예제 #16
0
/*
 * call-seq:
 *   Groonga::Context.new(options=nil)
 *
 * コンテキストを作成する。_options_に指定可能な値は以下の通
 * り。
 *
 * [+:encoding+]
 *   エンコーディングを指定する。エンコーディングの指定方法
 *   はGroonga::Encodingを参照。
 */
static VALUE
rb_grn_context_initialize (int argc, VALUE *argv, VALUE self)
{
    RbGrnContext *rb_grn_context;
    grn_ctx *context;
    int flags = 0;
    VALUE options, default_options;
    VALUE rb_encoding;

    rb_scan_args(argc, argv, "01", &options);
    default_options = rb_grn_context_s_get_default_options(rb_obj_class(self));
    if (NIL_P(default_options))
	default_options = rb_hash_new();

    if (NIL_P(options))
	options = rb_hash_new();
    options = rb_funcall(default_options, rb_intern("merge"), 1, options);

    rb_grn_scan_options(options,
			"encoding", &rb_encoding,
			NULL);

    rb_grn_context = ALLOC(RbGrnContext);
    DATA_PTR(self) = rb_grn_context;
    rb_grn_context->self = self;
    context = rb_grn_context->context = grn_ctx_open(flags);
    rb_grn_context_check(context, self);

    GRN_CTX_USER_DATA(context)->ptr = rb_grn_context;

    if (!NIL_P(rb_encoding)) {
	grn_encoding encoding;

	encoding = RVAL2GRNENCODING(rb_encoding, NULL);
	GRN_CTX_SET_ENCODING(context, encoding);
    }

    debug("context new: %p\n", context);

    return Qnil;
}
예제 #17
0
파일: context.c 프로젝트: jankun/debase
extern VALUE
context_create(VALUE thread, VALUE cDebugThread) {
  debug_context_t *context;
  VALUE locations;

  context = ALLOC(debug_context_t);
  context->stack_size = 0;
  locations = rb_funcall(thread, rb_intern("backtrace_locations"), 1, INT2FIX(1));
  context->calced_stack_size = locations != Qnil ? (int)RARRAY_LEN(locations) : 0;
  context->stack = NULL;
  context->thnum = ++thnum_current;
  context->thread = thread;
  context->flags = 0;
  context->last_file = NULL;
  context->last_line = -1;
  context->stop_frame = -1;
  context->thread_pause = 0;
  reset_stepping_stop_points(context);
  if(rb_obj_class(thread) == cDebugThread) CTX_FL_SET(context, CTX_FL_IGNORE);
  return Data_Wrap_Struct(cContext, Context_mark, Context_free, context);
}
예제 #18
0
VALUE cbasic_get_args(VALUE self) {
    basic_struct *this, *iterator_basic;
    CVecBasic *args = vecbasic_new();
    int size = 0;

    Data_Get_Struct(self, basic_struct, this);
    basic_get_args(this, args);

    size = vecbasic_size(args);
    VALUE ruby_array = rb_ary_new2(size);
    int i = 0;
    VALUE temp = NULL;
    for(i = 0; i < size; i++) {
        basic_struct *temp_basic = basic_new_heap();
        vecbasic_get(args, i, temp_basic);
        temp = Data_Wrap_Struct(rb_obj_class(self), NULL , cbasic_free_heap, temp_basic);
        rb_ary_push(ruby_array, temp);
    }
    vecbasic_free(args);
    return ruby_array;
}
예제 #19
0
VALUE cbasic_free_symbols(VALUE self) {
    basic_struct *this, *iterator_basic;
    CSetBasic *symbols = setbasic_new();
    int size = 0;

    Data_Get_Struct(self, basic_struct, this);
    basic_free_symbols(this, symbols);

    size = setbasic_size(symbols);
    VALUE ruby_array = rb_ary_new2(size);
    int i = 0;
    VALUE temp = NULL;
    for(i = 0; i < size; i++) {
        basic_struct *temp_basic = basic_new_heap();
        setbasic_get(symbols, i, temp_basic);
        temp = Data_Wrap_Struct(rb_obj_class(self), NULL , cbasic_free_heap, temp_basic);
        rb_ary_push(ruby_array, temp);
    }
    setbasic_free(symbols);
    return ruby_array;
}
예제 #20
0
파일: app.c 프로젝트: gensym/shoes
shoes_code
shoes_app_open(shoes_app *app, char *path)
{
  shoes_code code = SHOES_OK;
  int dialog = (rb_obj_class(app->self) == cDialog);

  code = shoes_native_app_open(app, path, dialog);
  if (code != SHOES_OK)
    return code;

  shoes_app_title(app, app->title);
  if (app->slot != NULL) shoes_native_slot_reset(app->slot);
  shoes_slot_init(app->canvas, app->slot, 0, 0, app->width, app->height, TRUE, TRUE);
  code = shoes_app_goto(app, path);
  if (code != SHOES_OK)
    return code;

  if (!app->hidden)
    shoes_native_app_show(app);

  return code;
}
예제 #21
0
/* :nodoc: */
static VALUE
rb_digest_base_finish(VALUE self)
{
    rb_digest_metadata_t *algo;
    void *pctx;
    VALUE str;

    algo = get_digest_base_metadata(rb_obj_class(self));

    Data_Get_Struct(self, void, pctx);

    char* buf = calloc(algo->digest_len, sizeof(char));

    algo->finish_func(pctx, (unsigned char*)buf);

    /* avoid potential coredump caused by use of a finished context */
    algo->init_func(pctx);

    str = rb_str_new(buf, algo->digest_len);
    free(buf);

    return str;
}
예제 #22
0
VALUE
rb_struct_initialize(VALUE self, SEL sel, VALUE values)
{
    VALUE klass = rb_obj_class(self);
    VALUE size;
    long n;

    rb_struct_modify(self);
    size = rb_struct_iv_get(klass, "__size__");
    n = FIX2LONG(size);
    if (n < RARRAY_LEN(values)) {
	rb_raise(rb_eArgError, "struct size differs");
    }
    for (int i = 0; i < RARRAY_LEN(values); i++) {
	GC_WB(&RSTRUCT_PTR(self)[i], RARRAY_AT(values, i));
    }
    if (n > RARRAY_LEN(values)) {
	for (int i = RARRAY_LEN(values); i < n; i++) {
	    RSTRUCT_PTR(self)[i] = Qnil;
	}
    }
    return Qnil;
}
예제 #23
0
파일: do_oracle.c 프로젝트: datamapper/do
static VALUE typecast_bind_value(VALUE connection, VALUE r_value) {
  VALUE r_class = rb_obj_class(r_value);
  VALUE oci8_conn = rb_iv_get(connection, "@connection");
  // replace nil value with '' as otherwise OCI8 cannot get bind variable type
  // '' will be inserted as NULL by Oracle
  if (NIL_P(r_value))
    return RUBY_STRING("");
  else if (r_class == rb_cString)
    // if string is longer than 4000 characters then convert to CLOB
    return RSTRING_LEN(r_value) <= 4000 ? r_value : rb_funcall(cOCI8_CLOB, DO_ID_NEW, 2, oci8_conn, r_value);
  else if (r_class == rb_cBigDecimal)
    return rb_funcall(r_value, DO_ID_TO_S, 1, RUBY_STRING("F"));
  else if (r_class == rb_cTrueClass)
    return INT2NUM(1);
  else if (r_class == rb_cFalseClass)
    return INT2NUM(0);
  else if (r_class == rb_cByteArray)
    return rb_funcall(cOCI8_BLOB, DO_ID_NEW, 2, oci8_conn, r_value);
  else if (r_class == rb_cClass)
    return rb_funcall(r_value, DO_ID_TO_S, 0);
  else
    return r_value;
}
예제 #24
0
void set_callback(VALUE* handler, const char* handler_name)
{
  if (!rb_block_given_p()) {
    rb_raise(rb_eArgError, "%s: no block", handler_name);
  }
  
  if (Qnil != *handler) {
    rb_raise(rb_eArgError, "%s should only be assigned once", handler_name);
  }
  
  *handler = rb_block_proc();
  /*
  * If you create a Ruby object from C and store it in a C global variable without 
  * exporting it to Ruby, you must at least tell the garbage collector about it, 
  * lest ye be reaped inadvertently:
  */
  rb_global_variable(handler);
  
  if (rb_obj_class(*handler) != rb_cProc) {
    rb_raise(rb_eTypeError, "%s got unexpected value: %s", handler_name, 
       RSTRING_PTR(inspect_rb_obj(*handler)));
  }
}
예제 #25
0
static VALUE
rg_initialize_copy(VALUE self, VALUE orig)
{
    boxed_holder* holder1;
    boxed_holder* holder2;

    if (self == orig) return self;

    if (!rb_obj_is_instance_of(orig, rb_obj_class(self))) {
        rb_raise(rb_eTypeError, "wrong argument class");
    }

    Data_Get_Struct(self, boxed_holder, holder1);
    Data_Get_Struct(orig, boxed_holder, holder2);

    holder1->boxed = g_boxed_copy(holder2->type, holder2->boxed);
    holder1->own   = TRUE;

    if (!holder1->boxed)
      rb_raise(rb_eRuntimeError, "g_boxed_copy() failed");

    return self;
}
예제 #26
0
/*
 * Return a pathname with +repl+ added as a suffix to the basename.
 *
 * If self has no extension part, +repl+ is appended.
 *
 *	Pathname.new('/usr/bin/shutdown').sub_ext('.rb')
 *	    #=> #<Pathname:/usr/bin/shutdown.rb>
 */
static VALUE
path_sub_ext(VALUE self, VALUE repl)
{
    VALUE str = get_strpath(self);
    VALUE str2;
    long extlen;
    const char *ext;
    const char *p;

    StringValue(repl);
    p = RSTRING_PTR(str);
    extlen = RSTRING_LEN(str);
    ext = ruby_enc_find_extname(p, &extlen, rb_enc_get(str));
    if (ext == NULL) {
        ext = p + RSTRING_LEN(str);
    }
    else if (extlen <= 1) {
        ext += extlen;
    }
    str2 = rb_str_subseq(str, 0, ext-p);
    rb_str_append(str2, repl);
    OBJ_INFECT(str2, str);
    return rb_class_new_instance(1, &str2, rb_obj_class(self));
}
예제 #27
0
/*
 * call-seq:
 *  dup
 *
 * Copy this Document.  An optional depth may be passed in, but it defaults
 * to a deep copy.  0 is a shallow copy, 1 is a deep copy.
 */
static VALUE duplicate_document(int argc, VALUE *argv, VALUE self)
{
  xmlDocPtr doc, dup;
  VALUE copy;
  VALUE level;
  VALUE error_list      = rb_ary_new();

  if(rb_scan_args(argc, argv, "01", &level) == 0)
    level = INT2NUM((long)1);

  Data_Get_Struct(self, xmlDoc, doc);

  xmlResetLastError();
  xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
  dup = xmlCopyDoc(doc, (int)NUM2INT(level));
  xmlSetStructuredErrorFunc(NULL, NULL);

  if(dup == NULL) return Qnil;

  dup->type = doc->type;
  copy = Nokogiri_wrap_xml_document(rb_obj_class(self), dup);
  rb_iv_set(copy, "@errors", error_list);
  return copy ;
}
예제 #28
0
파일: evilr.c 프로젝트: jeremyevans/evilr
/* call-seq:
 *   extend_between(mod){|p, c| } -> mod || nil
 *
 * Walks the receiver's singleton class's super chain until it reaches the receiver's
 * class, yielding the previous and current entries in the super chain at every step.
 * The first time the block returns +true+, +mod+ is inserted into the super chain
 * between the two values and the method returns immediately.  Raises +TypeError+ if
 * +mod+ is not a Module or if the receiver is an immediate.
 * If the block ever returns +true+, the return value is
 * +mod+.  If the block never returns +true+, the return value is +nil+.  On the first block call,
 * the first block argument is the receiver's singleton class, and on the last block call,
 * the last block argument is the receiver's class. */
static VALUE evilr_extend_between(VALUE self, VALUE mod) {
  VALUE sc, iclass, klass, prev, cur;

  evilr__check_immediates(self, mod);
  evilr__check_type(T_MODULE, mod);

  sc = rb_singleton_class(self);
  klass = rb_obj_class(self);
  rb_extend_object(self, mod);
  iclass = evilr__iclass_matching_before(sc, mod, klass);
  if (iclass == NULL) {
    rb_raise(rb_eArgError, "module already included in object's class");
  }
  evilr_unextend(self, mod);

  for (prev = sc, cur = RCLASS_SUPER(sc); prev && prev != klass; prev = cur, cur = cur ? RCLASS_SUPER(cur) : cur) {
    if (rb_yield_values(2, INCLUDE_BETWEEN_VAL(prev), INCLUDE_BETWEEN_VAL(cur)) == Qtrue) {
      RCLASS_SET_SUPER(prev, iclass);
      RCLASS_SET_SUPER(iclass, cur);
      return mod;
    }
  }
  return Qnil;
}
예제 #29
0
static VALUE
inspect_struct(VALUE s)
{
    const char *cname = rb_class2name(rb_obj_class(s));
    VALUE str, members;
    long i;

    members = rb_struct_members(s);
    str = rb_str_buf_new2("#<struct ");
    rb_str_cat2(str, cname);
    rb_str_cat2(str, " ");
    for (i=0; i<RSTRUCT(s)->len; i++) {
	VALUE slot;
	ID id;
	const char *p;

	if (i > 0) {
	    rb_str_cat2(str, ", ");
	}
	slot = RARRAY(members)->ptr[i];
	id = SYM2ID(slot);
	if (rb_is_local_id(id) || rb_is_const_id(id)) {
	    p = rb_id2name(id);
	    rb_str_cat2(str, p);
	}
	else {
	    rb_str_append(str, rb_inspect(slot));
	}
	rb_str_cat2(str, "=");
	rb_str_append(str, rb_inspect(RSTRUCT(s)->ptr[i]));
    }
    rb_str_cat2(str, ">");
    OBJ_INFECT(str, s);

    return str;
}
예제 #30
0
VALUE Constant_Traverser_root(VALUE self ){VALUE vals[0]; VALUE it ,_autovar=Qnil,_autovar_2=Qnil,_autovar_3=Qnil,_autovar_4=Qnil,_autovar_5=Qnil,_it=Qnil,_autovar_6=Qnil,__result=Qnil,_autovar_7=Qnil;VALUE bind2=bind_new2(2);   cstruct *ptr; Data_Get_Struct(self,cstruct,ptr);bind_aset(bind2,1,_autovar);bind_aset(bind2,2,_autovar_2);it=rb_funcall(self,sy_Constant_Traverser__at__contex_5f56,1,bind2);_autovar=bind_aget(bind2,1);;_autovar_2=bind_aget(bind2,2);;it=rb_funcall(self,sy_Constant_Traverser_src_dot_dup_d768,1,bind2); _autovar_3=it;;
cstruct oldpass1=*ptr; ptr->pos=ptr->len=0; ptr->ary=NULL;
                   ptr->src=failobj;ptr->ary=alloca(sizeof(VALUE));ptr->ary[0]=_autovar_3;ptr->len=1;
                   switch(FIX2LONG(rb_hash_aref(switchhash_Constant_Traverser_1,rb_obj_class(ame_curobj2(ptr))))){case 0/*Rule*/:; it=ptr->ary[ptr->pos];            ;ptr->pos++; _autovar_4=it;;
 break;
case 1/*Object*/:; if (1){it=failobj;goto pass1;} break;
}
                   goto success1;
                   pass1: *ptr=oldpass1;if (1){it=failobj;goto fail;}
                   success1: *ptr=oldpass1;
                it=_autovar_4; _autovar_5=it;;
cstruct oldpass2=*ptr; ptr->pos=ptr->len=0; ptr->ary=NULL;
                   ame_setsrc2(self,_autovar_5);
                   bind_aset(bind2,1,_autovar_2);it=rb_funcall(self,sy_Constant_Traverser_bind_lb_1_rb__lb__cf87,1,bind2);_autovar_2=bind_aget(bind2,1);;it=rb_funcall(self,sy_Constant_Traverser_src_dot_cfg_eq__15aa,1,bind2);bind_aset(bind2,1,_autovar_2);it=rb_funcall(self,sy_Constant_Traverser__at__contex_d6d5,1,bind2);_autovar_2=bind_aget(bind2,1);;  it=Constant_Traverser_traverse(self ); if (it==failobj){it=failobj;goto pass2;} _it=it;;
bind_aset(bind2,1,_it);it=rb_funcall(self,sy_Constant_Traverser_bind_lb_1_rb__dot__f702,1,bind2);_it=bind_aget(bind2,1);; _autovar_6=it;;

                   goto success2;
                   pass2: *ptr=oldpass2;if (1){it=failobj;goto fail;}
                   success2: *ptr=oldpass2;
                it=_autovar_6; __result=it;;
 _autovar_7=it;;
bind_aset(bind2,1,_autovar);bind_aset(bind2,2,_autovar_7);it=rb_funcall(self,sy_Constant_Traverser__at__contex_cdb3,1,bind2);_autovar=bind_aget(bind2,1);;_autovar_7=bind_aget(bind2,2);;
fail: return it;
}