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 | #include <mruby/proc.h>
|
---|
16 |
|
---|
17 | #define RSTRUCT_LEN(st) RARRAY_LEN(st)
|
---|
18 | #define RSTRUCT_PTR(st) RARRAY_PTR(st)
|
---|
19 |
|
---|
20 | static struct RClass *
|
---|
21 | struct_class(mrb_state *mrb)
|
---|
22 | {
|
---|
23 | return mrb_class_get(mrb, "Struct");
|
---|
24 | }
|
---|
25 |
|
---|
26 | static inline mrb_value
|
---|
27 | struct_ivar_get(mrb_state *mrb, mrb_value cls, mrb_sym id)
|
---|
28 | {
|
---|
29 | struct RClass* c = mrb_class_ptr(cls);
|
---|
30 | struct RClass* sclass = struct_class(mrb);
|
---|
31 | mrb_value ans;
|
---|
32 |
|
---|
33 | for (;;) {
|
---|
34 | ans = mrb_iv_get(mrb, mrb_obj_value(c), id);
|
---|
35 | if (!mrb_nil_p(ans)) return ans;
|
---|
36 | c = c->super;
|
---|
37 | if (c == sclass || c == 0)
|
---|
38 | return mrb_nil_value();
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | static mrb_value
|
---|
43 | struct_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 |
|
---|
56 | static mrb_value
|
---|
57 | struct_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 (%i required %i given)",
|
---|
70 | RARRAY_LEN(members), RSTRUCT_LEN(s));
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return members;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static mrb_value
|
---|
77 | mrb_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 |
|
---|
87 | static void
|
---|
88 | mrb_struct_modify(mrb_state *mrb, mrb_value strct)
|
---|
89 | {
|
---|
90 | mrb_check_frozen(mrb, mrb_basic_ptr(strct));
|
---|
91 | mrb_write_barrier(mrb, mrb_basic_ptr(strct));
|
---|
92 | }
|
---|
93 |
|
---|
94 | /* 15.2.18.4.6 */
|
---|
95 | /*
|
---|
96 | * call-seq:
|
---|
97 | * struct.members -> array
|
---|
98 | *
|
---|
99 | * Returns an array of strings representing the names of the instance
|
---|
100 | * variables.
|
---|
101 | *
|
---|
102 | * Customer = Struct.new(:name, :address, :zip)
|
---|
103 | * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
104 | * joe.members #=> [:name, :address, :zip]
|
---|
105 | */
|
---|
106 |
|
---|
107 | static mrb_value
|
---|
108 | mrb_struct_members(mrb_state *mrb, mrb_value obj)
|
---|
109 | {
|
---|
110 | return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj)));
|
---|
111 | }
|
---|
112 |
|
---|
113 | static mrb_value
|
---|
114 | mrb_struct_ref(mrb_state *mrb, mrb_value obj)
|
---|
115 | {
|
---|
116 | mrb_int i = mrb_fixnum(mrb_proc_cfunc_env_get(mrb, 0));
|
---|
117 | mrb_value *ptr = RSTRUCT_PTR(obj);
|
---|
118 |
|
---|
119 | if (!ptr) return mrb_nil_value();
|
---|
120 | return ptr[i];
|
---|
121 | }
|
---|
122 |
|
---|
123 | static mrb_sym
|
---|
124 | mrb_id_attrset(mrb_state *mrb, mrb_sym id)
|
---|
125 | {
|
---|
126 | #define ONSTACK_ALLOC_MAX 32
|
---|
127 | #define ONSTACK_STRLEN_MAX (ONSTACK_ALLOC_MAX - 1) /* '=' character */
|
---|
128 |
|
---|
129 | const char *name;
|
---|
130 | char *buf;
|
---|
131 | mrb_int len;
|
---|
132 | mrb_sym mid;
|
---|
133 | char onstack[ONSTACK_ALLOC_MAX];
|
---|
134 |
|
---|
135 | name = mrb_sym_name_len(mrb, id, &len);
|
---|
136 | if (len > ONSTACK_STRLEN_MAX) {
|
---|
137 | buf = (char *)mrb_malloc(mrb, (size_t)len+1);
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | buf = onstack;
|
---|
141 | }
|
---|
142 | memcpy(buf, name, (size_t)len);
|
---|
143 | buf[len] = '=';
|
---|
144 |
|
---|
145 | mid = mrb_intern(mrb, buf, len+1);
|
---|
146 | if (buf != onstack) {
|
---|
147 | mrb_free(mrb, buf);
|
---|
148 | }
|
---|
149 | return mid;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static mrb_value
|
---|
153 | mrb_struct_set_m(mrb_state *mrb, mrb_value obj)
|
---|
154 | {
|
---|
155 | mrb_int i = mrb_fixnum(mrb_proc_cfunc_env_get(mrb, 0));
|
---|
156 | mrb_value *ptr;
|
---|
157 | mrb_value val;
|
---|
158 |
|
---|
159 | mrb_get_args(mrb, "o", &val);
|
---|
160 | mrb_struct_modify(mrb, obj);
|
---|
161 | ptr = RSTRUCT_PTR(obj);
|
---|
162 | if (ptr == NULL || i >= RSTRUCT_LEN(obj)) {
|
---|
163 | mrb_ary_set(mrb, obj, i, val);
|
---|
164 | }
|
---|
165 | else {
|
---|
166 | ptr[i] = val;
|
---|
167 | }
|
---|
168 | return val;
|
---|
169 | }
|
---|
170 |
|
---|
171 | static void
|
---|
172 | make_struct_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c)
|
---|
173 | {
|
---|
174 | const mrb_value *ptr_members = RARRAY_PTR(members);
|
---|
175 | mrb_int i;
|
---|
176 | mrb_int len = RARRAY_LEN(members);
|
---|
177 | int ai = mrb_gc_arena_save(mrb);
|
---|
178 |
|
---|
179 | for (i=0; i<len; i++) {
|
---|
180 | mrb_sym id = mrb_symbol(ptr_members[i]);
|
---|
181 | mrb_method_t m;
|
---|
182 | mrb_value at = mrb_fixnum_value(i);
|
---|
183 | struct RProc *aref = mrb_proc_new_cfunc_with_env(mrb, mrb_struct_ref, 1, &at);
|
---|
184 | struct RProc *aset = mrb_proc_new_cfunc_with_env(mrb, mrb_struct_set_m, 1, &at);
|
---|
185 | MRB_METHOD_FROM_PROC(m, aref);
|
---|
186 | mrb_define_method_raw(mrb, c, id, m);
|
---|
187 | MRB_METHOD_FROM_PROC(m, aset);
|
---|
188 | mrb_define_method_raw(mrb, c, mrb_id_attrset(mrb, id), m);
|
---|
189 | mrb_gc_arena_restore(mrb, ai);
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | static mrb_value
|
---|
194 | make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass *klass)
|
---|
195 | {
|
---|
196 | mrb_value nstr;
|
---|
197 | mrb_sym id;
|
---|
198 | struct RClass *c;
|
---|
199 |
|
---|
200 | if (mrb_nil_p(name)) {
|
---|
201 | c = mrb_class_new(mrb, klass);
|
---|
202 | }
|
---|
203 | else {
|
---|
204 | /* old style: should we warn? */
|
---|
205 | mrb_to_str(mrb, name);
|
---|
206 | id = mrb_obj_to_sym(mrb, name);
|
---|
207 | if (!mrb_const_name_p(mrb, RSTRING_PTR(name), RSTRING_LEN(name))) {
|
---|
208 | mrb_name_error(mrb, id, "identifier %v needs to be constant", name);
|
---|
209 | }
|
---|
210 | if (mrb_const_defined_at(mrb, mrb_obj_value(klass), id)) {
|
---|
211 | mrb_warn(mrb, "redefining constant Struct::%v", name);
|
---|
212 | mrb_const_remove(mrb, mrb_obj_value(klass), id);
|
---|
213 | }
|
---|
214 | c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass);
|
---|
215 | }
|
---|
216 | MRB_SET_INSTANCE_TT(c, MRB_TT_ARRAY);
|
---|
217 | nstr = mrb_obj_value(c);
|
---|
218 | mrb_iv_set(mrb, nstr, mrb_intern_lit(mrb, "__members__"), members);
|
---|
219 |
|
---|
220 | mrb_define_class_method(mrb, c, "new", mrb_instance_new, MRB_ARGS_ANY());
|
---|
221 | mrb_define_class_method(mrb, c, "[]", mrb_instance_new, MRB_ARGS_ANY());
|
---|
222 | mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, MRB_ARGS_NONE());
|
---|
223 | /* RSTRUCT(nstr)->basic.c->super = c->c; */
|
---|
224 | make_struct_define_accessors(mrb, members, c);
|
---|
225 | return nstr;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /* 15.2.18.3.1 */
|
---|
229 | /*
|
---|
230 | * call-seq:
|
---|
231 | * Struct.new( [aString] [, aSym]+> ) -> StructClass
|
---|
232 | * StructClass.new(arg, ...) -> obj
|
---|
233 | * StructClass[arg, ...] -> obj
|
---|
234 | *
|
---|
235 | * Creates a new class, named by <i>aString</i>, containing accessor
|
---|
236 | * methods for the given symbols. If the name <i>aString</i> is
|
---|
237 | * omitted, an anonymous structure class will be created. Otherwise,
|
---|
238 | * the name of this struct will appear as a constant in class
|
---|
239 | * <code>Struct</code>, so it must be unique for all
|
---|
240 | * <code>Struct</code>s in the system and should start with a capital
|
---|
241 | * letter. Assigning a structure class to a constant effectively gives
|
---|
242 | * the class the name of the constant.
|
---|
243 | *
|
---|
244 | * <code>Struct::new</code> returns a new <code>Class</code> object,
|
---|
245 | * which can then be used to create specific instances of the new
|
---|
246 | * structure. The number of actual parameters must be
|
---|
247 | * less than or equal to the number of attributes defined for this
|
---|
248 | * class; unset parameters default to <code>nil</code>. Passing too many
|
---|
249 | * parameters will raise an <code>ArgumentError</code>.
|
---|
250 | *
|
---|
251 | * The remaining methods listed in this section (class and instance)
|
---|
252 | * are defined for this generated class.
|
---|
253 | *
|
---|
254 | * # Create a structure with a name in Struct
|
---|
255 | * Struct.new("Customer", :name, :address) #=> Struct::Customer
|
---|
256 | * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main">
|
---|
257 | *
|
---|
258 | * # Create a structure named by its constant
|
---|
259 | * Customer = Struct.new(:name, :address) #=> Customer
|
---|
260 | * Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
|
---|
261 | */
|
---|
262 | static mrb_value
|
---|
263 | mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
|
---|
264 | {
|
---|
265 | mrb_value name, rest;
|
---|
266 | mrb_value *pargv;
|
---|
267 | mrb_int argcnt;
|
---|
268 | mrb_int i;
|
---|
269 | mrb_value b, st;
|
---|
270 | mrb_sym id;
|
---|
271 | mrb_value *argv;
|
---|
272 | mrb_int argc;
|
---|
273 |
|
---|
274 | name = mrb_nil_value();
|
---|
275 | mrb_get_args(mrb, "*&", &argv, &argc, &b);
|
---|
276 | if (argc == 0) { /* special case to avoid crash */
|
---|
277 | mrb_argnum_error(mrb, argc, 1, -1);
|
---|
278 | }
|
---|
279 | else {
|
---|
280 | pargv = argv;
|
---|
281 | argcnt = argc;
|
---|
282 | if (argc > 0) {
|
---|
283 | name = argv[0];
|
---|
284 | if (mrb_symbol_p(name)) {
|
---|
285 | /* 1stArgument:symbol -> name=nil rest=argv[0..n] */
|
---|
286 | name = mrb_nil_value();
|
---|
287 | }
|
---|
288 | else {
|
---|
289 | pargv++;
|
---|
290 | argcnt--;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
|
---|
294 | for (i=0; i<argcnt; i++) {
|
---|
295 | id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]);
|
---|
296 | mrb_ary_set(mrb, rest, i, mrb_symbol_value(id));
|
---|
297 | }
|
---|
298 | st = make_struct(mrb, name, rest, mrb_class_ptr(klass));
|
---|
299 | if (!mrb_nil_p(b)) {
|
---|
300 | mrb_yield_with_class(mrb, b, 1, &st, st, mrb_class_ptr(st));
|
---|
301 | }
|
---|
302 |
|
---|
303 | return st;
|
---|
304 | }
|
---|
305 | /* not reached */
|
---|
306 | return mrb_nil_value();
|
---|
307 | }
|
---|
308 |
|
---|
309 | static mrb_int
|
---|
310 | num_members(mrb_state *mrb, struct RClass *klass)
|
---|
311 | {
|
---|
312 | mrb_value members;
|
---|
313 |
|
---|
314 | members = struct_ivar_get(mrb, mrb_obj_value(klass), mrb_intern_lit(mrb, "__members__"));
|
---|
315 | if (!mrb_array_p(members)) {
|
---|
316 | mrb_raise(mrb, E_TYPE_ERROR, "broken members");
|
---|
317 | }
|
---|
318 | return RARRAY_LEN(members);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* 15.2.18.4.8 */
|
---|
322 | /*
|
---|
323 | */
|
---|
324 | static mrb_value
|
---|
325 | mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb_value self)
|
---|
326 | {
|
---|
327 | struct RClass *klass = mrb_obj_class(mrb, self);
|
---|
328 | mrb_int i, n;
|
---|
329 |
|
---|
330 | n = num_members(mrb, klass);
|
---|
331 | if (n < argc) {
|
---|
332 | mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
|
---|
333 | }
|
---|
334 |
|
---|
335 | for (i = 0; i < argc; i++) {
|
---|
336 | mrb_ary_set(mrb, self, i, argv[i]);
|
---|
337 | }
|
---|
338 | for (i = argc; i < n; i++) {
|
---|
339 | mrb_ary_set(mrb, self, i, mrb_nil_value());
|
---|
340 | }
|
---|
341 | return self;
|
---|
342 | }
|
---|
343 |
|
---|
344 | static mrb_value
|
---|
345 | mrb_struct_initialize(mrb_state *mrb, mrb_value self)
|
---|
346 | {
|
---|
347 | mrb_value *argv;
|
---|
348 | mrb_int argc;
|
---|
349 |
|
---|
350 | mrb_get_args(mrb, "*!", &argv, &argc);
|
---|
351 | return mrb_struct_initialize_withArg(mrb, argc, argv, self);
|
---|
352 | }
|
---|
353 |
|
---|
354 | /* 15.2.18.4.9 */
|
---|
355 | /* :nodoc: */
|
---|
356 | static mrb_value
|
---|
357 | mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
|
---|
358 | {
|
---|
359 | mrb_value s;
|
---|
360 |
|
---|
361 | mrb_get_args(mrb, "o", &s);
|
---|
362 |
|
---|
363 | if (mrb_obj_equal(mrb, copy, s)) return copy;
|
---|
364 | if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) {
|
---|
365 | mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
|
---|
366 | }
|
---|
367 | if (!mrb_array_p(s)) {
|
---|
368 | mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct");
|
---|
369 | }
|
---|
370 | mrb_ary_replace(mrb, copy, s);
|
---|
371 | return copy;
|
---|
372 | }
|
---|
373 |
|
---|
374 | static mrb_value
|
---|
375 | struct_aref_sym(mrb_state *mrb, mrb_value obj, mrb_sym id)
|
---|
376 | {
|
---|
377 | mrb_value members, *ptr;
|
---|
378 | const mrb_value *ptr_members;
|
---|
379 | mrb_int i, len;
|
---|
380 |
|
---|
381 | members = struct_members(mrb, obj);
|
---|
382 | ptr_members = RARRAY_PTR(members);
|
---|
383 | len = RARRAY_LEN(members);
|
---|
384 | ptr = RSTRUCT_PTR(obj);
|
---|
385 | for (i=0; i<len; i++) {
|
---|
386 | mrb_value slot = ptr_members[i];
|
---|
387 | if (mrb_symbol_p(slot) && mrb_symbol(slot) == id) {
|
---|
388 | return ptr[i];
|
---|
389 | }
|
---|
390 | }
|
---|
391 | mrb_name_error(mrb, id, "no member '%n' in struct", id);
|
---|
392 | return mrb_nil_value(); /* not reached */
|
---|
393 | }
|
---|
394 |
|
---|
395 | static mrb_value
|
---|
396 | struct_aref_int(mrb_state *mrb, mrb_value s, mrb_int i)
|
---|
397 | {
|
---|
398 | mrb_int idx = i < 0 ? RSTRUCT_LEN(s) + i : i;
|
---|
399 |
|
---|
400 | if (idx < 0)
|
---|
401 | mrb_raisef(mrb, E_INDEX_ERROR,
|
---|
402 | "offset %i too small for struct(size:%i)", i, RSTRUCT_LEN(s));
|
---|
403 | if (RSTRUCT_LEN(s) <= idx)
|
---|
404 | mrb_raisef(mrb, E_INDEX_ERROR,
|
---|
405 | "offset %i too large for struct(size:%i)", i, RSTRUCT_LEN(s));
|
---|
406 | return RSTRUCT_PTR(s)[idx];
|
---|
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 | */
|
---|
428 | static mrb_value
|
---|
429 | mrb_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 '%v' 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 |
|
---|
448 | static mrb_value
|
---|
449 | mrb_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 '%n' in struct", 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 |
|
---|
492 | static mrb_value
|
---|
493 | mrb_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 '%v' 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 %i too small for struct(size:%i)", i, RSTRUCT_LEN(s));
|
---|
518 | }
|
---|
519 | if (RSTRUCT_LEN(s) <= i) {
|
---|
520 | mrb_raisef(mrb, E_INDEX_ERROR,
|
---|
521 | "offset %i too large for struct(size:%i)", i, RSTRUCT_LEN(s));
|
---|
522 | }
|
---|
523 | mrb_struct_modify(mrb, s);
|
---|
524 | return RSTRUCT_PTR(s)[i] = val;
|
---|
525 | }
|
---|
526 |
|
---|
527 | /* 15.2.18.4.1 */
|
---|
528 | /*
|
---|
529 | * call-seq:
|
---|
530 | * struct == other_struct -> true or false
|
---|
531 | *
|
---|
532 | * Equality---Returns <code>true</code> if <i>other_struct</i> is
|
---|
533 | * equal to this one: they must be of the same class as generated by
|
---|
534 | * <code>Struct::new</code>, and the values of all instance variables
|
---|
535 | * must be equal (according to <code>Object#==</code>).
|
---|
536 | *
|
---|
537 | * Customer = Struct.new(:name, :address, :zip)
|
---|
538 | * joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
539 | * joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
|
---|
540 | * jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
|
---|
541 | * joe == joejr #=> true
|
---|
542 | * joe == jane #=> false
|
---|
543 | */
|
---|
544 |
|
---|
545 | static mrb_value
|
---|
546 | mrb_struct_equal(mrb_state *mrb, mrb_value s)
|
---|
547 | {
|
---|
548 | mrb_value s2;
|
---|
549 | mrb_value *ptr, *ptr2;
|
---|
550 | mrb_int i, len;
|
---|
551 |
|
---|
552 | mrb_get_args(mrb, "o", &s2);
|
---|
553 | if (mrb_obj_equal(mrb, s, s2)) {
|
---|
554 | return mrb_true_value();
|
---|
555 | }
|
---|
556 | if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
|
---|
557 | return mrb_false_value();
|
---|
558 | }
|
---|
559 | if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
|
---|
560 | mrb_bug(mrb, "inconsistent struct"); /* should never happen */
|
---|
561 | }
|
---|
562 | ptr = RSTRUCT_PTR(s);
|
---|
563 | ptr2 = RSTRUCT_PTR(s2);
|
---|
564 | len = RSTRUCT_LEN(s);
|
---|
565 | for (i=0; i<len; i++) {
|
---|
566 | if (!mrb_equal(mrb, ptr[i], ptr2[i])) {
|
---|
567 | return mrb_false_value();
|
---|
568 | }
|
---|
569 | }
|
---|
570 |
|
---|
571 | return mrb_true_value();
|
---|
572 | }
|
---|
573 |
|
---|
574 | /* 15.2.18.4.12(x) */
|
---|
575 | /*
|
---|
576 | * code-seq:
|
---|
577 | * struct.eql?(other) -> true or false
|
---|
578 | *
|
---|
579 | * Two structures are equal if they are the same object, or if all their
|
---|
580 | * fields are equal (using <code>eql?</code>).
|
---|
581 | */
|
---|
582 | static mrb_value
|
---|
583 | mrb_struct_eql(mrb_state *mrb, mrb_value s)
|
---|
584 | {
|
---|
585 | mrb_value s2;
|
---|
586 | mrb_value *ptr, *ptr2;
|
---|
587 | mrb_int i, len;
|
---|
588 |
|
---|
589 | mrb_get_args(mrb, "o", &s2);
|
---|
590 | if (mrb_obj_equal(mrb, s, s2)) {
|
---|
591 | return mrb_true_value();
|
---|
592 | }
|
---|
593 | if (mrb_obj_class(mrb, s) != mrb_obj_class(mrb, s2)) {
|
---|
594 | return mrb_false_value();
|
---|
595 | }
|
---|
596 | if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
|
---|
597 | mrb_bug(mrb, "inconsistent struct"); /* should never happen */
|
---|
598 | }
|
---|
599 | ptr = RSTRUCT_PTR(s);
|
---|
600 | ptr2 = RSTRUCT_PTR(s2);
|
---|
601 | len = RSTRUCT_LEN(s);
|
---|
602 | for (i=0; i<len; i++) {
|
---|
603 | if (!mrb_eql(mrb, ptr[i], ptr2[i])) {
|
---|
604 | return mrb_false_value();
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | return mrb_true_value();
|
---|
609 | }
|
---|
610 |
|
---|
611 | /*
|
---|
612 | * call-seq:
|
---|
613 | * struct.length -> Fixnum
|
---|
614 | * struct.size -> Fixnum
|
---|
615 | *
|
---|
616 | * Returns number of struct members.
|
---|
617 | */
|
---|
618 | static mrb_value
|
---|
619 | mrb_struct_len(mrb_state *mrb, mrb_value self)
|
---|
620 | {
|
---|
621 | return mrb_fixnum_value(RSTRUCT_LEN(self));
|
---|
622 | }
|
---|
623 |
|
---|
624 | /*
|
---|
625 | * call-seq:
|
---|
626 | * struct.to_a -> array
|
---|
627 | * struct.values -> array
|
---|
628 | *
|
---|
629 | * Create an array from struct values.
|
---|
630 | */
|
---|
631 | static mrb_value
|
---|
632 | mrb_struct_to_a(mrb_state *mrb, mrb_value self)
|
---|
633 | {
|
---|
634 | return mrb_ary_new_from_values(mrb, RSTRUCT_LEN(self), RSTRUCT_PTR(self));
|
---|
635 | }
|
---|
636 |
|
---|
637 | /*
|
---|
638 | * call-seq:
|
---|
639 | * struct.to_h -> hash
|
---|
640 | *
|
---|
641 | * Create a hash from member names and struct values.
|
---|
642 | */
|
---|
643 | static mrb_value
|
---|
644 | mrb_struct_to_h(mrb_state *mrb, mrb_value self)
|
---|
645 | {
|
---|
646 | mrb_value members, ret;
|
---|
647 | mrb_int i;
|
---|
648 |
|
---|
649 | members = struct_members(mrb, self);
|
---|
650 | ret = mrb_hash_new_capa(mrb, RARRAY_LEN(members));
|
---|
651 |
|
---|
652 | for (i = 0; i < RARRAY_LEN(members); ++i) {
|
---|
653 | mrb_hash_set(mrb, ret, RARRAY_PTR(members)[i], RSTRUCT_PTR(self)[i]);
|
---|
654 | }
|
---|
655 |
|
---|
656 | return ret;
|
---|
657 | }
|
---|
658 |
|
---|
659 | static mrb_value
|
---|
660 | mrb_struct_values_at(mrb_state *mrb, mrb_value self)
|
---|
661 | {
|
---|
662 | mrb_int argc;
|
---|
663 | mrb_value *argv;
|
---|
664 |
|
---|
665 | mrb_get_args(mrb, "*", &argv, &argc);
|
---|
666 |
|
---|
667 | return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int);
|
---|
668 | }
|
---|
669 |
|
---|
670 | /*
|
---|
671 | * A <code>Struct</code> is a convenient way to bundle a number of
|
---|
672 | * attributes together, using accessor methods, without having to write
|
---|
673 | * an explicit class.
|
---|
674 | *
|
---|
675 | * The <code>Struct</code> class is a generator of specific classes,
|
---|
676 | * each one of which is defined to hold a set of variables and their
|
---|
677 | * accessors. In these examples, we'll call the generated class
|
---|
678 | * "<i>Customer</i>Class," and we'll show an example instance of that
|
---|
679 | * class as "<i>Customer</i>Inst."
|
---|
680 | *
|
---|
681 | * In the descriptions that follow, the parameter <i>symbol</i> refers
|
---|
682 | * to a symbol, which is either a quoted string or a
|
---|
683 | * <code>Symbol</code> (such as <code>:name</code>).
|
---|
684 | */
|
---|
685 | void
|
---|
686 | mrb_mruby_struct_gem_init(mrb_state* mrb)
|
---|
687 | {
|
---|
688 | struct RClass *st;
|
---|
689 | st = mrb_define_class(mrb, "Struct", mrb->object_class);
|
---|
690 | MRB_SET_INSTANCE_TT(st, MRB_TT_ARRAY);
|
---|
691 |
|
---|
692 | mrb_define_class_method(mrb, st, "new", mrb_struct_s_def, MRB_ARGS_ANY()); /* 15.2.18.3.1 */
|
---|
693 |
|
---|
694 | mrb_define_method(mrb, st, "==", mrb_struct_equal, MRB_ARGS_REQ(1)); /* 15.2.18.4.1 */
|
---|
695 | mrb_define_method(mrb, st, "[]", mrb_struct_aref, MRB_ARGS_REQ(1)); /* 15.2.18.4.2 */
|
---|
696 | mrb_define_method(mrb, st, "[]=", mrb_struct_aset, MRB_ARGS_REQ(2)); /* 15.2.18.4.3 */
|
---|
697 | mrb_define_method(mrb, st, "members", mrb_struct_members, MRB_ARGS_NONE()); /* 15.2.18.4.6 */
|
---|
698 | mrb_define_method(mrb, st, "initialize", mrb_struct_initialize, MRB_ARGS_ANY()); /* 15.2.18.4.8 */
|
---|
699 | mrb_define_method(mrb, st, "initialize_copy", mrb_struct_init_copy, MRB_ARGS_REQ(1)); /* 15.2.18.4.9 */
|
---|
700 | mrb_define_method(mrb, st, "eql?", mrb_struct_eql, MRB_ARGS_REQ(1)); /* 15.2.18.4.12(x) */
|
---|
701 |
|
---|
702 | mrb_define_method(mrb, st, "size", mrb_struct_len, MRB_ARGS_NONE());
|
---|
703 | mrb_define_method(mrb, st, "length", mrb_struct_len, MRB_ARGS_NONE());
|
---|
704 | mrb_define_method(mrb, st, "to_a", mrb_struct_to_a, MRB_ARGS_NONE());
|
---|
705 | mrb_define_method(mrb, st, "values", mrb_struct_to_a, MRB_ARGS_NONE());
|
---|
706 | mrb_define_method(mrb, st, "to_h", mrb_struct_to_h, MRB_ARGS_NONE());
|
---|
707 | mrb_define_method(mrb, st, "values_at", mrb_struct_values_at, MRB_ARGS_ANY());
|
---|
708 | }
|
---|
709 |
|
---|
710 | void
|
---|
711 | mrb_mruby_struct_gem_final(mrb_state* mrb)
|
---|
712 | {
|
---|
713 | }
|
---|