public
Created

Embedding a Ruby Interpreter- working with struct

  • Download Gist
embeded_struct.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
struct Address {
char * town;
};
 
struct Person {
struct Address * address;
char * name;
};
 
static VALUE wrap_person_get_address(VALUE self) {
struct Person * person;
Data_Get_Struct(self, struct Person, person);
VALUE wrapped_address =Data_Wrap_Struct(address_wrapper_class, NULL, NULL, person->address);
return wrapped_address;
}
 
...
 
address_wrapper_class = rb_define_class("Address", rb_cObject);
VALUE person_wrapper_class = rb_class_new(rb_cObject);
 
/*
Add #town and #town= methods to the new class
 
Here we associate the two implementation functions defined above with the
wrapper class, so that in ruby it will respond to the methods #town and #town=
// */
rb_define_method(address_wrapper_class, "town", wrap_address_get_town, 0); //getter method: #town
rb_define_method(address_wrapper_class, "town=", wrap_address_set_town, 1);//setter method: #town=
 
rb_define_method(person_wrapper_class, "name", wrap_person_get_name, 0);
rb_define_method(person_wrapper_class, "name=", wrap_person_set_name, 1);
 
rb_define_method(person_wrapper_class, "address", wrap_person_get_address,
0);
 
...
ruby_ext_test_api.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
============================================================================
Name : rub_ext_test.c
Author : Matej
Version :
Copyright :
Description : Working version with struct defined and inicialized by Ruby C API
============================================================================
*/
 
#include <stdio.h>
#include <stdlib.h>
 
#include "ruby.h"
 
static int id_print_method;
 
static VALUE klass;
 
struct Address {
char * town;
};
 
static VALUE wrap_print(VALUE args) {
VALUE *values = (VALUE *) args;
VALUE testClass = values[0];
VALUE adr = values[1];
 
return rb_funcall(testClass, id_print_method, 1, adr);
}
 
static VALUE protected_print(VALUE testClass, VALUE adr) {
int error;
VALUE args[2];
VALUE result;
 
args[0] = testClass;
args[1] = adr;
 
result = rb_protect(wrap_print, (VALUE) args, &error);
 
return error ? Qnil : result;
}
 
int main(int argc, char **argv) {
int error;
 
struct Address adr;
adr.town = "London";
 
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
ruby_script("demo_embedder"); /* sets name in error messages */
 
rb_protect((VALUE(*)(VALUE)) rb_require, (VALUE) "test", &error);
 
VALUE testClass = rb_class_new_instance(0, 0, rb_const_get(rb_cObject,
rb_intern("TestKlass")));
 
VALUE adr_ptr;
adr_ptr = Data_Wrap_Struct(klass,0,0,&adr);
 
VALUE adr_struct = rb_struct_define("Adresa","town" , 0);
VALUE inst_adr_struct=rb_struct_new(adr_struct,1);
 
 
rb_struct_aset(inst_adr_struct,rb_str_new2("town"),rb_str_new2("London"));
id_print_method = rb_intern("print_info");
 
// VALUE result;
// VALUE result = protected_print(testClass, adr_ptr);
VALUE result = protected_print(testClass, inst_adr_struct);
 
if (NIL_P(result))
printf("Nil!\n");
else
printf("Returned %d\n", NUM2INT(result));
 
 
puts("FINISHED"); /* prints FINISHED */
return ruby_cleanup(0);
}
ruby_ext_test_wrap.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*
============================================================================
Name : rub_ext_test.c
Author : Matej
Version :
Copyright :
Description : Wrapping struct (not working)
============================================================================
*/
 
#include <stdio.h>
#include <stdlib.h>
 
#include "ruby.h"
 
static int id_print_method;
 
static VALUE klass;
 
struct Address {
char * town;
};
 
static VALUE wrap_print(VALUE args) {
VALUE *values = (VALUE *) args;
VALUE testClass = values[0];
VALUE adr = values[1];
 
return rb_funcall(testClass, id_print_method, 1, adr);
}
 
static VALUE protected_print(VALUE testClass, VALUE adr) {
int error;
VALUE args[2];
VALUE result;
 
args[0] = testClass;
args[1] = adr;
 
result = rb_protect(wrap_print, (VALUE)args, &error);
 
return error ? Qnil : result;
}
 
int main(int argc, char **argv) {
int error;
 
 
struct Address adr;
adr.town = "London";
 
ruby_sysinit(&argc, &argv);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
ruby_script("demo_embedder"); /* sets name in error messages */
 
rb_protect((VALUE(*)(VALUE)) rb_require, (VALUE) "test", &error);
 
VALUE testClass = rb_class_new_instance(0, 0, rb_const_get(rb_cObject,
rb_intern("TestKlass")));
 
VALUE adr_ptr;
adr_ptr = Data_Wrap_Struct(klass,0,0,&adr);
 
id_print_method = rb_intern("print_info");
 
VALUE result = protected_print(testClass, adr_ptr);
 
if (NIL_P(result))
printf("Nil!\n");
else
printf("Returned %d\n", NUM2INT(result));
 
 
 
puts("FINISHED"); /* prints FINISHED */
return ruby_cleanup(0);
}
test.rb
Ruby
1 2 3 4 5 6 7 8 9 10
require 'test'
 
class TestKlass
def print_info(struct)
puts "In Ruby"
puts struct.inspect
puts struct.town
return 0
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.