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