diff options
Diffstat (limited to 'mrbgems/mruby-struct')
| -rw-r--r-- | mrbgems/mruby-struct/mrblib/struct.rb | 123 | ||||
| -rw-r--r-- | mrbgems/mruby-struct/src/struct.c | 519 | ||||
| -rw-r--r-- | mrbgems/mruby-struct/test/struct.rb | 119 |
3 files changed, 362 insertions, 399 deletions
diff --git a/mrbgems/mruby-struct/mrblib/struct.rb b/mrbgems/mruby-struct/mrblib/struct.rb index 5d0ede90f..2439e2a37 100644 --- a/mrbgems/mruby-struct/mrblib/struct.rb +++ b/mrbgems/mruby-struct/mrblib/struct.rb @@ -2,49 +2,96 @@ # Struct # # ISO 15.2.18 +class Struct -if Object.const_defined?(:Struct) - class Struct + ## + # Calls the given block for each element of +self+ + # and pass the respective element. + # + # ISO 15.2.18.4.4 + def each(&block) + self.class.members.each{|field| + block.call(self[field]) + } + self + end - ## - # Calls the given block for each element of +self+ - # and pass the respective element. - # - # ISO 15.2.18.4.4 - def each(&block) - self.class.members.each{|field| - block.call(self[field]) - } - self - end + ## + # Calls the given block for each element of +self+ + # and pass the name and value of the respective + # element. + # + # ISO 15.2.18.4.5 + def each_pair(&block) + self.class.members.each{|field| + block.call(field.to_sym, self[field]) + } + self + end - ## - # Calls the given block for each element of +self+ - # and pass the name and value of the respectiev - # element. - # - # ISO 15.2.18.4.5 - def each_pair(&block) - self.class.members.each{|field| - block.call(field.to_sym, self[field]) - } - self + ## + # Calls the given block for each element of +self+ + # and returns an array with all elements of which + # block is not false. + # + # ISO 15.2.18.4.7 + def select(&block) + ary = [] + self.class.members.each{|field| + val = self[field] + ary.push(val) if block.call(val) + } + ary + end + + def _inspect(recur_list) + return "#<struct #{self.class}:...>" if recur_list[self.object_id] + recur_list[self.object_id] = true + name = self.class.to_s + if name[0] == "#" + str = "#<struct " + else + str = "#<struct #{name} " + end + buf = [] + self.each_pair do |k,v| + buf.push k.to_s + "=" + v._inspect(recur_list) end + str + buf.join(", ") + ">" + end + + ## + # call-seq: + # struct.to_s -> string + # struct.inspect -> string + # + # Describe the contents of this struct in a string. + # + # 15.2.18.4.10(x) + # + def inspect + self._inspect({}) + end - ## - # Calls the given block for each element of +self+ - # and returns an array with all elements of which - # block is not false. - # - # ISO 15.2.18.4.7 - def select(&block) - ary = [] - self.class.members.each{|field| - val = self[field] - ary.push(val) if block.call(val) - } - ary + ## + # 15.2.18.4.11(x) + # + alias to_s inspect + + ## + # call-seq: + # hsh.dig(key,...) -> object + # + # Extracts the nested value specified by the sequence of <i>key</i> + # objects by calling +dig+ at each step, returning +nil+ if any + # intermediate step is +nil+. + # + def dig(idx,*args) + n = self[idx] + if args.size > 0 + n&.dig(*args) + else + n end end end - diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index 420052e4d..df76d7a33 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -4,46 +4,46 @@ ** See Copyright Notice in mruby.h */ -#include <ctype.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/range.h" - -#define RSTRUCT_LEN(st) mrb_ary_ptr(st)->len -#define RSTRUCT_PTR(st) mrb_ary_ptr(st)->ptr +#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/range.h> +#include <mruby/proc.h> +#include <mruby/presym.h> + +#define RSTRUCT_LEN(st) RARRAY_LEN(st) +#define RSTRUCT_PTR(st) RARRAY_PTR(st) static struct RClass * struct_class(mrb_state *mrb) { - return mrb_class_get(mrb, "Struct"); + return mrb_class_get_id(mrb, MRB_SYM(Struct)); } static inline mrb_value -struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id) +struct_ivar_get(mrb_state *mrb, mrb_value cls, mrb_sym id) { - struct RClass* kclass; + struct RClass* c = mrb_class_ptr(cls); struct RClass* sclass = struct_class(mrb); mrb_value ans; for (;;) { - ans = mrb_iv_get(mrb, c, id); + ans = mrb_iv_get(mrb, mrb_obj_value(c), id); if (!mrb_nil_p(ans)) return ans; - kclass = RCLASS_SUPER(c); - if (kclass == 0 || kclass == sclass) + c = c->super; + if (c == sclass || c == 0) return mrb_nil_value(); - c = mrb_obj_value(kclass); } } static mrb_value -mrb_struct_s_members(mrb_state *mrb, mrb_value klass) +struct_s_members(mrb_state *mrb, struct RClass *klass) { - mrb_value members = struct_ivar_get(mrb, klass, mrb_intern_lit(mrb, "__members__")); + mrb_value members = struct_ivar_get(mrb, mrb_obj_value(klass), MRB_SYM(__members__)); if (mrb_nil_p(members)) { mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct"); @@ -55,14 +55,20 @@ mrb_struct_s_members(mrb_state *mrb, mrb_value klass) } static mrb_value -mrb_struct_members(mrb_state *mrb, mrb_value s) +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 (!strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s)), "Struct")) { - if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { + mrb_value members = struct_s_members(mrb, mrb_obj_class(mrb, s)); + if (!mrb_array_p(s)) { + 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 (%S required %S given)", - mrb_fixnum_value(RARRAY_LEN(members)), mrb_fixnum_value(RSTRUCT_LEN(s))); + "struct size differs (%i required %i given)", + RARRAY_LEN(members), RSTRUCT_LEN(s)); } } return members; @@ -73,12 +79,19 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) { mrb_value members, ary; - members = mrb_struct_s_members(mrb, klass); + members = struct_s_members(mrb, mrb_class_ptr(klass)); ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members)); mrb_ary_replace(mrb, ary, members); return ary; } +static void +mrb_struct_modify(mrb_state *mrb, mrb_value strct) +{ + mrb_check_frozen(mrb, mrb_basic_ptr(strct)); + mrb_write_barrier(mrb, mrb_basic_ptr(strct)); +} + /* 15.2.18.4.6 */ /* * call-seq: @@ -93,132 +106,66 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass) */ static mrb_value -mrb_struct_members_m(mrb_state *mrb, mrb_value obj) +mrb_struct_members(mrb_state *mrb, mrb_value obj) { return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj))); } static mrb_value -mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id) -{ - mrb_value members, slot, *ptr; - const mrb_value *ptr_members; - mrb_int 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; i<len; i++) { - if (mrb_obj_equal(mrb, ptr_members[i], slot)) { - return ptr[i]; - } - } - mrb_raisef(mrb, E_INDEX_ERROR, "`%S' is not a struct member", mrb_sym2str(mrb, id)); - return mrb_nil_value(); /* not reached */ -} - -static mrb_value mrb_struct_ref(mrb_state *mrb, mrb_value obj) { - return mrb_struct_getmember(mrb, obj, mrb->c->ci->mid); -} + mrb_int i = mrb_integer(mrb_proc_cfunc_env_get(mrb, 0)); + mrb_value *ptr = RSTRUCT_PTR(obj); -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 const mrb_func_t ref_func[] = { - 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, -}; + if (!ptr) return mrb_nil_value(); + return ptr[i]; +} static mrb_sym mrb_id_attrset(mrb_state *mrb, mrb_sym id) { +#define ONSTACK_ALLOC_MAX 32 +#define ONSTACK_STRLEN_MAX (ONSTACK_ALLOC_MAX - 1) /* '=' character */ + const char *name; char *buf; mrb_int len; mrb_sym mid; + char onstack[ONSTACK_ALLOC_MAX]; - name = mrb_sym2name_len(mrb, id, &len); - buf = (char *)mrb_malloc(mrb, (size_t)len+2); + name = mrb_sym_name_len(mrb, id, &len); + if (len > ONSTACK_STRLEN_MAX) { + buf = (char *)mrb_malloc(mrb, (size_t)len+1); + } + else { + buf = onstack; + } memcpy(buf, name, (size_t)len); buf[len] = '='; - buf[len+1] = '\0'; mid = mrb_intern(mrb, buf, len+1); - mrb_free(mrb, buf); + if (buf != onstack) { + mrb_free(mrb, buf); + } return mid; } static mrb_value -mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val) +mrb_struct_set_m(mrb_state *mrb, mrb_value obj) { - const char *name; - mrb_int i, len, slen; - mrb_sym mid; - mrb_value members, slot, *ptr; - const mrb_value *ptr_members; - - /* get base id */ - name = mrb_sym2name_len(mrb, mrb->c->ci->mid, &slen); - mid = mrb_intern(mrb, name, slen-1); /* omit last "=" */ + mrb_int i = mrb_integer(mrb_proc_cfunc_env_get(mrb, 0)); + mrb_value *ptr; + mrb_value val = mrb_get_arg1(mrb); - members = mrb_struct_members(mrb, obj); - ptr_members = RARRAY_PTR(members); - len = RARRAY_LEN(members); + mrb_struct_modify(mrb, obj); ptr = RSTRUCT_PTR(obj); - for (i=0; i<len; i++) { - slot = ptr_members[i]; - if (mrb_symbol(slot) == mid) { - return ptr[i] = val; - } + if (ptr == NULL || i >= RSTRUCT_LEN(obj)) { + mrb_ary_set(mrb, obj, i, val); } - mrb_raisef(mrb, E_INDEX_ERROR, "`%S' is not a struct member", mrb_sym2str(mrb, mid)); - return mrb_nil_value(); /* not reached */ -} - -static mrb_value -mrb_struct_set_m(mrb_state *mrb, mrb_value obj) -{ - mrb_value val; - - mrb_get_args(mrb, "o", &val); - return mrb_struct_set(mrb, obj, val); -} - -static mrb_bool -is_local_id(mrb_state *mrb, const char *name) -{ - if (!name) return FALSE; - return !ISUPPER(name[0]); -} - -static mrb_bool -is_const_id(mrb_state *mrb, const char *name) -{ - if (!name) return FALSE; - return ISUPPER(name[0]); + else { + ptr[i] = val; + } + return val; } static void @@ -231,23 +178,20 @@ make_struct_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c for (i=0; i<len; i++) { mrb_sym id = mrb_symbol(ptr_members[i]); - const char *name = mrb_sym2name_len(mrb, id, NULL); - - if (is_local_id(mrb, name) || is_const_id(mrb, name)) { - if (i < N_REF_FUNC) { - mrb_define_method_id(mrb, c, id, ref_func[i], MRB_ARGS_NONE()); - } - else { - mrb_define_method_id(mrb, c, id, mrb_struct_ref, MRB_ARGS_NONE()); - } - mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, MRB_ARGS_REQ(1)); - mrb_gc_arena_restore(mrb, ai); - } + mrb_method_t m; + mrb_value at = mrb_fixnum_value(i); + struct RProc *aref = mrb_proc_new_cfunc_with_env(mrb, mrb_struct_ref, 1, &at); + struct RProc *aset = mrb_proc_new_cfunc_with_env(mrb, mrb_struct_set_m, 1, &at); + MRB_METHOD_FROM_PROC(m, aref); + mrb_define_method_raw(mrb, c, id, m); + MRB_METHOD_FROM_PROC(m, aset); + mrb_define_method_raw(mrb, c, mrb_id_attrset(mrb, id), m); + mrb_gc_arena_restore(mrb, ai); } } static mrb_value -make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass) +make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass *klass) { mrb_value nstr; mrb_sym id; @@ -258,20 +202,20 @@ make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * k } else { /* old style: should we warn? */ - name = mrb_str_to_str(mrb, name); + mrb_to_str(mrb, name); id = mrb_obj_to_sym(mrb, name); - if (!is_const_id(mrb, mrb_sym2name_len(mrb, id, NULL))) { - mrb_name_error(mrb, id, "identifier %S needs to be constant", name); + if (!mrb_const_name_p(mrb, RSTRING_PTR(name), RSTRING_LEN(name))) { + mrb_name_error(mrb, id, "identifier %v needs to be constant", name); } if (mrb_const_defined_at(mrb, mrb_obj_value(klass), id)) { - mrb_warn(mrb, "redefining constant Struct::%S", name); - /* ?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); */ + mrb_warn(mrb, "redefining constant Struct::%v", name); + mrb_const_remove(mrb, mrb_obj_value(klass), id); } c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); } MRB_SET_INSTANCE_TT(c, MRB_TT_ARRAY); nstr = mrb_obj_value(c); - mrb_iv_set(mrb, nstr, mrb_intern_lit(mrb, "__members__"), members); + mrb_iv_set(mrb, nstr, MRB_SYM(__members__), members); mrb_define_class_method(mrb, c, "new", mrb_instance_new, MRB_ARGS_ANY()); mrb_define_class_method(mrb, c, "[]", mrb_instance_new, MRB_ARGS_ANY()); @@ -319,52 +263,47 @@ static mrb_value mrb_struct_s_def(mrb_state *mrb, mrb_value klass) { mrb_value name, rest; - mrb_value *pargv; + const mrb_value *pargv; mrb_int argcnt; mrb_int i; mrb_value b, st; mrb_sym id; - mrb_value *argv; + const mrb_value *argv; mrb_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); + mrb_argnum_error(mrb, argc, 1, -1); } 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); + pargv = argv; + argcnt = argc; + if (argc > 0) { + name = argv[0]; + if (mrb_symbol_p(name)) { + /* 1stArgument:symbol -> name=nil rest=argv[0..n] */ 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++; + else { + pargv++; + argcnt--; } - rest = mrb_ary_new_from_values(mrb, argcnt, pargv); } - for (i=0; i<RARRAY_LEN(rest); i++) { + rest = mrb_ary_new_from_values(mrb, argcnt, pargv); + for (i=0; i<argcnt; i++) { id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]); mrb_ary_set(mrb, rest, i, mrb_symbol_value(id)); } - } - st = make_struct(mrb, name, rest, struct_class(mrb)); - if (!mrb_nil_p(b)) { - mrb_yield_with_class(mrb, b, 1, &st, st, mrb_class_ptr(klass)); - } + st = make_struct(mrb, name, 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; + return st; + } + /* not reached */ + return mrb_nil_value(); } static mrb_int @@ -372,7 +311,7 @@ num_members(mrb_state *mrb, struct RClass *klass) { mrb_value members; - members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern_lit(mrb, "__members__")); + members = struct_ivar_get(mrb, mrb_obj_value(klass), MRB_SYM(__members__)); if (!mrb_array_p(members)) { mrb_raise(mrb, E_TYPE_ERROR, "broken members"); } @@ -383,7 +322,7 @@ num_members(mrb_state *mrb, struct RClass *klass) /* */ static mrb_value -mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb_value self) +mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, const mrb_value *argv, mrb_value self) { struct RClass *klass = mrb_obj_class(mrb, self); mrb_int i, n; @@ -403,86 +342,21 @@ mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb } static mrb_value -mrb_struct_initialize_m(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value self) +mrb_struct_initialize(mrb_state *mrb, mrb_value self) { - mrb_value *argv; + const mrb_value *argv; mrb_int argc; - mrb_get_args(mrb, "*", &argv, &argc); + mrb_get_args(mrb, "*!", &argv, &argc); return mrb_struct_initialize_withArg(mrb, argc, argv, self); } -static mrb_value -inspect_struct(mrb_state *mrb, mrb_value s, mrb_bool recur) -{ - const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s)); - mrb_value members, str = mrb_str_new_lit(mrb, "#<struct "); - mrb_value *ptr; - const mrb_value *ptr_members; - mrb_int i, len; - - if (cn) { - mrb_str_append(mrb, str, mrb_str_new_cstr(mrb, cn)); - } - if (recur) { - return mrb_str_cat_lit(mrb, str, ":...>"); - } - - members = mrb_struct_members(mrb, s); - ptr_members = RARRAY_PTR(members); - ptr = RSTRUCT_PTR(s); - len = RSTRUCT_LEN(s); - for (i=0; i<len; i++) { - mrb_value slot; - mrb_sym id; - const char *name; - mrb_int namelen; - - if (i > 0) { - mrb_str_cat_lit(mrb, str, ", "); - } - else if (cn) { - mrb_str_cat_lit(mrb, str, " "); - } - slot = ptr_members[i]; - id = mrb_symbol(slot); - name = mrb_sym2name_len(mrb, id, &namelen); - if (is_local_id(mrb, name) || is_const_id(mrb, name)) { - mrb_str_append(mrb, str, mrb_str_new(mrb, name, namelen)); - } - else { - mrb_str_append(mrb, str, mrb_inspect(mrb, slot)); - } - mrb_str_cat_lit(mrb, str, "="); - mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i])); - } - mrb_str_cat_lit(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, FALSE); -} - /* 15.2.18.4.9 */ /* :nodoc: */ static mrb_value mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) { - mrb_value s; - mrb_int i, len; - - mrb_get_args(mrb, "o", &s); + 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))) { @@ -491,49 +365,43 @@ mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) if (!mrb_array_p(s)) { mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); } - if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { - mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); - } - len = RSTRUCT_LEN(copy); - for (i = 0; i < len; i++) { - mrb_ary_set(mrb, copy, i, RSTRUCT_PTR(s)[i]); - } + mrb_ary_replace(mrb, copy, s); return copy; } static mrb_value -struct_aref_sym(mrb_state *mrb, mrb_value s, mrb_sym id) +struct_aref_sym(mrb_state *mrb, mrb_value obj, mrb_sym id) { - mrb_value *ptr, members; + mrb_value members, *ptr; const mrb_value *ptr_members; mrb_int i, len; - ptr = RSTRUCT_PTR(s); - members = mrb_struct_members(mrb, s); + members = struct_members(mrb, obj); ptr_members = RARRAY_PTR(members); len = RARRAY_LEN(members); + ptr = RSTRUCT_PTR(obj); for (i=0; i<len; i++) { - if (mrb_symbol(ptr_members[i]) == id) { + mrb_value slot = ptr_members[i]; + if (mrb_symbol_p(slot) && mrb_symbol(slot) == id) { return ptr[i]; } } - mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", mrb_sym2str(mrb, id)); + mrb_name_error(mrb, id, "no member '%n' in struct", id); return mrb_nil_value(); /* not reached */ } static mrb_value struct_aref_int(mrb_state *mrb, mrb_value s, mrb_int i) { - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) - mrb_raisef(mrb, E_INDEX_ERROR, - "offset %S too small for struct(size:%S)", - mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); - if (RSTRUCT_LEN(s) <= i) + mrb_int idx = i < 0 ? RSTRUCT_LEN(s) + i : i; + + if (idx < 0) + mrb_raisef(mrb, E_INDEX_ERROR, + "offset %i too small for struct(size:%i)", i, RSTRUCT_LEN(s)); + if (RSTRUCT_LEN(s) <= idx) mrb_raisef(mrb, E_INDEX_ERROR, - "offset %S too large for struct(size:%S)", - mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); - return RSTRUCT_PTR(s)[i]; + "offset %i too large for struct(size:%i)", i, RSTRUCT_LEN(s)); + return RSTRUCT_PTR(s)[idx]; } /* 15.2.18.4.2 */ @@ -558,21 +426,16 @@ struct_aref_int(mrb_state *mrb, mrb_value s, mrb_int i) static mrb_value mrb_struct_aref(mrb_state *mrb, mrb_value s) { - mrb_value idx; + mrb_value idx = mrb_get_arg1(mrb); - mrb_get_args(mrb, "o", &idx); if (mrb_string_p(idx)) { - mrb_value sym = mrb_check_intern_str(mrb, idx); - - if (mrb_nil_p(sym)) { - mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", idx); - } - idx = sym; + mrb_sym sym = mrb_intern_str(mrb, idx); + idx = mrb_symbol_value(sym); } if (mrb_symbol_p(idx)) { return struct_aref_sym(mrb, s, mrb_symbol(idx)); } - return struct_aref_int(mrb, s, mrb_int(mrb, idx)); + return struct_aref_int(mrb, s, mrb_as_int(mrb, idx)); } static mrb_value @@ -582,22 +445,18 @@ mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) const mrb_value *ptr_members; mrb_int i, len; - members = mrb_struct_members(mrb, s); + members = struct_members(mrb, s); len = RARRAY_LEN(members); - if (RSTRUCT_LEN(s) != len) { - mrb_raisef(mrb, E_TYPE_ERROR, - "struct size differs (%S required %S given)", - mrb_fixnum_value(len), mrb_fixnum_value(RSTRUCT_LEN(s))); - } ptr = RSTRUCT_PTR(s); ptr_members = RARRAY_PTR(members); for (i=0; i<len; i++) { if (mrb_symbol(ptr_members[i]) == id) { + mrb_struct_modify(mrb, s); ptr[i] = val; return val; } } - mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", mrb_sym2str(mrb, id)); + mrb_name_error(mrb, id, "no member '%n' in struct", id); return val; /* not reach */ } @@ -633,29 +492,24 @@ mrb_struct_aset(mrb_state *mrb, mrb_value s) mrb_get_args(mrb, "oo", &idx, &val); if (mrb_string_p(idx)) { - mrb_value sym = mrb_check_intern_str(mrb, idx); - - if (mrb_nil_p(sym)) { - mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", idx); - } - idx = sym; + mrb_sym sym = mrb_intern_str(mrb, idx); + idx = mrb_symbol_value(sym); } if (mrb_symbol_p(idx)) { return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val); } - i = mrb_int(mrb, idx); + i = mrb_as_int(mrb, idx); if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) { mrb_raisef(mrb, E_INDEX_ERROR, - "offset %S too small for struct(size:%S)", - mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); + "offset %i too small for struct(size:%i)", i, RSTRUCT_LEN(s)); } if (RSTRUCT_LEN(s) <= i) { mrb_raisef(mrb, E_INDEX_ERROR, - "offset %S too large for struct(size:%S)", - mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); + "offset %i too large for struct(size:%i)", i, RSTRUCT_LEN(s)); } + mrb_struct_modify(mrb, s); return RSTRUCT_PTR(s)[i] = val; } @@ -680,37 +534,29 @@ mrb_struct_aset(mrb_state *mrb, mrb_value s) static mrb_value mrb_struct_equal(mrb_state *mrb, mrb_value s) { - mrb_value s2; + mrb_value s2 = mrb_get_arg1(mrb); mrb_value *ptr, *ptr2; mrb_int i, len; - mrb_bool equal_p; - mrb_get_args(mrb, "o", &s2); if (mrb_obj_equal(mrb, s, s2)) { - equal_p = 1; + return mrb_true_value(); } - else if (!strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s)), "Struct") || - mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) { - equal_p = 0; + if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) { + return mrb_false_value(); } - else if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { + if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { mrb_bug(mrb, "inconsistent struct"); /* should never happen */ - equal_p = 0; /* This substuture is just to suppress warnings. never called. */ } - else { - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - equal_p = 1; - for (i=0; i<len; i++) { - if (!mrb_equal(mrb, ptr[i], ptr2[i])) { - equal_p = 0; - break; - } + ptr = RSTRUCT_PTR(s); + ptr2 = RSTRUCT_PTR(s2); + len = RSTRUCT_LEN(s); + for (i=0; i<len; i++) { + if (!mrb_equal(mrb, ptr[i], ptr2[i])) { + return mrb_false_value(); } } - return mrb_bool_value(equal_p); + return mrb_true_value(); } /* 15.2.18.4.12(x) */ @@ -724,43 +570,35 @@ mrb_struct_equal(mrb_state *mrb, mrb_value s) static mrb_value mrb_struct_eql(mrb_state *mrb, mrb_value s) { - mrb_value s2; + mrb_value s2 = mrb_get_arg1(mrb); mrb_value *ptr, *ptr2; mrb_int i, len; - mrb_bool eql_p; - mrb_get_args(mrb, "o", &s2); if (mrb_obj_equal(mrb, s, s2)) { - eql_p = 1; + return mrb_true_value(); } - else if (strcmp(mrb_class_name(mrb, mrb_obj_class(mrb, s2)), "Struct") || - mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) { - eql_p = 0; + if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) { + return mrb_false_value(); } - else if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { + if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { mrb_bug(mrb, "inconsistent struct"); /* should never happen */ - eql_p = 0; /* This substuture is just to suppress warnings. never called. */ } - else { - ptr = RSTRUCT_PTR(s); - ptr2 = RSTRUCT_PTR(s2); - len = RSTRUCT_LEN(s); - eql_p = 1; - for (i=0; i<len; i++) { - if (!mrb_eql(mrb, ptr[i], ptr2[i])) { - eql_p = 0; - break; - } + ptr = RSTRUCT_PTR(s); + ptr2 = RSTRUCT_PTR(s2); + len = RSTRUCT_LEN(s); + for (i=0; i<len; i++) { + if (!mrb_eql(mrb, ptr[i], ptr2[i])) { + return mrb_false_value(); } } - return mrb_bool_value(eql_p); + return mrb_true_value(); } /* * call-seq: - * struct.length -> Fixnum - * struct.size -> Fixnum + * struct.length -> Integer + * struct.size -> Integer * * Returns number of struct members. */ @@ -795,7 +633,7 @@ mrb_struct_to_h(mrb_state *mrb, mrb_value self) mrb_value members, ret; mrb_int i; - members = mrb_struct_s_members(mrb, mrb_obj_value(mrb_class(mrb, self))); + members = struct_members(mrb, self); ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members)); for (i = 0; i < RARRAY_LEN(members); ++i) { @@ -809,7 +647,7 @@ static mrb_value mrb_struct_values_at(mrb_state *mrb, mrb_value self) { mrb_int argc; - mrb_value *argv; + const mrb_value *argv; mrb_get_args(mrb, "*", &argv, &argc); @@ -824,8 +662,8 @@ mrb_struct_values_at(mrb_state *mrb, mrb_value self) * 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.'' + * "<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 @@ -836,17 +674,16 @@ mrb_mruby_struct_gem_init(mrb_state* mrb) { struct RClass *st; st = mrb_define_class(mrb, "Struct", mrb->object_class); + MRB_SET_INSTANCE_TT(st, MRB_TT_ARRAY); mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, MRB_ARGS_ANY()); /* 15.2.18.3.1 */ mrb_define_method(mrb, st, "==", mrb_struct_equal, MRB_ARGS_REQ(1)); /* 15.2.18.4.1 */ mrb_define_method(mrb, st, "[]", mrb_struct_aref, MRB_ARGS_REQ(1)); /* 15.2.18.4.2 */ mrb_define_method(mrb, st, "[]=", mrb_struct_aset, MRB_ARGS_REQ(2)); /* 15.2.18.4.3 */ - mrb_define_method(mrb, st, "members", mrb_struct_members_m, MRB_ARGS_NONE()); /* 15.2.18.4.6 */ - mrb_define_method(mrb, st, "initialize", mrb_struct_initialize_m,MRB_ARGS_ANY()); /* 15.2.18.4.8 */ + mrb_define_method(mrb, st, "members", mrb_struct_members, MRB_ARGS_NONE()); /* 15.2.18.4.6 */ + mrb_define_method(mrb, st, "initialize", mrb_struct_initialize, MRB_ARGS_ANY()); /* 15.2.18.4.8 */ mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, MRB_ARGS_REQ(1)); /* 15.2.18.4.9 */ - mrb_define_method(mrb, st, "inspect", mrb_struct_inspect, MRB_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, MRB_ARGS_REQ(1)); /* 15.2.18.4.12(x) */ mrb_define_method(mrb, st, "size", mrb_struct_len, MRB_ARGS_NONE()); @@ -854,7 +691,7 @@ mrb_mruby_struct_gem_init(mrb_state* mrb) mrb_define_method(mrb, st, "to_a", mrb_struct_to_a, MRB_ARGS_NONE()); mrb_define_method(mrb, st, "values", mrb_struct_to_a, MRB_ARGS_NONE()); mrb_define_method(mrb, st, "to_h", mrb_struct_to_h, MRB_ARGS_NONE()); - mrb_define_method(mrb, st, "values_at", mrb_struct_values_at, MRB_ARGS_NONE()); + mrb_define_method(mrb, st, "values_at", mrb_struct_values_at, MRB_ARGS_ANY()); } void diff --git a/mrbgems/mruby-struct/test/struct.rb b/mrbgems/mruby-struct/test/struct.rb index 911e657bd..93930730b 100644 --- a/mrbgems/mruby-struct/test/struct.rb +++ b/mrbgems/mruby-struct/test/struct.rb @@ -2,42 +2,51 @@ # Struct ISO Test assert('Struct', '15.2.18') do - Struct.class == Class + assert_equal Class, Struct.class 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 == [] + assert_equal Struct, c.superclass + assert_equal [:m1, :m2], 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 + assert_true cc1 == cc2 + + Struct.new(:m1, :m2) { def foo; end } + assert_raise(NoMethodError) { Struct.new(:m1).new.foo } 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 + assert_equal 1, cc[:m1] + assert_equal 2, cc["m2"] + assert_equal 1, cc[0] + assert_equal 2, cc[-1] + assert_raise(TypeError) { cc[[]] } + assert_raise(IndexError) { cc[2] } + assert_raise(NameError) { cc['tama'] } 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 + assert_equal 3, cc[:m1] cc["m2"] = 3 assert_equal 3, cc["m2"] + cc[0] = 4 + assert_equal 4, cc[0] + cc[-1] = 5 + assert_equal 5, cc[-1] assert_raise(TypeError) { cc[[]] = 3 } + assert_raise(IndexError) { cc[2] = 7 } + assert_raise(NameError) { cc['pochi'] = 8 } end assert('Struct#each', '15.2.18.4.4') do @@ -47,7 +56,7 @@ assert('Struct#each', '15.2.18.4.4') do cc.each{|x| a << x } - a[0] == 1 and a[1] == 2 + assert_equal [1, 2], a end assert('Struct#each_pair', '15.2.18.4.5') do @@ -57,19 +66,17 @@ assert('Struct#each_pair', '15.2.18.4.5') do cc.each_pair{|k,v| a << [k,v] } - a[0] == [:m1, 1] and a[1] == [:m2, 2] + assert_equal [[:m1, 1], [:m2, 2]], a end assert('Struct#members', '15.2.18.4.6') do c = Struct.new(:m1, :m2) - cc = c.new(1,2) - cc.members == [:m1,:m2] + assert_equal [:m1, :m2], c.new(1,2).members 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] + assert_equal([2]) { c.new(1,2).select{|v| v % 2 == 0} } end assert('large struct') do @@ -100,10 +107,19 @@ assert('wrong struct arg count') do end end -assert('struct inspect') do +assert('struct dup') do c = Struct.new(:m1, :m2, :m3, :m4, :m5) cc = c.new(1,2,3,4,5) - assert_equal "#<struct #{c.inspect} m1=1, m2=2, m3=3, m4=4, m5=5>", cc.inspect + assert_nothing_raised { + assert_equal(cc, cc.dup) + } +end + +assert('struct inspect') do + c = Struct.new(:m1, :m2, :m3, :m4, :m5, :recur) + cc = c.new(1,2,3,4,5,nil) + cc.recur = cc + assert_equal "#<struct m1=1, m2=2, m3=3, m4=4, m5=5, recur=#<struct #{cc.class}:...>>", cc.inspect end assert('Struct#length, Struct#size') do @@ -129,3 +145,66 @@ assert('Struct#values_at') do assert_equal ['io', 'aki'], a.values_at(1, 0) assert_raise(IndexError) { a.values_at 2 } end + +assert("Struct#dig") do + a = Struct.new(:blue, :purple).new('aki', Struct.new(:red).new(1)) + assert_equal 'aki', a.dig(:blue) + assert_equal 1, a.dig(:purple, :red) + assert_equal 1, a.dig(1, 0) +end + +# TODO: suppress redefining Struct warning during test +# assert("Struct.new removes existing constant") do +# begin +# assert_not_equal Struct.new("Test", :a), Struct.new("Test", :a, :b) +# ensure +# Struct.remove_const :Test +# end +# end + +assert("Struct#initialize_copy requires struct to be the same type") do + begin + Struct.new("Test", :a) + a = Struct::Test.new("a") + Struct.remove_const :Test + Struct.new("Test", :a, :b) + assert_raise(TypeError) do + a.initialize_copy(Struct::Test.new("a", "b")) + end + ensure + Struct.remove_const :Test + end +end + +assert("Struct.new does not allow array") do + assert_raise(TypeError) do + Struct.new("Test", [:a]) + end +end + +assert("Struct.new does not allow invalid class name") do + assert_raise(NameError) { Struct.new("Test-", :a) } +end + +assert("Struct.new generates subclass of Struct") do + begin + original_struct = Struct + Struct = String + assert_equal original_struct, original_struct.new(:foo).superclass + ensure + Struct = original_struct + end +end + +assert 'Struct#freeze' do + c = Struct.new :m + + o = c.new + o.m = :test + assert_equal :test, o.m + + o.freeze + assert_raise(FrozenError) { o.m = :modify } + assert_raise(FrozenError) { o[:m] = :modify } + assert_equal :test, o.m +end |
