Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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