Is There an Inverse of 'rb_define_method'?

Hi,

Using “rb_define_method”, we can associate a method string with a function
pointer.

Given a method string, is there an easy way to get the function pointer
without going deep in the Ruby internals?

Regards,

Bill

William Djaja Tjokroaminata wrote:

Hi,

Using “rb_define_method”, we can associate a method string with a function
pointer.

Given a method string, is there an easy way to get the function pointer
without going deep in the Ruby internals?

Regards,

Bill

I think this is similar to trying to get the name of a variable referring
to an instance. See thread starting at [ruby-talk: 44901] for why this is
probably a dubious thing to do.

Perhaps you can hash the method string to the method name(s) when you
define it?

Hi,

I don’t think it is similar to get the name of a variable. When Ruby
calls a method, in the background it must call a C function. I want to
get the pointer of this function, not its name.

The problem is in my case, someone else (the user) will define the method
in Ruby, and so I cannot record it when the method is defined.

Basically, in my extension, instead of writing

rb_funcall (obj, rb_intern ("func_name"), n, ...);

I just want to call the function directly, something like

func_name_ptr (obj, n, ...);

to reduce the overhead (because this call is done hundreds of thousands
times per second).

Regards,

Bill

···

===========================================================================
George Ogata g_ogata@optushome.com.au wrote:

I think this is similar to trying to get the name of a variable referring
to an instance. See thread starting at [ruby-talk: 44901] for why this is
probably a dubious thing to do.

Perhaps you can hash the method string to the method name(s) when you
define it?

William Djaja Tjokroaminata wrote:

Hi,

I don’t think it is similar to get the name of a variable. When Ruby
calls a method, in the background it must call a C function. I want to
get the pointer of this function, not its name.

The problem is in my case, someone else (the user) will define the method
in Ruby, and so I cannot record it when the method is defined.

Basically, in my extension, instead of writing

rb_funcall (obj, rb_intern ("func_name"), n, ...);

I just want to call the function directly, something like

func_name_ptr (obj, n, ...);

to reduce the overhead (because this call is done hundreds of thousands
times per second).

Oops.

I misread yor post. I saw the “define_method” part of “rb_define_method”
and assumed “Module.define_method”. (Never mind the stuff about function
pointers… :))

Sorry, I don’t know much about ruby internals or writing extensions.
Hopefully someone else here can help you.

Hi,

I don’t think it is similar to get the name of a variable. When Ruby
calls a method, in the background it must call a C function. I want to
get the pointer of this function, not its name.

No such method is provided by the reason as you wrote:

The problem is in my case, someone else (the user) will define the method
in Ruby, and so I cannot record it when the method is defined.

Instead, these may be possible.

VALUE method = rb_obj_method(obj, rb_intern(“func_name”));

rb_method_call(n, argv, method);

···

At Thu, 8 Aug 2002 02:24:16 +0900, William Djaja Tjokroaminata wrote:


Nobu Nakada

Hi,

I’m sorry, but I cannot find the definition of “rb_method_call” by
‘grep’-ing it on all *.h and *.c. I am using “ruby 1.6.7
(2002-03-01) [i686-linux]”. If this function is indeed defined, it is one
step closer to what I want.

Regards,

Bill

···

=======================================================================
nobu.nokada@softhome.net wrote:

Instead, these may be possible.

VALUE method = rb_obj_method(obj, rb_intern(“func_name”));

rb_method_call(n, argv, method);


Nobu Nakada

Hi,

···

At Fri, 9 Aug 2002 03:06:56 +0900, William Djaja Tjokroaminata wrote:

I’m sorry, but I cannot find the definition of “rb_method_call” by
‘grep’-ing it on all *.h and *.c. I am using “ruby 1.6.7
(2002-03-01) [i686-linux]”. If this function is indeed defined, it is one
step closer to what I want.

Wmm, they are not defined yet both, just candidates for new
features I imagined.


Nobu Nakada

Hi,

In this case, then, instead of “rb_obj_method” which returns a VALUE, can
we have another function which will return directly the function pointer
itself? Because we are in the realm of C already, probably it is better
to bypass Ruby VALUE intermediary. (In my Ruby-C world, the C side does
not need to deal with VALUE unnecessarily; only when interfacing with the
Ruby user the data needs to be converted into VALUE.)

I have tried to dig into the Ruby internal struct’s themselves, but they
are very complicated, and without a standard function, this may cause
segmentation fault later…

Regards,

Bill

···

============================================================================
nobu.nokada@softhome.net wrote:

Hi,

VALUE method = rb_obj_method(obj, rb_intern(“func_name”));

rb_method_call(n, argv, method);

Wmm, they are not defined yet both, just candidates for new
features I imagined.


