Help with C embedding

K

Khat Harr

I'm having trouble with a weird error that I don't understand the cause
of. I'm pretty new to embedding so I'm sure I'm overlooking something.

The relevant C code is as follows:
========================================
VALUE input_trigger(VALUE button) {
PspCtrlButtons cbutt = (PspCtrlButtons)NUM2ULONG(button);
if((pad.Buttons & cbutt) && !(oldpad.Buttons & cbutt)) {return Qtrue;}
return Qfalse;
}

//this gets called during initialization:
rb_define_module_function(ruby_input_mod, "trigger?",
(VALUE (*)(...))input_trigger, 1);
========================================

I get errors when calling the method though:
========================================
result = Input.trigger?(16)
========================================

It's saying
========================================
can't convert Module into Integer
========================================

I don't understand why it expects a Module there.

Can anyone see what I'm doing wrong here?

I'd really appreciate any advice. :)
 
7

7stud --

Khat said:
I'm having trouble with a weird error that I don't understand the cause
of.

The error is coming from this line:
PspCtrlButtons cbutt = (PspCtrlButtons)NUM2ULONG(button);

Specifically, the function call:

NUM2ULONG(button)


As Brian Candler pointed out, your function signature here is faulty:

VALUE input_trigger(VALUE button)


You are actually doing something like this:

static VALUE input_trigger(VALUE self)
{
unsigned long num = NUM2ULONG(self);
}

The first parameter variable of a function automatically gets assigned
self. As a consequence, if you want to pass one argument to a
function, you have to define the function with two parameter variables:
the first parameter variable will be assigned self, and the second
parameter variable will be assigned the argument you pass.

In your code, the first parameter variable is named 'button', and when
you call the function, self is automatically assigned to the first
parameter variable, so self gets assigned to button. Because a module
is calling the function, self is the module, and the function NUM2ULONG
can't convert a module to an int (an unsigned long integer in this
case).
//this gets called during initialization:
rb_define_module_function(ruby_input_mod, "trigger?",
(VALUE (*)(...))input_trigger, 1);

Next, that cast to a variadic function produces an error when I compile
the code:

error: ISO C requires a named argument before ‘...’
 
K

Khat Harr

Awesome. Works like a charm. I'll read through that chapter. Thank
you.

I'm using gcc to compile and it doesn't have a problem with the variadic
cast, but yeah, I should probably clean that up anyway.

Thanks again guys. :)
 
B

Brian Candler

Khat said:
I'm using gcc to compile and it doesn't have a problem with the variadic
cast

Probably best just to copy what Ruby does.

[defines.h]
#ifdef __cplusplus
#define ANYARGS ...
#else
#define ANYARGS
#endif

[ruby.h]
void rb_define_module_function _((VALUE,const
char*,VALUE(*)(ANYARGS),int));

[class.c uses K&R prototypes :-(]
void
rb_define_module_function(module, name, func, argc)
VALUE module;
const char *name;
VALUE (*func)();
int argc;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top