Exemplo n.º 1
0
static VALUE
range_each(VALUE range, SEL sel)
{
    VALUE beg, end;

    RETURN_ENUMERATOR(range, 0, 0);

    beg = RANGE_BEG(range);
    end = RANGE_END(range);

    if (FIXNUM_P(beg) && FIXNUM_P(end)) { /* fixnums are special */
	long lim = FIX2LONG(end);
	long i;

	if (!EXCL(range))
	    lim += 1;
	for (i = FIX2LONG(beg); i < lim; i++) {
	    rb_yield(LONG2FIX(i));
	    RETURN_IF_BROKEN();
	}
    }
    else if (SYMBOL_P(beg) && SYMBOL_P(end)) { /* symbols are special */
	VALUE args[2];

	args[0] = rb_sym_to_s(end);
	args[1] = EXCL(range) ? Qtrue : Qfalse;
	rb_objc_block_call(rb_sym_to_s(beg), selUpto, 2, args, sym_each_i, 0);
    }
    else {
	VALUE tmp = rb_check_string_type(beg);

	if (!NIL_P(tmp)) {
	    VALUE args[2];

	    args[0] = end;
	    args[1] = EXCL(range) ? Qtrue : Qfalse;
	    rb_objc_block_call(beg, selUpto, 2, args, rb_yield, 0);
	}
	else {
	    if (!discrete_object_p(beg)) {
		rb_raise(rb_eTypeError, "can't iterate from %s",
			 rb_obj_classname(beg));
	    }
	    range_each_func(range, each_i, NULL);
	}
    }
    return range;
}
Exemplo n.º 2
0
static VALUE
nst_method_missing(int argc, VALUE *argv, VALUE self)
{
    VALUE s, tag, obj;

    if (argc == 2) {
        s = rb_sym_to_s(argv[0]);
        if (RSTRING_PTR(s)[RSTRING_LEN(s)-1] == '=') {
            tag = rb_str_intern(rb_str_new(RSTRING_PTR(s), RSTRING_LEN(s)-1));
            obj = nst_field(self, tag);
            if (RTEST(obj)) {
                rb_funcall(obj, rb_intern("store"), 1, argv[1]);
                return argv[1];
            }
        }
        return rb_call_super(argc,argv);
    }
    if (argc == 1) {
        obj = nst_field(self,argv[0]);
        if (RTEST(obj)) {
            return obj;
        }
    }
    return rb_call_super(argc,argv);
}
Exemplo n.º 3
0
Arquivo: cap2.c Projeto: lmars/cap2
/*
 * Converts a Ruby symbol into a cap_value_t capability value.
 */
