Project

General

Profile

Bug #6625 ยป 0001-compatible-marshal-loader.patch

nobu (Nobuyoshi Nakada), 06/22/2012 02:08 PM

View differences:

complex.c
21 21
static ID id_abs, id_abs2, id_arg, id_cmp, id_conj, id_convert,
22 22
    id_denominator, id_divmod, id_eqeq_p, id_expt, id_fdiv,  id_floor,
23 23
    id_idiv, id_imag, id_inspect, id_negate, id_numerator, id_quo,
24
    id_real, id_real_p, id_to_f, id_to_i, id_to_r, id_to_s;
24
    id_real, id_real_p, id_to_f, id_to_i, id_to_r, id_to_s,
25
    id_i_real, id_i_imag;
25 26

  
26 27
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
27 28

  
......
1244 1245

  
1245 1246
/* :nodoc: */
1246 1247
static VALUE
1248
nucomp_dumper(VALUE self)
1249
{
1250
    return self;
1251
}
1252

  
1253
/* :nodoc: */
1254
static VALUE
1255
nucomp_loader(VALUE self, VALUE a)
1256
{
1257
    get_dat1(self);
1258

  
1259
    dat->real = rb_ivar_get(a, id_i_real);
1260
    dat->imag = rb_ivar_get(a, id_i_imag);
1261

  
1262
    return self;
1263
}
1264

  
1265
/* :nodoc: */
1266
static VALUE
1247 1267
nucomp_marshal_dump(VALUE self)
1248 1268
{
1249 1269
    VALUE a;
1250 1270
    get_dat1(self);
1251 1271

  
1252 1272
    a = rb_assoc_new(dat->real, dat->imag);
1253
    rb_copy_generic_ivar(a, self);
1254 1273
    return a;
1255 1274
}
1256 1275

  
......
1258 1277
static VALUE
1259 1278
nucomp_marshal_load(VALUE self, VALUE a)
1260 1279
{
1261
    get_dat1(self);
1262

  
1263
    rb_check_frozen(self);
1264
    rb_check_trusted(self);
1265

  
1266 1280
    Check_Type(a, T_ARRAY);
1267 1281
    if (RARRAY_LEN(a) != 2)
1268 1282
	rb_raise(rb_eArgError, "marshaled complex must have an array whose length is 2 but %ld", RARRAY_LEN(a));
1269
    dat->real = RARRAY_PTR(a)[0];
1270
    dat->imag = RARRAY_PTR(a)[1];
1271
    rb_copy_generic_ivar(self, a);
1283
    rb_ivar_set(self, id_i_real, RARRAY_PTR(a)[0]);
1284
    rb_ivar_set(self, id_i_imag, RARRAY_PTR(a)[1]);
1272 1285
    return self;
1273 1286
}
1274 1287

  
......
1833 1846
void
1834 1847
Init_Complex(void)
1835 1848
{
1849
    VALUE compat;
1836 1850
#undef rb_intern
1837 1851
#define rb_intern(str) rb_intern_const(str)
1838 1852

  
......
1862 1876
    id_to_i = rb_intern("to_i");
1863 1877
    id_to_r = rb_intern("to_r");
1864 1878
    id_to_s = rb_intern("to_s");
1879
    id_i_real = rb_intern("@real");
1880
    id_i_imag = rb_intern("@image");
1865 1881

  
1866 1882
    rb_cComplex = rb_define_class("Complex", rb_cNumeric);
1867 1883

  
......
1951 1967
    rb_define_method(rb_cComplex, "inspect", nucomp_inspect, 0);
1952 1968

  
1953 1969
    rb_define_method(rb_cComplex, "marshal_dump", nucomp_marshal_dump, 0);
1954
    rb_define_method(rb_cComplex, "marshal_load", nucomp_marshal_load, 1);
1970
    compat = rb_define_class_under(rb_cComplex, "compatible", rb_cObject);
1971
    rb_define_method(compat, "marshal_load", nucomp_marshal_load, 1);
1972
    rb_marshal_define_compat(rb_cComplex, compat, nucomp_dumper, nucomp_loader);
1955 1973

  
1956 1974
    /* --- */
1957 1975

  
rational.c
25 25

  
26 26
static ID id_abs, id_cmp, id_convert, id_eqeq_p, id_expt, id_fdiv,
27 27
    id_floor, id_idiv, id_inspect, id_integer_p, id_negate, id_to_f,
28
    id_to_i, id_to_s, id_truncate;
28
    id_to_i, id_to_s, id_truncate, id_i_num, id_i_den;
29 29

  
30 30
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
31 31

  
......
1590 1590

  
1591 1591
/* :nodoc: */
1592 1592
static VALUE
1593
nurat_dumper(VALUE self)
1594
{
1595
    return self;
1596
}
1597

  
1598
/* :nodoc: */
1599
static VALUE
1600
nurat_loader(VALUE self, VALUE a)
1601
{
1602
    get_dat1(self);
1603

  
1604
    dat->num = rb_ivar_get(a, id_i_num);
1605
    dat->den = rb_ivar_get(a, id_i_den);
1606

  
1607
    return self;
1608
}
1609

  
1610
/* :nodoc: */
1611
static VALUE
1593 1612
nurat_marshal_dump(VALUE self)
1594 1613
{
1595 1614
    VALUE a;
1596 1615
    get_dat1(self);
1597 1616

  
1598 1617
    a = rb_assoc_new(dat->num, dat->den);
1599
    rb_copy_generic_ivar(a, self);
1600 1618
    return a;
1601 1619
}
1602 1620

  
......
1604 1622
static VALUE
1605 1623
nurat_marshal_load(VALUE self, VALUE a)
1606 1624
{
1607
    get_dat1(self);
1608

  
1609 1625
    rb_check_frozen(self);
1610 1626
    rb_check_trusted(self);
1611 1627

  
1612 1628
    Check_Type(a, T_ARRAY);
1613 1629
    if (RARRAY_LEN(a) != 2)
1614 1630
	rb_raise(rb_eArgError, "marshaled rational must have an array whose length is 2 but %ld", RARRAY_LEN(a));
1615
    dat->num = RARRAY_PTR(a)[0];
1616
    dat->den = RARRAY_PTR(a)[1];
1617
    rb_copy_generic_ivar(self, a);
1618

  
1619
    if (f_zero_p(dat->den))
1631
    if (f_zero_p(RARRAY_PTR(a)[1]))
1620 1632
	rb_raise_zerodiv();
1621 1633

  
1634
    rb_ivar_set(self, id_i_num, RARRAY_PTR(a)[0]);
1635
    rb_ivar_set(self, id_i_den, RARRAY_PTR(a)[1]);
1636

  
1622 1637
    return self;
1623 1638
}
1624 1639

  
......
2295 2310
void
2296 2311
Init_Rational(void)
2297 2312
{
2313
    VALUE compat;
2298 2314
#undef rb_intern
2299 2315
#define rb_intern(str) rb_intern_const(str)
2300 2316

  
......
2315 2331
    id_to_i = rb_intern("to_i");
2316 2332
    id_to_s = rb_intern("to_s");
2317 2333
    id_truncate = rb_intern("truncate");
2334
    id_i_num = rb_intern("@numerator");
2335
    id_i_den = rb_intern("@denominator");
2318 2336

  
2319 2337
    rb_cRational = rb_define_class("Rational", rb_cNumeric);
2320 2338

  
......
2375 2393
    rb_define_method(rb_cRational, "inspect", nurat_inspect, 0);
2376 2394

  
2377 2395
    rb_define_method(rb_cRational, "marshal_dump", nurat_marshal_dump, 0);
2378
    rb_define_method(rb_cRational, "marshal_load", nurat_marshal_load, 1);
2396
    compat = rb_define_class_under(rb_cRational, "compatible", rb_cObject);
2397
    rb_define_method(compat, "marshal_load", nurat_marshal_load, 1);
2398
    rb_marshal_define_compat(rb_cRational, compat, nurat_dumper, nurat_loader);
2379 2399

  
2380 2400
    /* --- */
2381 2401

  
2382
-