示例#1
0
/* call-seq: define_function(name) { |args,...| }
 *
 * Define a function named +name+ with +args+.  The arity of the block
 * will be used as the arity for the function defined.
 */
static VALUE define_function(VALUE self, VALUE name)
{
  sqlite3RubyPtr ctx;
  VALUE block;
  int status;

  Data_Get_Struct(self, sqlite3Ruby, ctx);
  REQUIRE_OPEN_DB(ctx);

  block = rb_block_proc();

  status = sqlite3_create_function(
    ctx->db,
    StringValuePtr(name),
    rb_proc_arity(block),
    SQLITE_UTF8,
    (void *)block,
    rb_sqlite3_func,
    NULL,
    NULL
  );

  CHECK(ctx->db, status);

  return self;
}
示例#2
0
int
rb_node_arity(NODE* body)
{
    switch (nd_type(body)) {
      case NODE_CFUNC:
	if (body->nd_argc < 0)
	    return -1;
	return body->nd_argc;
      case NODE_ZSUPER:
	return -1;
      case NODE_ATTRSET:
	return 1;
      case NODE_IVAR:
	return 0;
      case NODE_BMETHOD:
	return rb_proc_arity(body->nd_cval);
      case RUBY_VM_METHOD_NODE:
	{
	    rb_iseq_t *iseq;
	    GetISeqPtr((VALUE)body->nd_body, iseq);
	    if (iseq->arg_rest == -1 && iseq->arg_opts == 0) {
		return iseq->argc;
	    }
	    else {
		return -(iseq->argc + 1 + iseq->arg_post_len);
	    }
	}
      default:
	rb_raise(rb_eArgError, "invalid node 0x%x", nd_type(body));
    }
}
示例#3
0
文件: hash.c 项目: Jaharmi/MacRuby
static void
default_proc_arity_check(VALUE proc)
{
    const int arity = rb_proc_arity(proc);
    if (arity != 0 && arity != 2) {
	rb_raise(rb_eTypeError, "expected Proc with 2 arguments (but got %d)",
		arity);
    }
}
示例#4
0
文件: proc.c 项目: MSch/MacRuby
VALUE
rb_proc_check_and_call(VALUE proc, int argc, VALUE *argv)
{
    VALUE tmp = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
    if (NIL_P(tmp)) {
        rb_raise(rb_eTypeError,
		"wrong type %s (expected Proc)",
		rb_obj_classname(proc));
    }
    proc = tmp;

    const int arity = rb_proc_arity(proc);
    if (arity != argc) {
	rb_raise(rb_eArgError, "expected Proc with %d arguments (got %d)",
		argc, arity);
    }
    return proc_call(proc, 0, argc, argv);
}
示例#5
0
int
rb_node_arity(NODE* body)
{
    int n;

    switch (nd_type(body)) {
    case NODE_CFUNC:
	if (body->nd_argc < 0)
	    return -1;
	return body->nd_argc;
    case NODE_ZSUPER:
	return -1;
    case NODE_ATTRSET:
	return 1;
    case NODE_IVAR:
	return 0;
    case NODE_BMETHOD:
	return rb_proc_arity(body->nd_cval);
    case NODE_SCOPE:
	body = body->nd_next;	/* skip NODE_SCOPE */
	if (nd_type(body) == NODE_BLOCK)
	    body = body->nd_head;
	if (!body)
	    return 0;
	n = body->nd_frml ? RARRAY_LEN(body->nd_frml) : 0;
	if (body->nd_opt || body->nd_rest)
	    n = -n - 1;
	return n;
    case RUBY_VM_METHOD_NODE:{
	    rb_iseq_t *iseq;
	    GetISeqPtr((VALUE)body->nd_body, iseq);
	    if (iseq->arg_rest == 0 && iseq->arg_opts == 0) {
		return iseq->argc;
	    }
	    else {
		return -iseq->argc - 1;
	    }
	}
    default:
	rb_raise(rb_eArgError, "invalid node 0x%x", nd_type(body));
    }
}
示例#6
0
文件: proc.c 项目: MSch/MacRuby
int
rb_node_arity(NODE* body)
{
    // TODO should be replaced by the roxor.cpp's stuff
    switch (nd_type(body)) {
	case NODE_CFUNC:
	    if (body->nd_argc < 0) {
		return -1;
	    }
	    return body->nd_argc;
	case NODE_ZSUPER:
	    return -1;
	case NODE_ATTRSET:
	    return 1;
	case NODE_IVAR:
	    return 0;
	case NODE_BMETHOD:
	    return rb_proc_arity(body->nd_cval);
	default:
	    rb_raise(rb_eArgError, "invalid node 0x%x", nd_type(body));
    }
}
示例#7
0
文件: proc.c 项目: MSch/MacRuby
static VALUE
proc_arity(VALUE self, SEL sel)
{
    return INT2FIX(rb_proc_arity(self));
}