Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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