2

我正在处理应该是一个相当基本的迭代。我知道我可以使用 Ruby 代码来完成它,但我已经在使用 C 扩展,所以我更愿意将此函数与其余代码一起保存在 C 中 - 特别是因为这应该可以(以一种或另一种方式)在没有问题。

问题在于 rb_block_call。以下是 README.EXT 描述 rb_block_call 的方式:

VALUE rb_block_call(VALUE recv, ID mid, int argc, VALUE * argv,
          VALUE (*func) (ANYARGS), VALUE data2)

调用 recv 上的方法,方法名称由符号 mid 指定,提供 func 作为块。func 将从 yield 作为第一个参数接收值,data2 作为第二个参数,argc/argv 作为第三/第四个参数。

所以,我的理解(通过查看 Ruby 内部验证)是接收函数应该如下所示:

VALUE function( VALUE rb_yield_value, VALUE data2, int argc, VALUE argv );

在这里,我们遇到了问题。在我的用例(我将在下面包括)中, rb_yield_value 和 data2 按预期传递;另一方面,argc 始终设置为 1,argv[0] 为 rb_yield_value,argv[1] 为 false,argv[2] 为 rb_yield_value,argv[3] 抛出异常。

我为 argc 和 argv 传递了什么并不重要;传递 0 和 NULL 结果相同,1 和 VALUE 设置为 Qtrue 也是如此。带有 argc/argv 的所有内容都保持原样。

这是我正在使用的代码:

VALUE rb_RPBDB_DatabaseObject_internal_cursorForCallingContext( VALUE rb_self ) {

    //  when we are looking for the contextual iterator, we look up the current backtrace
    //  at each level of the backtrace we have an object and a method;
    //  if this object and method match keys present in self (tracking calling contexts for iteration in this iteration class) return cursor

    VALUE   rb_cursor_context_storage_hash  =   rb_RPBDB_DatabaseObject_internal_cursorContextStorageHash( rb_self );

    VALUE   rb_cursor   =   Qnil;

    if ( RHASH_SIZE( rb_cursor_context_storage_hash ) ) {

        rb_block_call(  rb_mKernel, 
                        rb_intern( "each_backtrace_frame" ), 
                        1, 
                        & rb_cursor_context_storage_hash, 
                        rb_RPBDB_DatabaseObject_internal_each_backtrace_frame, 
                        rb_cursor );    
    }

    return rb_cursor;
}

//  walk up the stack one frame at a time
//  for each frame we need to see if object/method are defined in our context storage hash
VALUE rb_RPBDB_DatabaseObject_internal_each_backtrace_frame(    VALUE   rb_this_backtrace_frame_hash, 
                                                                VALUE   rb_cursor_return,
                                                                int     argc,
                                                                VALUE*  args )  {

    //  why are we getting 3 args when argc is 1 and none of the 3 match what was passed?
    VALUE   rb_cursor_context_storage_hash  =   args[ 0 ];

    //  each frame is identifiable as object/method
    VALUE   rb_this_frame_object    =   rb_hash_aref(   rb_this_backtrace_frame_hash,
                                                        ID2SYM( rb_intern( "object" ) ) );
    VALUE   rb_this_frame_method    =   rb_hash_aref(   rb_this_backtrace_frame_hash,
                                                        ID2SYM( rb_intern( "method" ) ) );

    //  we likely have "block in ..." for our method; we only want the "..."
    rb_this_frame_method    =   ID2SYM( rb_to_id( rb_funcall(   rb_obj_as_string( rb_this_frame_method ),
                                                                rb_intern( "gsub" ),
                                                                2,
                                                                rb_str_new2( "block in " ),
                                                                rb_str_new2( "" ) ) ) );

    VALUE   rb_cursor_object_context_hash   =   rb_RPBDB_DatabaseObject_internal_cursorObjectContextStorageHash(    rb_cursor_context_storage_hash,
                                                                                                                    rb_this_frame_object);

    if ( RHASH_SIZE( rb_cursor_object_context_hash ) )  {

        rb_cursor_return    =   rb_hash_aref(   rb_cursor_object_context_hash,
                                                rb_this_frame_method );

    }

    return rb_cursor_return;
}

Ruby 内部似乎没有很多带有 argc/argv 的 rb_block_call 示例......最多只有一两个,我相信它们都只是在内部传递值而不是使用它们。

想法?

4

1 回答 1

4

我对 Ruby C 扩展很陌生,但我认为你的困惑在哪里。

VALUE rb_block_call(VALUE recv, ID mid, int argc, VALUE argv[],
    VALUE (*func) (ANYARGS), VALUE data2)

argc/argv 是您调用的 Ruby 函数的参数。

在作为块调用的 C 函数中:

VALUE block_function(VALUE rb_yield_value, VALUE data2, int argc, VALUE argv[])

argc/argv 是块的参数。

一个简单的例子是注入

这是 C 语言的翻译:[1,2,3].inject { |sum, e| 总和 + e }

#include "ruby.h"

static VALUE rb_puts(VALUE obj) {
  return rb_funcall(rb_mKernel, rb_intern("puts"), 1, obj);
}

static VALUE inject_block(VALUE yield_value, VALUE data2, int argc, VALUE argv[]) {
  printf("\nyield_value:\n");
  rb_puts(yield_value);
  printf("data2:\n");
  rb_puts(data2);
  printf("argc: %d\n", argc);
  printf("argv:\n");
  int i;
  for(i = 0; i < argc; ++i) {
    printf("argv %d:\n", i);
    rb_puts(argv[i]);
  }

  VALUE sum = argv[0];
  VALUE e = argv[1];// or yield_value
  return INT2FIX(FIX2INT(sum) + FIX2INT(e));
}

static VALUE rb_block_call_test(int argc, VALUE argv[]) {
  VALUE ary = rb_ary_new();
  int i;
  for(i = 0; i < 3; ++i) {
    rb_ary_push(ary, INT2FIX(i+1));
  }
  VALUE block_argv[1];
  block_argv[0] = INT2FIX(0);
  ary = rb_block_call(ary,
                rb_intern("inject"),
                1, // argc
                block_argv, //argv is a C-array of VALUE
                inject_block,
                Qtrue // data2
                );
  return ary;
}

void Init_rb_block_call() {
  rb_define_global_function("rb_block_call_test", rb_block_call_test, 0);
}

哪个输出(对 rb_block_call_test 的调用):

yield_value: 0 # sum = argv[0]
data2: true
argc: 2
argv:
argv 0: 0 # sum
argv 1: 1 # e

yield_value: 1
data2: true
argc: 2
argv:
argv 0: 1
argv 1: 2

yield_value: 3
data2: true
argc: 2
argv:
argv 0: 3
argv 1: 3

# => 6

我相信 yield_value 总是 argv[0]

如果要在块和调用者之间传递信息,则使用 data2

在您的示例中,我想#each_backtrace_frame 正在产生一个“backtrace_frame”,这就是该块的 argc/argv 始终为 1/the_backtrace_frame 的原因。我相信 #each_backtrace_frame 接受任意数量的参数,因为当您尝试传递一些参数时它不会引发任何错误。

于 2010-07-11T10:06:51.803 回答