Skip to content

HTTPS clone URL

Subversion checkout URL

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