value.h: introduce `MRB_TT_STRUCT` for `mruby-struct`.
MODIFY
include/mruby/value.h
include/mruby/value.h
@@ -150,6 +150,7 @@ static const unsigned int IEEE754_INFINITY_BITS_SINGLE = 0x7F800000;
f(MRB_TT_ENV, struct REnv, "env") \
f(MRB_TT_DATA, struct RData, "Data") \
f(MRB_TT_FIBER, struct RFiber, "Fiber") \
+ f(MRB_TT_STRUCT, struct RArray, "Struct") \
f(MRB_TT_ISTRUCT, struct RIStruct, "istruct") \
f(MRB_TT_BREAK, struct RBreak, "break") \
f(MRB_TT_COMPLEX, struct RComplex, "Complex") \

MODIFY
mrbgems/mruby-os-memsize/src/memsize.c
mrbgems/mruby-os-memsize/src/memsize.c
@@ -88,6 +88,7 @@ os_memsize_of_object(mrb_state* mrb, mrb_value obj)
mrb_hash_memsize(obj);
break;
}
+ case MRB_TT_STRUCT:
case MRB_TT_ARRAY: {
mrb_int len = RARRAY_LEN(obj);
/* Arrays that do not fit within an RArray perform a heap allocation

MODIFY
mrbgems/mruby-struct/src/struct.c
mrbgems/mruby-struct/src/struct.c
#define RSTRUCT_LEN(st) RARRAY_LEN(st)
#define RSTRUCT_PTR(st) RARRAY_PTR(st)
+#define mrb_struct_p(o) (mrb_type(o) == MRB_TT_STRUCT)
+
static struct RClass *
struct_class(mrb_state *mrb)
{
@@ -58,19 +60,14 @@ static mrb_value
struct_members(mrb_state *mrb, mrb_value s)
{
mrb_value members = struct_s_members(mrb, mrb_obj_class(mrb, s));
- if (!mrb_array_p(s)) {
+ if (!mrb_struct_p(s) || RSTRUCT_LEN(s) == 0) {
mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
}
if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
- if (RSTRUCT_LEN(s) == 0) { /* probably uninitialized */
- mrb_ary_resize(mrb, s, RARRAY_LEN(members));
- }
- else {
mrb_raisef(mrb, E_TYPE_ERROR,
"struct size differs (%i required %i given)",
RARRAY_LEN(members), RSTRUCT_LEN(s));
}
- }
return members;
}
@@ -213,7 +210,7 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass *kl
}
c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
}
- MRB_SET_INSTANCE_TT(c, MRB_TT_ARRAY);
+ MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT);
nstr = mrb_obj_value(c);
mrb_iv_set(mrb, nstr, MRB_SYM(__members__), members);
@@ -362,7 +359,7 @@ mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) {
mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
}
- if (!mrb_array_p(s)) {
+ if (!mrb_struct_p(s)) {
mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
}
mrb_ary_replace(mrb, copy, s);

MODIFY
src/gc.c
src/gc.c
@@ -742,6 +742,7 @@ gc_mark_children(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
}
break;
+ case MRB_TT_STRUCT:
case MRB_TT_ARRAY:
{
struct RArray *a = (struct RArray*)obj;
@@ -857,6 +858,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end)
}
break;
+ case MRB_TT_STRUCT:
case MRB_TT_ARRAY:
if (ARY_SHARED_P(obj))
mrb_ary_decref(mrb, ((struct RArray*)obj)->as.heap.aux.shared);
@@ -1041,6 +1043,7 @@ gc_gray_counts(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
}
break;
+ case MRB_TT_STRUCT:
case MRB_TT_ARRAY:
{
struct RArray *a = (struct RArray*)obj;

value.h: introduce `MRB_TT_STRUCT` for `mruby-struct`.