cap_value_t cap2_sym_to_cap(VALUE cap) {
    Check_Type(cap, T_SYMBOL);

    cap = rb_sym_to_s(cap);

    return cap2_cap_value(StringValueCStr(cap));
}
Exemplo n.º 4
0
static VALUE header_hash_tidy_key(VALUE key) {
  if (TYPE(key) == T_SYMBOL) {
    key = rb_sym_to_s(key);
  } else {
    Check_Type(key, T_STRING);
    key = rb_enc_str_new(RSTRING_PTR(key), RSTRING_LEN(key), u8_encoding);
  }
  nyara_headerlize(key);
  return key;
}
Exemplo n.º 5
0
static VALUE
location_base_label(rb_backtrace_location_t *loc)
{
    switch (loc->type) {
      case LOCATION_TYPE_ISEQ:
      case LOCATION_TYPE_ISEQ_CALCED:
	return loc->body.iseq.iseq->location.base_label;
      case LOCATION_TYPE_CFUNC:
	return rb_sym_to_s(ID2SYM(loc->body.cfunc.mid));
      case LOCATION_TYPE_IFUNC:
      default:
	rb_bug("location_base_label: unreachable");
	UNREACHABLE;
    }
}
Exemplo n.º 6
0
static int hash_to_zval(VALUE key, VALUE value, VALUE zval_array) {
    zval *v;
    zval *ary;

    if (key == Qundef) {
        return ST_CONTINUE;
    }

    ary = (zval *)zval_array;
    v = value_to_zval(value);

    if (key == Qtrue) {
        zend_hash_index_update(Z_ARRVAL_P(ary), 1, &v, sizeof(zval *), NULL);
        return ST_CONTINUE;
    }

    if (key == Qfalse || key == Qnil) {
        zend_hash_index_update(Z_ARRVAL_P(ary), 0, &v, sizeof(zval *), NULL);
        return ST_CONTINUE;
    }

    if (TYPE(key) == T_FIXNUM) {
        int idx = FIX2INT(key);
        zend_hash_index_update(Z_ARRVAL_P(ary), idx, &v, sizeof(zval *), NULL);
        return ST_CONTINUE;
    }

    switch (TYPE(key)) {
        case T_BIGNUM:
            key = rb_big2str(key, 10);
            break;
        case T_SYMBOL:
            key = rb_sym_to_s(key);
            break;
        case T_STRING:
            key = rb_string_value(&key);
            break;
        default:
            rb_raise(rb_eRuntimeError, "invalid key (%d)", TYPE(key));
    }

    //ZEND_HANDLE_NUMERIC(RSTRING_PTR(key), RSTRING_LEN(key), zend_hash_index_update(Z_ARRVAL_P(ary), idx, &v, sizeof(zval *), NULL));
    zend_symtable_update(Z_ARRVAL_P(ary), RSTRING_PTR(key), RSTRING_LEN(key) + 1, &v, sizeof(zval *), NULL);

    return ST_CONTINUE;
}
Exemplo n.º 7
0
/*
 *  call-seq:
 *     WIN32OLE_EVENT#off_event([event])
 *
 *  removes the callback of event.
 *
 *    ie = WIN32OLE.new('InternetExplorer.Application')
 *    ev = WIN32OLE_EVENT.new(ie)
 *    ev.on_event('BeforeNavigate2') {|*args|
 *      args.last[6] = true
 *    }
 *      ...
 *    ev.off_event('BeforeNavigate2')
 *      ...
 */
