static VALUE ossl_pkcs7_set_certificates(VALUE self, VALUE ary) { STACK_OF(X509) *certs; X509 *cert; certs = pkcs7_get_certs(self); while((cert = sk_X509_pop(certs))) X509_free(cert); rb_block_call(ary, rb_intern("each"), 0, 0, ossl_pkcs7_set_certs_i, self); return ary; }
static VALUE lazy_drop(VALUE obj, VALUE n) { long len = NUM2LONG(n); if (len < 0) { rb_raise(rb_eArgError, "attempt to drop negative size"); } return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_drop_func, n), rb_ary_new3(1, n), lazy_drop_size); }
static VALUE next_i(VALUE curr, VALUE obj) { struct enumerator *e = enumerator_ptr(obj); VALUE nil = Qnil; VALUE result; result = rb_block_call(obj, id_each, 0, 0, next_ii, obj); e->stop_exc = rb_exc_new2(rb_eStopIteration, "iteration reached an end"); rb_ivar_set(e->stop_exc, id_result, result); return rb_fiber_yield(1, &nil); }
static VALUE ossl_pkcs7_set_crls(VALUE self, VALUE ary) { STACK_OF(X509_CRL) *crls; X509_CRL *crl; crls = pkcs7_get_crls(self); while((crl = sk_X509_CRL_pop(crls))) X509_CRL_free(crl); rb_block_call(ary, rb_intern("each"), 0, 0, ossl_pkcs7_set_crls_i, self); return ary; }
void cb_storage_callback(lcb_t handle, const void *cookie, lcb_storage_t operation, lcb_error_t error, const lcb_store_resp_t *resp) { struct cb_context_st *ctx = (struct cb_context_st *)cookie; struct cb_bucket_st *bucket = ctx->bucket; VALUE key, cas, exc, res; key = STR_NEW((const char*)resp->v.v0.key, resp->v.v0.nkey); cb_strip_key_prefix(bucket, key); cas = resp->v.v0.cas > 0 ? ULL2NUM(resp->v.v0.cas) : Qnil; ctx->operation = storage_opcode_to_sym(operation); exc = cb_check_error(error, "failed to store value", key); if (exc != Qnil) { rb_ivar_set(exc, cb_id_iv_cas, cas); rb_ivar_set(exc, cb_id_iv_operation, ctx->operation); ctx->exception = exc; } if (bucket->async) { /* asynchronous */ if (RTEST(ctx->observe_options)) { VALUE args[2]; /* it's ok to pass pointer to stack struct here */ args[0] = rb_hash_new(); rb_hash_aset(args[0], key, cas); args[1] = ctx->observe_options; rb_block_call(bucket->self, cb_id_observe_and_wait, 2, args, storage_observe_callback, (VALUE)ctx); ctx->observe_options = Qnil; } else if (ctx->proc != Qnil) { res = rb_class_new_instance(0, NULL, cb_cResult); rb_ivar_set(res, cb_id_iv_error, exc); rb_ivar_set(res, cb_id_iv_key, key); rb_ivar_set(res, cb_id_iv_operation, ctx->operation); rb_ivar_set(res, cb_id_iv_cas, cas); cb_proc_call(bucket, ctx->proc, 1, res); } } else { /* synchronous */ rb_hash_aset(ctx->rv, key, cas); } if (!RTEST(ctx->observe_options)) { ctx->nqueries--; if (ctx->nqueries == 0) { ctx->proc = Qnil; if (bucket->async) { cb_context_free(ctx); } } } (void)handle; }
/* * Call the ping handler after aqcuiring the GVL. */ static void * rbverse_cb_ping_body( void *ptr ) { const char **args = (const char **)ptr; const VALUE cb_args = rb_ary_new(); const VALUE observers = rb_iv_get( rbverse_mVerse, "@observers" ); rb_ary_push( cb_args, rb_str_new2(args[0]) ); rb_ary_push( cb_args, rb_str_new2(args[1]) ); rb_block_call( observers, rb_intern("each"), 0, 0, rbverse_cb_ping_i, cb_args ); return NULL; }
/* * Return a pathname which is substituted by String#sub. * * path1 = Pathname.new('/usr/bin/perl') * path1.sub('perl', 'ruby') * #=> #<Pathname:/usr/bin/ruby> */ static VALUE path_sub(int argc, VALUE *argv, VALUE self) { VALUE str = get_strpath(self); if (rb_block_given_p()) { str = rb_block_call(str, rb_intern("sub"), argc, argv, 0, 0); } else { str = rb_funcallv(str, rb_intern("sub"), argc, argv); } return rb_class_new_instance(1, &str, rb_obj_class(self)); }
static VALUE rb_set_merge(VALUE set, SEL sel, VALUE other) { rb_set_modify_check(set); VALUE klass = *(VALUE *)other; if (klass == rb_cCFSet || klass == rb_cNSSet || klass == rb_cNSMutableSet) CFSetApplyFunction((CFMutableSetRef)other, rb_set_union_callback, (void *)set); else rb_block_call(other, rb_intern("each"), 0, 0, merge_i, (VALUE)set); return set; }
static VALUE lazy_drop(VALUE obj, VALUE n) { NODE *memo; long len = NUM2LONG(n); if (len < 0) { rb_raise(rb_eArgError, "attempt to drop negative size"); } memo = NEW_MEMO(0, 0, len); return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_drop_func, (VALUE) memo), rb_ary_new3(1, n)); }
static VALUE range_first(int argc, VALUE *argv, VALUE range) { VALUE n, ary[2]; if (argc == 0) return RANGE_BEG(range); rb_scan_args(argc, argv, "1", &n); ary[0] = n; ary[1] = rb_ary_new2(NUM2LONG(n)); rb_block_call(range, idEach, 0, 0, first_i, (VALUE)ary); return ary[1]; }
void test_array_iterate(){ VALUE ary = rb_ary_new(); ID fEach = rb_intern("each"); rb_ary_push( ary, INT2NUM(1) ); rb_ary_push( ary, INT2NUM(2) ); rb_ary_push( ary, INT2NUM(3) ); printf("test_array_iterate\n"); rb_block_call( ary, fEach, 0, nullptr, (VALUE(*)(...))array_iteration_block, Qnil ); }
static VALUE enumerator_block_call(VALUE obj, rb_block_call_func *func, VALUE arg) { int argc = 0; VALUE *argv = 0; const struct enumerator *e = enumerator_ptr(obj); ID meth = e->meth; if (e->args) { argc = RARRAY_LENINT(e->args); argv = RARRAY_PTR(e->args); } return rb_block_call(e->obj, meth, argc, argv, func, arg); }
/* * call-seq: * each_cons(n) {...} * each_cons(n) * * Iterates the given block for each array of consecutive <n> * elements. If no block is given, returns an enumerator. * * e.g.: * (1..10).each_cons(3) {|a| p a} * # outputs below * [1, 2, 3] * [2, 3, 4] * [3, 4, 5] * [4, 5, 6] * [5, 6, 7] * [6, 7, 8] * [7, 8, 9] * [8, 9, 10] * */ static VALUE enum_each_cons(VALUE obj, VALUE n) { long size = NUM2LONG(n); VALUE args[2]; if (size <= 0) rb_raise(rb_eArgError, "invalid size"); RETURN_ENUMERATOR(obj, 1, &n); args[0] = rb_ary_new2(size); args[1] = (VALUE)size; rb_block_call(obj, SYM2ID(sym_each), 0, 0, each_cons_i, (VALUE)args); return Qnil; }
/* * Opens the file for reading or writing. * * See File.open. */ static VALUE path_open(int argc, VALUE *argv, VALUE self) { VALUE args[4]; int n; args[0] = get_strpath(self); n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]); if (rb_block_given_p()) { return rb_block_call(rb_cFile, rb_intern("open"), 1+n, args, 0, 0); } else { return rb_funcallv(rb_cFile, rb_intern("open"), 1+n, args); } }
/* * call-seq: * pathname.each_line {|line| ... } * pathname.each_line(sep=$/ [, open_args]) {|line| block } -> nil * pathname.each_line(limit [, open_args]) {|line| block } -> nil * pathname.each_line(sep, limit [, open_args]) {|line| block } -> nil * pathname.each_line(...) -> an_enumerator * * #each_line iterates over the line in the file. It yields a String object * for each line. * * This method is availabel since 1.8.1. */ static VALUE path_each_line(int argc, VALUE *argv, VALUE self) { VALUE args[4]; int n; args[0] = get_strpath(self); n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]); if (rb_block_given_p()) { return rb_block_call(rb_cIO, rb_intern("foreach"), 1+n, args, 0, 0); } else { return rb_funcall2(rb_cIO, rb_intern("foreach"), 1+n, args); } }
/* * call-seq: * enum.each {...} * * Iterates the given block using the object and the method specified * in the first place. * */ static VALUE enumerator_each(VALUE obj) { struct enumerator *e; int argc = 0; VALUE *argv = 0; if (!rb_block_given_p()) return obj; e = enumerator_ptr(obj); if (e->args) { argc = RARRAY_LEN(e->args); argv = RARRAY_PTR(e->args); } return rb_block_call(e->method, SYM2ID(sym_call), argc, argv, e->iter, (VALUE)e); }
static VALUE rb_set_initialize(int argc, VALUE *argv, VALUE set) { VALUE val; set = (VALUE)objc_msgSend((id)set, selInit); rb_scan_args(argc, argv, "01", &val); if (!NIL_P(val)) { rb_block_call(val, rb_intern("each"), 0, 0, initialize_i, (VALUE)set); } else if (rb_block_given_p()) { rb_warning("given block not used"); } return set; }
/* * call-seq: * e.with_index {|(*args), idx| ... } * * Iterates the given block for each elements with an index, which * start from 0. * */ static VALUE enumerator_with_index(VALUE obj) { struct enumerator *e = enumerator_ptr(obj); VALUE memo = 0; int argc = 0; VALUE *argv = 0; RETURN_ENUMERATOR(obj, 0, 0); if (e->args) { argc = RARRAY_LEN(e->args); argv = RARRAY_PTR(e->args); } return rb_block_call(e->method, SYM2ID(sym_call), argc, argv, enumerator_with_index_i, (VALUE)&memo); }
/* * call-seq: * enum.each {...} * * Iterates the given block using the object and the method specified * in the first place. If no block is given, returns self. * */ static VALUE enumerator_each(VALUE obj) { struct enumerator *e; int argc = 0; VALUE *argv = 0; if (!rb_block_given_p()) return obj; e = enumerator_ptr(obj); if (e->args) { argc = RARRAY_LENINT(e->args); argv = RARRAY_PTR(e->args); } return rb_block_call(e->obj, e->meth, argc, argv, enumerator_each_i, (VALUE)e); }
static VALUE lazy_zip(int argc, VALUE *argv, VALUE obj) { VALUE ary; int i; if (rb_block_given_p()) { return rb_call_super(argc, argv); } ary = rb_ary_new2(argc); for (i = 0; i < argc; i++) { rb_ary_push(ary, rb_funcall(argv[i], id_lazy, 0)); } return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_zip_func, ary), rb_ary_new4(argc, argv)); }
/* * call-seq: * e.each_slice(n) {...} * e.each_slice(n) * * Iterates the given block for each slice of <n> elements. If no * block is given, returns an enumerator. * * e.g.: * (1..10).each_slice(3) {|a| p a} * # outputs below * [1, 2, 3] * [4, 5, 6] * [7, 8, 9] * [10] * */ static VALUE enum_each_slice(VALUE obj, VALUE n) { long size = NUM2LONG(n); VALUE args[2], ary; if (size <= 0) rb_raise(rb_eArgError, "invalid slice size"); RETURN_ENUMERATOR(obj, 1, &n); args[0] = rb_ary_new2(size); args[1] = (VALUE)size; rb_block_call(obj, SYM2ID(sym_each), 0, 0, each_slice_i, (VALUE)args); ary = args[0]; if (RARRAY_LEN(ary) > 0) rb_yield(ary); return Qnil; }
/* * call-seq: * e.with_object(obj) {|(*args), memo_obj| ... } * e.with_object(obj) * * Iterates the given block for each element with an arbitrary * object given, and returns the initially given object. * * If no block is given, returns an enumerator. * */ static VALUE enumerator_with_object(VALUE obj, VALUE memo) { struct enumerator *e; int argc = 0; VALUE *argv = 0; RETURN_ENUMERATOR(obj, 1, &memo); e = enumerator_ptr(obj); if (e->args) { argc = RARRAY_LEN(e->args); argv = RARRAY_PTR(e->args); } rb_block_call(e->obj, e->meth, argc, argv, enumerator_with_object_i, memo); return memo; }
/* get directory content from ruby */ static int my_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { /* add . and .. automatically */ filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); /* call Fuse.readdir */ VALUE ret = rb_funcall(Fuse, rb_intern("readdir"), 1, rb_str_new2(path)); /* check that the return value responds to #each */ VALUE success = rb_funcall(ret, rb_intern("respond_to?"), 1, ID2SYM(rb_intern("each"))); /* TODO: every invalid directory will result in ENOENT, maybe allow * ruby to omit other errors */ if (success == Qfalse || success == Qnil) return -ENOENT; /* fill buf with values yielded by ret.each */ void *data[2] = {filler, buf}; rb_block_call(ret, rb_intern("each"), 0, NULL, fill_me, (VALUE)data); return 0; }
static VALUE scene_on_contact_begin(VALUE rcv, SEL sel) { VALUE block = rb_current_block(); if (block == Qnil) { rb_raise(rb_eArgError, "block not given"); } block = rb_retain(block); // FIXME need release... auto listener = cocos2d::EventListenerPhysicsContact::create(); listener->onContactBegin = [block](cocos2d::PhysicsContact &contact) -> bool { // VALUE touch_obj = rb_class_wrap_new((void *)touch, // rb_cTouch); return RTEST(rb_block_call(block, 0, NULL)); }; return scene_add_listener(rcv, listener); }
static VALUE lazy_take(VALUE obj, VALUE n) { long len = NUM2LONG(n); VALUE lazy; if (len < 0) { rb_raise(rb_eArgError, "attempt to take negative size"); } if (len == 0) { VALUE len = INT2FIX(0); lazy = lazy_to_enum_i(obj, sym_cycle, 1, &len, 0); } else { lazy = rb_block_call(rb_cLazy, id_new, 1, &obj, lazy_take_func, n); } return lazy_set_method(lazy, rb_ary_new3(1, n), lazy_take_size); }
static VALUE lazy_cycle(int argc, VALUE *argv, VALUE obj) { VALUE args; int len = rb_long2int((long)argc + 2); if (rb_block_given_p()) { return rb_call_super(argc, argv); } args = rb_ary_tmp_new(len); rb_ary_push(args, obj); rb_ary_push(args, sym_cycle); if (argc > 0) { rb_ary_cat(args, argv, argc); } return lazy_set_method(rb_block_call(rb_cLazy, id_new, len, RARRAY_PTR(args), lazy_cycle_func, args /* prevent from GC */), rb_ary_new4(argc, argv)); }
// ---------------------------------------- // #draw VALUE Ashton_ParticleEmitter_draw(VALUE self) { EMITTER(); if(emitter->count == 0) return Qnil; VALUE window = rb_gv_get("$window"); VALUE z = rb_float_new(emitter->z); // Enable the shader, if provided. if(!NIL_P(emitter->rb_shader)) rb_funcall(emitter->rb_shader, rb_intern("enable"), 1, z); // Run the actual drawing operation at the correct Z-order. rb_block_call(window, rb_intern("gl"), 1, &z, RUBY_METHOD_FUNC(draw_block), self); // Disable the shader, if provided. if(!NIL_P(emitter->rb_shader)) rb_funcall(emitter->rb_shader, rb_intern("disable"), 1, z); return Qnil; }
/* * call-seq: * Lazy.new(obj, size=nil) { |yielder, *values| ... } * * Creates a new Lazy enumerator. When the enumerator is actually enumerated * (e.g. by calling #force), +obj+ will be enumerated and each value passed * to the given block. The block can yield values back using +yielder+. * For example, to create a method +filter_map+ in both lazy and * non-lazy fashions: * * module Enumerable * def filter_map(&block) * map(&block).compact * end * end * * class Enumerator::Lazy * def filter_map * Lazy.new(self) do |yielder, *values| * result = yield *values * yielder << result if result * end * end * end * * (1..Float::INFINITY).lazy.filter_map{|i| i*i if i.even?}.first(5) * # => [4, 16, 36, 64, 100] */ static VALUE lazy_initialize(int argc, VALUE *argv, VALUE self) { VALUE obj, size = Qnil; VALUE generator; rb_check_arity(argc, 1, 2); if (!rb_block_given_p()) { rb_raise(rb_eArgError, "tried to call lazy new without a block"); } obj = argv[0]; if (argc > 1) { size = argv[1]; } generator = generator_allocate(rb_cGenerator); rb_block_call(generator, id_initialize, 0, 0, lazy_init_block_i, obj); enumerator_init(self, generator, sym_each, 0, 0, 0, size); rb_ivar_set(self, id_receiver, obj); return self; }
/* * call-seq: * e.with_index(offset = 0) {|(*args), idx| ... } * e.with_index(offset = 0) * * Iterates the given block for each element with an index, which * starts from +offset+. If no block is given, returns an enumerator. * */ static VALUE enumerator_with_index(int argc, VALUE *argv, VALUE obj) { struct enumerator *e; VALUE memo; rb_scan_args(argc, argv, "01", &memo); RETURN_ENUMERATOR(obj, argc, argv); memo = NIL_P(memo) ? 0 : (VALUE)NUM2LONG(memo); e = enumerator_ptr(obj); if (e->args) { argc = RARRAY_LENINT(e->args); argv = RARRAY_PTR(e->args); } else { argc = 0; argv = NULL; } return rb_block_call(e->obj, e->meth, argc, argv, enumerator_with_index_i, (VALUE)&memo); }
static VALUE scene_on_touch_event(VALUE rcv, SEL sel, mc_Scene_EventType type) { VALUE block = rb_current_block(); if (block == Qnil) { rb_raise(rb_eArgError, "block not given"); } block = rb_retain(block); // FIXME need release... auto scene = SCENE(rcv); if (scene->touch_listener == NULL) { scene->touch_listener = cocos2d::EventListenerTouchOneByOne::create(); } else { scene->getEventDispatcher()->removeEventListener(scene->touch_listener); } auto lambda = [block](cocos2d::Touch *touch, cocos2d::Event *event) -> bool { VALUE touch_obj = rb_class_wrap_new((void *)touch, rb_cTouch); return RTEST(rb_block_call(block, 1, &touch_obj)); }; switch (type) { case ON_BEGIN: scene->touch_listener->onTouchBegan = lambda; break; case ON_MOVE: scene->touch_listener->onTouchMoved = lambda; break; case ON_END: scene->touch_listener->onTouchEnded = lambda; break; case ON_CANCEL: scene->touch_listener->onTouchCancelled = lambda; break; } return scene_add_listener(rcv, scene->touch_listener); }