From 138ecf4723078cf8ef4342fb3995db23003eff01 Mon Sep 17 00:00:00 2001 From: mattn Date: Fri, 1 Mar 2013 13:37:46 +0900 Subject: Pluggable Struct --- include/mrbconf.h | 4 - include/mruby/struct.h | 27 -- include/mruby/value.h | 22 +- mrbgems/mruby-struct/mrbgem.rake | 4 + mrbgems/mruby-struct/src/struct.c | 785 ++++++++++++++++++++++++++++++++++++ mrbgems/mruby-struct/test/struct.rb | 77 ++++ src/etc.c | 1 - src/gc.c | 3 - src/init.c | 4 - src/object.c | 1 - src/struct.c | 781 ----------------------------------- test/t/struct.rb | 77 ---- 12 files changed, 876 insertions(+), 910 deletions(-) delete mode 100644 include/mruby/struct.h create mode 100644 mrbgems/mruby-struct/mrbgem.rake create mode 100644 mrbgems/mruby-struct/src/struct.c create mode 100644 mrbgems/mruby-struct/test/struct.rb delete mode 100644 src/struct.c delete mode 100644 test/t/struct.rb diff --git a/include/mrbconf.h b/include/mrbconf.h index 45e3d6034..c84480af7 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -45,7 +45,6 @@ /* -DDISABLE_XXXX to drop following features */ //#define DISABLE_SPRINTF /* Kernel.sprintf method */ -//#define DISABLE_STRUCT /* Struct class */ //#define DISABLE_STDIO /* use of stdio */ /* -DENABLE_XXXX to enable following features */ @@ -84,9 +83,6 @@ typedef short mrb_sym; #ifndef DISABLE_SPRINTF #define ENABLE_SPRINTF #endif -#ifndef DISABLE_STRUCT -#define ENABLE_STRUCT -#endif #ifndef DISABLE_STDIO #define ENABLE_STDIO #endif diff --git a/include/mruby/struct.h b/include/mruby/struct.h deleted file mode 100644 index cfe6df135..000000000 --- a/include/mruby/struct.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -** mruby/struct.h - Struct class -** -** See Copyright Notice in mruby.h -*/ - -#ifndef MRUBY_STRUCT_H -#define MRUBY_STRUCT_H - -#if defined(__cplusplus) -extern "C" { -#endif - -struct RStruct { - struct RBasic basic; - long len; - mrb_value *ptr; -}; -#define RSTRUCT(st) ((struct RStruct*)((st).value.p)) -#define RSTRUCT_LEN(st) ((int)(RSTRUCT(st)->len)) -#define RSTRUCT_PTR(st) (RSTRUCT(st)->ptr) - -#if defined(__cplusplus) -} /* extern "C" { */ -#endif - -#endif /* MRUBY_STRUCT_H */ diff --git a/include/mruby/value.h b/include/mruby/value.h index 17f51db94..1dfa7b975 100644 --- a/include/mruby/value.h +++ b/include/mruby/value.h @@ -29,12 +29,11 @@ enum mrb_vtype { MRB_TT_HASH, /* 16 */ MRB_TT_STRING, /* 17 */ MRB_TT_RANGE, /* 18 */ - MRB_TT_STRUCT, /* 19 */ - MRB_TT_EXCEPTION, /* 20 */ - MRB_TT_FILE, /* 21 */ - MRB_TT_ENV, /* 22 */ - MRB_TT_DATA, /* 23 */ - MRB_TT_MAXDEFINE /* 24 */ + MRB_TT_EXCEPTION, /* 19 */ + MRB_TT_FILE, /* 20 */ + MRB_TT_ENV, /* 21 */ + MRB_TT_DATA, /* 22 */ + MRB_TT_MAXDEFINE /* 23 */ }; typedef struct mrb_value { @@ -89,12 +88,11 @@ enum mrb_vtype { MRB_TT_HASH, /* 17 */ MRB_TT_STRING, /* 18 */ MRB_TT_RANGE, /* 19 */ - MRB_TT_STRUCT, /* 20 */ - MRB_TT_EXCEPTION, /* 21 */ - MRB_TT_FILE, /* 22 */ - MRB_TT_ENV, /* 23 */ - MRB_TT_DATA, /* 24 */ - MRB_TT_MAXDEFINE /* 25 */ + MRB_TT_EXCEPTION, /* 20 */ + MRB_TT_FILE, /* 21 */ + MRB_TT_ENV, /* 22 */ + MRB_TT_DATA, /* 23 */ + MRB_TT_MAXDEFINE /* 24 */ }; #ifdef MRB_ENDIAN_BIG diff --git a/mrbgems/mruby-struct/mrbgem.rake b/mrbgems/mruby-struct/mrbgem.rake new file mode 100644 index 000000000..476e990da --- /dev/null +++ b/mrbgems/mruby-struct/mrbgem.rake @@ -0,0 +1,4 @@ +MRuby::Gem::Specification.new('mruby-struct') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' +end diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c new file mode 100644 index 000000000..131702e9c --- /dev/null +++ b/mrbgems/mruby-struct/src/struct.c @@ -0,0 +1,785 @@ +/* +** struct.c - Struct class +** +** See Copyright Notice in mruby.h +*/ + +#include +#include +#include "mruby.h" +#include "mruby/array.h" +#include "mruby/string.h" +#include "mruby/class.h" +#include "mruby/variable.h" + +struct RStruct { + struct RBasic basic; + long len; + mrb_value *ptr; +}; + +#define RSTRUCT(st) ((struct RStruct*)((st).value.p)) +#define RSTRUCT_LEN(st) ((int)(RSTRUCT(st)->len)) +#define RSTRUCT_PTR(st) (RSTRUCT(st)->ptr) + +static struct RClass * +struct_class(mrb_state *mrb) +{ + return mrb_class_get(mrb, "Struct"); +} + +static inline mrb_value +struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id) +{ + struct RClass* kclass; + struct RClass* sclass = struct_class(mrb); + + mrb_value ans; + for (;;) { + ans = mrb_iv_get(mrb, c, id); + if (!mrb_nil_p(ans)) return ans; + kclass = RCLASS_SUPER(c); + if (kclass == 0 || kclass == sclass) + return mrb_nil_value(); + c = mrb_obj_value(kclass); + } +} + +mrb_value +mrb_struct_iv_get(mrb_state *mrb, mrb_value c, const char *name) +{ + return struct_ivar_get(mrb, c, mrb_intern(mrb, name)); +} + +mrb_value +mrb_struct_s_members(mrb_state *mrb, mrb_value klass) +{ + mrb_value members = struct_ivar_get(mrb, klass, mrb_intern(mrb, "__members__")); + + if (mrb_nil_p(members)) { + mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct"); + } + if (!mrb_array_p(members)) { + mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); + } + return members; +} + +mrb_value +mrb_struct_members(mrb_state *mrb, mrb_value s) +{ + mrb_value members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_obj_class(mrb, s))); + if (mrb_type(s) == MRB_TT_STRUCT) { + if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { + mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", + RARRAY_LEN(members), RSTRUCT_LEN(s)); + } + } + return members; +} + +static mrb_value +mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) +{ + mrb_value members, ary; + mrb_value *p, *pend; + + members = mrb_struct_s_members(mrb, klass); + ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members)); + p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members); + while (p < pend) { + mrb_ary_push(mrb, ary, *p); + p++; + } + + return ary; +} + +static inline void +struct_copy(mrb_value *dst, const mrb_value *src, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) { + dst[i] = src[i]; + } +} + +/* 15.2.18.4.6 */ +/* + * call-seq: + * struct.members -> array + * + * Returns an array of strings representing the names of the instance + * variables. + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * joe.members #=> [:name, :address, :zip] + */ + +static mrb_value +mrb_struct_members_m(mrb_state *mrb, mrb_value obj) +{ + return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj))); +} + +mrb_value +mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id) +{ + mrb_value members, slot, *ptr, *ptr_members; + long i, len; + + ptr = RSTRUCT_PTR(obj); + members = mrb_struct_members(mrb, obj); + ptr_members = RARRAY_PTR(members); + slot = mrb_symbol_value(id); + len = RARRAY_LEN(members); + for (i=0; ici->mid); +} + +static mrb_value mrb_struct_ref0(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[0];} +static mrb_value mrb_struct_ref1(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[1];} +static mrb_value mrb_struct_ref2(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[2];} +static mrb_value mrb_struct_ref3(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[3];} +static mrb_value mrb_struct_ref4(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[4];} +static mrb_value mrb_struct_ref5(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[5];} +static mrb_value mrb_struct_ref6(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[6];} +static mrb_value mrb_struct_ref7(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[7];} +static mrb_value mrb_struct_ref8(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[8];} +static mrb_value mrb_struct_ref9(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[9];} + +#define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) +#define N_REF_FUNC numberof(ref_func) + +static mrb_value (*const ref_func[])(mrb_state*, mrb_value) = { + mrb_struct_ref0, + mrb_struct_ref1, + mrb_struct_ref2, + mrb_struct_ref3, + mrb_struct_ref4, + mrb_struct_ref5, + mrb_struct_ref6, + mrb_struct_ref7, + mrb_struct_ref8, + mrb_struct_ref9, +}; + +mrb_sym +mrb_id_attrset(mrb_state *mrb, mrb_sym id) +{ + const char *name; + char *buf; + int len; + mrb_sym mid; + + name = mrb_sym2name_len(mrb, id, &len); + buf = (char *)mrb_malloc(mrb, len+2); + memcpy(buf, name, len); + buf[len] = '='; + buf[len+1] = '\0'; + + mid = mrb_intern2(mrb, buf, len+1); + mrb_free(mrb, buf); + return mid; +} + +static mrb_value +mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val) +{ + const char *name; + int i, len; + mrb_sym mid; + mrb_value members, slot, *ptr, *ptr_members; + + /* get base id */ + name = mrb_sym2name_len(mrb, mrb->ci->mid, &len); + mid = mrb_intern2(mrb, name, len-1); /* omit last "=" */ + + members = mrb_struct_members(mrb, obj); + ptr_members = RARRAY_PTR(members); + len = RARRAY_LEN(members); + ptr = RSTRUCT_PTR(obj); + for (i=0; itLAST_TOKEN) +#define is_local_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_LOCAL) +int +mrb_is_local_id(mrb_sym id) +{ + return is_local_id(id); +} + +#define is_const_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_CONST) +int +mrb_is_const_id(mrb_sym id) +{ + return is_const_id(id); +} + +static mrb_value +make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass) +{ + mrb_value nstr, *ptr_members; + mrb_sym id; + long i, len; + struct RClass *c; + + if (mrb_nil_p(name)) { + c = mrb_class_new(mrb, klass); + } + else { + /* old style: should we warn? */ + name = mrb_str_to_str(mrb, name); + id = mrb_to_id(mrb, name); + if (!mrb_is_const_id(id)) { + mrb_name_error(mrb, id, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name)); + } + if (mrb_const_defined_at(mrb, klass, id)) { + mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name)); + //?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); + } + c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); + } + MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT); + nstr = mrb_obj_value(c); + mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members); + + mrb_define_class_method(mrb, c, "new", mrb_instance_new, ARGS_ANY()); + mrb_define_class_method(mrb, c, "[]", mrb_instance_new, ARGS_ANY()); + mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, ARGS_NONE()); + //RSTRUCT(nstr)->basic.c->super = c->c; + ptr_members = RARRAY_PTR(members); + len = RARRAY_LEN(members); + for (i=0; i< len; i++) { + mrb_sym id = mrb_symbol(ptr_members[i]); + if (mrb_is_local_id(id) || mrb_is_const_id(id)) { + if (i < N_REF_FUNC) { + mrb_define_method_id(mrb, c, id, ref_func[i], ARGS_NONE()); + } + else { + mrb_define_method_id(mrb, c, id, mrb_struct_ref, ARGS_NONE()); + } + mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, ARGS_REQ(1)); + } + } + + return nstr; +} + +mrb_value +mrb_struct_define(mrb_state *mrb, const char *name, ...) +{ + va_list ar; + mrb_value nm, ary; + char *mem; + + if (!name) nm = mrb_nil_value(); + else nm = mrb_str_new2(mrb, name); + ary = mrb_ary_new(mrb); + + va_start(ar, name); + while ((mem = va_arg(ar, char*)) != 0) { + mrb_sym slot = mrb_intern(mrb, mem); + mrb_ary_push(mrb, ary, mrb_symbol_value(slot)); + } + va_end(ar); + + return make_struct(mrb, nm, ary, struct_class(mrb)); +} + +/* 15.2.18.3.1 */ +/* + * call-seq: + * Struct.new( [aString] [, aSym]+> ) -> StructClass + * StructClass.new(arg, ...) -> obj + * StructClass[arg, ...] -> obj + * + * Creates a new class, named by aString, containing accessor + * methods for the given symbols. If the name aString is + * omitted, an anonymous structure class will be created. Otherwise, + * the name of this struct will appear as a constant in class + * Struct, so it must be unique for all + * Structs 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. + * + * Struct::new returns a new Class 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 nil. Passing too many + * parameters will raise an ArgumentError. + * + * The remaining methods listed in this section (class and instance) + * are defined for this generated class. + * + * # Create a structure with a name in Struct + * Struct.new("Customer", :name, :address) #=> Struct::Customer + * Struct::Customer.new("Dave", "123 Main") #=> # + * + * # Create a structure named by its constant + * Customer = Struct.new(:name, :address) #=> Customer + * Customer.new("Dave", "123 Main") #=> # + */ +static mrb_value +mrb_struct_s_def(mrb_state *mrb, mrb_value klass) +{ + mrb_value name, rest; + mrb_value *pargv; + int argcnt; + long i; + mrb_value b, st; + mrb_sym id; + mrb_value *argv; + int argc; + + name = mrb_nil_value(); + rest = mrb_nil_value(); + mrb_get_args(mrb, "*&", &argv, &argc, &b); + if (argc == 0) { /* special case to avoid crash */ + rest = mrb_ary_new(mrb); + } + else { + if (argc > 0) name = argv[0]; + if (argc > 1) rest = argv[1]; + if (mrb_array_p(rest)) { + if (!mrb_nil_p(name) && mrb_symbol_p(name)) { + /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ + mrb_ary_unshift(mrb, rest, name); + name = mrb_nil_value(); + } + } + else { + pargv = &argv[1]; + argcnt = argc-1; + if (!mrb_nil_p(name) && mrb_symbol_p(name)) { + /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ + name = mrb_nil_value(); + pargv = &argv[0]; + argcnt++; + } + rest = mrb_ary_new_from_values(mrb, argcnt, pargv); + } + for (i=0; iptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n); + st->len = n; + struct_copy(st->ptr, argv, argc); + + return self; +} + +static mrb_value +mrb_struct_initialize_m(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value self) +{ + mrb_value *argv; + int argc; + + mrb_get_args(mrb, "*", &argv, &argc); + return mrb_struct_initialize_withArg(mrb, argc, argv, self); +} + +mrb_value +mrb_struct_initialize(mrb_state *mrb, mrb_value self, mrb_value values) +{ + return mrb_struct_initialize_withArg(mrb, RARRAY_LEN(values), RARRAY_PTR(values), self); +} + +static mrb_value +inspect_struct(mrb_state *mrb, mrb_value s, int recur) +{ + const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s)); + mrb_value members, str = mrb_str_new(mrb, "#"); + } + + members = mrb_struct_members(mrb, s); + ptr_members = RARRAY_PTR(members); + ptr = RSTRUCT_PTR(s); + len = RSTRUCT_LEN(s); + for (i=0; i 0) { + mrb_str_cat2(mrb, str, ", "); + } + else if (cn) { + mrb_str_cat2(mrb, str, " "); + } + slot = ptr_members[i]; + id = mrb_symbol(slot); + if (mrb_is_local_id(id) || mrb_is_const_id(id)) { + const char *name; + int len; + + name = mrb_sym2name_len(mrb, id, &len); + mrb_str_append(mrb, str, mrb_str_new(mrb, name, len)); + } + else { + mrb_str_append(mrb, str, mrb_inspect(mrb, slot)); + } + mrb_str_cat2(mrb, str, "="); + mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i])); + } + mrb_str_cat2(mrb, str, ">"); + + return str; +} + +/* + * call-seq: + * struct.to_s -> string + * struct.inspect -> string + * + * Describe the contents of this struct in a string. + */ +static mrb_value +mrb_struct_inspect(mrb_state *mrb, mrb_value s) +{ + return inspect_struct(mrb, s, 0); +} + +/* 15.2.18.4.9 */ +/* :nodoc: */ +mrb_value +mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) +{ + mrb_value s; + + mrb_get_args(mrb, "o", &s); + + 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 (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { + mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); + } + struct_copy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), RSTRUCT_LEN(copy)); + + return copy; +} + +static mrb_value +mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id) +{ + mrb_value *ptr, members, *ptr_members; + long i, len; + + ptr = RSTRUCT_PTR(s); + members = mrb_struct_members(mrb, s); + ptr_members = RARRAY_PTR(members); + len = RARRAY_LEN(members); + for (i=0; i anObject + * struct[fixnum] -> anObject + * + * Attribute Reference---Returns the value of the instance variable + * named by symbol, or indexed (0..length-1) by + * fixnum. Will raise NameError if the named + * variable does not exist, or IndexError if the index is + * out of range. + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * + * joe["name"] #=> "Joe Smith" + * joe[:name] #=> "Joe Smith" + * joe[0] #=> "Joe Smith" + */ +mrb_value +mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx) +{ + long i; + + if (mrb_string_p(idx) || mrb_symbol_p(idx)) { + return mrb_struct_aref_id(mrb, s, mrb_to_id(mrb, idx)); + } + + i = mrb_fixnum(idx); + if (i < 0) i = RSTRUCT_LEN(s) + i; + if (i < 0) + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + if (RSTRUCT_LEN(s) <= i) + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + return RSTRUCT_PTR(s)[i]; +} + +mrb_value +mrb_struct_aref(mrb_state *mrb, mrb_value s) +{ + mrb_value idx; + + mrb_get_args(mrb, "o", &idx); + return mrb_struct_aref_n(mrb, s, idx); +} + +static mrb_value +mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) +{ + mrb_value members, *ptr, *ptr_members; + long i, len; + + members = mrb_struct_members(mrb, s); + len = RARRAY_LEN(members); + if (RSTRUCT_LEN(s) != len) { + mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", + len, RSTRUCT_LEN(s)); + } + ptr = RSTRUCT_PTR(s); + ptr_members = RARRAY_PTR(members); + for (i=0; i obj + * struct[fixnum] = obj -> obj + * + * Attribute Assignment---Assigns to the instance variable named by + * symbol or fixnum the value obj and + * returns it. Will raise a NameError if the named + * variable does not exist, or an IndexError if the index + * is out of range. + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * + * joe["name"] = "Luke" + * joe[:zip] = "90210" + * + * joe.name #=> "Luke" + * joe.zip #=> "90210" + */ + +mrb_value +mrb_struct_aset(mrb_state *mrb, mrb_value s) +{ + long i; + mrb_value idx; + mrb_value val; + + mrb_get_args(mrb, "oo", &idx, &val); + + if (mrb_string_p(idx) || mrb_symbol_p(idx)) { + return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val); + } + + i = mrb_fixnum(idx); + if (i < 0) i = RSTRUCT_LEN(s) + i; + if (i < 0) { + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + } + if (RSTRUCT_LEN(s) <= i) { + mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + } + return RSTRUCT_PTR(s)[i] = val; +} + +/* 15.2.18.4.1 */ +/* + * call-seq: + * struct == other_struct -> true or false + * + * Equality---Returns true if other_struct is + * equal to this one: they must be of the same class as generated by + * Struct::new, and the values of all instance variables + * must be equal (according to Object#==). + * + * Customer = Struct.new(:name, :address, :zip) + * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) + * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345) + * joe == joejr #=> true + * joe == jane #=> false + */ + +static mrb_value +mrb_struct_equal(mrb_state *mrb, mrb_value s) +{ + mrb_value s2; + mrb_value *ptr, *ptr2; + long i, len; + + mrb_get_args(mrb, "o", &s2); + if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); + if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); + if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); + if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { + mrb_bug("inconsistent struct"); /* should never happen */ + } + ptr = RSTRUCT_PTR(s); + ptr2 = RSTRUCT_PTR(s2); + len = RSTRUCT_LEN(s); + for (i=0; i true or false + * + * Two structures are equal if they are the same object, or if all their + * fields are equal (using eql?). + */ +static mrb_value +mrb_struct_eql(mrb_state *mrb, mrb_value s) +{ + mrb_value s2; + mrb_value *ptr, *ptr2; + long i, len; + + mrb_get_args(mrb, "o", &s2); + if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); + if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); + if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); + if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { + mrb_bug("inconsistent struct"); /* should never happen */ + } + + ptr = RSTRUCT_PTR(s); + ptr2 = RSTRUCT_PTR(s2); + len = RSTRUCT_LEN(s); + for (i=0; iStruct is a convenient way to bundle a number of + * attributes together, using accessor methods, without having to write + * an explicit class. + * + * The Struct 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 + * ``CustomerClass,'' and we'll show an example instance of that + * class as ``CustomerInst.'' + * + * In the descriptions that follow, the parameter symbol refers + * to a symbol, which is either a quoted string or a + * Symbol (such as :name). + */ +void +mrb_init_struct(mrb_state *mrb) +{ + struct RClass *st; + st = mrb_define_class(mrb, "Struct", mrb->object_class); + + mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, ARGS_ANY()); /* 15.2.18.3.1 */ + + mrb_define_method(mrb, st, "==", mrb_struct_equal, ARGS_REQ(1)); /* 15.2.18.4.1 */ + mrb_define_method(mrb, st, "[]", mrb_struct_aref, ARGS_REQ(1)); /* 15.2.18.4.2 */ + mrb_define_method(mrb, st, "[]=", mrb_struct_aset, ARGS_REQ(2)); /* 15.2.18.4.3 */ + mrb_define_method(mrb, st, "members", mrb_struct_members_m, ARGS_NONE()); /* 15.2.18.4.6 */ + mrb_define_method(mrb, st, "initialize", mrb_struct_initialize_m,ARGS_ANY()); /* 15.2.18.4.8 */ + mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, ARGS_REQ(1)); /* 15.2.18.4.9 */ + mrb_define_method(mrb, st, "inspect", mrb_struct_inspect, ARGS_NONE()); /* 15.2.18.4.10(x) */ + mrb_define_alias(mrb, st, "to_s", "inspect"); /* 15.2.18.4.11(x) */ + mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */ + +} diff --git a/mrbgems/mruby-struct/test/struct.rb b/mrbgems/mruby-struct/test/struct.rb new file mode 100644 index 000000000..d79b30c0e --- /dev/null +++ b/mrbgems/mruby-struct/test/struct.rb @@ -0,0 +1,77 @@ +## +# Struct ISO Test + +if Object.const_defined?(:Struct) + assert('Struct', '15.2.18') do + Struct.class == Class + end + + assert('Struct superclass', '15.2.18.2') do + Struct.superclass == Object + end + + assert('Struct.new', '15.2.18.3.1') do + c = Struct.new(:m1, :m2) + c.superclass == Struct and + c.members == [:m1,:m2] + end + + # Check crash bug with Struc.new and no params. + assert('Struct.new', '15.2.18.3.1') do + c = Struct.new() + c.superclass == Struct and c.members == [] + end + + assert('Struct#==', '15.2.18.4.1') do + c = Struct.new(:m1, :m2) + cc1 = c.new(1,2) + cc2 = c.new(1,2) + cc1 == cc2 + end + + assert('Struct#[]', '15.2.18.4.2') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc[:m1] == 1 and cc["m2"] == 2 + end + + assert('Struct#[]=', '15.2.18.4.3') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc[:m1] = 3 + cc[:m1] == 3 + end + + assert('Struct#each', '15.2.18.4.4') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + a = [] + cc.each{|x| + a << x + } + a[0] == 1 and a[1] == 2 + end + + assert('Struct#each_pair', '15.2.18.4.5') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + a = [] + cc.each_pair{|k,v| + a << [k,v] + } + a[0] == [:m1, 1] and a[1] == [:m2, 2] + end + + assert('Struct#members', '15.2.18.4.6') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc.members == [:m1,:m2] + end + + assert('Struct#select', '15.2.18.4.7') do + c = Struct.new(:m1, :m2) + cc = c.new(1,2) + cc.select{|v| v % 2 == 0} == [2] + end +end + diff --git a/src/etc.c b/src/etc.c index 6a43ddd31..644465c80 100644 --- a/src/etc.c +++ b/src/etc.c @@ -170,7 +170,6 @@ mrb_obj_id(mrb_value obj) case MRB_TT_ARRAY: case MRB_TT_HASH: case MRB_TT_RANGE: - case MRB_TT_STRUCT: case MRB_TT_EXCEPTION: case MRB_TT_FILE: case MRB_TT_DATA: diff --git a/src/gc.c b/src/gc.c index 5cc794fd9..c48e6949a 100644 --- a/src/gc.c +++ b/src/gc.c @@ -86,9 +86,6 @@ typedef struct { struct RArray array; struct RHash hash; struct RRange range; -#ifdef ENABLE_STRUCT - struct RStruct strct; -#endif struct RData data; struct RProc proc; } as; diff --git a/src/init.c b/src/init.c index fa2d5d305..0d1a24881 100644 --- a/src/init.c +++ b/src/init.c @@ -20,7 +20,6 @@ void mrb_init_array(mrb_state*); void mrb_init_hash(mrb_state*); void mrb_init_numeric(mrb_state*); void mrb_init_range(mrb_state*); -void mrb_init_struct(mrb_state*); void mrb_init_gc(mrb_state*); void mrb_init_print(mrb_state*); void mrb_init_math(mrb_state*); @@ -48,9 +47,6 @@ mrb_init_core(mrb_state *mrb) mrb_init_hash(mrb); DONE; mrb_init_numeric(mrb); DONE; mrb_init_range(mrb); DONE; -#ifdef ENABLE_STRUCT - mrb_init_struct(mrb); DONE; -#endif mrb_init_gc(mrb); DONE; #ifdef ENABLE_STDIO mrb_init_print(mrb); DONE; diff --git a/src/object.c b/src/object.c index e087c35c0..6707fc6e4 100644 --- a/src/object.c +++ b/src/object.c @@ -379,7 +379,6 @@ static const struct types { {MRB_TT_HASH, "Hash"}, {MRB_TT_STRING, "String"}, {MRB_TT_RANGE, "Range"}, - {MRB_TT_STRUCT, "Struct"}, // {MRB_TT_BIGNUM, "Bignum"}, {MRB_TT_FILE, "File"}, {MRB_TT_DATA, "Data"}, /* internal use: wrapped C pointers */ diff --git a/src/struct.c b/src/struct.c deleted file mode 100644 index d7b63259e..000000000 --- a/src/struct.c +++ /dev/null @@ -1,781 +0,0 @@ -/* -** struct.c - Struct class -** -** See Copyright Notice in mruby.h -*/ - -#include "mruby.h" -#ifdef ENABLE_STRUCT -#include -#include "error.h" -#include "mruby/struct.h" -#include "mruby/array.h" -#include - -#include "mruby/string.h" -#include "mruby/class.h" -#include "mruby/variable.h" - - -static struct RClass * -struct_class(mrb_state *mrb) -{ - return mrb_class_get(mrb, "Struct"); -} - -static inline mrb_value -struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id) -{ - struct RClass* kclass; - struct RClass* sclass = struct_class(mrb); - - mrb_value ans; - for (;;) { - ans = mrb_iv_get(mrb, c, id); - if (!mrb_nil_p(ans)) return ans; - kclass = RCLASS_SUPER(c); - if (kclass == 0 || kclass == sclass) - return mrb_nil_value(); - c = mrb_obj_value(kclass); - } -} - -mrb_value -mrb_struct_iv_get(mrb_state *mrb, mrb_value c, const char *name) -{ - return struct_ivar_get(mrb, c, mrb_intern(mrb, name)); -} - -mrb_value -mrb_struct_s_members(mrb_state *mrb, mrb_value klass) -{ - mrb_value members = struct_ivar_get(mrb, klass, mrb_intern(mrb, "__members__")); - - if (mrb_nil_p(members)) { - mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct"); - } - if (!mrb_array_p(members)) { - mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); - } - return members; -} - -mrb_value -mrb_struct_members(mrb_state *mrb, mrb_value s) -{ - mrb_value members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_obj_class(mrb, s))); - if (mrb_type(s) == MRB_TT_STRUCT) { - if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { - mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", - RARRAY_LEN(members), RSTRUCT_LEN(s)); - } - } - return members; -} - -static mrb_value -mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) -{ - mrb_value members, ary; - mrb_value *p, *pend; - - members = mrb_struct_s_members(mrb, klass); - ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members)); - p = RARRAY_PTR(members); pend = p + RARRAY_LEN(members); - while (p < pend) { - mrb_ary_push(mrb, ary, *p); - p++; - } - - return ary; -} - -static inline void -struct_copy(mrb_value *dst, const mrb_value *src, size_t size) -{ - size_t i; - - for (i = 0; i < size; i++) { - dst[i] = src[i]; - } -} - -/* 15.2.18.4.6 */ -/* - * call-seq: - * struct.members -> array - * - * Returns an array of strings representing the names of the instance - * variables. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * joe.members #=> [:name, :address, :zip] - */ - -static mrb_value -mrb_struct_members_m(mrb_state *mrb, mrb_value obj) -{ - return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj))); -} - -mrb_value -mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id) -{ - mrb_value members, slot, *ptr, *ptr_members; - long i, len; - - ptr = RSTRUCT_PTR(obj); - members = mrb_struct_members(mrb, obj); - ptr_members = RARRAY_PTR(members); - slot = mrb_symbol_value(id); - len = RARRAY_LEN(members); - for (i=0; ici->mid); -} - -static mrb_value mrb_struct_ref0(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[0];} -static mrb_value mrb_struct_ref1(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[1];} -static mrb_value mrb_struct_ref2(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[2];} -static mrb_value mrb_struct_ref3(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[3];} -static mrb_value mrb_struct_ref4(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[4];} -static mrb_value mrb_struct_ref5(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[5];} -static mrb_value mrb_struct_ref6(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[6];} -static mrb_value mrb_struct_ref7(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[7];} -static mrb_value mrb_struct_ref8(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[8];} -static mrb_value mrb_struct_ref9(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[9];} - -#define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) -#define N_REF_FUNC numberof(ref_func) - -static mrb_value (*const ref_func[])(mrb_state*, mrb_value) = { - mrb_struct_ref0, - mrb_struct_ref1, - mrb_struct_ref2, - mrb_struct_ref3, - mrb_struct_ref4, - mrb_struct_ref5, - mrb_struct_ref6, - mrb_struct_ref7, - mrb_struct_ref8, - mrb_struct_ref9, -}; - -mrb_sym -mrb_id_attrset(mrb_state *mrb, mrb_sym id) -{ - const char *name; - char *buf; - int len; - mrb_sym mid; - - name = mrb_sym2name_len(mrb, id, &len); - buf = (char *)mrb_malloc(mrb, len+2); - memcpy(buf, name, len); - buf[len] = '='; - buf[len+1] = '\0'; - - mid = mrb_intern2(mrb, buf, len+1); - mrb_free(mrb, buf); - return mid; -} - -static mrb_value -mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val) -{ - const char *name; - int i, len; - mrb_sym mid; - mrb_value members, slot, *ptr, *ptr_members; - - /* get base id */ - name = mrb_sym2name_len(mrb, mrb->ci->mid, &len); - mid = mrb_intern2(mrb, name, len-1); /* omit last "=" */ - - members = mrb_struct_members(mrb, obj); - ptr_members = RARRAY_PTR(members); - len = RARRAY_LEN(members); - ptr = RSTRUCT_PTR(obj); - for (i=0; itLAST_TOKEN) -#define is_local_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_LOCAL) -int -mrb_is_local_id(mrb_sym id) -{ - return is_local_id(id); -} - -#define is_const_id(id) (is_notop_id(id))//&&((id)&ID_SCOPE_MASK)==ID_CONST) -int -mrb_is_const_id(mrb_sym id) -{ - return is_const_id(id); -} - -static mrb_value -make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass) -{ - mrb_value nstr, *ptr_members; - mrb_sym id; - long i, len; - struct RClass *c; - - if (mrb_nil_p(name)) { - c = mrb_class_new(mrb, klass); - } - else { - /* old style: should we warn? */ - name = mrb_str_to_str(mrb, name); - id = mrb_to_id(mrb, name); - if (!mrb_is_const_id(id)) { - mrb_name_error(mrb, id, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name)); - } - if (mrb_const_defined_at(mrb, klass, id)) { - mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name)); - //?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); - } - c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); - } - MRB_SET_INSTANCE_TT(c, MRB_TT_STRUCT); - nstr = mrb_obj_value(c); - mrb_iv_set(mrb, nstr, mrb_intern(mrb, "__members__"), members); - - mrb_define_class_method(mrb, c, "new", mrb_instance_new, ARGS_ANY()); - mrb_define_class_method(mrb, c, "[]", mrb_instance_new, ARGS_ANY()); - mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, ARGS_NONE()); - //RSTRUCT(nstr)->basic.c->super = c->c; - ptr_members = RARRAY_PTR(members); - len = RARRAY_LEN(members); - for (i=0; i< len; i++) { - mrb_sym id = mrb_symbol(ptr_members[i]); - if (mrb_is_local_id(id) || mrb_is_const_id(id)) { - if (i < N_REF_FUNC) { - mrb_define_method_id(mrb, c, id, ref_func[i], ARGS_NONE()); - } - else { - mrb_define_method_id(mrb, c, id, mrb_struct_ref, ARGS_NONE()); - } - mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, ARGS_REQ(1)); - } - } - - return nstr; -} - -mrb_value -mrb_struct_define(mrb_state *mrb, const char *name, ...) -{ - va_list ar; - mrb_value nm, ary; - char *mem; - - if (!name) nm = mrb_nil_value(); - else nm = mrb_str_new2(mrb, name); - ary = mrb_ary_new(mrb); - - va_start(ar, name); - while ((mem = va_arg(ar, char*)) != 0) { - mrb_sym slot = mrb_intern(mrb, mem); - mrb_ary_push(mrb, ary, mrb_symbol_value(slot)); - } - va_end(ar); - - return make_struct(mrb, nm, ary, struct_class(mrb)); -} - -/* 15.2.18.3.1 */ -/* - * call-seq: - * Struct.new( [aString] [, aSym]+> ) -> StructClass - * StructClass.new(arg, ...) -> obj - * StructClass[arg, ...] -> obj - * - * Creates a new class, named by aString, containing accessor - * methods for the given symbols. If the name aString is - * omitted, an anonymous structure class will be created. Otherwise, - * the name of this struct will appear as a constant in class - * Struct, so it must be unique for all - * Structs 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. - * - * Struct::new returns a new Class 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 nil. Passing too many - * parameters will raise an ArgumentError. - * - * The remaining methods listed in this section (class and instance) - * are defined for this generated class. - * - * # Create a structure with a name in Struct - * Struct.new("Customer", :name, :address) #=> Struct::Customer - * Struct::Customer.new("Dave", "123 Main") #=> # - * - * # Create a structure named by its constant - * Customer = Struct.new(:name, :address) #=> Customer - * Customer.new("Dave", "123 Main") #=> # - */ -static mrb_value -mrb_struct_s_def(mrb_state *mrb, mrb_value klass) -{ - mrb_value name, rest; - mrb_value *pargv; - int argcnt; - long i; - mrb_value b, st; - mrb_sym id; - mrb_value *argv; - int argc; - - name = mrb_nil_value(); - rest = mrb_nil_value(); - mrb_get_args(mrb, "*&", &argv, &argc, &b); - if (argc == 0) { /* special case to avoid crash */ - rest = mrb_ary_new(mrb); - } - else { - if (argc > 0) name = argv[0]; - if (argc > 1) rest = argv[1]; - if (mrb_array_p(rest)) { - if (!mrb_nil_p(name) && mrb_symbol_p(name)) { - /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ - mrb_ary_unshift(mrb, rest, name); - name = mrb_nil_value(); - } - } - else { - pargv = &argv[1]; - argcnt = argc-1; - if (!mrb_nil_p(name) && mrb_symbol_p(name)) { - /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ - name = mrb_nil_value(); - pargv = &argv[0]; - argcnt++; - } - rest = mrb_ary_new_from_values(mrb, argcnt, pargv); - } - for (i=0; iptr = (mrb_value *)mrb_calloc(mrb, sizeof(mrb_value), n); - st->len = n; - struct_copy(st->ptr, argv, argc); - - return self; -} - -static mrb_value -mrb_struct_initialize_m(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value self) -{ - mrb_value *argv; - int argc; - - mrb_get_args(mrb, "*", &argv, &argc); - return mrb_struct_initialize_withArg(mrb, argc, argv, self); -} - -mrb_value -mrb_struct_initialize(mrb_state *mrb, mrb_value self, mrb_value values) -{ - return mrb_struct_initialize_withArg(mrb, RARRAY_LEN(values), RARRAY_PTR(values), self); -} - -static mrb_value -inspect_struct(mrb_state *mrb, mrb_value s, int recur) -{ - const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s)); - mrb_value members, str = mrb_str_new(mrb, "#"); - } - - members = mrb_struct_members(mrb, s); - ptr_members = RARRAY_PTR(members); - ptr = RSTRUCT_PTR(s); - len = RSTRUCT_LEN(s); - for (i=0; i 0) { - mrb_str_cat2(mrb, str, ", "); - } - else if (cn) { - mrb_str_cat2(mrb, str, " "); - } - slot = ptr_members[i]; - id = mrb_symbol(slot); - if (mrb_is_local_id(id) || mrb_is_const_id(id)) { - const char *name; - int len; - - name = mrb_sym2name_len(mrb, id, &len); - mrb_str_append(mrb, str, mrb_str_new(mrb, name, len)); - } - else { - mrb_str_append(mrb, str, mrb_inspect(mrb, slot)); - } - mrb_str_cat2(mrb, str, "="); - mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i])); - } - mrb_str_cat2(mrb, str, ">"); - - return str; -} - -/* - * call-seq: - * struct.to_s -> string - * struct.inspect -> string - * - * Describe the contents of this struct in a string. - */ -static mrb_value -mrb_struct_inspect(mrb_state *mrb, mrb_value s) -{ - return inspect_struct(mrb, s, 0); -} - -/* 15.2.18.4.9 */ -/* :nodoc: */ -mrb_value -mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) -{ - mrb_value s; - - mrb_get_args(mrb, "o", &s); - - 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 (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { - mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); - } - struct_copy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), RSTRUCT_LEN(copy)); - - return copy; -} - -static mrb_value -mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id) -{ - mrb_value *ptr, members, *ptr_members; - long i, len; - - ptr = RSTRUCT_PTR(s); - members = mrb_struct_members(mrb, s); - ptr_members = RARRAY_PTR(members); - len = RARRAY_LEN(members); - for (i=0; i anObject - * struct[fixnum] -> anObject - * - * Attribute Reference---Returns the value of the instance variable - * named by symbol, or indexed (0..length-1) by - * fixnum. Will raise NameError if the named - * variable does not exist, or IndexError if the index is - * out of range. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * - * joe["name"] #=> "Joe Smith" - * joe[:name] #=> "Joe Smith" - * joe[0] #=> "Joe Smith" - */ -mrb_value -mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx) -{ - long i; - - if (mrb_string_p(idx) || mrb_symbol_p(idx)) { - return mrb_struct_aref_id(mrb, s, mrb_to_id(mrb, idx)); - } - - i = mrb_fixnum(idx); - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - if (RSTRUCT_LEN(s) <= i) - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - return RSTRUCT_PTR(s)[i]; -} - -mrb_value -mrb_struct_aref(mrb_state *mrb, mrb_value s) -{ - mrb_value idx; - - mrb_get_args(mrb, "o", &idx); - return mrb_struct_aref_n(mrb, s, idx); -} - -static mrb_value -mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) -{ - mrb_value members, *ptr, *ptr_members; - long i, len; - - members = mrb_struct_members(mrb, s); - len = RARRAY_LEN(members); - if (RSTRUCT_LEN(s) != len) { - mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%ld required %ld given)", - len, RSTRUCT_LEN(s)); - } - ptr = RSTRUCT_PTR(s); - ptr_members = RARRAY_PTR(members); - for (i=0; i obj - * struct[fixnum] = obj -> obj - * - * Attribute Assignment---Assigns to the instance variable named by - * symbol or fixnum the value obj and - * returns it. Will raise a NameError if the named - * variable does not exist, or an IndexError if the index - * is out of range. - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * - * joe["name"] = "Luke" - * joe[:zip] = "90210" - * - * joe.name #=> "Luke" - * joe.zip #=> "90210" - */ - -mrb_value -mrb_struct_aset(mrb_state *mrb, mrb_value s) -{ - long i; - mrb_value idx; - mrb_value val; - - mrb_get_args(mrb, "oo", &idx, &val); - - if (mrb_string_p(idx) || mrb_symbol_p(idx)) { - return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val); - } - - i = mrb_fixnum(idx); - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) { - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - } - if (RSTRUCT_LEN(s) <= i) { - mrb_raisef(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - } - return RSTRUCT_PTR(s)[i] = val; -} - -/* 15.2.18.4.1 */ -/* - * call-seq: - * struct == other_struct -> true or false - * - * Equality---Returns true if other_struct is - * equal to this one: they must be of the same class as generated by - * Struct::new, and the values of all instance variables - * must be equal (according to Object#==). - * - * Customer = Struct.new(:name, :address, :zip) - * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) - * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345) - * joe == joejr #=> true - * joe == jane #=> false - */ - -static mrb_value -mrb_struct_equal(mrb_state *mrb, mrb_value s) -{ - mrb_value s2; - mrb_value *ptr, *ptr2; - long i, len; - - mrb_get_args(mrb, "o", &s2); - if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); - if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); - if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); - if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { - mrb_bug("inconsistent struct"); /* should never happen */ - } - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - for (i=0; i true or false - * - * Two structures are equal if they are the same object, or if all their - * fields are equal (using eql?). - */ -static mrb_value -mrb_struct_eql(mrb_state *mrb, mrb_value s) -{ - mrb_value s2; - mrb_value *ptr, *ptr2; - long i, len; - - mrb_get_args(mrb, "o", &s2); - if (mrb_obj_equal(mrb, s, s2)) return mrb_true_value(); - if (mrb_type(s2) != MRB_TT_STRUCT) return mrb_false_value(); - if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) return mrb_false_value(); - if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { - mrb_bug("inconsistent struct"); /* should never happen */ - } - - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - for (i=0; iStruct is a convenient way to bundle a number of - * attributes together, using accessor methods, without having to write - * an explicit class. - * - * The Struct 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 - * ``CustomerClass,'' and we'll show an example instance of that - * class as ``CustomerInst.'' - * - * In the descriptions that follow, the parameter symbol refers - * to a symbol, which is either a quoted string or a - * Symbol (such as :name). - */ -void -mrb_init_struct(mrb_state *mrb) -{ - struct RClass *st; - st = mrb_define_class(mrb, "Struct", mrb->object_class); - - mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, ARGS_ANY()); /* 15.2.18.3.1 */ - - mrb_define_method(mrb, st, "==", mrb_struct_equal, ARGS_REQ(1)); /* 15.2.18.4.1 */ - mrb_define_method(mrb, st, "[]", mrb_struct_aref, ARGS_REQ(1)); /* 15.2.18.4.2 */ - mrb_define_method(mrb, st, "[]=", mrb_struct_aset, ARGS_REQ(2)); /* 15.2.18.4.3 */ - mrb_define_method(mrb, st, "members", mrb_struct_members_m, ARGS_NONE()); /* 15.2.18.4.6 */ - mrb_define_method(mrb, st, "initialize", mrb_struct_initialize_m,ARGS_ANY()); /* 15.2.18.4.8 */ - mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, ARGS_REQ(1)); /* 15.2.18.4.9 */ - mrb_define_method(mrb, st, "inspect", mrb_struct_inspect, ARGS_NONE()); /* 15.2.18.4.10(x) */ - mrb_define_alias(mrb, st, "to_s", "inspect"); /* 15.2.18.4.11(x) */ - mrb_define_method(mrb, st, "eql?", mrb_struct_eql, ARGS_REQ(1)); /* 15.2.18.4.12(x) */ - -} -#endif /* ENABLE_STRUCT */ diff --git a/test/t/struct.rb b/test/t/struct.rb deleted file mode 100644 index d79b30c0e..000000000 --- a/test/t/struct.rb +++ /dev/null @@ -1,77 +0,0 @@ -## -# Struct ISO Test - -if Object.const_defined?(:Struct) - assert('Struct', '15.2.18') do - Struct.class == Class - end - - assert('Struct superclass', '15.2.18.2') do - Struct.superclass == Object - end - - assert('Struct.new', '15.2.18.3.1') do - c = Struct.new(:m1, :m2) - c.superclass == Struct and - c.members == [:m1,:m2] - end - - # Check crash bug with Struc.new and no params. - assert('Struct.new', '15.2.18.3.1') do - c = Struct.new() - c.superclass == Struct and c.members == [] - end - - assert('Struct#==', '15.2.18.4.1') do - c = Struct.new(:m1, :m2) - cc1 = c.new(1,2) - cc2 = c.new(1,2) - cc1 == cc2 - end - - assert('Struct#[]', '15.2.18.4.2') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc[:m1] == 1 and cc["m2"] == 2 - end - - assert('Struct#[]=', '15.2.18.4.3') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc[:m1] = 3 - cc[:m1] == 3 - end - - assert('Struct#each', '15.2.18.4.4') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - a = [] - cc.each{|x| - a << x - } - a[0] == 1 and a[1] == 2 - end - - assert('Struct#each_pair', '15.2.18.4.5') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - a = [] - cc.each_pair{|k,v| - a << [k,v] - } - a[0] == [:m1, 1] and a[1] == [:m2, 2] - end - - assert('Struct#members', '15.2.18.4.6') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc.members == [:m1,:m2] - end - - assert('Struct#select', '15.2.18.4.7') do - c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc.select{|v| v % 2 == 0} == [2] - end -end - -- cgit v1.2.3