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