-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.c
1868 lines (1669 loc) · 41.9 KB
/
marshal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
marshal.c -
$Author: mame $
created at: Thu Apr 27 16:30:01 JST 1995
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#include "ruby/ruby.h"
#include "ruby/io.h"
#include "ruby/st.h"
#include "ruby/util.h"
#include "ruby/encoding.h"
#include "id.h"
#include <math.h>
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#ifdef HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
#define BITSPERSHORT (2*CHAR_BIT)
#define SHORTMASK ((1<<BITSPERSHORT)-1)
#define SHORTDN(x) RSHIFT(x,BITSPERSHORT)
#if SIZEOF_SHORT == SIZEOF_BDIGITS
#define SHORTLEN(x) (x)
#else
static int
shortlen(long len, BDIGIT *ds)
{
BDIGIT num;
int offset = 0;
num = ds[len-1];
while (num) {
num = SHORTDN(num);
offset++;
}
return (len - 1)*sizeof(BDIGIT)/2 + offset;
}
#define SHORTLEN(x) shortlen((x),d)
#endif
#define MARSHAL_MAJOR 4
#define MARSHAL_MINOR 8
#define TYPE_NIL '0'
#define TYPE_TRUE 'T'
#define TYPE_FALSE 'F'
#define TYPE_FIXNUM 'i'
#define TYPE_EXTENDED_R 'e'
#define TYPE_UCLASS 'C'
#define TYPE_OBJECT 'o'
#define TYPE_DATA 'd'
#define TYPE_USERDEF 'u'
#define TYPE_USRMARSHAL 'U'
#define TYPE_FLOAT 'f'
#define TYPE_BIGNUM 'l'
#define TYPE_STRING '"'
#define TYPE_REGEXP '/'
#define TYPE_ARRAY '['
#define TYPE_HASH '{'
#define TYPE_HASH_DEF '}'
#define TYPE_STRUCT 'S'
#define TYPE_MODULE_OLD 'M'
#define TYPE_CLASS 'c'
#define TYPE_MODULE 'm'
#define TYPE_SYMBOL ':'
#define TYPE_SYMLINK ';'
#define TYPE_IVAR 'I'
#define TYPE_LINK '@'
static ID s_dump, s_load, s_mdump, s_mload;
static ID s_dump_data, s_load_data, s_alloc;
static ID s_getbyte, s_read, s_write, s_binmode;
ID rb_id_encoding(void);
typedef struct {
VALUE newclass;
VALUE oldclass;
VALUE (*dumper)(VALUE);
VALUE (*loader)(VALUE, VALUE);
} marshal_compat_t;
static st_table *compat_allocator_tbl;
static VALUE compat_allocator_tbl_wrapper;
static int
mark_marshal_compat_i(st_data_t key, st_data_t value)
{
#if !WITH_OBJC
marshal_compat_t *p = (marshal_compat_t *)value;
#endif
rb_gc_mark(p->newclass);
rb_gc_mark(p->oldclass);
return ST_CONTINUE;
}
static void
mark_marshal_compat_t(void *tbl)
{
if (!tbl) return;
st_foreach(tbl, mark_marshal_compat_i, 0);
}
void
rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE))
{
marshal_compat_t *compat;
rb_alloc_func_t allocator = rb_get_alloc_func(newclass);
if (!allocator) {
rb_raise(rb_eTypeError, "no allocator");
}
compat = ALLOC(marshal_compat_t);
compat->newclass = Qnil;
compat->oldclass = Qnil;
compat->newclass = newclass;
compat->oldclass = oldclass;
compat->dumper = dumper;
compat->loader = loader;
st_insert(compat_allocator_tbl, (st_data_t)allocator, (st_data_t)compat);
}
struct dump_arg {
VALUE obj;
VALUE str, dest;
st_table *symbols;
st_table *data;
int taint;
st_table *compat_tbl;
VALUE wrapper;
st_table *encodings;
};
struct dump_call_arg {
VALUE obj;
struct dump_arg *arg;
int limit;
};
static void
check_dump_arg(struct dump_arg *arg)
{
if (!DATA_PTR(arg->wrapper)) {
rb_raise(rb_eRuntimeError, "Marshal.dump reentered");
}
}
static void
mark_dump_arg(void *ptr)
{
#if !WITH_OBJC
struct dump_arg *p = ptr;
if (!ptr)
return;
#endif
rb_mark_set(p->data);
rb_mark_hash(p->compat_tbl);
}
static VALUE
class2path(VALUE klass)
{
VALUE path;
if (klass == rb_cNSObject) {
path = rb_str_new2("Object");
}
else if (klass == rb_cNSMutableString) {
path = rb_str_new2("String");
}
else if (klass == rb_cNSMutableArray) {
path = rb_str_new2("Array");
}
else if (klass == rb_cNSMutableHash) {
path = rb_str_new2("Hash");
}
else {
path = rb_class_path(klass);
}
const char *n = RSTRING_PTR(path);
if (n[0] == '#') {
rb_raise(rb_eTypeError, "can't dump anonymous %s %s",
(TYPE(klass) == T_CLASS ? "class" : "module"),
n);
}
if (rb_path2class(n) != rb_class_real(klass)) {
rb_raise(rb_eTypeError, "%s can't be referred", n);
}
return path;
}
static void w_long(long, struct dump_arg*);
static void
w_nbyte(const char *s, int n, struct dump_arg *arg)
{
VALUE buf = arg->str;
rb_str_buf_cat(buf, s, n);
if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
if (arg->taint) OBJ_TAINT(buf);
rb_io_write(arg->dest, 0, buf);
rb_str_resize(buf, 0);
}
}
static void
w_byte(char c, struct dump_arg *arg)
{
w_nbyte(&c, 1, arg);
}
static void
w_bytes(const char *s, int n, struct dump_arg *arg)
{
w_long(n, arg);
w_nbyte(s, n, arg);
}
static void
w_short(int x, struct dump_arg *arg)
{
w_byte((char)((x >> 0) & 0xff), arg);
w_byte((char)((x >> 8) & 0xff), arg);
}
static void
w_long(long x, struct dump_arg *arg)
{
char buf[sizeof(long)+1];
int i, len = 0;
#if SIZEOF_LONG > 4
if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
/* big long does not fit in 4 bytes */
rb_raise(rb_eTypeError, "long too big to dump");
}
#endif
if (x == 0) {
w_byte(0, arg);
return;
}
if (0 < x && x < 123) {
w_byte((char)(x + 5), arg);
return;
}
if (-124 < x && x < 0) {
w_byte((char)((x - 5)&0xff), arg);
return;
}
for (i=1;i<sizeof(long)+1;i++) {
buf[i] = x & 0xff;
x = RSHIFT(x,8);
if (x == 0) {
buf[0] = i;
break;
}
if (x == -1) {
buf[0] = -i;
break;
}
}
len = i;
for (i=0;i<=len;i++) {
w_byte(buf[i], arg);
}
}
#ifdef DBL_MANT_DIG
#define DECIMAL_MANT (53-16) /* from IEEE754 double precision */
#if DBL_MANT_DIG > 32
#define MANT_BITS 32
#elif DBL_MANT_DIG > 24
#define MANT_BITS 24
#elif DBL_MANT_DIG > 16
#define MANT_BITS 16
#else
#define MANT_BITS 8
#endif
static int
save_mantissa(double d, char *buf)
{
int e, i = 0;
unsigned long m;
double n;
d = modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
if (d > 0) {
buf[i++] = 0;
do {
d = modf(ldexp(d, MANT_BITS), &n);
m = (unsigned long)n;
#if MANT_BITS > 24
buf[i++] = m >> 24;
#endif
#if MANT_BITS > 16
buf[i++] = m >> 16;
#endif
#if MANT_BITS > 8
buf[i++] = m >> 8;
#endif
buf[i++] = m;
} while (d > 0);
while (!buf[i - 1]) --i;
}
return i;
}
static double
load_mantissa(double d, const char *buf, int len)
{
if (--len > 0 && !*buf++) { /* binary mantissa mark */
int e, s = d < 0, dig = 0;
unsigned long m;
modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
do {
m = 0;
switch (len) {
default: m = *buf++ & 0xff;
#if MANT_BITS > 24
case 3: m = (m << 8) | (*buf++ & 0xff);
#endif
#if MANT_BITS > 16
case 2: m = (m << 8) | (*buf++ & 0xff);
#endif
#if MANT_BITS > 8
case 1: m = (m << 8) | (*buf++ & 0xff);
#endif
}
dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
d += ldexp((double)m, dig);
} while ((len -= MANT_BITS / 8) > 0);
d = ldexp(d, e - DECIMAL_MANT);
if (s) d = -d;
}
return d;
}
#else
#define load_mantissa(d, buf, len) (d)
#define save_mantissa(d, buf) 0
#endif
#ifdef DBL_DIG
#define FLOAT_DIG (DBL_DIG+2)
#else
#define FLOAT_DIG 17
#endif
static void
w_float(double d, struct dump_arg *arg)
{
char buf[FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
if (isinf(d)) {
if (d < 0) strcpy(buf, "-inf");
else strcpy(buf, "inf");
}
else if (isnan(d)) {
strcpy(buf, "nan");
}
else if (d == 0.0) {
if (1.0/d < 0) strcpy(buf, "-0");
else strcpy(buf, "0");
}
else {
int len;
/* xxx: should not use system's sprintf(3) */
snprintf(buf, sizeof(buf), "%.*g", FLOAT_DIG, d);
len = strlen(buf);
w_bytes(buf, len + save_mantissa(d, buf + len), arg);
return;
}
w_bytes(buf, strlen(buf), arg);
}
static void
w_symbol(ID id, struct dump_arg *arg)
{
const char *sym;
st_data_t num;
if (st_lookup(arg->symbols, id, &num)) {
w_byte(TYPE_SYMLINK, arg);
w_long((long)num, arg);
}
else {
sym = rb_id2name(id);
if (!sym) {
rb_raise(rb_eTypeError, "can't dump anonymous ID %ld", id);
}
w_byte(TYPE_SYMBOL, arg);
w_bytes(sym, strlen(sym), arg);
st_add_direct(arg->symbols, id, arg->symbols->num_entries);
}
}
static void
w_unique(const char *s, struct dump_arg *arg)
{
if (s[0] == '#') {
rb_raise(rb_eTypeError, "can't dump anonymous class %s", s);
}
w_symbol(rb_intern(s), arg);
}
static void w_object(VALUE,struct dump_arg*,int);
static int
hash_each(VALUE key, VALUE value, struct dump_call_arg *arg)
{
w_object(key, arg->arg, arg->limit);
w_object(value, arg->arg, arg->limit);
return ST_CONTINUE;
}
static void
w_extended(VALUE klass, struct dump_arg *arg, int check)
{
#if 0
const char *path;
if (check && RCLASS_SINGLETON(klass)) {
#if !WITH_OBJC // TODO
if (RCLASS_M_TBL(klass)->num_entries ||
(RCLASS_IV_TBL(klass) && RCLASS_IV_TBL(klass)->num_entries > 1)) {
rb_raise(rb_eTypeError, "singleton can't be dumped");
}
klass = RCLASS_SUPER(klass);
#endif
}
while (TYPE(klass) == T_ICLASS) {
path = rb_class2name(RBASIC(klass)->klass);
w_byte(TYPE_EXTENDED_R, arg);
w_unique(path, arg);
klass = RCLASS_SUPER(klass);
}
#endif
if (RCLASS_SINGLETON(klass)) {
VALUE ary = rb_attr_get(klass, idIncludedModules);
if (ary != Qnil) {
for (int i = 0, count = RARRAY_LEN(ary); i < count; i++) {
VALUE mod = RARRAY_AT(ary, i);
const char *path = rb_class2name(mod);
w_byte(TYPE_EXTENDED_R, arg);
w_unique(path, arg);
}
}
}
}
static void
w_class(char type, VALUE obj, struct dump_arg *arg, int check)
{
volatile VALUE p;
const char *path;
st_data_t real_obj;
VALUE klass;
if (st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) {
obj = (VALUE)real_obj;
}
klass = CLASS_OF(obj);
w_extended(klass, arg, check);
w_byte(type, arg);
p = class2path(rb_class_real(klass));
path = RSTRING_PTR(p);
w_unique(path, arg);
}
static void
#if WITH_OBJC
w_uclass(VALUE obj, bool is_pure, struct dump_arg *arg)
#else
w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
#endif
{
VALUE klass = CLASS_OF(obj);
w_extended(klass, arg, Qtrue);
klass = rb_class_real(klass);
#if WITH_OBJC
if (!is_pure) {
#else
if (klass != super) {
#endif
w_byte(TYPE_UCLASS, arg);
w_unique(RSTRING_PTR(class2path(klass)), arg);
}
}
static int
w_obj_each(ID id, VALUE value, struct dump_call_arg *arg)
{
if (id == rb_id_encoding()) return ST_CONTINUE;
w_symbol(id, arg->arg);
w_object(value, arg->arg, arg->limit);
return ST_CONTINUE;
}
static void
w_encoding(VALUE obj, long num, struct dump_call_arg *arg)
{
rb_encoding *enc = 0;
#if WITH_OBJC
VALUE name;
enc = rb_enc_get(obj);
if (enc == NULL) {
w_long(num, arg->arg);
return;
}
name = rb_enc_name2(enc);
#else
int encidx = rb_enc_get_index(obj);
rb_encoding *enc = 0;
st_data_t name;
if (encidx <= 0 || !(enc = rb_enc_from_index(encidx))) {
w_long(num, arg->arg);
return;
}
w_long(num + 1, arg->arg);
w_symbol(rb_id_encoding(), arg->arg);
do {
if (!arg->arg->encodings)
arg->arg->encodings = st_init_strcasetable();
else if (st_lookup(arg->arg->encodings, (st_data_t)rb_enc_name(enc), &name))
break;
name = (st_data_t)rb_str_new2(rb_enc_name(enc));
st_insert(arg->arg->encodings, (st_data_t)rb_enc_name(enc), name);
} while (0);
#endif
w_object(name, arg->arg, arg->limit);
}
static void
w_ivar(VALUE obj, st_table *tbl, struct dump_call_arg *arg)
{
long num = tbl ? tbl->num_entries : 0;
w_encoding(obj, num, arg);
if (tbl) {
st_foreach_safe(tbl, w_obj_each, (st_data_t)arg);
}
}
static void
w_objivar(VALUE obj, struct dump_call_arg *arg)
{
#if WITH_OBJC
VALUE ary = rb_obj_instance_variables(obj);
int i, len = RARRAY_LEN(ary);
w_encoding(obj, len, arg);
for (i = 0; i < len; i++) {
ID var_id = SYM2ID(RARRAY_AT(ary, i));
VALUE var_val = rb_ivar_get(obj, var_id);
w_obj_each(var_id, var_val, arg);
}
#else
VALUE *ptr;
long i, len, num;
len = ROBJECT_NUMIV(obj);
ptr = ROBJECT_IVPTR(obj);
num = 0;
for (i = 0; i < len; i++)
if (ptr[i] != Qundef)
num += 1;
w_encoding(obj, num, arg);
if (num != 0) {
rb_ivar_foreach(obj, w_obj_each, (st_data_t)arg);
}
#endif
}
static void
w_object(VALUE obj, struct dump_arg *arg, int limit)
{
struct dump_call_arg c_arg;
st_table *ivtbl = 0;
st_data_t num;
int hasiv = 0;
#if WITH_OBJC
// TODO
#define has_ivars(obj, ivtbl) (false)
#else
#define has_ivars(obj, ivtbl) ((ivtbl = rb_generic_ivar_table(obj)) != 0 || \
(!SPECIAL_CONST_P(obj) && !ENCODING_IS_ASCII8BIT(obj)))
#endif
if (limit == 0) {
rb_raise(rb_eArgError, "exceed depth limit");
}
limit--;
c_arg.limit = limit;
c_arg.arg = arg;
if (st_lookup(arg->data, obj, &num)) {
w_byte(TYPE_LINK, arg);
w_long((long)num, arg);
return;
}
if ((hasiv = has_ivars(obj, ivtbl)) != 0) {
w_byte(TYPE_IVAR, arg);
}
if (obj == Qnil) {
w_byte(TYPE_NIL, arg);
}
else if (obj == Qtrue) {
w_byte(TYPE_TRUE, arg);
}
else if (obj == Qfalse) {
w_byte(TYPE_FALSE, arg);
}
else if (FIXNUM_P(obj)) {
#if SIZEOF_LONG <= 4
w_byte(TYPE_FIXNUM, arg);
w_long(FIX2INT(obj), arg);
#else
if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
w_byte(TYPE_FIXNUM, arg);
w_long(FIX2LONG(obj), arg);
}
else {
w_object(rb_int2big(FIX2LONG(obj)), arg, limit);
}
#endif
}
else if (FIXFLOAT_P(obj)) {
w_byte(TYPE_FLOAT, arg);
w_float(RFLOAT_VALUE(obj), arg);
}
else if (SYMBOL_P(obj)) {
w_symbol(SYM2ID(obj), arg);
}
else {
if (OBJ_TAINTED(obj)) {
arg->taint = Qtrue;
}
if (rb_obj_respond_to(obj, s_mdump, Qtrue)) {
volatile VALUE v;
st_add_direct(arg->data, obj, arg->data->num_entries);
v = rb_funcall(obj, s_mdump, 0, 0);
check_dump_arg(arg);
w_class(TYPE_USRMARSHAL, obj, arg, Qfalse);
w_object(v, arg, limit);
if (hasiv) w_ivar(obj, 0, &c_arg);
return;
}
if (rb_obj_respond_to(obj, s_dump, Qtrue)) {
VALUE v;
st_table *ivtbl2 = 0;
int hasiv2;
v = rb_funcall(obj, s_dump, 1, INT2NUM(limit));
check_dump_arg(arg);
if (TYPE(v) != T_STRING) {
rb_raise(rb_eTypeError, "_dump() must return string");
}
if ((hasiv2 = has_ivars(v, ivtbl2)) != 0 && !hasiv) {
w_byte(TYPE_IVAR, arg);
}
w_class(TYPE_USERDEF, obj, arg, Qfalse);
w_bytes(RSTRING_PTR(v), RSTRING_LEN(v), arg);
if (hasiv2) {
w_ivar(v, ivtbl2, &c_arg);
}
else if (hasiv) {
w_ivar(obj, ivtbl, &c_arg);
}
st_add_direct(arg->data, obj, arg->data->num_entries);
return;
}
st_add_direct(arg->data, obj, arg->data->num_entries);
if (!NATIVE(obj)) {
st_data_t compat_data;
rb_alloc_func_t allocator = rb_get_alloc_func(RBASIC(obj)->klass);
if (st_lookup(compat_allocator_tbl,
(st_data_t)allocator,
&compat_data)) {
marshal_compat_t *compat = (marshal_compat_t*)compat_data;
VALUE real_obj = obj;
obj = compat->dumper(real_obj);
st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
}
}
switch (TYPE(obj)) {
case T_CLASS:
if (RCLASS_SINGLETON(obj)) {
rb_raise(rb_eTypeError, "singleton class can't be dumped");
}
w_byte(TYPE_CLASS, arg);
{
volatile VALUE path = class2path(obj);
w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
}
break;
case T_MODULE:
w_byte(TYPE_MODULE, arg);
{
VALUE path = class2path(obj);
w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
}
break;
case T_BIGNUM:
w_byte(TYPE_BIGNUM, arg);
{
char sign = RBIGNUM_SIGN(obj) ? '+' : '-';
long len = RBIGNUM_LEN(obj);
BDIGIT *d = RBIGNUM_DIGITS(obj);
w_byte(sign, arg);
w_long(SHORTLEN(len), arg); /* w_short? */
while (len--) {
#if SIZEOF_BDIGITS > SIZEOF_SHORT
BDIGIT num = *d;
int i;
for (i=0; i<SIZEOF_BDIGITS; i+=SIZEOF_SHORT) {
w_short(num & SHORTMASK, arg);
num = SHORTDN(num);
if (len == 0 && num == 0) break;
}
#else
w_short(*d, arg);
#endif
d++;
}
}
break;
case T_STRING:
#if WITH_OBJC
w_uclass(obj, rb_objc_str_is_pure(obj), arg);
#else
w_uclass(obj, rb_cString, arg);
#endif
w_byte(TYPE_STRING, arg);
w_bytes(RSTRING_PTR(obj), RSTRING_LEN(obj), arg);
break;
case T_REGEXP:
w_uclass(obj, rb_cRegexp, arg);
w_byte(TYPE_REGEXP, arg);
w_bytes(RREGEXP(obj)->str, RREGEXP(obj)->len, arg);
w_byte((char)rb_reg_options(obj), arg);
break;
case T_ARRAY:
#if WITH_OBJC
w_uclass(obj, rb_objc_ary_is_pure(obj), arg);
#else
w_uclass(obj, rb_cArray, arg);
#endif
w_byte(TYPE_ARRAY, arg);
{
long len = RARRAY_LEN(obj);
#if WITH_OBJC
long i;
w_long(len, arg);
for (i = 0; i < len; i++)
w_object(RARRAY_AT(obj, i), arg, limit);
#else
VALUE *ptr = RARRAY_PTR(obj);
w_long(len, arg);
while (len--) {
w_object(*ptr, arg, limit);
ptr++;
}
#endif
}
break;
case T_HASH:
#if WITH_OBJC
w_uclass(obj, rb_objc_hash_is_pure(obj), arg);
#else
w_uclass(obj, rb_cHash, arg);
#endif
#if WITH_OBJC
w_byte(TYPE_HASH, arg);
/* TODO: encode ifnone too */
#else
if (NIL_P(RHASH(obj)->ifnone)) {
w_byte(TYPE_HASH, arg);
}
else if (FL_TEST(obj, FL_USER2)) {
/* FL_USER2 means HASH_PROC_DEFAULT (see hash.c) */
rb_raise(rb_eTypeError, "can't dump hash with default proc");
}
else {
w_byte(TYPE_HASH_DEF, arg);
}
#endif
w_long(RHASH_SIZE(obj), arg);
rb_hash_foreach(obj, hash_each, (st_data_t)&c_arg);
#if !WITH_OBJC
if (!NIL_P(RHASH(obj)->ifnone)) {
w_object(RHASH(obj)->ifnone, arg, limit);
}
#endif
break;
case T_STRUCT:
w_class(TYPE_STRUCT, obj, arg, Qtrue);
{
long len = RSTRUCT_LEN(obj);
VALUE mem;
long i;
w_long(len, arg);
mem = rb_struct_members(obj);
for (i=0; i<len; i++) {
w_symbol(SYM2ID(RARRAY_AT(mem, i)), arg);
w_object(RSTRUCT_PTR(obj)[i], arg, limit);
}
}
break;
case T_NATIVE:
case T_OBJECT:
w_class(TYPE_OBJECT, obj, arg, Qtrue);
w_objivar(obj, &c_arg);
break;
case T_DATA:
{
VALUE v;
if (!rb_obj_respond_to(obj, s_dump_data, Qtrue)) {
rb_raise(rb_eTypeError,
"no marshal_dump is defined for class %s",
rb_obj_classname(obj));
}
v = rb_funcall(obj, s_dump_data, 0);
check_dump_arg(arg);
w_class(TYPE_DATA, obj, arg, Qtrue);
w_object(v, arg, limit);
}
break;
default:
rb_raise(rb_eTypeError, "can't dump %s",
rb_obj_classname(obj));
break;
}
}
if (hasiv) {
w_ivar(obj, ivtbl, &c_arg);
}
}
static VALUE
dump(struct dump_call_arg *arg)
{
w_object(arg->obj, arg->arg, arg->limit);
if (arg->arg->dest) {
rb_io_write(arg->arg->dest, 0, arg->arg->str);
rb_str_resize(arg->arg->str, 0);
}
return 0;
}
static VALUE
dump_ensure(struct dump_arg *arg)
{
if (!DATA_PTR(arg->wrapper)) return 0;
st_free_table(arg->symbols);
st_free_table(arg->data);
st_free_table(arg->compat_tbl);
DATA_PTR(arg->wrapper) = 0;
arg->wrapper = 0;
if (arg->taint) {
OBJ_TAINT(arg->str);
}
return 0;
}
/*
* call-seq:
* dump( obj [, anIO] , limit=--1 ) => anIO
*
* Serializes obj and all descendent objects. If anIO is
* specified, the serialized data will be written to it, otherwise the
* data will be returned as a String. If limit is specified, the
* traversal of subobjects will be limited to that depth. If limit is
* negative, no checking of depth will be performed.
*
* class Klass
* def initialize(str)
* @str = str
* end
* def sayHello
* @str
* end
* end
*
* (produces no output)
*
* o = Klass.new("hello\n")
* data = Marshal.dump(o)
* obj = Marshal.load(data)
* obj.sayHello #=> "hello\n"
*/
static VALUE
marshal_dump(VALUE self, SEL sel, int argc, VALUE *argv)
{
VALUE obj, port, a1, a2;
int limit = -1;
struct dump_arg *arg;
struct dump_call_arg *c_arg;
port = Qnil;
rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
arg = (struct dump_arg *)xmalloc(sizeof(struct dump_arg));
c_arg = (struct dump_call_arg *)xmalloc(sizeof(struct dump_call_arg));
if (argc == 3) {
if (!NIL_P(a2)) {
limit = NUM2INT(a2);
}
if (NIL_P(a1)) {
goto type_error;
}
port = a1;
}
else if (argc == 2) {
if (FIXNUM_P(a1)) {
limit = FIX2INT(a1);
}
else if (NIL_P(a1)) {
goto type_error;
}
else {
port = a1;
}
}
arg->dest = 0;
if (!NIL_P(port)) {
if (!rb_obj_respond_to(port, s_write, Qtrue)) {
type_error:
rb_raise(rb_eTypeError, "instance of IO needed");
}
GC_WB(&arg->str, rb_bytestring_new());
GC_WB(&arg->dest, port);
if (rb_obj_respond_to(port, s_binmode, Qtrue)) {
rb_funcall2(port, s_binmode, 0, 0);
}
}
else {
port = rb_bytestring_new();
GC_WB(&arg->str, port);
}
GC_WB(&arg->symbols, st_init_numtable());
GC_WB(&arg->data, st_init_numtable());
arg->taint = Qfalse;
GC_WB(&arg->compat_tbl, st_init_numtable());
GC_WB(&arg->wrapper, Data_Wrap_Struct(rb_cData, mark_dump_arg, 0, arg));
arg->encodings = 0;
GC_WB(&c_arg->obj, obj);
GC_WB(&c_arg->arg, arg);
c_arg->limit = limit;
w_byte(MARSHAL_MAJOR, arg);
w_byte(MARSHAL_MINOR, arg);
rb_ensure(dump, (VALUE)c_arg, dump_ensure, (VALUE)arg);
return port;