[270] | 1 | /*
|
---|
| 2 | ** struct.c - Struct class
|
---|
| 3 | **
|
---|
| 4 | ** See Copyright Notice in mruby.h
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #include <string.h>
|
---|
| 8 | #include "mruby.h"
|
---|
| 9 | #include "mruby/array.h"
|
---|
| 10 | #include "mruby/string.h"
|
---|
| 11 | #include "mruby/class.h"
|
---|
| 12 | #include "mruby/variable.h"
|
---|
| 13 | #include "mruby/hash.h"
|
---|
| 14 | #include "mruby/range.h"
|
---|
| 15 |
|
---|
| 16 | #define RSTRUCT_LEN(st) mrb_ary_ptr(st)->len
|
---|
| 17 | #define RSTRUCT_PTR(st) mrb_ary_ptr(st)->ptr
|
---|
| 18 |
|
---|
| 19 | static struct RClass *
|
---|
| 20 | struct_class(mrb_state *mrb)
|
---|
| 21 | {
|
---|
| 22 | return mrb_class_get(mrb, "Struct");
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | static inline mrb_value
|
---|
| 26 | struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id)
|
---|
| 27 | {
|
---|
| 28 | struct RClass* kclass;
|
---|
| 29 | struct RClass* sclass = struct_class(mrb);
|
---|
| 30 | mrb_value ans;
|
---|
| 31 |
|
---|
| 32 | for (;;) {
|
---|
| 33 | ans = mrb_iv_get(mrb, c, id);
|
---|
| 34 | if (!mrb_nil_p(ans)) return ans;
|
---|
| 35 | kclass = RCLASS_SUPER(c);
|
---|
| 36 | if (kclass == 0 || kclass == sclass)
|
---|
| 37 | return mrb_nil_value();
|
---|
| 38 | c = mrb_obj_value(kclass);
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | static mrb_value
|
---|
| 43 | struct_s_members(mrb_state *mrb, struct RClass *klass)
|
---|
| 44 | {
|
---|
| 45 | mrb_value members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern_lit(mrb, "__members__"));
|
---|
| 46 |
|
---|
| 47 | if (mrb_nil_p(members)) {
|
---|
| 48 | mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct");
|
---|
| 49 | }
|
---|
| 50 | if (!mrb_array_p(members)) {
|
---|
| 51 | mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
|
---|
| 52 | }
|
---|
| 53 | return members;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | static mrb_value
|
---|
| 57 | struct_members(mrb_state *mrb, mrb_value s)
|
---|
| 58 | {
|
---|
| 59 | mrb_value members = struct_s_members(mrb, mrb_obj_class(mrb, s));
|
---|
| 60 | if (!mrb_array_p(s)) {
|
---|
| 61 | mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
|
---|
| 62 | }
|
---|
| 63 | if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
|
---|
| 64 | mrb_raisef(mrb, E_TYPE_ERROR,
|
---|
| 65 | "struct size differs (%S required %S given)",
|
---|
| 66 | mrb_fixnum_value(RARRAY_LEN(members)), mrb_fixnum_value(RSTRUCT_LEN(s)));
|
---|
| 67 | }
|
---|
| 68 | return members;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | static mrb_value
|
---|
| 72 | mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
|
---|
| 73 | {
|
---|
| 74 | mrb_value members, ary;
|
---|
| 75 |
|
---|
| 76 | members = struct_s_members(mrb, mrb_class_ptr(klass));
|
---|
| 77 | ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members));
|
---|
| 78 | mrb_ary_replace(mrb, ary, members);
|
---|
| 79 | return ary;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /* 15.2.18.4.6 */
|
---|
| 83 | /*
|
---|
| 84 | * call-seq:
|
---|
| 85 | * struct.members -> array
|
---|
| 86 | *
|
---|
| 87 | * Returns an array of strings representing the names of the instance
|
---|
| 88 | * variables.
|
---|
| 89 | *
|
---|
| 90 | * Customer = Struct.new(:name, :address, :zip)
|
---|
| 91 | * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
| 92 | * joe.members #=> [:name, :address, :zip]
|
---|
| 93 | */
|
---|
| 94 |
|
---|
| 95 | static mrb_value
|
---|
| 96 | mrb_struct_members(mrb_state *mrb, mrb_value obj)
|
---|
| 97 | {
|
---|
| 98 | return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj)));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | static mrb_value
|
---|
| 102 | mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id)
|
---|
| 103 | {
|
---|
| 104 | mrb_value members, slot, *ptr;
|
---|
| 105 | const mrb_value *ptr_members;
|
---|
| 106 | mrb_int i, len;
|
---|
| 107 |
|
---|
| 108 | ptr = RSTRUCT_PTR(obj);
|
---|
| 109 | members = struct_members(mrb, obj);
|
---|
| 110 | ptr_members = RARRAY_PTR(members);
|
---|
| 111 | slot = mrb_symbol_value(id);
|
---|
| 112 | len = RARRAY_LEN(members);
|
---|
| 113 | for (i=0; i<len; i++) {
|
---|
| 114 | if (mrb_obj_equal(mrb, ptr_members[i], slot)) {
|
---|
| 115 | return ptr[i];
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | mrb_raisef(mrb, E_INDEX_ERROR, "'%S' is not a struct member", mrb_sym2str(mrb, id));
|
---|
| 119 | return mrb_nil_value(); /* not reached */
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | static mrb_value
|
---|
| 123 | mrb_struct_ref(mrb_state *mrb, mrb_value obj)
|
---|
| 124 | {
|
---|
| 125 | return mrb_struct_getmember(mrb, obj, mrb->c->ci->mid);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | static mrb_value mrb_struct_ref0(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[0];}
|
---|
| 129 | static mrb_value mrb_struct_ref1(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[1];}
|
---|
| 130 | static mrb_value mrb_struct_ref2(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[2];}
|
---|
| 131 | static mrb_value mrb_struct_ref3(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[3];}
|
---|
| 132 | static mrb_value mrb_struct_ref4(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[4];}
|
---|
| 133 | static mrb_value mrb_struct_ref5(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[5];}
|
---|
| 134 | static mrb_value mrb_struct_ref6(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[6];}
|
---|
| 135 | static mrb_value mrb_struct_ref7(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[7];}
|
---|
| 136 | static mrb_value mrb_struct_ref8(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[8];}
|
---|
| 137 | static mrb_value mrb_struct_ref9(mrb_state* mrb, mrb_value obj) {return RSTRUCT_PTR(obj)[9];}
|
---|
| 138 |
|
---|
| 139 | #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
|
---|
| 140 | #define N_REF_FUNC numberof(ref_func)
|
---|
| 141 |
|
---|
| 142 | static const mrb_func_t ref_func[] = {
|
---|
| 143 | mrb_struct_ref0,
|
---|
| 144 | mrb_struct_ref1,
|
---|
| 145 | mrb_struct_ref2,
|
---|
| 146 | mrb_struct_ref3,
|
---|
| 147 | mrb_struct_ref4,
|
---|
| 148 | mrb_struct_ref5,
|
---|
| 149 | mrb_struct_ref6,
|
---|
| 150 | mrb_struct_ref7,
|
---|
| 151 | mrb_struct_ref8,
|
---|
| 152 | mrb_struct_ref9,
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | static mrb_sym
|
---|
| 156 | mrb_id_attrset(mrb_state *mrb, mrb_sym id)
|
---|
| 157 | {
|
---|
| 158 | const char *name;
|
---|
| 159 | char *buf;
|
---|
| 160 | mrb_int len;
|
---|
| 161 | mrb_sym mid;
|
---|
| 162 |
|
---|
| 163 | name = mrb_sym2name_len(mrb, id, &len);
|
---|
| 164 | buf = (char *)mrb_malloc(mrb, (size_t)len+2);
|
---|
| 165 | memcpy(buf, name, (size_t)len);
|
---|
| 166 | buf[len] = '=';
|
---|
| 167 | buf[len+1] = '\0';
|
---|
| 168 |
|
---|
| 169 | mid = mrb_intern(mrb, buf, len+1);
|
---|
| 170 | mrb_free(mrb, buf);
|
---|
| 171 | return mid;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | static mrb_value
|
---|
| 175 | mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val)
|
---|
| 176 | {
|
---|
| 177 | const char *name;
|
---|
| 178 | mrb_int i, len, slen;
|
---|
| 179 | mrb_sym mid;
|
---|
| 180 | mrb_value members, slot, *ptr;
|
---|
| 181 | const mrb_value *ptr_members;
|
---|
| 182 |
|
---|
| 183 | /* get base id */
|
---|
| 184 | name = mrb_sym2name_len(mrb, mrb->c->ci->mid, &slen);
|
---|
| 185 | mid = mrb_intern(mrb, name, slen-1); /* omit last "=" */
|
---|
| 186 |
|
---|
| 187 | members = struct_members(mrb, obj);
|
---|
| 188 | ptr_members = RARRAY_PTR(members);
|
---|
| 189 | len = RARRAY_LEN(members);
|
---|
| 190 | ptr = RSTRUCT_PTR(obj);
|
---|
| 191 | for (i=0; i<len; i++) {
|
---|
| 192 | slot = ptr_members[i];
|
---|
| 193 | if (mrb_symbol(slot) == mid) {
|
---|
| 194 | return ptr[i] = val;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | mrb_raisef(mrb, E_INDEX_ERROR, "'%S' is not a struct member", mrb_sym2str(mrb, mid));
|
---|
| 198 | return mrb_nil_value(); /* not reached */
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | static mrb_value
|
---|
| 202 | mrb_struct_set_m(mrb_state *mrb, mrb_value obj)
|
---|
| 203 | {
|
---|
| 204 | mrb_value val;
|
---|
| 205 |
|
---|
| 206 | mrb_get_args(mrb, "o", &val);
|
---|
| 207 | return mrb_struct_set(mrb, obj, val);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | static mrb_bool
|
---|
| 211 | is_local_id(mrb_state *mrb, const char *name)
|
---|
| 212 | {
|
---|
| 213 | if (!name) return FALSE;
|
---|
| 214 | return !ISUPPER(name[0]);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | static mrb_bool
|
---|
| 218 | is_const_id(mrb_state *mrb, const char *name)
|
---|
| 219 | {
|
---|
| 220 | if (!name) return FALSE;
|
---|
| 221 | return ISUPPER(name[0]);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | static void
|
---|
| 225 | make_struct_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c)
|
---|
| 226 | {
|
---|
| 227 | const mrb_value *ptr_members = RARRAY_PTR(members);
|
---|
| 228 | mrb_int i;
|
---|
| 229 | mrb_int len = RARRAY_LEN(members);
|
---|
| 230 | int ai = mrb_gc_arena_save(mrb);
|
---|
| 231 |
|
---|
| 232 | for (i=0; i<len; i++) {
|
---|
| 233 | mrb_sym id = mrb_symbol(ptr_members[i]);
|
---|
| 234 | const char *name = mrb_sym2name_len(mrb, id, NULL);
|
---|
| 235 |
|
---|
| 236 | if (is_local_id(mrb, name) || is_const_id(mrb, name)) {
|
---|
| 237 | if (i < N_REF_FUNC) {
|
---|
| 238 | mrb_define_method_id(mrb, c, id, ref_func[i], MRB_ARGS_NONE());
|
---|
| 239 | }
|
---|
| 240 | else {
|
---|
| 241 | mrb_define_method_id(mrb, c, id, mrb_struct_ref, MRB_ARGS_NONE());
|
---|
| 242 | }
|
---|
| 243 | mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, MRB_ARGS_REQ(1));
|
---|
| 244 | mrb_gc_arena_restore(mrb, ai);
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | static mrb_value
|
---|
| 250 | make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass)
|
---|
| 251 | {
|
---|
| 252 | mrb_value nstr;
|
---|
| 253 | mrb_sym id;
|
---|
| 254 | struct RClass *c;
|
---|
| 255 |
|
---|
| 256 | if (mrb_nil_p(name)) {
|
---|
| 257 | c = mrb_class_new(mrb, klass);
|
---|
| 258 | }
|
---|
| 259 | else {
|
---|
| 260 | /* old style: should we warn? */
|
---|
| 261 | name = mrb_str_to_str(mrb, name);
|
---|
| 262 | id = mrb_obj_to_sym(mrb, name);
|
---|
| 263 | if (!is_const_id(mrb, mrb_sym2name_len(mrb, id, NULL))) {
|
---|
| 264 | mrb_name_error(mrb, id, "identifier %S needs to be constant", name);
|
---|
| 265 | }
|
---|
| 266 | if (mrb_const_defined_at(mrb, mrb_obj_value(klass), id)) {
|
---|
| 267 | mrb_warn(mrb, "redefining constant Struct::%S", name);
|
---|
| 268 | /* ?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); */
|
---|
| 269 | }
|
---|
| 270 | c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
|
---|
| 271 | }
|
---|
| 272 | MRB_SET_INSTANCE_TT(c, MRB_TT_ARRAY);
|
---|
| 273 | nstr = mrb_obj_value(c);
|
---|
| 274 | mrb_iv_set(mrb, nstr, mrb_intern_lit(mrb, "__members__"), members);
|
---|
| 275 |
|
---|
| 276 | mrb_define_class_method(mrb, c, "new", mrb_instance_new, MRB_ARGS_ANY());
|
---|
| 277 | mrb_define_class_method(mrb, c, "[]", mrb_instance_new, MRB_ARGS_ANY());
|
---|
| 278 | mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, MRB_ARGS_NONE());
|
---|
| 279 | /* RSTRUCT(nstr)->basic.c->super = c->c; */
|
---|
| 280 | make_struct_define_accessors(mrb, members, c);
|
---|
| 281 | return nstr;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | /* 15.2.18.3.1 */
|
---|
| 285 | /*
|
---|
| 286 | * call-seq:
|
---|
| 287 | * Struct.new( [aString] [, aSym]+> ) -> StructClass
|
---|
| 288 | * StructClass.new(arg, ...) -> obj
|
---|
| 289 | * StructClass[arg, ...] -> obj
|
---|
| 290 | *
|
---|
| 291 | * Creates a new class, named by <i>aString</i>, containing accessor
|
---|
| 292 | * methods for the given symbols. If the name <i>aString</i> is
|
---|
| 293 | * omitted, an anonymous structure class will be created. Otherwise,
|
---|
| 294 | * the name of this struct will appear as a constant in class
|
---|
| 295 | * <code>Struct</code>, so it must be unique for all
|
---|
| 296 | * <code>Struct</code>s in the system and should start with a capital
|
---|
| 297 | * letter. Assigning a structure class to a constant effectively gives
|
---|
| 298 | * the class the name of the constant.
|
---|
| 299 | *
|
---|
| 300 | * <code>Struct::new</code> returns a new <code>Class</code> object,
|
---|
| 301 | * which can then be used to create specific instances of the new
|
---|
| 302 | * structure. The number of actual parameters must be
|
---|
| 303 | * less than or equal to the number of attributes defined for this
|
---|
| 304 | * class; unset parameters default to <code>nil</code>. Passing too many
|
---|
| 305 | * parameters will raise an <code>ArgumentError</code>.
|
---|
| 306 | *
|
---|
| 307 | * The remaining methods listed in this section (class and instance)
|
---|
| 308 | * are defined for this generated class.
|
---|
| 309 | *
|
---|
| 310 | * # Create a structure with a name in Struct
|
---|
| 311 | * Struct.new("Customer", :name, :address) #=> Struct::Customer
|
---|
| 312 | * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main">
|
---|
| 313 | *
|
---|
| 314 | * # Create a structure named by its constant
|
---|
| 315 | * Customer = Struct.new(:name, :address) #=> Customer
|
---|
| 316 | * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
|
---|
| 317 | */
|
---|
| 318 | static mrb_value
|
---|
| 319 | mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
|
---|
| 320 | {
|
---|
| 321 | mrb_value name, rest;
|
---|
| 322 | mrb_value *pargv;
|
---|
| 323 | mrb_int argcnt;
|
---|
| 324 | mrb_int i;
|
---|
| 325 | mrb_value b, st;
|
---|
| 326 | mrb_sym id;
|
---|
| 327 | mrb_value *argv;
|
---|
| 328 | mrb_int argc;
|
---|
| 329 |
|
---|
| 330 | name = mrb_nil_value();
|
---|
| 331 | rest = mrb_nil_value();
|
---|
| 332 | mrb_get_args(mrb, "*&", &argv, &argc, &b);
|
---|
| 333 | if (argc == 0) { /* special case to avoid crash */
|
---|
| 334 | rest = mrb_ary_new(mrb);
|
---|
| 335 | }
|
---|
| 336 | else {
|
---|
| 337 | if (argc > 0) name = argv[0];
|
---|
| 338 | if (argc > 1) rest = argv[1];
|
---|
| 339 | if (mrb_array_p(rest)) {
|
---|
| 340 | if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
|
---|
| 341 | /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
|
---|
| 342 | mrb_ary_unshift(mrb, rest, name);
|
---|
| 343 | name = mrb_nil_value();
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | else {
|
---|
| 347 | pargv = &argv[1];
|
---|
| 348 | argcnt = argc-1;
|
---|
| 349 | if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
|
---|
| 350 | /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
|
---|
| 351 | name = mrb_nil_value();
|
---|
| 352 | pargv = &argv[0];
|
---|
| 353 | argcnt++;
|
---|
| 354 | }
|
---|
| 355 | rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
|
---|
| 356 | }
|
---|
| 357 | for (i=0; i<RARRAY_LEN(rest); i++) {
|
---|
| 358 | id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]);
|
---|
| 359 | mrb_ary_set(mrb, rest, i, mrb_symbol_value(id));
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 | st = make_struct(mrb, name, rest, struct_class(mrb));
|
---|
| 363 | if (!mrb_nil_p(b)) {
|
---|
| 364 | mrb_yield_with_class(mrb, b, 1, &st, st, mrb_class_ptr(klass));
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | return st;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | static mrb_int
|
---|
| 371 | num_members(mrb_state *mrb, struct RClass *klass)
|
---|
| 372 | {
|
---|
| 373 | mrb_value members;
|
---|
| 374 |
|
---|
| 375 | members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern_lit(mrb, "__members__"));
|
---|
| 376 | if (!mrb_array_p(members)) {
|
---|
| 377 | mrb_raise(mrb, E_TYPE_ERROR, "broken members");
|
---|
| 378 | }
|
---|
| 379 | return RARRAY_LEN(members);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | /* 15.2.18.4.8 */
|
---|
| 383 | /*
|
---|
| 384 | */
|
---|
| 385 | static mrb_value
|
---|
| 386 | mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb_value self)
|
---|
| 387 | {
|
---|
| 388 | struct RClass *klass = mrb_obj_class(mrb, self);
|
---|
| 389 | mrb_int i, n;
|
---|
| 390 |
|
---|
| 391 | n = num_members(mrb, klass);
|
---|
| 392 | if (n < argc) {
|
---|
| 393 | mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | for (i = 0; i < argc; i++) {
|
---|
| 397 | mrb_ary_set(mrb, self, i, argv[i]);
|
---|
| 398 | }
|
---|
| 399 | for (i = argc; i < n; i++) {
|
---|
| 400 | mrb_ary_set(mrb, self, i, mrb_nil_value());
|
---|
| 401 | }
|
---|
| 402 | return self;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | static mrb_value
|
---|
| 406 | mrb_struct_initialize(mrb_state *mrb, mrb_value self)
|
---|
| 407 | {
|
---|
| 408 | mrb_value *argv;
|
---|
| 409 | mrb_int argc;
|
---|
| 410 |
|
---|
| 411 | mrb_get_args(mrb, "*", &argv, &argc);
|
---|
| 412 | return mrb_struct_initialize_withArg(mrb, argc, argv, self);
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | /* 15.2.18.4.9 */
|
---|
| 416 | /* :nodoc: */
|
---|
| 417 | static mrb_value
|
---|
| 418 | mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
|
---|
| 419 | {
|
---|
| 420 | mrb_value s;
|
---|
| 421 | mrb_int i, len;
|
---|
| 422 |
|
---|
| 423 | mrb_get_args(mrb, "o", &s);
|
---|
| 424 |
|
---|
| 425 | if (mrb_obj_equal(mrb, copy, s)) return copy;
|
---|
| 426 | if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) {
|
---|
| 427 | mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
|
---|
| 428 | }
|
---|
| 429 | if (!mrb_array_p(s)) {
|
---|
| 430 | mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
|
---|
| 431 | }
|
---|
| 432 | if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) {
|
---|
| 433 | mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch");
|
---|
| 434 | }
|
---|
| 435 | len = RSTRUCT_LEN(copy);
|
---|
| 436 | for (i = 0; i < len; i++) {
|
---|
| 437 | mrb_ary_set(mrb, copy, i, RSTRUCT_PTR(s)[i]);
|
---|
| 438 | }
|
---|
| 439 | return copy;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | static mrb_value
|
---|
| 443 | struct_aref_sym(mrb_state *mrb, mrb_value s, mrb_sym id)
|
---|
| 444 | {
|
---|
| 445 | mrb_value *ptr, members;
|
---|
| 446 | const mrb_value *ptr_members;
|
---|
| 447 | mrb_int i, len;
|
---|
| 448 |
|
---|
| 449 | ptr = RSTRUCT_PTR(s);
|
---|
| 450 | members = struct_members(mrb, s);
|
---|
| 451 | ptr_members = RARRAY_PTR(members);
|
---|
| 452 | len = RARRAY_LEN(members);
|
---|
| 453 | for (i=0; i<len; i++) {
|
---|
| 454 | if (mrb_symbol(ptr_members[i]) == id) {
|
---|
| 455 | return ptr[i];
|
---|
| 456 | }
|
---|
| 457 | }
|
---|
| 458 | mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", mrb_sym2str(mrb, id));
|
---|
| 459 | return mrb_nil_value(); /* not reached */
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | static mrb_value
|
---|
| 463 | struct_aref_int(mrb_state *mrb, mrb_value s, mrb_int i)
|
---|
| 464 | {
|
---|
| 465 | if (i < 0) i = RSTRUCT_LEN(s) + i;
|
---|
| 466 | if (i < 0)
|
---|
| 467 | mrb_raisef(mrb, E_INDEX_ERROR,
|
---|
| 468 | "offset %S too small for struct(size:%S)",
|
---|
| 469 | mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
|
---|
| 470 | if (RSTRUCT_LEN(s) <= i)
|
---|
| 471 | mrb_raisef(mrb, E_INDEX_ERROR,
|
---|
| 472 | "offset %S too large for struct(size:%S)",
|
---|
| 473 | mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
|
---|
| 474 | return RSTRUCT_PTR(s)[i];
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | /* 15.2.18.4.2 */
|
---|
| 478 | /*
|
---|
| 479 | * call-seq:
|
---|
| 480 | * struct[symbol] -> anObject
|
---|
| 481 | * struct[fixnum] -> anObject
|
---|
| 482 | *
|
---|
| 483 | * Attribute Reference---Returns the value of the instance variable
|
---|
| 484 | * named by <i>symbol</i>, or indexed (0..length-1) by
|
---|
| 485 | * <i>fixnum</i>. Will raise <code>NameError</code> if the named
|
---|
| 486 | * variable does not exist, or <code>IndexError</code> if the index is
|
---|
| 487 | * out of range.
|
---|
| 488 | *
|
---|
| 489 | * Customer = Struct.new(:name, :address, :zip)
|
---|
| 490 | * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
| 491 | *
|
---|
| 492 | * joe["name"] #=> "Joe Smith"
|
---|
| 493 | * joe[:name] #=> "Joe Smith"
|
---|
| 494 | * joe[0] #=> "Joe Smith"
|
---|
| 495 | */
|
---|
| 496 | static mrb_value
|
---|
| 497 | mrb_struct_aref(mrb_state *mrb, mrb_value s)
|
---|
| 498 | {
|
---|
| 499 | mrb_value idx;
|
---|
| 500 |
|
---|
| 501 | mrb_get_args(mrb, "o", &idx);
|
---|
| 502 | if (mrb_string_p(idx)) {
|
---|
| 503 | mrb_value sym = mrb_check_intern_str(mrb, idx);
|
---|
| 504 |
|
---|
| 505 | if (mrb_nil_p(sym)) {
|
---|
| 506 | mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", idx);
|
---|
| 507 | }
|
---|
| 508 | idx = sym;
|
---|
| 509 | }
|
---|
| 510 | if (mrb_symbol_p(idx)) {
|
---|
| 511 | return struct_aref_sym(mrb, s, mrb_symbol(idx));
|
---|
| 512 | }
|
---|
| 513 | return struct_aref_int(mrb, s, mrb_int(mrb, idx));
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | static mrb_value
|
---|
| 517 | mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
|
---|
| 518 | {
|
---|
| 519 | mrb_value members, *ptr;
|
---|
| 520 | const mrb_value *ptr_members;
|
---|
| 521 | mrb_int i, len;
|
---|
| 522 |
|
---|
| 523 | members = struct_members(mrb, s);
|
---|
| 524 | len = RARRAY_LEN(members);
|
---|
| 525 | if (RSTRUCT_LEN(s) != len) {
|
---|
| 526 | mrb_raisef(mrb, E_TYPE_ERROR,
|
---|
| 527 | "struct size differs (%S required %S given)",
|
---|
| 528 | mrb_fixnum_value(len), mrb_fixnum_value(RSTRUCT_LEN(s)));
|
---|
| 529 | }
|
---|
| 530 | ptr = RSTRUCT_PTR(s);
|
---|
| 531 | ptr_members = RARRAY_PTR(members);
|
---|
| 532 | for (i=0; i<len; i++) {
|
---|
| 533 | if (mrb_symbol(ptr_members[i]) == id) {
|
---|
| 534 | ptr[i] = val;
|
---|
| 535 | return val;
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 | mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", mrb_sym2str(mrb, id));
|
---|
| 539 | return val; /* not reach */
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | /* 15.2.18.4.3 */
|
---|
| 543 | /*
|
---|
| 544 | * call-seq:
|
---|
| 545 | * struct[symbol] = obj -> obj
|
---|
| 546 | * struct[fixnum] = obj -> obj
|
---|
| 547 | *
|
---|
| 548 | * Attribute Assignment---Assigns to the instance variable named by
|
---|
| 549 | * <i>symbol</i> or <i>fixnum</i> the value <i>obj</i> and
|
---|
| 550 | * returns it. Will raise a <code>NameError</code> if the named
|
---|
| 551 | * variable does not exist, or an <code>IndexError</code> if the index
|
---|
| 552 | * is out of range.
|
---|
| 553 | *
|
---|
| 554 | * Customer = Struct.new(:name, :address, :zip)
|
---|
| 555 | * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
| 556 | *
|
---|
| 557 | * joe["name"] = "Luke"
|
---|
| 558 | * joe[:zip] = "90210"
|
---|
| 559 | *
|
---|
| 560 | * joe.name #=> "Luke"
|
---|
| 561 | * joe.zip #=> "90210"
|
---|
| 562 | */
|
---|
| 563 |
|
---|
| 564 | static mrb_value
|
---|
| 565 | mrb_struct_aset(mrb_state *mrb, mrb_value s)
|
---|
| 566 | {
|
---|
| 567 | mrb_int i;
|
---|
| 568 | mrb_value idx;
|
---|
| 569 | mrb_value val;
|
---|
| 570 |
|
---|
| 571 | mrb_get_args(mrb, "oo", &idx, &val);
|
---|
| 572 |
|
---|
| 573 | if (mrb_string_p(idx)) {
|
---|
| 574 | mrb_value sym = mrb_check_intern_str(mrb, idx);
|
---|
| 575 |
|
---|
| 576 | if (mrb_nil_p(sym)) {
|
---|
| 577 | mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", idx);
|
---|
| 578 | }
|
---|
| 579 | idx = sym;
|
---|
| 580 | }
|
---|
| 581 | if (mrb_symbol_p(idx)) {
|
---|
| 582 | return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val);
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | i = mrb_int(mrb, idx);
|
---|
| 586 | if (i < 0) i = RSTRUCT_LEN(s) + i;
|
---|
| 587 | if (i < 0) {
|
---|
| 588 | mrb_raisef(mrb, E_INDEX_ERROR,
|
---|
| 589 | "offset %S too small for struct(size:%S)",
|
---|
| 590 | mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
|
---|
| 591 | }
|
---|
| 592 | if (RSTRUCT_LEN(s) <= i) {
|
---|
| 593 | mrb_raisef(mrb, E_INDEX_ERROR,
|
---|
| 594 | "offset %S too large for struct(size:%S)",
|
---|
| 595 | mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
|
---|
| 596 | }
|
---|
| 597 | return RSTRUCT_PTR(s)[i] = val;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | /* 15.2.18.4.1 */
|
---|
| 601 | /*
|
---|
| 602 | * call-seq:
|
---|
| 603 | * struct == other_struct -> true or false
|
---|
| 604 | *
|
---|
| 605 | * Equality---Returns <code>true</code> if <i>other_struct</i> is
|
---|
| 606 | * equal to this one: they must be of the same class as generated by
|
---|
| 607 | * <code>Struct::new</code>, and the values of all instance variables
|
---|
| 608 | * must be equal (according to <code>Object#==</code>).
|
---|
| 609 | *
|
---|
| 610 | * Customer = Struct.new(:name, :address, :zip)
|
---|
| 611 | * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
| 612 | * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
| 613 | * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
|
---|
| 614 | * joe == joejr #=> true
|
---|
| 615 | * joe == jane #=> false
|
---|
| 616 | */
|
---|
| 617 |
|
---|
| 618 | static mrb_value
|
---|
| 619 | mrb_struct_equal(mrb_state *mrb, mrb_value s)
|
---|
| 620 | {
|
---|
| 621 | mrb_value s2;
|
---|
| 622 | mrb_value *ptr, *ptr2;
|
---|
| 623 | mrb_int i, len;
|
---|
| 624 |
|
---|
| 625 | mrb_get_args(mrb, "o", &s2);
|
---|
| 626 | if (mrb_obj_equal(mrb, s, s2)) {
|
---|
| 627 | return mrb_true_value();
|
---|
| 628 | }
|
---|
| 629 | if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
|
---|
| 630 | return mrb_false_value();
|
---|
| 631 | }
|
---|
| 632 | if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
|
---|
| 633 | mrb_bug(mrb, "inconsistent struct"); /* should never happen */
|
---|
| 634 | }
|
---|
| 635 | ptr = RSTRUCT_PTR(s);
|
---|
| 636 | ptr2 = RSTRUCT_PTR(s2);
|
---|
| 637 | len = RSTRUCT_LEN(s);
|
---|
| 638 | for (i=0; i<len; i++) {
|
---|
| 639 | if (!mrb_equal(mrb, ptr[i], ptr2[i])) {
|
---|
| 640 | return mrb_false_value();
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | return mrb_true_value();
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | /* 15.2.18.4.12(x) */
|
---|
| 648 | /*
|
---|
| 649 | * code-seq:
|
---|
| 650 | * struct.eql?(other) -> true or false
|
---|
| 651 | *
|
---|
| 652 | * Two structures are equal if they are the same object, or if all their
|
---|
| 653 | * fields are equal (using <code>eql?</code>).
|
---|
| 654 | */
|
---|
| 655 | static mrb_value
|
---|
| 656 | mrb_struct_eql(mrb_state *mrb, mrb_value s)
|
---|
| 657 | {
|
---|
| 658 | mrb_value s2;
|
---|
| 659 | mrb_value *ptr, *ptr2;
|
---|
| 660 | mrb_int i, len;
|
---|
| 661 |
|
---|
| 662 | mrb_get_args(mrb, "o", &s2);
|
---|
| 663 | if (mrb_obj_equal(mrb, s, s2)) {
|
---|
| 664 | return mrb_true_value();
|
---|
| 665 | }
|
---|
| 666 | if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
|
---|
| 667 | return mrb_false_value();
|
---|
| 668 | }
|
---|
| 669 | if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
|
---|
| 670 | mrb_bug(mrb, "inconsistent struct"); /* should never happen */
|
---|
| 671 | }
|
---|
| 672 | ptr = RSTRUCT_PTR(s);
|
---|
| 673 | ptr2 = RSTRUCT_PTR(s2);
|
---|
| 674 | len = RSTRUCT_LEN(s);
|
---|
| 675 | for (i=0; i<len; i++) {
|
---|
| 676 | if (!mrb_eql(mrb, ptr[i], ptr2[i])) {
|
---|
| 677 | return mrb_false_value();
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | return mrb_true_value();
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /*
|
---|
| 685 | * call-seq:
|
---|
| 686 | * struct.length -> Fixnum
|
---|
| 687 | * struct.size -> Fixnum
|
---|
| 688 | *
|
---|
| 689 | * Returns number of struct members.
|
---|
| 690 | */
|
---|
| 691 | static mrb_value
|
---|
| 692 | mrb_struct_len(mrb_state *mrb, mrb_value self)
|
---|
| 693 | {
|
---|
| 694 | return mrb_fixnum_value(RSTRUCT_LEN(self));
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | /*
|
---|
| 698 | * call-seq:
|
---|
| 699 | * struct.to_a -> array
|
---|
| 700 | * struct.values -> array
|
---|
| 701 | *
|
---|
| 702 | * Create an array from struct values.
|
---|
| 703 | */
|
---|
| 704 | static mrb_value
|
---|
| 705 | mrb_struct_to_a(mrb_state *mrb, mrb_value self)
|
---|
| 706 | {
|
---|
| 707 | return mrb_ary_new_from_values(mrb, RSTRUCT_LEN(self), RSTRUCT_PTR(self));
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | /*
|
---|
| 711 | * call-seq:
|
---|
| 712 | * struct.to_h -> hash
|
---|
| 713 | *
|
---|
| 714 | * Create a hash from member names and struct values.
|
---|
| 715 | */
|
---|
| 716 | static mrb_value
|
---|
| 717 | mrb_struct_to_h(mrb_state *mrb, mrb_value self)
|
---|
| 718 | {
|
---|
| 719 | mrb_value members, ret;
|
---|
| 720 | mrb_int i;
|
---|
| 721 |
|
---|
| 722 | members = struct_s_members(mrb, mrb_class(mrb, self));
|
---|
| 723 | ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members));
|
---|
| 724 |
|
---|
| 725 | for (i = 0; i < RARRAY_LEN(members); ++i) {
|
---|
| 726 | mrb_hash_set(mrb, ret, RARRAY_PTR(members)[i], RSTRUCT_PTR(self)[i]);
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | return ret;
|
---|
| 730 | }
|
---|
| 731 |
|
---|
| 732 | static mrb_value
|
---|
| 733 | mrb_struct_values_at(mrb_state *mrb, mrb_value self)
|
---|
| 734 | {
|
---|
| 735 | mrb_int argc;
|
---|
| 736 | mrb_value *argv;
|
---|
| 737 |
|
---|
| 738 | mrb_get_args(mrb, "*", &argv, &argc);
|
---|
| 739 |
|
---|
| 740 | return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int);
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | /*
|
---|
| 744 | * A <code>Struct</code> is a convenient way to bundle a number of
|
---|
| 745 | * attributes together, using accessor methods, without having to write
|
---|
| 746 | * an explicit class.
|
---|
| 747 | *
|
---|
| 748 | * The <code>Struct</code> class is a generator of specific classes,
|
---|
| 749 | * each one of which is defined to hold a set of variables and their
|
---|
| 750 | * accessors. In these examples, we'll call the generated class
|
---|
| 751 | * "<i>Customer</i>Class," and we'll show an example instance of that
|
---|
| 752 | * class as "<i>Customer</i>Inst."
|
---|
| 753 | *
|
---|
| 754 | * In the descriptions that follow, the parameter <i>symbol</i> refers
|
---|
| 755 | * to a symbol, which is either a quoted string or a
|
---|
| 756 | * <code>Symbol</code> (such as <code>:name</code>).
|
---|
| 757 | */
|
---|
| 758 | void
|
---|
| 759 | mrb_mruby_struct_gem_init(mrb_state* mrb)
|
---|
| 760 | {
|
---|
| 761 | struct RClass *st;
|
---|
| 762 | st = mrb_define_class(mrb, "Struct", mrb->object_class);
|
---|
| 763 |
|
---|
| 764 | mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, MRB_ARGS_ANY()); /* 15.2.18.3.1 */
|
---|
| 765 |
|
---|
| 766 | mrb_define_method(mrb, st, "==", mrb_struct_equal, MRB_ARGS_REQ(1)); /* 15.2.18.4.1 */
|
---|
| 767 | mrb_define_method(mrb, st, "[]", mrb_struct_aref, MRB_ARGS_REQ(1)); /* 15.2.18.4.2 */
|
---|
| 768 | mrb_define_method(mrb, st, "[]=", mrb_struct_aset, MRB_ARGS_REQ(2)); /* 15.2.18.4.3 */
|
---|
| 769 | mrb_define_method(mrb, st, "members", mrb_struct_members, MRB_ARGS_NONE()); /* 15.2.18.4.6 */
|
---|
| 770 | mrb_define_method(mrb, st, "initialize", mrb_struct_initialize, MRB_ARGS_ANY()); /* 15.2.18.4.8 */
|
---|
| 771 | mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, MRB_ARGS_REQ(1)); /* 15.2.18.4.9 */
|
---|
| 772 | mrb_define_method(mrb, st, "eql?", mrb_struct_eql, MRB_ARGS_REQ(1)); /* 15.2.18.4.12(x) */
|
---|
| 773 |
|
---|
| 774 | mrb_define_method(mrb, st, "size", mrb_struct_len, MRB_ARGS_NONE());
|
---|
| 775 | mrb_define_method(mrb, st, "length", mrb_struct_len, MRB_ARGS_NONE());
|
---|
| 776 | mrb_define_method(mrb, st, "to_a", mrb_struct_to_a, MRB_ARGS_NONE());
|
---|
| 777 | mrb_define_method(mrb, st, "values", mrb_struct_to_a, MRB_ARGS_NONE());
|
---|
| 778 | mrb_define_method(mrb, st, "to_h", mrb_struct_to_h, MRB_ARGS_NONE());
|
---|
| 779 | mrb_define_method(mrb, st, "values_at", mrb_struct_values_at, MRB_ARGS_NONE());
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | void
|
---|
| 783 | mrb_mruby_struct_gem_final(mrb_state* mrb)
|
---|
| 784 | {
|
---|
| 785 | }
|
---|