Nobu Nakada

Hi,

In this case, then, instead of “rb_obj_method” which returns a VALUE, can
we have another function which will return directly the function pointer
itself? Because we are in the realm of C already, probably it is better
to bypass Ruby VALUE intermediary. (In my Ruby-C world, the C side does
not need to deal with VALUE unnecessarily; only when interfacing with the
Ruby user the data needs to be converted into VALUE.)

You know why it can’t work:

The problem is in my case, someone else (the user) will define the method
in Ruby, and so I cannot record it when the method is defined.

And also, how can you know about the arguments?

If you will let users choose from functions you provide, you
can hold the pointer internally. Otherwise, it would be
impossible in general. C has no such dynamic feature.

···

At Fri, 9 Aug 2002 22:04:07 +0900, William Djaja Tjokroaminata wrote:


Nobu Nakada

Well, I have not digged too deep into the Ruby C source code; however, to
me the answer for the dynamic arguments should be from C variable number
of arguments.

I don’t know the exact internal of Ruby, but to me, when parsing a Ruby
script, I guess the “rb_define_method” is called with argc either equal to
-1 or -2. Therefore, if I know that all the C functions are derived like
this, there is no problem for me to call the C functions directly: if all
are -1, then the function is of the form “func (int argc, VALUE *argc,
VALUE self)”, and if all are -2, then the function is of the form “func
(VALUE self, VALUE args)”.

Regards,

Bill

···

============================================================================
nobu.nokada@softhome.net wrote:

You know why it can’t work:

The problem is in my case, someone else (the user) will define the method
in Ruby, and so I cannot record it when the method is defined.

And also, how can you know about the arguments?

If you will let users choose from functions you provide, you
can hold the pointer internally. Otherwise, it would be
impossible in general. C has no such dynamic feature.


Nobu Nakada

Hi,

Well, I have not digged too deep into the Ruby C source code; however, to
me the answer for the dynamic arguments should be from C variable number
of arguments.

It’s done at compile time, but not run time. Only you can be
sure that a method is callable in a particular form, when the
function signature is known exactly.

I don’t know the exact internal of Ruby, but to me, when parsing a Ruby
script, I guess the “rb_define_method” is called with argc either equal to
-1 or -2. Therefore, if I know that all the C functions are derived like
this, there is no problem for me to call the C functions directly: if all
are -1, then the function is of the form “func (int argc, VALUE *argc,
VALUE self)”, and if all are -2, then the function is of the form “func
(VALUE self, VALUE args)”.

However, there are many other functions with non-negative
arity, “func(self, arg1)”, “func(self, arg1, arg2)” and so on.
To invoke C function corresponding to ruby method, you must
distinguish them, and call_cfunc() does it.

And a user can redefine methods, then the methods don’t have
corresponding C functions.

···

At Tue, 13 Aug 2002 02:08:14 +0900, William Djaja Tjokroaminata wrote:


Nobu Nakada

-------------------------------------

And a user can redefine methods, then the methods don't have
corresponding C functions.

-------------------------------------
This may be a little bit tricky, because then it means I have to obtain
the function pointer only after the parsing (of all user scripts) is
completely done.

It was previously explained, it *don't* exist a function pointer for user
defs. ruby just has nodes which are processed by rb_eval()

pigeon% ruby -rii -e 'def a(b,c) puts b, c; end; dump Object, :a'
Method Object::a
SCOPE
  BLOCK
    ARGS(b, c)
    NEWLINE <-e:1>
    FCALL puts
      ARRAY
        LVAR b
        LVAR c

pigeon%

Guy Decoux

I am sorry, but when I reread the entire thread, probably now I should
have made it clear that the question is, whether there is an inverse of
"rb_define_method", assuming the method is written by a user in Ruby, and
not in a C extension.

Yes, in a C extension a user can define any arity function, and therefore
probably it is impossible to get the inverse. However, if we assume that
the function that we are looking for is definitely derived from a function
written in Ruby, probably it is possible to obtain the function pointer.

Regards,

Bill

Thanks. A concise, precise, answer like this is what I am looking
for. So basically, for a user-defined Ruby function, there is no
corresponding “physical, explicit” C function. (Makes sense, if we think
long enough.)

Regards,

Bill

···

==========================================================================
ts decoux@moulon.inra.fr wrote:

It was previously explained, it don’t exist a function pointer for user
defs. ruby just has nodes which are processed by rb_eval()

pigeon% ruby -rii -e ‘def a(b,c) puts b, c; end; dump Object, :a’
Method Object::a
SCOPE
BLOCK
ARGS(b, c)
NEWLINE <-e:1>
FCALL puts
ARRAY
LVAR b
LVAR c

pigeon%

Guy Decoux