2 #include <mruby/class.h>
3 #include <mruby/string.h>
4 #include <mruby/istruct.h>
7 istruct_test_initialize(mrb_state *mrb, mrb_value self)
9 char *string = (char*)mrb_istruct_ptr(self);
10 mrb_int size = mrb_istruct_size();
11 mrb_value object = mrb_get_arg1(mrb);
13 if (mrb_fixnum_p(object)) {
14 strncpy(string, "fixnum", size-1);
16 #ifndef MRB_WITHOUT_FLOAT
17 else if (mrb_float_p(object)) {
18 strncpy(string, "float", size-1);
21 else if (mrb_string_p(object)) {
22 strncpy(string, "string", size-1);
25 strncpy(string, "anything", size-1);
28 string[size - 1] = 0; // force NULL at the end
33 istruct_test_to_s(mrb_state *mrb, mrb_value self)
35 return mrb_str_new_cstr(mrb, (const char*)mrb_istruct_ptr(self));
39 istruct_test_length(mrb_state *mrb, mrb_value self)
41 return mrb_fixnum_value(mrb_istruct_size());
45 istruct_test_test_receive(mrb_state *mrb, mrb_value self)
47 mrb_value object = mrb_get_arg1(mrb);
49 if (mrb_obj_class(mrb, object) != mrb_class_get(mrb, "InlineStructTest"))
51 mrb_raise(mrb, E_TYPE_ERROR, "Expected InlineStructTest");
53 return mrb_bool_value(((char*)mrb_istruct_ptr(object))[0] == 's');
57 istruct_test_test_receive_direct(mrb_state *mrb, mrb_value self)
60 mrb_get_args(mrb, "I", &ptr);
61 return mrb_bool_value(ptr[0] == 's');
65 istruct_test_mutate(mrb_state *mrb, mrb_value self)
67 char *ptr = (char*)mrb_istruct_ptr(self);
68 memcpy(ptr, "mutate", 6);
69 return mrb_nil_value();
72 void mrb_mruby_inline_struct_gem_test(mrb_state *mrb)
76 cls = mrb_define_class(mrb, "InlineStructTest", mrb->object_class);
77 MRB_SET_INSTANCE_TT(cls, MRB_TT_ISTRUCT);
78 mrb_define_method(mrb, cls, "initialize", istruct_test_initialize, MRB_ARGS_REQ(1));
79 mrb_define_method(mrb, cls, "to_s", istruct_test_to_s, MRB_ARGS_NONE());
80 mrb_define_method(mrb, cls, "mutate", istruct_test_mutate, MRB_ARGS_NONE());
81 mrb_define_class_method(mrb, cls, "length", istruct_test_length, MRB_ARGS_NONE());
82 mrb_define_class_method(mrb, cls, "test_receive", istruct_test_test_receive, MRB_ARGS_REQ(1));
83 mrb_define_class_method(mrb, cls, "test_receive_direct", istruct_test_test_receive_direct, MRB_ARGS_REQ(1));