Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 562 lines (480 sloc) 12.826 kb
fedf489 2000-05-01
matz authored
1 /**********************************************************************
3db12e8 Initial revision
matz authored
2
3 struct.c -
4
5 $Author$
6 $Date$
7 created at: Tue Mar 22 18:44:30 JST 1995
8
8e5c3b2 * dir.c (dir_s_glob): supprt backslash escape of metacharacters
matz authored
9 Copyright (C) 1993-2001 Yukihiro Matsumoto
fedf489 2000-05-01
matz authored
10
11 **********************************************************************/
3db12e8 Initial revision
matz authored
12
13 #include "ruby.h"
14
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
15 VALUE rb_cStruct;
3db12e8 Initial revision
matz authored
16
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
17 static VALUE struct_alloc _((VALUE));
65a5162 1.4.0
matz authored
18
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
19 VALUE
20 rb_struct_iv_get(c, name)
21 VALUE c;
65a5162 1.4.0
matz authored
22 char *name;
23 {
24 ID id;
25
26 id = rb_intern(name);
27 for (;;) {
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
28 if (rb_ivar_defined(c, id))
29 return rb_ivar_get(c, id);
30 c = RCLASS(c)->super;
31 if (c == 0 || c == rb_cStruct)
65a5162 1.4.0
matz authored
32 return Qnil;
33 }
34 }
35
36 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
37 rb_struct_s_members(obj)
38 VALUE obj;
39 {
40 VALUE member, ary;
41 VALUE *p, *pend;
42
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
43 member = rb_struct_iv_get(obj, "__member__");
3db12e8 Initial revision
matz authored
44 if (NIL_P(member)) {
8b1de0b 2000-05-24
matz authored
45 rb_bug("uninitialized struct");
3db12e8 Initial revision
matz authored
46 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
47 ary = rb_ary_new2(RARRAY(member)->len);
48 p = RARRAY(member)->ptr; pend = p + RARRAY(member)->len;
3db12e8 Initial revision
matz authored
49 while (p < pend) {
7194267 2000-04-10
matz authored
50 rb_ary_push(ary, rb_str_new2(rb_id2name(SYM2ID(*p))));
3db12e8 Initial revision
matz authored
51 p++;
52 }
53
54 return ary;
55 }
56
57 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
58 rb_struct_members(obj)
3db12e8 Initial revision
matz authored
59 VALUE obj;
60 {
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
61 return rb_struct_s_members(rb_obj_class(obj));
3db12e8 Initial revision
matz authored
62 }
63
64 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
65 rb_struct_getmember(obj, id)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
66 VALUE obj;
3db12e8 Initial revision
matz authored
67 ID id;
68 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
69 VALUE member, slot;
65a5162 1.4.0
matz authored
70 long i;
3db12e8 Initial revision
matz authored
71
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
72 member = rb_struct_iv_get(rb_obj_class(obj), "__member__");
3db12e8 Initial revision
matz authored
73 if (NIL_P(member)) {
8b1de0b 2000-05-24
matz authored
74 rb_bug("uninitialized struct");
3db12e8 Initial revision
matz authored
75 }
8b1de0b 2000-05-24
matz authored
76 slot = ID2SYM(id);
3db12e8 Initial revision
matz authored
77 for (i=0; i<RARRAY(member)->len; i++) {
78 if (RARRAY(member)->ptr[i] == slot) {
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
79 return RSTRUCT(obj)->ptr[i];
3db12e8 Initial revision
matz authored
80 }
81 }
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
82 rb_name_error(id, "%s is not struct member", rb_id2name(id));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
83 return Qnil; /* not reached */
3db12e8 Initial revision
matz authored
84 }
85
86 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
87 rb_struct_ref(obj)
3db12e8 Initial revision
matz authored
88 VALUE obj;
89 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
90 return rb_struct_getmember(obj, rb_frame_last_func());
3db12e8 Initial revision
matz authored
91 }
92
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
93 static VALUE rb_struct_ref0(obj) VALUE obj; {return RSTRUCT(obj)->ptr[0];}
94 static VALUE rb_struct_ref1(obj) VALUE obj; {return RSTRUCT(obj)->ptr[1];}
95 static VALUE rb_struct_ref2(obj) VALUE obj; {return RSTRUCT(obj)->ptr[2];}
96 static VALUE rb_struct_ref3(obj) VALUE obj; {return RSTRUCT(obj)->ptr[3];}
97 static VALUE rb_struct_ref4(obj) VALUE obj; {return RSTRUCT(obj)->ptr[4];}
98 static VALUE rb_struct_ref5(obj) VALUE obj; {return RSTRUCT(obj)->ptr[5];}
99 static VALUE rb_struct_ref6(obj) VALUE obj; {return RSTRUCT(obj)->ptr[6];}
100 static VALUE rb_struct_ref7(obj) VALUE obj; {return RSTRUCT(obj)->ptr[7];}
101 static VALUE rb_struct_ref8(obj) VALUE obj; {return RSTRUCT(obj)->ptr[8];}
102 static VALUE rb_struct_ref9(obj) VALUE obj; {return RSTRUCT(obj)->ptr[9];}
3db12e8 Initial revision
matz authored
103
65a5162 1.4.0
matz authored
104 static VALUE (*ref_func[10])() = {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
105 rb_struct_ref0,
106 rb_struct_ref1,
107 rb_struct_ref2,
108 rb_struct_ref3,
109 rb_struct_ref4,
110 rb_struct_ref5,
111 rb_struct_ref6,
112 rb_struct_ref7,
113 rb_struct_ref8,
114 rb_struct_ref9,
3db12e8 Initial revision
matz authored
115 };
116
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
117 static void
118 rb_struct_modify(s)
119 VALUE s;
120 {
121 if (OBJ_FROZEN(s)) rb_error_frozen("Struct");
122 if (!OBJ_TAINTED(s) && rb_safe_level() >= 4)
123 rb_raise(rb_eSecurityError, "Insecure: can't modify Struct");
124 }
125
3db12e8 Initial revision
matz authored
126 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
127 rb_struct_set(obj, val)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
128 VALUE obj, val;
3db12e8 Initial revision
matz authored
129 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
130 VALUE member, slot;
65a5162 1.4.0
matz authored
131 long i;
3db12e8 Initial revision
matz authored
132
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
133 member = rb_struct_iv_get(rb_obj_class(obj), "__member__");
3db12e8 Initial revision
matz authored
134 if (NIL_P(member)) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
135 rb_bug("non-initialized struct");
3db12e8 Initial revision
matz authored
136 }
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
137 rb_struct_modify(obj);
3db12e8 Initial revision
matz authored
138 for (i=0; i<RARRAY(member)->len; i++) {
139 slot = RARRAY(member)->ptr[i];
7194267 2000-04-10
matz authored
140 if (rb_id_attrset(SYM2ID(slot)) == rb_frame_last_func()) {
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
141 return RSTRUCT(obj)->ptr[i] = val;
3db12e8 Initial revision
matz authored
142 }
143 }
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
144 rb_name_error(rb_frame_last_func(), "`%s' is not a struct member",
145 rb_id2name(rb_frame_last_func()));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
146 return Qnil; /* not reached */
3db12e8 Initial revision
matz authored
147 }
148
149 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
150 make_struct(name, member, klass)
151 VALUE name, member, klass;
3db12e8 Initial revision
matz authored
152 {
153 VALUE nstr;
154 ID id;
65a5162 1.4.0
matz authored
155 long i;
3db12e8 Initial revision
matz authored
156
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
157 if (NIL_P(name)) {
158 nstr = rb_class_new(klass);
159 }
160 else {
fd06a2a * eval.c (block_pass): should not downgrade safe level.
matz authored
161 char *cname = StringValuePtr(name);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
162 id = rb_intern(cname);
9d228b1 19991214
matz authored
163 if (!rb_is_const_id(id)) {
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
164 rb_name_error(id, "identifier %s needs to be constant", cname);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
165 }
166 nstr = rb_define_class_under(klass, cname, klass);
3db12e8 Initial revision
matz authored
167 }
65a5162 1.4.0
matz authored
168 rb_iv_set(nstr, "__size__", INT2NUM(RARRAY(member)->len));
3db12e8 Initial revision
matz authored
169 rb_iv_set(nstr, "__member__", member);
170
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
171 rb_define_singleton_method(nstr, "allocate", struct_alloc, 0);
172 rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
173 rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
174 rb_define_singleton_method(nstr, "members", rb_struct_s_members, 0);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
175 for (i=0; i< RARRAY(member)->len; i++) {
7194267 2000-04-10
matz authored
176 ID id = SYM2ID(RARRAY(member)->ptr[i]);
3db12e8 Initial revision
matz authored
177 if (i<10) {
178 rb_define_method_id(nstr, id, ref_func[i], 0);
179 }
180 else {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
181 rb_define_method_id(nstr, id, rb_struct_ref, 0);
3db12e8 Initial revision
matz authored
182 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
183 rb_define_method_id(nstr, rb_id_attrset(id), rb_struct_set, 1);
3db12e8 Initial revision
matz authored
184 }
185
186 return nstr;
187 }
188
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
189 #ifdef HAVE_STDARG_PROTOTYPES
190 #include <stdarg.h>
191 #define va_init_list(a,b) va_start(a,b)
192 #else
3db12e8 Initial revision
matz authored
193 #include <varargs.h>
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
194 #define va_init_list(a,b) va_start(a)
195 #endif
3db12e8 Initial revision
matz authored
196
197 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
198 #ifdef HAVE_STDARG_PROTOTYPES
65a5162 1.4.0
matz authored
199 rb_struct_define(const char *name, ...)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
200 #else
201 rb_struct_define(name, va_alist)
65a5162 1.4.0
matz authored
202 const char *name;
3db12e8 Initial revision
matz authored
203 va_dcl
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
204 #endif
3db12e8 Initial revision
matz authored
205 {
206 va_list ar;
207 VALUE nm, ary;
208 char *mem;
209
9d228b1 19991214
matz authored
210 if (!name) nm = Qnil;
211 else nm = rb_str_new2(name);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
212 ary = rb_ary_new();
3db12e8 Initial revision
matz authored
213
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
214 va_init_list(ar, name);
3db12e8 Initial revision
matz authored
215 while (mem = va_arg(ar, char*)) {
216 ID slot = rb_intern(mem);
7194267 2000-04-10
matz authored
217 rb_ary_push(ary, ID2SYM(slot));
3db12e8 Initial revision
matz authored
218 }
219 va_end(ar);
220
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
221 return make_struct(nm, ary, rb_cStruct);
3db12e8 Initial revision
matz authored
222 }
223
224 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
225 rb_struct_s_def(argc, argv, klass)
3db12e8 Initial revision
matz authored
226 int argc;
227 VALUE *argv;
9d228b1 19991214
matz authored
228 VALUE klass;
3db12e8 Initial revision
matz authored
229 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
230 VALUE name, rest;
65a5162 1.4.0
matz authored
231 long i;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
232 VALUE st;
9d228b1 19991214
matz authored
233 ID id;
3db12e8 Initial revision
matz authored
234
235 rb_scan_args(argc, argv, "1*", &name, &rest);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
236 for (i=0; i<RARRAY(rest)->len; i++) {
9d228b1 19991214
matz authored
237 id = rb_to_id(RARRAY(rest)->ptr[i]);
7194267 2000-04-10
matz authored
238 RARRAY(rest)->ptr[i] = ID2SYM(id);
3db12e8 Initial revision
matz authored
239 }
d137568 matz
matz authored
240 if (!NIL_P(name) && TYPE(name) != T_STRING) {
9d228b1 19991214
matz authored
241 id = rb_to_id(name);
7194267 2000-04-10
matz authored
242 rb_ary_unshift(rest, ID2SYM(id));
9d228b1 19991214
matz authored
243 name = Qnil;
244 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
245 st = make_struct(name, rest, klass);
246
247 return st;
3db12e8 Initial revision
matz authored
248 }
249
65a5162 1.4.0
matz authored
250 static VALUE
251 rb_struct_initialize(self, values)
252 VALUE self, values;
3db12e8 Initial revision
matz authored
253 {
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
254 VALUE klass = rb_obj_class(self);
3db12e8 Initial revision
matz authored
255 VALUE size;
65a5162 1.4.0
matz authored
256 long n;
3db12e8 Initial revision
matz authored
257
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
258 rb_struct_modify(self);
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
259 size = rb_struct_iv_get(klass, "__size__");
7194267 2000-04-10
matz authored
260 n = FIX2LONG(size);
65a5162 1.4.0
matz authored
261 if (n < RARRAY(values)->len) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
262 rb_raise(rb_eArgError, "struct size differs");
3db12e8 Initial revision
matz authored
263 }
65a5162 1.4.0
matz authored
264 MEMCPY(RSTRUCT(self)->ptr, RARRAY(values)->ptr, VALUE, RARRAY(values)->len);
265 if (n > RARRAY(values)->len) {
266 rb_mem_clear(RSTRUCT(self)->ptr+RARRAY(values)->len,
267 n-RARRAY(values)->len);
3db12e8 Initial revision
matz authored
268 }
65a5162 1.4.0
matz authored
269 return Qnil;
270 }
271
272 static VALUE
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
273 struct_alloc(klass)
65a5162 1.4.0
matz authored
274 VALUE klass;
275 {
276 VALUE size;
277 long n;
278 NEWOBJ(st, struct RStruct);
279 OBJSETUP(st, klass, T_STRUCT);
280
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
281 size = rb_struct_iv_get(klass, "__size__");
65a5162 1.4.0
matz authored
282 n = FIX2LONG(size);
283
284 st->ptr = ALLOC_N(VALUE, n);
285 rb_mem_clear(st->ptr, n);
286 st->len = n;
287
288 return (VALUE)st;
289 }
290
291 VALUE
292 rb_struct_alloc(klass, values)
293 VALUE klass, values;
294 {
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
295 return rb_class_new_instance(RARRAY(values)->len, RARRAY(values)->ptr, klass);
3db12e8 Initial revision
matz authored
296 }
297
298 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
299 #ifdef HAVE_STDARG_PROTOTYPES
300 rb_struct_new(VALUE klass, ...)
301 #else
302 rb_struct_new(klass, va_alist)
303 VALUE klass;
3db12e8 Initial revision
matz authored
304 va_dcl
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
305 #endif
3db12e8 Initial revision
matz authored
306 {
65a5162 1.4.0
matz authored
307 VALUE sz, *mem;
308 long size, i;
3db12e8 Initial revision
matz authored
309 va_list args;
310
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
311 sz = rb_struct_iv_get(klass, "__size__");
65a5162 1.4.0
matz authored
312 size = FIX2LONG(sz);
313 mem = ALLOCA_N(VALUE, size);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
314 va_init_list(args, klass);
315 for (i=0; i<size; i++) {
65a5162 1.4.0
matz authored
316 mem[i] = va_arg(args, VALUE);
3db12e8 Initial revision
matz authored
317 }
318 va_end(args);
319
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
320 return rb_class_new_instance(size, mem, klass);
3db12e8 Initial revision
matz authored
321 }
322
323 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
324 rb_struct_each(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
325 VALUE s;
3db12e8 Initial revision
matz authored
326 {
65a5162 1.4.0
matz authored
327 long i;
3db12e8 Initial revision
matz authored
328
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
329 for (i=0; i<RSTRUCT(s)->len; i++) {
330 rb_yield(RSTRUCT(s)->ptr[i]);
3db12e8 Initial revision
matz authored
331 }
65a5162 1.4.0
matz authored
332 return s;
3db12e8 Initial revision
matz authored
333 }
334
335 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
336 rb_struct_to_s(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
337 VALUE s;
3db12e8 Initial revision
matz authored
338 {
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
339 char *cname = rb_class2name(rb_obj_class(s));
1ce6f06 matz
matz authored
340 VALUE str = rb_str_new(0, strlen(cname) + 4);
3db12e8 Initial revision
matz authored
341
1ce6f06 matz
matz authored
342 sprintf(RSTRING(str)->ptr, "#<%s>", cname);
52f90f6 matz
matz authored
343 RSTRING(str)->len = strlen(RSTRING(str)->ptr);
1ce6f06 matz
matz authored
344 return str;
3db12e8 Initial revision
matz authored
345 }
346
347 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
348 inspect_struct(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
349 VALUE s;
3db12e8 Initial revision
matz authored
350 {
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
351 char *cname = rb_class2name(rb_obj_class(s));
3db12e8 Initial revision
matz authored
352 VALUE str, member;
65a5162 1.4.0
matz authored
353 long i;
3db12e8 Initial revision
matz authored
354
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
355 member = rb_struct_iv_get(rb_obj_class(s), "__member__");
3db12e8 Initial revision
matz authored
356 if (NIL_P(member)) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
357 rb_bug("non-initialized struct");
3db12e8 Initial revision
matz authored
358 }
359
abfaac7 * ruby.c (proc_options): unexpected SecurityError happens when -T4.
matz authored
360 str = rb_str_buf_new2("#<");
7194267 2000-04-10
matz authored
361 rb_str_cat2(str, cname);
362 rb_str_cat2(str, " ");
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
363 for (i=0; i<RSTRUCT(s)->len; i++) {
3db12e8 Initial revision
matz authored
364 VALUE str2, slot;
365 char *p;
366
367 if (i > 0) {
7194267 2000-04-10
matz authored
368 rb_str_cat2(str, ", ");
3db12e8 Initial revision
matz authored
369 }
370 slot = RARRAY(member)->ptr[i];
7194267 2000-04-10
matz authored
371 p = rb_id2name(SYM2ID(slot));
372 rb_str_cat2(str, p);
373 rb_str_cat2(str, "=");
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
374 str2 = rb_inspect(RSTRUCT(s)->ptr[i]);
7194267 2000-04-10
matz authored
375 rb_str_append(str, str2);
3db12e8 Initial revision
matz authored
376 }
7194267 2000-04-10
matz authored
377 rb_str_cat2(str, ">");
bf70582 2000-02-23
matz authored
378 OBJ_INFECT(str, s);
3db12e8 Initial revision
matz authored
379
380 return str;
381 }
382
383 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
384 rb_struct_inspect(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
385 VALUE s;
3db12e8 Initial revision
matz authored
386 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
387 if (rb_inspecting_p(s)) {
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
388 char *cname = rb_class2name(rb_obj_class(s));
1ce6f06 matz
matz authored
389 VALUE str = rb_str_new(0, strlen(cname) + 8);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
390
1ce6f06 matz
matz authored
391 sprintf(RSTRING(str)->ptr, "#<%s:...>", cname);
52f90f6 matz
matz authored
392 RSTRING(str)->len = strlen(RSTRING(str)->ptr);
1ce6f06 matz
matz authored
393 return str;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
394 }
395 return rb_protect_inspect(inspect_struct, s, 0);
3db12e8 Initial revision
matz authored
396 }
397
398 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
399 rb_struct_to_a(s)
400 VALUE s;
401 {
402 return rb_ary_new4(RSTRUCT(s)->len, RSTRUCT(s)->ptr);
403 }
404
405 static VALUE
406 rb_struct_clone(s)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
407 VALUE s;
3db12e8 Initial revision
matz authored
408 {
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
409 VALUE clone = rb_obj_clone(s);
410
411 RSTRUCT(clone)->ptr = ALLOC_N(VALUE, RSTRUCT(s)->len);
412 RSTRUCT(clone)->len = RSTRUCT(s)->len;
413 MEMCPY(RSTRUCT(clone)->ptr, RSTRUCT(s)->ptr, VALUE, RSTRUCT(clone)->len);
de71615 20000105
matz authored
414
1fe40b7 * marshal.c (r_object): better allocation type check for
matz authored
415 return clone;
3db12e8 Initial revision
matz authored
416 }
417
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
418 static VALUE
419 rb_struct_aref_id(s, id)
420 VALUE s;
421 ID id;
422 {
423 VALUE member;
65a5162 1.4.0
matz authored
424 long i, len;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
425
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
426 member = rb_struct_iv_get(rb_obj_class(s), "__member__");
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
427 if (NIL_P(member)) {
428 rb_bug("non-initialized struct");
429 }
430
431 len = RARRAY(member)->len;
432 for (i=0; i<len; i++) {
7194267 2000-04-10
matz authored
433 if (SYM2ID(RARRAY(member)->ptr[i]) == id) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
434 return RSTRUCT(s)->ptr[i];
435 }
436 }
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
437 rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
438 return Qnil; /* not reached */
439 }
440
3db12e8 Initial revision
matz authored
441 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
442 rb_struct_aref(s, idx)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
443 VALUE s, idx;
3db12e8 Initial revision
matz authored
444 {
65a5162 1.4.0
matz authored
445 long i;
3db12e8 Initial revision
matz authored
446
31c5530 2000-03-13
matz authored
447 if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
448 return rb_struct_aref_id(s, rb_to_id(idx));
449 }
450
65a5162 1.4.0
matz authored
451 i = NUM2LONG(idx);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
452 if (i < 0) i = RSTRUCT(s)->len + i;
3db12e8 Initial revision
matz authored
453 if (i < 0)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
454 rb_raise(rb_eIndexError, "offset %d too small for struct(size:%d)",
455 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
456 if (RSTRUCT(s)->len <= i)
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
457 rb_raise(rb_eIndexError, "offset %d too large for struct(size:%d)",
458 i, RSTRUCT(s)->len);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
459 return RSTRUCT(s)->ptr[i];
3db12e8 Initial revision
matz authored
460 }
461
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
462 static VALUE
463 rb_struct_aset_id(s, id, val)
464 VALUE s, val;
465 ID id;
466 {
467 VALUE member;
65a5162 1.4.0
matz authored
468 long i, len;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
469
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
470 member = rb_struct_iv_get(rb_obj_class(s), "__member__");
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
471 if (NIL_P(member)) {
472 rb_bug("non-initialized struct");
473 }
474
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
475 rb_struct_modify(s);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
476 len = RARRAY(member)->len;
477 for (i=0; i<len; i++) {
7194267 2000-04-10
matz authored
478 if (SYM2ID(RARRAY(member)->ptr[i]) == id) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
479 RSTRUCT(s)->ptr[i] = val;
480 return val;
481 }
482 }
ffe1cf5 * error.c (exc_exception): clone the receiver exception instead of
matz authored
483 rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
484 }
485
3db12e8 Initial revision
matz authored
486 VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
487 rb_struct_aset(s, idx, val)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
488 VALUE s, idx, val;
3db12e8 Initial revision
matz authored
489 {
65a5162 1.4.0
matz authored
490 long i;
3db12e8 Initial revision
matz authored
491
31c5530 2000-03-13
matz authored
492 if (TYPE(idx) == T_STRING || TYPE(idx) == T_SYMBOL) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
493 return rb_struct_aset_id(s, rb_to_id(idx), val);
494 }
495
65a5162 1.4.0
matz authored
496 i = NUM2LONG(idx);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
497 if (i < 0) i = RSTRUCT(s)->len + i;
abc49e4 2000-06-14
matz authored
498 if (i < 0) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
499 rb_raise(rb_eIndexError, "offset %d too small for struct(size:%d)",
500 i, RSTRUCT(s)->len);
abc49e4 2000-06-14
matz authored
501 }
502 if (RSTRUCT(s)->len <= i) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
503 rb_raise(rb_eIndexError, "offset %d too large for struct(size:%d)",
504 i, RSTRUCT(s)->len);
abc49e4 2000-06-14
matz authored
505 }
14cd947 * struct.c (rb_struct_modify): should check frozen and taint
matz authored
506 rb_struct_modify(s);
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
507 return RSTRUCT(s)->ptr[i] = val;
3db12e8 Initial revision
matz authored
508 }
509
510 static VALUE
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
511 rb_struct_equal(s, s2)
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
512 VALUE s, s2;
3db12e8 Initial revision
matz authored
513 {
65a5162 1.4.0
matz authored
514 long i;
3db12e8 Initial revision
matz authored
515
b47a994 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
516 if (s == s2) return Qtrue;
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
517 if (TYPE(s2) != T_STRUCT) return Qfalse;
c786866 * range.c (range_step): 'iter' here should be an array.
matz authored
518 if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
519 if (RSTRUCT(s)->len != RSTRUCT(s2)->len) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
520 rb_bug("inconsistent struct"); /* should never happen */
3db12e8 Initial revision
matz authored
521 }
522
7ea2ced This commit was generated by cvs2svn to compensate for changes in r11,
matz authored
523 for (i=0; i<RSTRUCT(s)->len; i++) {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
524 if (!rb_equal(RSTRUCT(s)->ptr[i], RSTRUCT(s2)->ptr[i])) return Qfalse;
3db12e8 Initial revision
matz authored
525 }
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
526 return Qtrue;
3db12e8 Initial revision
matz authored
527 }
528
529 static VALUE
c18d374 991207
matz authored
530 rb_struct_size(s)
531 VALUE s;
532 {
533 return INT2FIX(RSTRUCT(s)->len);
534 }
535
3db12e8 Initial revision
matz authored
536 void
537 Init_Struct()
538 {
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
539 rb_cStruct = rb_define_class("Struct", rb_cObject);
540 rb_include_module(rb_cStruct, rb_mEnumerable);
3db12e8 Initial revision
matz authored
541
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
542 rb_define_singleton_method(rb_cStruct, "new", rb_struct_s_def, -1);
3db12e8 Initial revision
matz authored
543
65a5162 1.4.0
matz authored
544 rb_define_method(rb_cStruct, "initialize", rb_struct_initialize, -2);
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
545 rb_define_method(rb_cStruct, "clone", rb_struct_clone, 0);
3db12e8 Initial revision
matz authored
546
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
547 rb_define_method(rb_cStruct, "==", rb_struct_equal, 1);
3db12e8 Initial revision
matz authored
548
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
549 rb_define_method(rb_cStruct, "to_s", rb_struct_to_s, 0);
550 rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
551 rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0);
552 rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0);
c18d374 991207
matz authored
553 rb_define_method(rb_cStruct, "size", rb_struct_size, 0);
554 rb_define_method(rb_cStruct, "length", rb_struct_size, 0);
3db12e8 Initial revision
matz authored
555
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
556 rb_define_method(rb_cStruct, "each", rb_struct_each, 0);
557 rb_define_method(rb_cStruct, "[]", rb_struct_aref, 1);
558 rb_define_method(rb_cStruct, "[]=", rb_struct_aset, 2);
3db12e8 Initial revision
matz authored
559
210367e This commit was generated by cvs2svn to compensate for changes in r372,
matz authored
560 rb_define_method(rb_cStruct, "members", rb_struct_members, 0);
3db12e8 Initial revision
matz authored
561 }
Something went wrong with that request. Please try again.