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