static VALUE
enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv)
{
    struct enumerator *ptr = enumerator_ptr(enum_obj);

    ptr->method = rb_obj_method(obj, meth);
    if (rb_block_given_p()) {
	ptr->proc = rb_block_proc();
	ptr->iter = enumerator_iter_i;
    }
    else {
	ptr->iter = enumerator_each_i;
    }
    if (argc) ptr->args = rb_ary_new4(argc, argv);
    ptr->fib = 0;
    ptr->dst = Qnil;
    ptr->no_next = Qfalse;

    return enum_obj;
}
示例#2
0
static VALUE Unpacker_register_type(int argc, VALUE* argv, VALUE self)
{
    UNPACKER(self, uk);

    int ext_type;
    VALUE proc;
    VALUE arg;
    VALUE ext_class;

    switch (argc) {
    case 1:
        /* register_type(0x7f) {|data| block... } */
        rb_need_block();
#ifdef HAVE_RB_BLOCK_LAMBDA
        proc = rb_block_lambda();
#else
        /* MRI 1.8 */
        proc = rb_block_proc();
#endif
        arg = proc;
        ext_class = Qnil;
        break;
    case 3:
        /* register_type(0x7f, Time, :from_msgpack_ext) */
        ext_class = argv[1];
        arg = argv[2];
        proc = rb_obj_method(ext_class, arg);
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 3)", argc);
    }

    ext_type = rb_num2int(argv[0]);
    if(ext_type < -128 || ext_type > 127) {
        rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
    }

    msgpack_unpacker_ext_registry_put(&uk->ext_registry, ext_class, ext_type, proc, arg);

    return Qnil;
}
示例#3
0
static VALUE state_machine_add(int argc, VALUE argv[], VALUE self) {
    VALUE pool = rb_ivar_get(self, rb_intern("@pool"));
    volatile VALUE key_or_hash, value, parent, block;
    rb_scan_args(argc, argv, "12&", &key_or_hash, &value, &parent, &block);
    if (rb_block_given_p()) {
        rb_hash_aset(pool, key_or_hash, block);
    } else {
        if (RB_TYPE_P(key_or_hash, T_HASH)) {
            volatile VALUE ary = rb_funcall(key_or_hash, rb_intern("to_a"), 0);
            int i, len = RARRAY_LENINT(ary);
            for (i = 0; i < len; ++i) {
                volatile VALUE pair = RARRAY_PTR(ary)[i];
                state_machine_add(2, (VALUE[]){ RARRAY_PTR(pair)[0], RARRAY_PTR(pair)[1] }, self);
            }
        } else {
            if (rb_obj_is_proc(value) == Qtrue) {
                rb_hash_aset(pool, key_or_hash, value);
            }  else {
                rb_hash_aset(pool, key_or_hash, rb_obj_method(NIL_P(parent) ? rb_ivar_get(self, rb_intern("@parent")) : parent, value));
            }
        }
    }
示例#4
0
static VALUE Factory_register_type(int argc, VALUE* argv, VALUE self)
{
    FACTORY(self, fc);

    int ext_type;
    VALUE ext_module;
    VALUE options;
    VALUE packer_arg, unpacker_arg;
    VALUE packer_proc, unpacker_proc;

    if (OBJ_FROZEN(self)) {
        rb_raise(rb_eRuntimeError, "can't modify frozen Factory");
    }

    switch (argc) {
    case 2:
        /* register_type(0x7f, Time) */
        packer_arg = ID2SYM(rb_intern("to_msgpack_ext"));
        unpacker_arg = ID2SYM(rb_intern("from_msgpack_ext"));
        break;
    case 3:
        /* register_type(0x7f, Time, packer: proc-like, unapcker: proc-like) */
        options = argv[2];
        if(rb_type(options) != T_HASH) {
            rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
        }
        packer_arg = rb_hash_aref(options, ID2SYM(rb_intern("packer")));
        unpacker_arg = rb_hash_aref(options, ID2SYM(rb_intern("unpacker")));
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
    }

    ext_type = NUM2INT(argv[0]);
    if(ext_type < -128 || ext_type > 127) {
        rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
    }

    ext_module = argv[1];
    if(rb_type(ext_module) != T_MODULE && rb_type(ext_module) != T_CLASS) {
        rb_raise(rb_eArgError, "expected Module/Class but found %s.", rb_obj_classname(ext_module));
    }

    packer_proc = Qnil;
    unpacker_proc = Qnil;

    if(packer_arg != Qnil) {
        packer_proc = rb_funcall(packer_arg, rb_intern("to_proc"), 0);
    }

    if(unpacker_arg != Qnil) {
        if(rb_type(unpacker_arg) == T_SYMBOL || rb_type(unpacker_arg) == T_STRING) {
            unpacker_proc = rb_obj_method(ext_module, unpacker_arg);
        } else {
            unpacker_proc = rb_funcall(unpacker_arg, rb_intern("method"), 1, ID2SYM(rb_intern("call")));
        }
    }

    msgpack_packer_ext_registry_put(&fc->pkrg, ext_module, ext_type, packer_proc, packer_arg);

    if (ext_module == rb_cSymbol) {
        fc->has_symbol_ext_type = true;
    }

    msgpack_unpacker_ext_registry_put(&fc->ukrg, ext_module, ext_type, unpacker_proc, unpacker_arg);

    return Qnil;
}
示例#5
0
static VALUE kernel_spec_rb_obj_method(VALUE self, VALUE obj, VALUE method) {
  return rb_obj_method(obj, method);
}