static VALUE
fev_off_event(int argc, VALUE *argv, VALUE self)
{
    VALUE event = Qnil;
    VALUE events;

    rb_scan_args(argc, argv, "01", &event);
    if(!NIL_P(event)) {
        if(!RB_TYPE_P(event, T_STRING) && !RB_TYPE_P(event, T_SYMBOL)) {
            rb_raise(rb_eTypeError, "wrong argument type (expected String or Symbol)");
        }
        if (RB_TYPE_P(event, T_SYMBOL)) {
            event = rb_sym_to_s(event);
        }
    }
    events = rb_ivar_get(self, id_events);
    if (NIL_P(events)) {
        return Qnil;
    }
    ole_delete_event(events, event);
    return Qnil;
}
Exemplo n.º 8
0
static VALUE
ev_on_event(int argc, VALUE *argv, VALUE self, VALUE is_ary_arg)
{
    struct oleeventdata *poleev;
    VALUE event, args, data;
    Data_Get_Struct(self, struct oleeventdata, poleev);
    if (poleev->pConnectionPoint == NULL) {
        rb_raise(eWIN32OLERuntimeError, "IConnectionPoint not found. You must call advise at first.");
    }
    rb_scan_args(argc, argv, "01*", &event, &args);
    if(!NIL_P(event)) {
        if(!RB_TYPE_P(event, T_STRING) && !RB_TYPE_P(event, T_SYMBOL)) {
            rb_raise(rb_eTypeError, "wrong argument type (expected String or Symbol)");
        }
        if (RB_TYPE_P(event, T_SYMBOL)) {
            event = rb_sym_to_s(event);
        }
    }
    data = rb_ary_new3(4, rb_block_proc(), event, args, is_ary_arg);
    add_event_call_back(self, event, data);
    return Qnil;
}
Exemplo n.º 9
0
static VALUE
//iter_struct_inspect(na_loop_t *const lp)
iter_struct_inspect(char *ptr, size_t pos, VALUE opt)
{
    VALUE   types, defs, def, name, elmt, vary, v, x;
    size_t  ofs;
    long    i, len;
    narray_view_t *ne;

    types = RARRAY_AREF(opt,0);
    defs = RARRAY_AREF(opt,1);

    len = RARRAY_LEN(types);
    vary = rb_ary_new2(len);

    for (i=0; i<len; i++) {
        def  = RARRAY_AREF(defs,i);
        name = RARRAY_AREF(def,0);
        ofs  = NUM2SIZET(RARRAY_AREF(def,2));
        elmt = RARRAY_AREF(types,i);
        GetNArrayView(elmt,ne);
        ne->offset = pos + ofs;
        v = rb_str_concat(rb_sym_to_s(name), rb_str_new2(": "));
        x = rb_funcall(elmt, rb_intern("format_to_a"), 0);        // <-- fix me
        if (ne->base.ndim==0) {
            x = rb_funcall(x, rb_intern("first"), 0);
        }
        x = rb_funcall(x, rb_intern("to_s"), 0);
        v = rb_str_concat(v, x);
        rb_ary_push(vary, v);
    }
    v = rb_ary_join(vary, rb_str_new2(", "));
    v = rb_str_concat(rb_str_new2("["), v);
    v = rb_str_concat(v, rb_str_new2("]"));
    return v;
}
Exemplo n.º 10
0
static VALUE param_hash_aset(VALUE self, VALUE key, VALUE value) {
  if (TYPE(key) == T_SYMBOL) {
    key = rb_sym_to_s(key);
  }
  return rb_hash_aset(self, key, value);
}
Exemplo n.º 11
0
static VALUE
rsym_match(VALUE sym, SEL sel, VALUE other)
{
    return rb_str_match(rb_sym_to_s(sym), other);
}
Exemplo n.º 12
0
static VALUE
rsym_succ(VALUE sym, SEL sel)
{
    return rb_str_intern(rb_str_succ(rb_sym_to_s(sym)));
}
Exemplo n.º 13
0
static VALUE
rsym_to_s(VALUE sym, SEL sel)
{
    return rb_sym_to_s(sym);
}
Exemplo n.º 14
0
static VALUE
range_step(VALUE range, SEL sel, int argc, VALUE *argv)
{
    VALUE b, e, step, tmp;

    RETURN_ENUMERATOR(range, argc, argv);

    b = RANGE_BEG(range);
    e = RANGE_END(range);
    if (argc == 0) {
	step = INT2FIX(1);
    }
    else {
	rb_scan_args(argc, argv, "01", &step);
	if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
	    step = rb_to_int(step);
	}
	VALUE zero = INT2FIX(0);
	if (rb_vm_call(step, selLT, 1, &zero)) {
	    rb_raise(rb_eArgError, "step can't be negative");
	}
	else if (!rb_vm_call(step, selGT, 1, &zero)) {
	    rb_raise(rb_eArgError, "step can't be 0");
	}
    }

    if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
	long end = FIX2LONG(e);
	long i, unit = FIX2LONG(step);

	if (!EXCL(range))
	    end += 1;
	i = FIX2LONG(b);
	while (i < end) {
	    rb_yield(LONG2NUM(i));
	    RETURN_IF_BROKEN();
	    if (i + unit < i) break;
	    i += unit;
	}

    }
    else if (SYMBOL_P(b) && SYMBOL_P(e)) { /* symbols are special */
	VALUE args[2];
	VALUE iter[2];

	args[0] = rb_sym_to_s(e);
	args[1] = EXCL(range) ? Qtrue : Qfalse;
	iter[0] = INT2FIX(1);
	iter[1] = step;
	rb_objc_block_call(rb_sym_to_s(b), selUpto, 2, args, sym_step_i, (VALUE)iter);
    }
    else if (ruby_float_step(b, e, step, EXCL(range))) {
	/* done */
    }
    else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
	     !NIL_P(rb_check_to_integer(b, "to_int")) ||
	     !NIL_P(rb_check_to_integer(e, "to_int"))) {
	SEL op = EXCL(range) ? selLT : selLE;
	VALUE v = b;
	int i = 0;

	while (RTEST(rb_vm_call(v, op, 1, &e))) {
	    rb_yield(v);
	    RETURN_IF_BROKEN();
	    i++;
	    VALUE tmp = rb_vm_call(INT2NUM(i), selMULT, 1, &step);
	    v = rb_vm_call(b, selPLUS, 1, &tmp);
	}
    }
    else {
	tmp = rb_check_string_type(b);

	if (!NIL_P(tmp)) {
	    VALUE args[2], iter[2];

	    b = tmp;
	    args[0] = e;
	    args[1] = EXCL(range) ? Qtrue : Qfalse;
	    iter[0] = INT2FIX(1);
	    iter[1] = step;
	    rb_objc_block_call(b, selUpto, 2, args, step_i, (VALUE)iter);
	}
	else {
	    VALUE args[2];

	    if (!discrete_object_p(b)) {
		rb_raise(rb_eTypeError, "can't iterate from %s",
			 rb_obj_classname(b));
	    }
	    args[0] = INT2FIX(1);
	    args[1] = step;
	    return range_each_func(range, step_i, args);
	}
    }
    return range;
}
Exemplo n.º 15
0
zval *value_to_zval(VALUE v) {
    zval *zv;
    MAKE_STD_ZVAL(zv);

    switch (TYPE(v)) {
        case T_FALSE:
            ZVAL_FALSE(zv);
            return zv;
        case T_TRUE:
            ZVAL_TRUE(zv);
            return zv;
        case T_UNDEF:
        case T_NIL:
            ZVAL_NULL(zv);
            return zv;
        case T_FIXNUM:
            ZVAL_LONG(zv, rb_fix2int(v));
            return zv;
        case T_BIGNUM:
            ZVAL_LONG(zv, rb_big2long(v)); // FIXME: bignum over long
            return zv;
        case T_FLOAT:
            ZVAL_DOUBLE(zv, RFLOAT_VALUE(v));
            return zv;
        case T_ARRAY:
            {
                int i;
                array_init(zv);
                for(i=0;i<RARRAY_LEN(v);++i) {
                    zval *add = value_to_zval(RARRAY_PTR(v)[i]);
                    zend_hash_next_index_insert(Z_ARRVAL_P(zv), &add, sizeof(zval *), NULL);
                }
                return zv;
            }
        case T_HASH:
            {
                array_init(zv);
                rb_hash_foreach(v, hash_to_zval, (VALUE)zv);
                return zv;
            }
        case T_SYMBOL:
            {
                VALUE symbol_str = rb_sym_to_s(v);
                ZVAL_STRINGL(zv, StringValuePtr(symbol_str), RSTRING_LEN(symbol_str), 1);
                return zv;
            }
        case T_STRING:
            {
                ZVAL_STRINGL(zv, StringValuePtr(v), RSTRING_LEN(v), 1);
                return zv;
            }
        default:
            {
                if (CLASS_OF(v) == cPhpEmbedValue) {
                    php_value* pv;
                    Data_Get_Struct(v, php_value, pv);
                    MAKE_COPY_ZVAL(&pv->value, zv);
                    return zv;
                }
            }
            FREE_ZVAL(zv);
            rb_raise(rb_eRuntimeError, "no implemented");
    }
}
Exemplo n.º 16
0
VALUE convert_value_to_php_string(VALUE v) {
    switch (TYPE(v)) {
        case T_FALSE:
            return rb_str_new_cstr("false");
        case T_TRUE:
            return rb_str_new_cstr("true");
        case T_UNDEF:
        case T_NIL:
            return rb_str_new_cstr("null");
        case T_FIXNUM:
            return rb_fix2str(v, 10);
        case T_BIGNUM:
            return rb_big2str(v, 10);
        case T_FLOAT:
            return rb_funcall(v, rb_intern("to_s"), 0);
        case T_ARRAY:
            {
                int i;
                VALUE ret = rb_str_new_cstr("array(");
                for(i=0;i<RARRAY_LEN(v);++i) {
                    VALUE p = convert_value_to_php_string(RARRAY_PTR(v)[i]);
                    if (TYPE(p) == T_STRING) {
                        rb_str_cat2(ret, StringValuePtr(p));
                    }
                    if (i != RARRAY_LEN(v)-1) {
                        rb_str_cat2(ret, ",");
                    }
                }
                rb_str_cat2(ret, ")");
                return ret;
            }
        case T_HASH:
            {
                VALUE ret = rb_str_new_cstr("array(");
                VALUE ary = rb_ary_new();
                VALUE *p;
                int i, len;

                rb_hash_foreach(v, hash_to_php_string_array, ary);

                len = RARRAY_LEN(ary);
                p = RARRAY_PTR(ary);
                for(i=0; i<len; ++i) {
                    rb_str_cat2(ret, StringValuePtr(p[i]));
                    if (i != len-1) {
                        rb_str_cat2(ret, ",");
                    }
                }

                rb_str_cat2(ret, ")");
                return ret;
            }
        case T_SYMBOL:
            {
                VALUE symbol_str = rb_sym_to_s(v);
                VALUE ret = rb_str_new_cstr("'");
                rb_str_cat2(ret, StringValuePtr(symbol_str));
                rb_str_cat2(ret, "'");
                return ret;
            }
        case T_STRING:
            {
                VALUE ret = rb_str_new_cstr("'");
                rb_str_cat2(ret, StringValuePtr(v));
                rb_str_cat2(ret, "'");
                return ret;
            }
        default:
            rb_raise(rb_eRuntimeError, "no implemented");
    }
}
Exemplo n.º 17
0
Arquivo: range.c Projeto: DashYang/sim
static VALUE
range_step(int argc, VALUE *argv, VALUE range)
{
    VALUE b, e, step, tmp;

    RETURN_SIZED_ENUMERATOR(range, argc, argv, range_step_size);

    b = RANGE_BEG(range);
    e = RANGE_END(range);
    if (argc == 0) {
	step = INT2FIX(1);
    }
    else {
	rb_scan_args(argc, argv, "01", &step);
	if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
	    step = rb_to_int(step);
	}
	if (rb_funcall(step, '<', 1, INT2FIX(0))) {
	    rb_raise(rb_eArgError, "step can't be negative");
	}
	else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
	    rb_raise(rb_eArgError, "step can't be 0");
	}
    }

    if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
	long end = FIX2LONG(e);
	long i, unit = FIX2LONG(step);

	if (!EXCL(range))
	    end += 1;
	i = FIX2LONG(b);
	while (i < end) {
	    rb_yield(LONG2NUM(i));
	    if (i + unit < i) break;
	    i += unit;
	}

    }
    else if (SYMBOL_P(b) && SYMBOL_P(e)) { /* symbols are special */
	VALUE args[2], iter[2];

	args[0] = rb_sym_to_s(e);
	args[1] = EXCL(range) ? Qtrue : Qfalse;
	iter[0] = INT2FIX(1);
	iter[1] = step;
	rb_block_call(rb_sym_to_s(b), rb_intern("upto"), 2, args, sym_step_i, (VALUE)iter);
    }
    else if (ruby_float_step(b, e, step, EXCL(range))) {
	/* done */
    }
    else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
	     !NIL_P(rb_check_to_integer(b, "to_int")) ||
	     !NIL_P(rb_check_to_integer(e, "to_int"))) {
	ID op = EXCL(range) ? '<' : idLE;
	VALUE v = b;
	int i = 0;

	while (RTEST(rb_funcall(v, op, 1, e))) {
	    rb_yield(v);
	    i++;
	    v = rb_funcall(b, '+', 1, rb_funcall(INT2NUM(i), '*', 1, step));
	}
    }
    else {
	tmp = rb_check_string_type(b);

	if (!NIL_P(tmp)) {
	    VALUE args[2], iter[2];

	    b = tmp;
	    args[0] = e;
	    args[1] = EXCL(range) ? Qtrue : Qfalse;
	    iter[0] = INT2FIX(1);
	    iter[1] = step;
	    rb_block_call(b, rb_intern("upto"), 2, args, step_i, (VALUE)iter);
	}
	else {
	    VALUE args[2];

	    if (!discrete_object_p(b)) {
		rb_raise(rb_eTypeError, "can't iterate from %s",
			 rb_obj_classname(b));
	    }
	    args[0] = INT2FIX(1);
	    args[1] = step;
	    range_each_func(range, step_i, (VALUE)args);
	}
    }
    return range;
}
Exemplo n.º 18
0
static VALUE param_hash_aref(VALUE self, VALUE key) {
  if (TYPE(key) == T_SYMBOL) {
    key = rb_sym_to_s(key);
  }
  return rb_hash_aref(self, key);
}
Exemplo n.º 19
0
static VALUE param_hash_key_p(VALUE self, VALUE key) {
  if (TYPE(key) == T_SYMBOL) {
    key = rb_sym_to_s(key);
  }
  return nyara_rb_hash_has_key(self, key) ? Qtrue : Qfalse;
}