mruby-data: new gem for Data class introduced in Ruby 3.2
ADD
mrbgems/mruby-data/mrbgem.rake
+MRuby::Gem::Specification.new('mruby-data') do |spec|
+ spec.license = 'MIT'
+ spec.author = 'mruby developers'
+ spec.summary = 'standard Data class'
+end

ADD
mrbgems/mruby-data/src/data.c
+/*
+** data.c - Data class
+**
+** See Copyright Notice in mruby.h
+*/
+
+#include <string.h>
+#include <mruby.h>
+#include <mruby/array.h>
+#include <mruby/string.h>
+#include <mruby/class.h>
+#include <mruby/variable.h>
+#include <mruby/hash.h>
+#include <mruby/proc.h>
+#include <mruby/internal.h>
+#include <mruby/presym.h>
+
+#define RDATA_LEN(st) RARRAY_LEN(st)
+#define RDATA_PTR(st) RARRAY_PTR(st)
+
+#define mrb_dataobj_p(o) (mrb_type(o) == MRB_TT_STRUCT)
+
+static struct RClass *
+data_class(mrb_state *mrb)
+{
+ return mrb_class_get_id(mrb, MRB_SYM(Data));
+}
+
+static void
+data_corrupted(mrb_state *mrb)
+{
+ mrb_raise(mrb, E_TYPE_ERROR, "corrupted data");
+}
+
+static mrb_value
+data_s_members(mrb_state *mrb, struct RClass *c)
+{
+ struct RClass* sclass = data_class(mrb);
+ mrb_value mem;
+
+ for (;;) {
+ mem = mrb_iv_get(mrb, mrb_obj_value(c), MRB_SYM(__members__));
+ if (!mrb_nil_p(mem)) {
+ if (!mrb_array_p(mem)) {
+ data_corrupted(mrb);
+ }
+ return mem;
+ }
+ c = c->super;
+ if (c == sclass || c == 0) {
+ mrb_raise(mrb, E_TYPE_ERROR, "uninitialized data");
+ }
+ }
+}
+
+static mrb_value
+data_members(mrb_state *mrb, mrb_value obj)
+{
+ if (!mrb_dataobj_p(obj) || RDATA_LEN(obj) == 0) {
+ data_corrupted(mrb);
+ }
+ mrb_value members = data_s_members(mrb, mrb_obj_class(mrb, obj));
+ if (RDATA_LEN(obj) != RARRAY_LEN(members)) {
+ mrb_raisef(mrb, E_TYPE_ERROR,
+ "data size differs (%i required %i given)",
+ RARRAY_LEN(members), RDATA_LEN(obj));
+ }
+ return members;
+}
+
+static mrb_value
+mrb_data_s_members_m(mrb_state *mrb, mrb_value klass)
+{
+ mrb_value members, ary;
+
+ members = data_s_members(mrb, mrb_class_ptr(klass));
+ ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members));
+ mrb_ary_replace(mrb, ary, members);
+ return ary;
+}
+
+/*
+ * call-seq:
+ * data.members -> array
+ *
+ * Returns an array of strings representing the names of the instance
+ * variables.
+ *
+ * Customer = Data.define(:name, :address, :zip)
+ * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ * joe.members #=> [:name, :address, :zip]
+ */
+
+static mrb_value
+mrb_data_members(mrb_state *mrb, mrb_value obj)
+{
+ return mrb_data_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj)));
+}
+
+static mrb_value
+mrb_data_ref(mrb_state *mrb, mrb_value obj)
+{
+ mrb_int i = mrb_integer(mrb_proc_cfunc_env_get(mrb, 0));
+ mrb_value *ptr = RDATA_PTR(obj);
+
+ if (!ptr) return mrb_nil_value();
+ return ptr[i];
+}
+
+static void
+make_data_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c)
+{
+ const mrb_value *ptr_members = RARRAY_PTR(members);
+ mrb_int i;
+ mrb_int len = RARRAY_LEN(members);
+ int ai = mrb_gc_arena_save(mrb);
+
+ for (i=0; i<len; i++) {
+ mrb_sym id = mrb_symbol(ptr_members[i]);
+ mrb_method_t m;
+ mrb_value at = mrb_fixnum_value(i);
+ struct RProc *aref = mrb_proc_new_cfunc_with_env(mrb, mrb_data_ref, 1, &at);
+ MRB_METHOD_FROM_PROC(m, aref);
+ mrb_define_method_raw(mrb, c, id, m);
+ mrb_gc_arena_restore(mrb, ai);
+ }
+}
+
+static mrb_value
+make_data(mrb_state *mrb, mrb_value members, struct RClass *klass)
+{
+ mrb_value nstr;
+ mrb_sym id;
+ struct RClass *c = mrb_class_new(mrb, klass);
+ MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT);
+ nstr = mrb_obj_value(c);
+ mrb_iv_set(mrb, nstr, MRB_SYM(__members__), members);
+
+ mrb_define_class_method_id(mrb, c, MRB_SYM(new), mrb_instance_new, MRB_ARGS_ANY());
+ mrb_define_class_method_id(mrb, c, MRB_SYM(members), mrb_data_s_members_m, MRB_ARGS_NONE());
+ /* RSTRUCT(nstr)->basic.c->super = c->c; */
+ make_data_define_accessors(mrb, members, c);
+ return nstr;
+}
+
+/*
+ * call-seq:
+ * StructClass.new(arg, ...) -> obj
+ * StructClass[arg, ...] -> obj
+ *
+ * Creates a new class, named by <i>aString</i>, containing accessor
+ * methods for the given symbols. If the name <i>aString</i> is
+ * omitted, an anonymous structure class will be created. Otherwise,
+ * the name of this struct will appear as a constant in class
+ * <code>Struct</code>, so it must be unique for all
+ * <code>Struct</code>s in the system and should start with a capital
+ * letter. Assigning a structure class to a constant effectively gives
+ * the class the name of the constant.
+ *
+ * <code>Struct::new</code> returns a new <code>Class</code> object,
+ * which can then be used to create specific instances of the new
+ * structure. The number of actual parameters must be
+ * less than or equal to the number of attributes defined for this
+ * class; unset parameters default to <code>nil</code>. Passing too many
+ * parameters will raise an <code>ArgumentError</code>.
+ *
+ * The remaining methods listed in this section (class and instance)
+ * are defined for this generated class.
+ *
+ * # Create a structure named by its constant
+ * Customer = Struct.new(:name, :address) #=> Customer
+ * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
+ */
+static mrb_value
+mrb_data_s_def(mrb_state *mrb, mrb_value klass)
+{
+ mrb_value rest;
+ mrb_int i;
+ mrb_value b, st;
+ mrb_sym id;
+ const mrb_value *argv;
+ mrb_int argc;
+
+ mrb_get_args(mrb, "*&", &argv, &argc, &b);
+ if (argc == 0) { /* special case to avoid crash */
+ mrb_argnum_error(mrb, argc, 1, -1);
+ }
+ else {
+ rest = mrb_ary_new_from_values(mrb, argc, argv);
+ for (i=0; i<argc; i++) {
+ id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]);
+ mrb_ary_set(mrb, rest, i, mrb_symbol_value(id));
+ }
+ st = make_data(mrb, rest, mrb_class_ptr(klass));
+ if (!mrb_nil_p(b)) {
+ mrb_yield_with_class(mrb, b, 1, &st, st, mrb_class_ptr(st));
+ }
+
+ return st;
+ }
+ /* not reached */
+ return mrb_nil_value();
+}
+
+static mrb_int
+num_members(mrb_state *mrb, struct RClass *klass)
+{
+ mrb_value members = data_s_members(mrb, klass);
+ return RARRAY_LEN(members);
+}
+
+static mrb_value
+mrb_data_initialize(mrb_state *mrb, mrb_value self)
+{
+ struct RClass *klass = mrb_obj_class(mrb, self);
+ const mrb_value *argv;
+ mrb_int argc;
+ mrb_int i, n;
+
+ mrb_get_args(mrb, "*!", &argv, &argc);
+ n = num_members(mrb, klass);
+ if (n < argc) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
+ }
+
+ for (i = 0; i < argc; i++) {
+ mrb_ary_set(mrb, self, i, argv[i]);
+ }
+ for (i = argc; i < n; i++) {
+ mrb_ary_set(mrb, self, i, mrb_nil_value());
+ }
+ return self;
+}
+
+static mrb_value
+mrb_data_init_copy(mrb_state *mrb, mrb_value copy)
+{
+ mrb_value s = mrb_get_arg1(mrb);
+
+ if (mrb_obj_equal(mrb, copy, s)) return 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_dataobj_p(s)) {
+ mrb_raise(mrb, E_TYPE_ERROR, "corrupted Data");
+ }
+ mrb_ary_replace(mrb, copy, s);
+ return copy;
+}
+
+/*
+ * call-seq:
+ * data == other_data -> true or false
+ *
+ * Equality---Returns <code>true</code> if <i>other_data</i> is
+ * equal to this one: they must be of the same class as generated by
+ * <code>Struct::define</code>, and all values of must be equal
+ * (according to <code>Object#==</code>).
+ *
+ * Customer = Data.define(:name, :address, :zip)
+ * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ * joe2 = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
+ * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
+ * joe == joe2 #=> true
+ * joe == jane #=> false
+ */
+
+static mrb_value
+mrb_data_equal(mrb_state *mrb, mrb_value s)
+{
+ mrb_value s2 = mrb_get_arg1(mrb);
+ mrb_value *ptr, *ptr2;
+ mrb_int i, len;
+
+ if (mrb_obj_equal(mrb, s, s2)) {
+ return mrb_true_value();
+ }
+ if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
+ return mrb_false_value();
+ }
+ if (RDATA_LEN(s) != RDATA_LEN(s2)) {
+ return mrb_false_value();
+ }
+ ptr = RDATA_PTR(s);
+ ptr2 = RDATA_PTR(s2);
+ len = RDATA_LEN(s);
+ for (i=0; i<len; i++) {
+ if (!mrb_equal(mrb, ptr[i], ptr2[i])) {
+ return mrb_false_value();
+ }
+ }
+
+ return mrb_true_value();
+}
+
+/*
+ * call-seq:
+ * data.eql?(other) -> true or false
+ *
+ * Two structures are equal if they are the same object, or if all their
+ * fields are equal (using <code>Object#eql?</code>).
+ */
+static mrb_value
+mrb_data_eql(mrb_state *mrb, mrb_value s)
+{
+ mrb_value s2 = mrb_get_arg1(mrb);
+ mrb_value *ptr, *ptr2;
+ mrb_int i, len;
+
+ if (mrb_obj_equal(mrb, s, s2)) {
+ return mrb_true_value();
+ }
+ if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
+ return mrb_false_value();
+ }
+ if (RDATA_LEN(s) != RDATA_LEN(s2)) {
+ return mrb_false_value();
+ }
+ ptr = RDATA_PTR(s);
+ ptr2 = RDATA_PTR(s2);
+ len = RDATA_LEN(s);
+ for (i=0; i<len; i++) {
+ if (!mrb_eql(mrb, ptr[i], ptr2[i])) {
+ return mrb_false_value();
+ }
+ }
+
+ return mrb_true_value();
+}
+
+/*
+ * call-seq:
+ * data.to_h -> hash
+ *
+ * Create a hash from member names and values.
+ */
+static mrb_value
+mrb_data_to_h(mrb_state *mrb, mrb_value self)
+{
+ mrb_value members, ret;
+ mrb_int i;
+
+ members = data_members(mrb, self);
+ ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members));
+
+ for (i = 0; i < RARRAY_LEN(members); ++i) {
+ mrb_hash_set(mrb, ret, RARRAY_PTR(members)[i], RDATA_PTR(self)[i]);
+ }
+
+ return ret;
+}
+
+/*
+ * call-seq:
+ * data.to_s -> string
+ * data.inspect -> string
+ *
+ * Returns a string representation of Data
+ */
+static mrb_value
+mrb_data_to_s(mrb_state *mrb, mrb_value self)
+{
+ mrb_value members, ret;
+ mrb_value *ptr;
+ mrb_int i;
+
+ members = data_members(mrb, self);
+ ret = mrb_str_new_lit(mrb, "#<data ");
+ ptr = RDATA_PTR(self);
+ for (i=0; i < RARRAY_LEN(members); i++) {
+ mrb_int len;
+ const char *name = mrb_sym_name_len(mrb, mrb_symbol(RARRAY_PTR(members)[i]), &len);
+ if (i>0) mrb_str_cat_lit(mrb, ret, ", ");
+ mrb_str_cat(mrb, ret, name, len);
+ mrb_str_cat_lit(mrb, ret, "=");
+ mrb_str_cat_str(mrb, ret, mrb_inspect(mrb, ptr[i]));
+ }
+ mrb_str_cat_lit(mrb, ret, ">");
+
+ return ret;
+}
+
+/*
+ * A <code>Data</code> is a convenient way to bundle a number of
+ * attributes together, using accessor methods, without having to write
+ * an explicit class.
+ *
+ * The <code>Struct</code> class is a generator of specific classes,
+ * each one of which is defined to hold a set of variables and their
+ * accessors. In these examples, we'll call the generated class
+ * "<i>Customer</i>Class," and we'll show an example instance of that
+ * class as "<i>Customer</i>Inst."
+ *
+ * In the descriptions that follow, the parameter <i>symbol</i> refers
+ * to a symbol, which is either a quoted string or a
+ * <code>Symbol</code> (such as <code>:name</code>).
+ */
+void
+mrb_mruby_data_gem_init(mrb_state* mrb)
+{
+ struct RClass *d;
+ d = mrb_define_class(mrb, "Data", mrb->object_class);
+ MRB_SET_INSTANCE_TT(d, MRB_TT_STRUCT);
+
+ mrb_undef_class_method(mrb, d, "new");
+ mrb_define_class_method(mrb, d, "define", mrb_data_s_def, MRB_ARGS_ANY());
+
+ mrb_define_method(mrb, d, "==", mrb_data_equal, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, d, "members", mrb_data_members, MRB_ARGS_NONE());
+ mrb_define_method(mrb, d, "initialize", mrb_data_initialize, MRB_ARGS_ANY());
+ mrb_define_method(mrb, d, "initialize_copy", mrb_data_init_copy, MRB_ARGS_ANY());
+ mrb_define_method(mrb, d, "eql?", mrb_data_eql, MRB_ARGS_REQ(1));
+
+ mrb_define_method(mrb, d, "to_h", mrb_data_to_h, MRB_ARGS_NONE());
+ mrb_define_method(mrb, d, "to_s", mrb_data_to_s, MRB_ARGS_NONE());
+ mrb_define_method(mrb, d, "inspect", mrb_data_to_s, MRB_ARGS_NONE());
+}
+
+void
+mrb_mruby_data_gem_final(mrb_state* mrb)
+{
+}

