source: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-struct/src/struct.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 19.9 KB
Line 
1/*
2** struct.c - Struct class
3**
4** See Copyright Notice in mruby.h
5*/
6
7#include <string.h>
8#include <mruby.h>
9#include <mruby/array.h>
10#include <mruby/string.h>
11#include <mruby/class.h>
12#include <mruby/variable.h>
13#include <mruby/hash.h>
14#include <mruby/range.h>
15
16#define RSTRUCT_LEN(st) mrb_ary_ptr(st)->len
17#define RSTRUCT_PTR(st) mrb_ary_ptr(st)->ptr
18
19static struct RClass *
20struct_class(mrb_state *mrb)
21{
22 return mrb_class_get(mrb, "Struct");
23}
24
25static inline mrb_value
26struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id)
27{
28 struct RClass* kclass;
29 struct RClass* sclass = struct_class(mrb);
30 mrb_value ans;
31
32 for (;;) {
33 ans = mrb_iv_get(mrb, c, id);
34 if (!mrb_nil_p(ans)) return ans;
35 kclass = RCLASS_SUPER(c);
36 if (kclass == 0 || kclass == sclass)
37 return mrb_nil_value();
38 c = mrb_obj_value(kclass);
39 }
40}
41
42static mrb_value
43struct_s_members(mrb_state *mrb, struct RClass *klass)
44{
45 mrb_value members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern_lit(mrb, "__members__"));
46
47 if (mrb_nil_p(members)) {
48 mrb_raise(mrb, E_TYPE_ERROR, "uninitialized struct");
49 }
50 if (!mrb_array_p(members)) {
51 mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
52 }
53 return members;
54}
55
56static mrb_value
57struct_members(mrb_state *mrb, mrb_value s)
58{
59 mrb_value members = struct_s_members(mrb, mrb_obj_class(mrb, s));
60 if (!mrb_array_p(s)) {
61 mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
62 }
63 if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
64 if (RSTRUCT_LEN(s) == 0) { /* probably uninitialized */
65 mrb_ary_resize(mrb, s, RARRAY_LEN(members));
66 }
67 else {
68 mrb_raisef(mrb, E_TYPE_ERROR,
69 "struct size differs (%S required %S given)",
70 mrb_fixnum_value(RARRAY_LEN(members)), mrb_fixnum_value(RSTRUCT_LEN(s)));
71 }
72 }
73 return members;
74}
75
76static mrb_value
77mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
78{
79 mrb_value members, ary;
80
81 members = struct_s_members(mrb, mrb_class_ptr(klass));
82 ary = mrb_ary_new_capa(mrb, RARRAY_LEN(members));
83 mrb_ary_replace(mrb, ary, members);
84 return ary;
85}
86
87static void
88mrb_struct_modify(mrb_state *mrb, mrb_value strct)
89{
90 if (MRB_FROZEN_P(mrb_basic_ptr(strct))) {
91 mrb_raise(mrb, E_RUNTIME_ERROR, "can't modify frozen struct");
92 }
93
94 mrb_write_barrier(mrb, mrb_basic_ptr(strct));
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
110static mrb_value
111mrb_struct_members(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
116static mrb_value struct_aref_sym(mrb_state *mrb, mrb_value obj, mrb_sym id);
117
118static mrb_value
119mrb_struct_ref(mrb_state *mrb, mrb_value obj)
120{
121 return struct_aref_sym(mrb, obj, mrb->c->ci->mid);
122}
123
124static mrb_sym
125mrb_id_attrset(mrb_state *mrb, mrb_sym id)
126{
127 const char *name;
128 char *buf;
129 mrb_int len;
130 mrb_sym mid;
131
132 name = mrb_sym2name_len(mrb, id, &len);
133 buf = (char *)mrb_malloc(mrb, (size_t)len+2);
134 memcpy(buf, name, (size_t)len);
135 buf[len] = '=';
136 buf[len+1] = '\0';
137
138 mid = mrb_intern(mrb, buf, len+1);
139 mrb_free(mrb, buf);
140 return mid;
141}
142
143static mrb_value mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val);
144
145static mrb_value
146mrb_struct_set_m(mrb_state *mrb, mrb_value obj)
147{
148 mrb_value val;
149
150 const char *name;
151 mrb_int slen;
152 mrb_sym mid;
153
154 mrb_get_args(mrb, "o", &val);
155
156 /* get base id */
157 name = mrb_sym2name_len(mrb, mrb->c->ci->mid, &slen);
158 mid = mrb_intern(mrb, name, slen-1); /* omit last "=" */
159
160 return mrb_struct_aset_sym(mrb, obj, mid, val);
161}
162
163static mrb_bool
164is_local_id(mrb_state *mrb, const char *name)
165{
166 if (!name) return FALSE;
167 return !ISUPPER(name[0]);
168}
169
170static mrb_bool
171is_const_id(mrb_state *mrb, const char *name)
172{
173 if (!name) return FALSE;
174 return ISUPPER(name[0]);
175}
176
177static void
178make_struct_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c)
179{
180 const mrb_value *ptr_members = RARRAY_PTR(members);
181 mrb_int i;
182 mrb_int len = RARRAY_LEN(members);
183 int ai = mrb_gc_arena_save(mrb);
184
185 for (i=0; i<len; i++) {
186 mrb_sym id = mrb_symbol(ptr_members[i]);
187 const char *name = mrb_sym2name_len(mrb, id, NULL);
188
189 if (is_local_id(mrb, name) || is_const_id(mrb, name)) {
190 mrb_define_method_id(mrb, c, id, mrb_struct_ref, MRB_ARGS_NONE());
191 mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, MRB_ARGS_REQ(1));
192 mrb_gc_arena_restore(mrb, ai);
193 }
194 }
195}
196
197static mrb_value
198make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass)
199{
200 mrb_value nstr;
201 mrb_sym id;
202 struct RClass *c;
203
204 if (mrb_nil_p(name)) {
205 c = mrb_class_new(mrb, klass);
206 }
207 else {
208 /* old style: should we warn? */
209 name = mrb_str_to_str(mrb, name);
210 id = mrb_obj_to_sym(mrb, name);
211 if (!is_const_id(mrb, mrb_sym2name_len(mrb, id, NULL))) {
212 mrb_name_error(mrb, id, "identifier %S needs to be constant", name);
213 }
214 if (mrb_const_defined_at(mrb, mrb_obj_value(klass), id)) {
215 mrb_warn(mrb, "redefining constant Struct::%S", name);
216 mrb_const_remove(mrb, mrb_obj_value(klass), id);
217 }
218 c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
219 }
220 MRB_SET_INSTANCE_TT(c, MRB_TT_ARRAY);
221 nstr = mrb_obj_value(c);
222 mrb_iv_set(mrb, nstr, mrb_intern_lit(mrb, "__members__"), members);
223
224 mrb_define_class_method(mrb, c, "new", mrb_instance_new, MRB_ARGS_ANY());
225 mrb_define_class_method(mrb, c, "[]", mrb_instance_new, MRB_ARGS_ANY());
226 mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, MRB_ARGS_NONE());
227 /* RSTRUCT(nstr)->basic.c->super = c->c; */
228 make_struct_define_accessors(mrb, members, c);
229 return nstr;
230}
231
232/* 15.2.18.3.1 */
233/*
234 * call-seq:
235 * Struct.new( [aString] [, aSym]+> ) -> StructClass
236 * StructClass.new(arg, ...) -> obj
237 * StructClass[arg, ...] -> obj
238 *
239 * Creates a new class, named by <i>aString</i>, containing accessor
240 * methods for the given symbols. If the name <i>aString</i> is
241 * omitted, an anonymous structure class will be created. Otherwise,
242 * the name of this struct will appear as a constant in class
243 * <code>Struct</code>, so it must be unique for all
244 * <code>Struct</code>s in the system and should start with a capital
245 * letter. Assigning a structure class to a constant effectively gives
246 * the class the name of the constant.
247 *
248 * <code>Struct::new</code> returns a new <code>Class</code> object,
249 * which can then be used to create specific instances of the new
250 * structure. The number of actual parameters must be
251 * less than or equal to the number of attributes defined for this
252 * class; unset parameters default to <code>nil</code>. Passing too many
253 * parameters will raise an <code>ArgumentError</code>.
254 *
255 * The remaining methods listed in this section (class and instance)
256 * are defined for this generated class.
257 *
258 * # Create a structure with a name in Struct
259 * Struct.new("Customer", :name, :address) #=> Struct::Customer
260 * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main">
261 *
262 * # Create a structure named by its constant
263 * Customer = Struct.new(:name, :address) #=> Customer
264 * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
265 */
266static mrb_value
267mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
268{
269 mrb_value name, rest;
270 mrb_value *pargv;
271 mrb_int argcnt;
272 mrb_int i;
273 mrb_value b, st;
274 mrb_sym id;
275 mrb_value *argv;
276 mrb_int argc;
277
278 name = mrb_nil_value();
279 rest = mrb_nil_value();
280 mrb_get_args(mrb, "*&", &argv, &argc, &b);
281 if (argc == 0) { /* special case to avoid crash */
282 rest = mrb_ary_new(mrb);
283 }
284 else {
285 if (argc > 0) name = argv[0];
286 pargv = &argv[1];
287 argcnt = argc-1;
288 if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
289 /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
290 name = mrb_nil_value();
291 pargv = &argv[0];
292 argcnt++;
293 }
294 rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
295 for (i=0; i<RARRAY_LEN(rest); i++) {
296 id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]);
297 mrb_ary_set(mrb, rest, i, mrb_symbol_value(id));
298 }
299 }
300 st = make_struct(mrb, name, rest, mrb_class_ptr(klass));
301 if (!mrb_nil_p(b)) {
302 mrb_yield_with_class(mrb, b, 1, &st, st, mrb_class_ptr(st));
303 }
304
305 return st;
306}
307
308static mrb_int
309num_members(mrb_state *mrb, struct RClass *klass)
310{
311 mrb_value members;
312
313 members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern_lit(mrb, "__members__"));
314 if (!mrb_array_p(members)) {
315 mrb_raise(mrb, E_TYPE_ERROR, "broken members");
316 }
317 return RARRAY_LEN(members);
318}
319
320/* 15.2.18.4.8 */
321/*
322 */
323static mrb_value
324mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb_value self)
325{
326 struct RClass *klass = mrb_obj_class(mrb, self);
327 mrb_int i, n;
328
329 n = num_members(mrb, klass);
330 if (n < argc) {
331 mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
332 }
333
334 for (i = 0; i < argc; i++) {
335 mrb_ary_set(mrb, self, i, argv[i]);
336 }
337 for (i = argc; i < n; i++) {
338 mrb_ary_set(mrb, self, i, mrb_nil_value());
339 }
340 return self;
341}
342
343static mrb_value
344mrb_struct_initialize(mrb_state *mrb, mrb_value self)
345{
346 mrb_value *argv;
347 mrb_int argc;
348
349 mrb_get_args(mrb, "*", &argv, &argc);
350 return mrb_struct_initialize_withArg(mrb, argc, argv, self);
351}
352
353/* 15.2.18.4.9 */
354/* :nodoc: */
355static mrb_value
356mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
357{
358 mrb_value s;
359
360 mrb_get_args(mrb, "o", &s);
361
362 if (mrb_obj_equal(mrb, copy, s)) return copy;
363 if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) {
364 mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
365 }
366 if (!mrb_array_p(s)) {
367 mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
368 }
369 mrb_ary_replace(mrb, copy, s);
370 return copy;
371}
372
373static mrb_value
374struct_aref_sym(mrb_state *mrb, mrb_value obj, mrb_sym id)
375{
376 mrb_value members, *ptr;
377 const mrb_value *ptr_members;
378 mrb_int i, len;
379
380 members = struct_members(mrb, obj);
381 ptr_members = RARRAY_PTR(members);
382 len = RARRAY_LEN(members);
383 ptr = RSTRUCT_PTR(obj);
384 for (i=0; i<len; i++) {
385 mrb_value slot = ptr_members[i];
386 if (mrb_symbol_p(slot) && mrb_symbol(slot) == id) {
387 return ptr[i];
388 }
389 }
390 mrb_raisef(mrb, E_INDEX_ERROR, "'%S' is not a struct member", mrb_sym2str(mrb, id));
391 return mrb_nil_value(); /* not reached */
392}
393
394static mrb_value
395struct_aref_int(mrb_state *mrb, mrb_value s, mrb_int i)
396{
397 if (i < 0) i = RSTRUCT_LEN(s) + i;
398 if (i < 0)
399 mrb_raisef(mrb, E_INDEX_ERROR,
400 "offset %S too small for struct(size:%S)",
401 mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
402 if (RSTRUCT_LEN(s) <= i)
403 mrb_raisef(mrb, E_INDEX_ERROR,
404 "offset %S too large for struct(size:%S)",
405 mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
406 return RSTRUCT_PTR(s)[i];
407}
408
409/* 15.2.18.4.2 */
410/*
411 * call-seq:
412 * struct[symbol] -> anObject
413 * struct[fixnum] -> anObject
414 *
415 * Attribute Reference---Returns the value of the instance variable
416 * named by <i>symbol</i>, or indexed (0..length-1) by
417 * <i>fixnum</i>. Will raise <code>NameError</code> if the named
418 * variable does not exist, or <code>IndexError</code> if the index is
419 * out of range.
420 *
421 * Customer = Struct.new(:name, :address, :zip)
422 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
423 *
424 * joe["name"] #=> "Joe Smith"
425 * joe[:name] #=> "Joe Smith"
426 * joe[0] #=> "Joe Smith"
427 */
428static mrb_value
429mrb_struct_aref(mrb_state *mrb, mrb_value s)
430{
431 mrb_value idx;
432
433 mrb_get_args(mrb, "o", &idx);
434 if (mrb_string_p(idx)) {
435 mrb_value sym = mrb_check_intern_str(mrb, idx);
436
437 if (mrb_nil_p(sym)) {
438 mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%S' in struct", idx);
439 }
440 idx = sym;
441 }
442 if (mrb_symbol_p(idx)) {
443 return struct_aref_sym(mrb, s, mrb_symbol(idx));
444 }
445 return struct_aref_int(mrb, s, mrb_int(mrb, idx));
446}
447
448static mrb_value
449mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
450{
451 mrb_value members, *ptr;
452 const mrb_value *ptr_members;
453 mrb_int i, len;
454
455 members = struct_members(mrb, s);
456 len = RARRAY_LEN(members);
457 ptr = RSTRUCT_PTR(s);
458 ptr_members = RARRAY_PTR(members);
459 for (i=0; i<len; i++) {
460 if (mrb_symbol(ptr_members[i]) == id) {
461 mrb_struct_modify(mrb, s);
462 ptr[i] = val;
463 return val;
464 }
465 }
466 mrb_name_error(mrb, id, "no member '%S' in struct", mrb_sym2str(mrb, id));
467 return val; /* not reach */
468}
469
470/* 15.2.18.4.3 */
471/*
472 * call-seq:
473 * struct[symbol] = obj -> obj
474 * struct[fixnum] = obj -> obj
475 *
476 * Attribute Assignment---Assigns to the instance variable named by
477 * <i>symbol</i> or <i>fixnum</i> the value <i>obj</i> and
478 * returns it. Will raise a <code>NameError</code> if the named
479 * variable does not exist, or an <code>IndexError</code> if the index
480 * is out of range.
481 *
482 * Customer = Struct.new(:name, :address, :zip)
483 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
484 *
485 * joe["name"] = "Luke"
486 * joe[:zip] = "90210"
487 *
488 * joe.name #=> "Luke"
489 * joe.zip #=> "90210"
490 */
491
492static mrb_value
493mrb_struct_aset(mrb_state *mrb, mrb_value s)
494{
495 mrb_int i;
496 mrb_value idx;
497 mrb_value val;
498
499 mrb_get_args(mrb, "oo", &idx, &val);
500
501 if (mrb_string_p(idx)) {
502 mrb_value sym = mrb_check_intern_str(mrb, idx);
503
504 if (mrb_nil_p(sym)) {
505 mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%S' in struct", idx);
506 }
507 idx = sym;
508 }
509 if (mrb_symbol_p(idx)) {
510 return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val);
511 }
512
513 i = mrb_int(mrb, idx);
514 if (i < 0) i = RSTRUCT_LEN(s) + i;
515 if (i < 0) {
516 mrb_raisef(mrb, E_INDEX_ERROR,
517 "offset %S too small for struct(size:%S)",
518 mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
519 }
520 if (RSTRUCT_LEN(s) <= i) {
521 mrb_raisef(mrb, E_INDEX_ERROR,
522 "offset %S too large for struct(size:%S)",
523 mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
524 }
525 mrb_struct_modify(mrb, s);
526 return RSTRUCT_PTR(s)[i] = val;
527}
528
529/* 15.2.18.4.1 */
530/*
531 * call-seq:
532 * struct == other_struct -> true or false
533 *
534 * Equality---Returns <code>true</code> if <i>other_struct</i> is
535 * equal to this one: they must be of the same class as generated by
536 * <code>Struct::new</code>, and the values of all instance variables
537 * must be equal (according to <code>Object#==</code>).
538 *
539 * Customer = Struct.new(:name, :address, :zip)
540 * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
541 * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
542 * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
543 * joe == joejr #=> true
544 * joe == jane #=> false
545 */
546
547static mrb_value
548mrb_struct_equal(mrb_state *mrb, mrb_value s)
549{
550 mrb_value s2;
551 mrb_value *ptr, *ptr2;
552 mrb_int i, len;
553
554 mrb_get_args(mrb, "o", &s2);
555 if (mrb_obj_equal(mrb, s, s2)) {
556 return mrb_true_value();
557 }
558 if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
559 return mrb_false_value();
560 }
561 if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
562 mrb_bug(mrb, "inconsistent struct"); /* should never happen */
563 }
564 ptr = RSTRUCT_PTR(s);
565 ptr2 = RSTRUCT_PTR(s2);
566 len = RSTRUCT_LEN(s);
567 for (i=0; i<len; i++) {
568 if (!mrb_equal(mrb, ptr[i], ptr2[i])) {
569 return mrb_false_value();
570 }
571 }
572
573 return mrb_true_value();
574}
575
576/* 15.2.18.4.12(x) */
577/*
578 * code-seq:
579 * struct.eql?(other) -> true or false
580 *
581 * Two structures are equal if they are the same object, or if all their
582 * fields are equal (using <code>eql?</code>).
583 */
584static mrb_value
585mrb_struct_eql(mrb_state *mrb, mrb_value s)
586{
587 mrb_value s2;
588 mrb_value *ptr, *ptr2;
589 mrb_int i, len;
590
591 mrb_get_args(mrb, "o", &s2);
592 if (mrb_obj_equal(mrb, s, s2)) {
593 return mrb_true_value();
594 }
595 if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
596 return mrb_false_value();
597 }
598 if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
599 mrb_bug(mrb, "inconsistent struct"); /* should never happen */
600 }
601 ptr = RSTRUCT_PTR(s);
602 ptr2 = RSTRUCT_PTR(s2);
603 len = RSTRUCT_LEN(s);
604 for (i=0; i<len; i++) {
605 if (!mrb_eql(mrb, ptr[i], ptr2[i])) {
606 return mrb_false_value();
607 }
608 }
609
610 return mrb_true_value();
611}
612
613/*
614 * call-seq:
615 * struct.length -> Fixnum
616 * struct.size -> Fixnum
617 *
618 * Returns number of struct members.
619 */
620static mrb_value
621mrb_struct_len(mrb_state *mrb, mrb_value self)
622{
623 return mrb_fixnum_value(RSTRUCT_LEN(self));
624}
625
626/*
627 * call-seq:
628 * struct.to_a -> array
629 * struct.values -> array
630 *
631 * Create an array from struct values.
632 */
633static mrb_value
634mrb_struct_to_a(mrb_state *mrb, mrb_value self)
635{
636 return mrb_ary_new_from_values(mrb, RSTRUCT_LEN(self), RSTRUCT_PTR(self));
637}
638
639/*
640 * call-seq:
641 * struct.to_h -> hash
642 *
643 * Create a hash from member names and struct values.
644 */
645static mrb_value
646mrb_struct_to_h(mrb_state *mrb, mrb_value self)
647{
648 mrb_value members, ret;
649 mrb_int i;
650
651 members = struct_members(mrb, self);
652 ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members));
653
654 for (i = 0; i < RARRAY_LEN(members); ++i) {
655 mrb_hash_set(mrb, ret, RARRAY_PTR(members)[i], RSTRUCT_PTR(self)[i]);
656 }
657
658 return ret;
659}
660
661static mrb_value
662mrb_struct_values_at(mrb_state *mrb, mrb_value self)
663{
664 mrb_int argc;
665 mrb_value *argv;
666
667 mrb_get_args(mrb, "*", &argv, &argc);
668
669 return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int);
670}
671
672/*
673 * A <code>Struct</code> is a convenient way to bundle a number of
674 * attributes together, using accessor methods, without having to write
675 * an explicit class.
676 *
677 * The <code>Struct</code> class is a generator of specific classes,
678 * each one of which is defined to hold a set of variables and their
679 * accessors. In these examples, we'll call the generated class
680 * "<i>Customer</i>Class," and we'll show an example instance of that
681 * class as "<i>Customer</i>Inst."
682 *
683 * In the descriptions that follow, the parameter <i>symbol</i> refers
684 * to a symbol, which is either a quoted string or a
685 * <code>Symbol</code> (such as <code>:name</code>).
686 */
687void
688mrb_mruby_struct_gem_init(mrb_state* mrb)
689{
690 struct RClass *st;
691 st = mrb_define_class(mrb, "Struct", mrb->object_class);
692 MRB_SET_INSTANCE_TT(st, MRB_TT_ARRAY);
693
694 mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, MRB_ARGS_ANY()); /* 15.2.18.3.1 */
695
696 mrb_define_method(mrb, st, "==", mrb_struct_equal, MRB_ARGS_REQ(1)); /* 15.2.18.4.1 */
697 mrb_define_method(mrb, st, "[]", mrb_struct_aref, MRB_ARGS_REQ(1)); /* 15.2.18.4.2 */
698 mrb_define_method(mrb, st, "[]=", mrb_struct_aset, MRB_ARGS_REQ(2)); /* 15.2.18.4.3 */
699 mrb_define_method(mrb, st, "members", mrb_struct_members, MRB_ARGS_NONE()); /* 15.2.18.4.6 */
700 mrb_define_method(mrb, st, "initialize", mrb_struct_initialize, MRB_ARGS_ANY()); /* 15.2.18.4.8 */
701 mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, MRB_ARGS_REQ(1)); /* 15.2.18.4.9 */
702 mrb_define_method(mrb, st, "eql?", mrb_struct_eql, MRB_ARGS_REQ(1)); /* 15.2.18.4.12(x) */
703
704 mrb_define_method(mrb, st, "size", mrb_struct_len, MRB_ARGS_NONE());
705 mrb_define_method(mrb, st, "length", mrb_struct_len, MRB_ARGS_NONE());
706 mrb_define_method(mrb, st, "to_a", mrb_struct_to_a, MRB_ARGS_NONE());
707 mrb_define_method(mrb, st, "values", mrb_struct_to_a, MRB_ARGS_NONE());
708 mrb_define_method(mrb, st, "to_h", mrb_struct_to_h, MRB_ARGS_NONE());
709 mrb_define_method(mrb, st, "values_at", mrb_struct_values_at, MRB_ARGS_ANY());
710}
711
712void
713mrb_mruby_struct_gem_final(mrb_state* mrb)
714{
715}
Note: See TracBrowser for help on using the repository browser.