Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-struct/src/struct.c

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