Commit d6876aec authored by mattn's avatar mattn

Should be MRB_DATA class. RStruct isn't managed by mruby core. So it should be...

Should be MRB_DATA class. RStruct isn't managed by mruby core. So it should be self-managed class instances.
parent 3f1814ce
...@@ -10,17 +10,25 @@ ...@@ -10,17 +10,25 @@
#include "mruby/array.h" #include "mruby/array.h"
#include "mruby/string.h" #include "mruby/string.h"
#include "mruby/class.h" #include "mruby/class.h"
#include "mruby/data.h"
#include "mruby/variable.h" #include "mruby/variable.h"
struct RStruct { struct RStruct {
struct RBasic basic; struct RBasic basic;
long len; mrb_value values;
mrb_value *ptr;
}; };
#define RSTRUCT(st) ((struct RStruct*)((st).value.p)) static void
#define RSTRUCT_LEN(st) ((int)(RSTRUCT(st)->len)) rstruct_free(mrb_state *mrb, void *ptr)
#define RSTRUCT_PTR(st) (RSTRUCT(st)->ptr) {
mrb_free(mrb, ptr);
}
static struct mrb_data_type mrb_struct_type = { "mrb_struct", rstruct_free };
#define RSTRUCT(st) ((struct RStruct*)(DATA_PTR(st)))
#define RSTRUCT_LEN(st) ((int)(RARRAY_LEN(RSTRUCT(st)->values)))
#define RSTRUCT_PTR(st) (RARRAY_PTR(RSTRUCT(st)->values))
static struct RClass * static struct RClass *
struct_class(mrb_state *mrb) struct_class(mrb_state *mrb)
...@@ -272,6 +280,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k ...@@ -272,6 +280,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k
} }
c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
} }
//MRB_SET_INSTANCE_TT(c, MRB_TT_DATA);
nstr = mrb_obj_value(c); nstr = mrb_obj_value(c);
mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members); mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members);
...@@ -429,10 +438,11 @@ mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_val ...@@ -429,10 +438,11 @@ mrb_struct_initialize_withArg(mrb_state *mrb, int argc, mrb_value *argv, mrb_val
if (n < argc) { if (n < argc) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs"); mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
} }
st = RSTRUCT(self); st = (struct RStruct *) mrb_malloc(mrb, sizeof(struct RStruct));
st->ptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n); DATA_PTR(self) = st;
st->len = n; DATA_TYPE(self) = &mrb_struct_type;
struct_copy(st->ptr, argv, argc); st->values = mrb_ary_new_from_values(mrb, argc, argv);
mrb_iv_set(mrb, self, mrb_intern(mrb, "__values__"), mrb_nil_value());
return self; return self;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment