[Kde-bindings] QtRuby bindings progress

Richard Dale Richard_Dale at tipitina.demon.co.uk
Sat Jul 5 16:19:42 UTC 2003


I've just got the first 'building block' for the ruby bindings, the 
missing_method() implentation in 'C', here is the ruby equivalent of the BOOT 
function in XS:

VALUE cTest;

static VALUE t_method_missing(int argc, VALUE * argv, VALUE self)
{
	printf("In t_method_missing(argc: %d, argv[0]: %s argv[1]: %d)\n",
			argc,
			rb_id2name( SYM2ID(argv[0]) ),
			NUM2INT(argv[1]) );
	return self;
}

void
Init_Qt()
{
    init_qt_Smoke();
    qt_Smoke->binding = new QtRubySmokeBinding(qt_Smoke);
    install_handlers(Qt_handlers);

	ruby_init();
    cTest = rb_define_class("Qt", rb_cObject);
    rb_define_method(cTest, "method_missing", t_method_missing, -1);

	pointer_map = rb_ary_new();
	rb_gc_register_address(&pointer_map);
//	rv_this = ??;
//	rb_gc_register_address(&rv_this);

	rb_eval_string("x = Qt.new");
	rb_eval_string("x.foobar(3)");
	
    methcache.setAutoDelete(1);
    classcache.setAutoDelete(1);
}

When the code above is run, it produces the following output:

baldhead duke 1086% ./rubylib
Introducing QtRuby...
In t_method_missing(argc: 2, argv[0]: foobar argv[1]: 3)
Init_Qt returned...

So it has picked up the method name 'foobar', the number of arguments and an 
arg value of 3. Woo hoo! So not too difficult to call a target SMOKE method.

I've called it QtRuby in the sources ( 'Cute Ruby' :) ) to distinguish it from 
the SWIG based RubyQt. So far all I've done is hack out the Perl guts stuff, 
the next stage is to gradually replace it all with equivalent ruby runtime 
code..

Ruby garbage collection is 'mark and sweep' rather than reference counting 
like perl. The call to 'rb_gc_register_address(pointer_map)' above means 
"mark pointer_map in use within C interface", and  
'rb_gc_unregister_address(pointer_map)', releases it. So it might be 
necessary to have a reference counting scheme with the C interface that calls 
the two ruby functions when an internal ref_count (on smokeruby_object 
struct?) is incremented to 1 initially, and the other when it falls to 0 
again.

-- Richard

-------------- next part --------------
A non-text attachment was scrubbed...
Name: qtruby_0.1.tar.gz
Type: application/x-tgz
Size: 20369 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20030705/e2462258/attachment.bin>


More information about the Kde-bindings mailing list