Skip to content

HTTPS clone URL

Subversion checkout URL

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