ADD
mrbgems/mruby-data/test/data.rb
+##
+# Struct ISO Test
+
+assert('Data') do
+ assert_equal Class, Data.class
+end
+
+assert('Data.define') do
+ c = Data.define(:m1, :m2)
+ assert_equal Data, c.superclass
+ assert_equal [:m1, :m2], c.members
+end
+
+assert('Data#==') do
+ c = Data.define(:m1, :m2)
+ cc1 = c.new(1,2)
+ cc2 = c.new(1,2)
+ assert_true cc1 == cc2
+end
+
+assert('Data#members') do
+ c = Data.define(:m1, :m2)
+ assert_equal [:m1, :m2], c.new(1,2).members
+end
+
+assert('wrong struct arg count') do
+ c = Data.define(:m1)
+ assert_raise ArgumentError do
+ cc = c.new(1,2,3)
+ end
+end
+
+assert('data dup') do
+ c = Data.define(:m1, :m2, :m3, :m4, :m5)
+ cc = c.new(1,2,3,4,5)
+ assert_nothing_raised {
+ assert_equal(cc, cc.dup)
+ }
+end
+
+assert('Data inspect') do
+ c = Data.define(:m1, :m2, :m3, :m4, :m5)
+ cc = c.new(1,2,3,4,5)
+ assert_equal "#<data m1=1, m2=2, m3=3, m4=4, m5=5>", cc.inspect
+end
+
+assert('Data#to_h') do
+ s = Data.define(:white, :red, :green).new('ruuko', 'yuzuki', 'hitoe')
+ assert_equal({:white => 'ruuko', :red => 'yuzuki', :green => 'hitoe'}) { s.to_h }
+end
+
+assert("Data.define does not allow array") do
+ assert_raise(TypeError) do
+ Data.define("Test", [:a])
+ end
+end
+
+assert("Data.define generates subclass of Data") do
+ begin
+ original_struct = Data
+ Data = String
+ assert_equal original_struct, original_struct.define(:foo).superclass
+ ensure
+ Data = original_struct
+ end
+end
+
+assert 'Data#freeze' do
+ c = Data.define(:m)
+
+ o = c.new(:test)
+ assert_equal :test, o.m
+ assert_nothing_raised {
+ o.freeze
+ }
+end

MODIFY
mrbgems/stdlib-ext.gembox
mrbgems/stdlib-ext.gembox
@@ -13,6 +13,9 @@ MRuby::GemBox.new do |conf|
# Use standard Struct class
conf.gem :core => "mruby-struct"
+ # Use standard Data class
+ conf.gem :core => "mruby-data"
+
# Use Random class
conf.gem :core => "mruby-random"
end

mruby-data: new gem for Data class introduced in Ruby 3.2