Skip to content

HTTPS clone URL

Subversion checkout URL

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