Ruby
2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
|
Go to the documentation of this file.
28 #define BITSPERSHORT (2*CHAR_BIT)
29 #define SHORTMASK ((1<<BITSPERSHORT)-1)
30 #define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
32 #if SIZEOF_SHORT == SIZEOF_BDIGIT
33 #define SHORTLEN(x) (x)
48 #define SHORTLEN(x) shortlen((x),d)
51 #define MARSHAL_MAJOR 4
52 #define MARSHAL_MINOR 8
56 #define TYPE_FALSE 'F'
57 #define TYPE_FIXNUM 'i'
59 #define TYPE_EXTENDED 'e'
60 #define TYPE_UCLASS 'C'
61 #define TYPE_OBJECT 'o'
63 #define TYPE_USERDEF 'u'
64 #define TYPE_USRMARSHAL 'U'
65 #define TYPE_FLOAT 'f'
66 #define TYPE_BIGNUM 'l'
67 #define TYPE_STRING '"'
68 #define TYPE_REGEXP '/'
69 #define TYPE_ARRAY '['
71 #define TYPE_HASH_DEF '}'
72 #define TYPE_STRUCT 'S'
73 #define TYPE_MODULE_OLD 'M'
74 #define TYPE_CLASS 'c'
75 #define TYPE_MODULE 'm'
77 #define TYPE_SYMBOL ':'
78 #define TYPE_SYMLINK ';'
83 static ID s_dump, s_load, s_mdump, s_mload;
84 static ID s_dump_data, s_load_data, s_alloc, s_call;
85 static ID s_getbyte, s_read, s_write, s_binmode;
86 static ID s_encoding_short;
88 #define name_s_dump "_dump"
89 #define name_s_load "_load"
90 #define name_s_mdump "marshal_dump"
91 #define name_s_mload "marshal_load"
92 #define name_s_dump_data "_dump_data"
93 #define name_s_load_data "_load_data"
94 #define name_s_alloc "_alloc"
95 #define name_s_call "call"
96 #define name_s_getbyte "getbyte"
97 #define name_s_read "read"
98 #define name_s_write "write"
99 #define name_s_binmode "binmode"
100 #define name_s_encoding_short "E"
109 static st_table *compat_allocator_tbl;
110 static VALUE compat_allocator_tbl_wrapper;
124 mark_marshal_compat_t(
void *tbl)
130 static st_table *compat_allocator_table(
void);
187 return check_dump_arg(ret,
arg,
name);
190 #define dump_funcall(arg, obj, sym, argc, argv) \
191 check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
192 #define dump_check_funcall(arg, obj, sym, argc, argv) \
193 check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
198 mark_dump_arg(
void *
ptr)
210 free_dump_arg(
void *
ptr)
217 memsize_dump_arg(
const void *
ptr)
224 {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
258 static void w_long(
long,
struct dump_arg*);
286 #define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
291 w_byte((
char)((x >> 0) & 0xff),
arg);
292 w_byte((
char)((x >> 8) & 0xff),
arg);
322 if (0 < x && x < 123) {
326 if (-124 < x && x < 0) {
330 for (
i=1;
i<(
int)
sizeof(
long)+1;
i++) {
346 #define DECIMAL_MANT (53-16)
348 #if DBL_MANT_DIG > 32
350 #elif DBL_MANT_DIG > 24
352 #elif DBL_MANT_DIG > 16
362 if (--
len > 0 && !*
buf++) {
363 int e, s = d < 0, dig = 0;
370 default: m = *
buf++ & 0xff;
372 case 3: m = (m << 8) | (*
buf++ & 0xff);
375 case 2: m = (m << 8) | (*
buf++ & 0xff);
378 case 1: m = (m << 8) | (*
buf++ & 0xff);
382 d +=
ldexp((
double)m, dig);
383 }
while ((
len -= MANT_BITS / 8) > 0);
384 d =
ldexp(d, e - DECIMAL_MANT);
390 #define load_mantissa(d, buf, len) (d)
394 #define FLOAT_DIG (DBL_DIG+2)
416 int decpt, sign, digs,
len = 0;
417 char *e, *p =
ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
418 if (sign)
buf[
len++] =
'-';
420 if (decpt < -3 || decpt > digs) {
422 if (--digs > 0)
buf[
len++] =
'.';
427 else if (decpt > 0) {
430 if ((digs -= decpt) > 0) {
459 w_long((
long)num,
arg);
467 encname = encoding_name(
sym,
arg);
468 if (
NIL_P(encname) ||
478 if (!
NIL_P(encname)) {
483 w_encoding(encname, &c_arg);
491 must_not_be_anonymous(
"class", s);
502 w_object(value,
arg->arg,
arg->limit);
506 #define SINGLETON_DUMP_UNABLE_P(klass) \
507 (rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \
508 (RCLASS_IV_TBL(klass) && RCLASS_IV_TBL(klass)->num_entries > 1))
536 if (
arg->compat_tbl &&
554 if (
klass != super) {
560 #define to_be_skipped_id(id) (id == rb_id_encoding() || id == s_encoding_short || !rb_id2str(id))
576 if (
id == s_encoding_short) {
588 w_object(value,
arg->arg,
arg->limit);
641 w_symbol(
ID2SYM(s_encoding_short),
arg->arg);
667 if (num) *ivobj =
obj;
688 w_long(num,
arg->arg);
689 num -= w_encoding(encname,
arg);
691 w_ivar_each(ivobj, num,
arg);
701 w_long(num,
arg->arg);
702 w_ivar_each(
obj, num,
arg);
725 w_long((
long)num,
arg);
786 hasiv = has_ivars(
obj, (encname = encoding_name(
obj,
arg)), &ivobj);
787 hasiv2 = has_ivars(
v, (encname2 = encoding_name(
v,
arg)), &ivobj2);
797 w_ivar(hasiv, ivobj, encname, &c_arg);
805 hasiv = has_ivars(
obj, (encname = encoding_name(
obj,
arg)), &ivobj);
815 if (!
arg->compat_tbl) {
819 if (
obj != real_obj && ivobj ==
Qundef) hasiv = 0;
866 w_long((
long)slen,
arg);
867 for (j = 0; j <
len; j++) {
868 #if SIZEOF_BDIGIT > SIZEOF_SHORT
875 if (j ==
len - 1 && num == 0)
break;
897 w_byte((
char)opts,
arg);
953 w_objivar(
obj, &c_arg);
962 "no _dump_data is defined for class %"PRIsVALUE,
979 w_ivar(hasiv, ivobj, encname, &c_arg);
986 if (!
arg->symbols)
return;
991 if (
arg->compat_tbl) {
995 if (
arg->encodings) {
1001 NORETURN(
static inline void io_needed(
void));
1052 if (
NIL_P(a1)) io_needed();
1055 else if (
argc == 2) {
1057 else if (
NIL_P(a1)) io_needed();
1060 return rb_marshal_dump_limited(
obj, port,
limit);
1073 arg->compat_tbl = 0;
1095 clear_dump_arg(
arg);
1116 if (!
arg->symbols) {
1122 #define load_funcall(arg, obj, sym, argc, argv) \
1123 check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
1128 mark_load_arg(
void *
ptr)
1139 free_load_arg(
void *
ptr)
1141 clear_load_arg(
ptr);
1146 memsize_load_arg(
const void *
ptr)
1153 {mark_load_arg, free_load_arg, memsize_load_arg,},
1157 #define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
1163 NORETURN(
static void too_short(
void));
1179 static unsigned char
1182 if (
arg->buflen == 0) {
1194 return arg->buf[
arg->offset++];
1211 if (
arg->readable >0 ||
arg->buflen > 0) {
1212 c = r_byte1_buffered(
arg);
1226 long_toobig(
int size)
1236 int c = (
signed char)r_byte(
arg);
1239 if (c == 0)
return 0;
1241 if (4 < c && c < 128) {
1244 if (c > (
int)
sizeof(
long)) long_toobig(c);
1251 if (-129 < c && c < -4) {
1255 if (c > (
int)
sizeof(
long)) long_toobig(c);
1258 x &= ~((
long)0xff << (8*
i));
1296 if (len <= arg->buflen) {
1302 long buflen =
arg->buflen;
1303 long readable =
arg->readable + 1;
1304 long tmp_len, read_len, need_len =
len - buflen;
1308 read_len = need_len > readable ? need_len : readable;
1311 if (
NIL_P(tmp)) too_short();
1316 if (tmp_len < need_len) too_short();
1321 if (tmp_len > need_len) {
1322 buflen = tmp_len - need_len;
1324 arg->buflen = buflen;
1335 #define r_bytes(arg) r_bytes0(r_long(arg), (arg))
1353 if (
arg->readable > 0 ||
arg->buflen > 0) {
1366 static const char name_encoding[8] =
"encoding";
1371 if (l <= 0)
return -1;
1372 if (l ==
sizeof(name_encoding) &&
1373 memcmp(p, name_encoding,
sizeof(name_encoding)) == 0) {
1377 else if (l == 1 && *p ==
'E') {
1389 long num = r_long(
arg);
1408 long num = r_long(
arg);
1411 idx = sym2encidx(
sym, r_object(
arg));
1425 switch ((
type = r_byte(
arg))) {
1432 return r_symreal(
arg, ivar);
1437 return r_symlink(
arg);
1444 return r_symbol(
arg);
1495 v = r_fixup_compat(
v,
arg);
1496 v = r_post_proc(
v,
arg);
1528 int idx = sym2encidx(
sym, val);
1536 if (has_encoding) *has_encoding =
TRUE;
1541 }
while (--
len > 0);
1556 #define path2module(path) must_be_module(rb_path_to_class(path), path)
1578 if (oldclass) *oldclass = compat->
oldclass;
1580 if (!
arg->compat_tbl) {
1593 return obj_alloc_by_klass(path2class(
path),
arg, 0);
1607 #define prohibit_ivar(type, str) do { \
1608 if (!ivp || !*ivp) break; \
1609 rb_raise(rb_eTypeError, \
1610 "can't override instance variable of "type" `%"PRIsVALUE"'", \
1629 v = r_post_proc(
v,
arg);
1636 v = r_object0(
arg, &ivar, extmod);
1664 must_be_module(m,
path);
1667 v = r_object0(
arg, 0, extmod);
1678 VALUE c = path2class(r_unique(
arg));
1683 v = r_object0(
arg, 0, extmod);
1691 if (
TYPE(
v) !=
TYPE(tmp))
goto format_error;
1699 v = r_leave(
v,
arg);
1704 v = r_leave(
v,
arg);
1709 v = r_leave(
v,
arg);
1714 long i = r_long(
arg);
1717 v = r_leave(
v,
arg);
1742 v = r_leave(
v,
arg);
1754 data = r_bytes0(
len * 2,
arg);
1759 v = r_leave(
v,
arg);
1765 v = r_leave(
v,
arg);
1771 int options = r_byte(
arg);
1772 int has_encoding =
FALSE;
1776 r_ivar(
str, &has_encoding,
arg);
1779 if (!has_encoding) {
1784 for (;
len-- > 0; *dst++ = *
src++) {
1786 case '\\': bs++;
break;
1787 case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
1788 case 'm':
case 'o':
case 'p':
case 'q':
case 'u':
case 'y':
1789 case 'E':
case 'F':
case 'H':
case 'I':
case 'J':
case 'K':
1790 case 'L':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
1791 case 'S':
case 'T':
case 'U':
case 'V':
case 'X':
case 'Y':
1794 default: bs = 0;
break;
1800 v = r_leave(
v,
arg);
1810 arg->readable +=
len - 1;
1815 v = r_leave(
v,
arg);
1827 arg->readable += (
len - 1) * 2;
1838 v = r_leave(
v,
arg);
1861 arg->readable += (
len - 1) * 2;
1862 v = r_entry0(
v, idx,
arg);
1873 slot = r_symbol(
arg);
1890 v = r_leave(
v,
arg);
1906 data = r_string(
arg);
1917 v = r_post_proc(
v,
arg);
1928 v = obj_alloc_by_klass(
klass,
arg, &oldclass);
1929 if (!
NIL_P(extmod)) {
1931 append_extmod(
v, extmod);
1938 data = r_object(
arg);
1940 v = r_fixup_compat(
v,
arg);
1941 v = r_copy_ivar(
v, data);
1942 v = r_post_proc(
v,
arg);
1943 if (!
NIL_P(extmod)) {
1944 if (oldclass) append_extmod(
v, extmod);
1953 v = obj_alloc_by_path(r_unique(
arg),
arg);
1957 v = r_entry0(
v, idx,
arg);
1959 v = r_leave(
v,
arg);
1970 v = obj_alloc_by_klass(
klass,
arg, &oldclass);
1977 "class %"PRIsVALUE" needs to have instance method `_load_data'",
1980 r = r_object0(
arg, 0, extmod);
1982 v = r_leave(
v,
arg);
1993 v = r_leave(
v,
arg);
2001 v = path2class(
str);
2004 v = r_leave(
v,
arg);
2015 v = r_leave(
v,
arg);
2021 v = r_symreal(
arg, *ivp);
2025 v = r_symreal(
arg, 0);
2028 v = r_leave(
v,
arg);
2050 return r_object0(
arg, 0,
Qnil);
2063 if (!
arg->symbols)
return;
2068 if (
arg->compat_tbl) {
2070 arg->compat_tbl = 0;
2096 return rb_marshal_load_with_proc(port, proc);
2100 rb_marshal_load_with_proc(
VALUE port,
VALUE proc)
2122 arg->compat_tbl = 0;
2134 clear_load_arg(
arg);
2136 \tformat version %d.%d required; %d.%d given",
2140 rb_warn(
"incompatible marshal file format (can be read)\n\
2141 \tformat version %d.%d required; %d.%d given",
2145 if (!
NIL_P(proc))
arg->proc = proc;
2147 clear_load_arg(
arg);
2268 #define rb_intern(str) rb_intern_const(str)
2271 #define set_id(sym) sym = rb_intern_const(name_##sym)
2284 set_id(s_encoding_short);
2297 compat_allocator_table(
void)
2299 if (compat_allocator_tbl)
return compat_allocator_tbl;
2301 #undef RUBY_UNTYPED_DATA_WARNING
2302 #define RUBY_UNTYPED_DATA_WARNING 0
2303 compat_allocator_tbl_wrapper =
2306 return compat_allocator_tbl;
2312 return rb_marshal_dump_limited(
obj, port, -1);
2318 return rb_marshal_load_with_proc(port,
Qnil);
void rb_prepend_module(VALUE klass, VALUE module)
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
#define load_mantissa(d, buf, len)
char * ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
#define TypedData_Make_Struct(klass, type, data_type, sval)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
#define INTEGER_PACK_NEGATIVE
void rb_gc_register_mark_object(VALUE obj)
VALUE(* loader)(VALUE, VALUE)
VALUE rb_struct_members(VALUE)
VALUE rb_str_buf_new(long)
void rb_warn(const char *fmt,...)
size_t rb_hash_size_num(VALUE hash)
VALUE rb_struct_s_keyword_init(VALUE klass)
rb_alloc_func_t rb_get_alloc_func(VALUE)
VALUE rb_int2big(intptr_t n)
st_table * st_init_numtable(void)
#define RREGEXP_SRC_LEN(r)
int rb_utf8_encindex(void)
#define path2module(path)
int st_delete(st_table *tab, st_data_t *key, st_data_t *value)
#define RB_TYPE_P(obj, type)
rb_encoding * rb_enc_get(VALUE obj)
void st_add_direct(st_table *tab, st_data_t key, st_data_t value)
#define rb_enc_asciicompat(enc)
long ruby_marshal_read_long(const char **buf, long len)
VALUE rb_define_module(const char *name)
#define INTEGER_PACK_LITTLE_ENDIAN
VALUE rb_check_string_type(VALUE)
#define Data_Wrap_Struct(klass, mark, free, sval)
#define SPECIAL_CONST_P(x)
VALUE rb_struct_initialize(VALUE, VALUE)
#define RBASIC_SET_CLASS(obj, cls)
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
st_table * rb_init_identtable(void)
int rb_obj_respond_to(VALUE, ID, int)
int st_insert(st_table *tab, st_data_t key, st_data_t value)
VALUE rb_ary_pop(VALUE ary)
void rb_mark_set(st_table *tbl)
int rb_respond_to(VALUE, ID)
#define SINGLETON_DUMP_UNABLE_P(klass)
MJIT_FUNC_EXPORTED VALUE rb_hash_new_with_size(st_index_t size)
VALUE rb_str_resize(VALUE, long)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
int rb_enc_get_index(VALUE obj)
const typedef OnigEncodingType rb_encoding
VALUE rb_str_intern(VALUE)
#define prohibit_ivar(type, str)
VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc)
rb_encoding * rb_enc_from_index(int index)
VALUE rb_ary_tmp_new(long capa)
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
const char * rb_builtin_type_name(int t)
VALUE rb_reg_new_str(VALUE, int)
VALUE rb_str_equal(VALUE str1, VALUE str2)
VALUE rb_ary_push(VALUE ary, VALUE item)
RUBY_EXTERN double nan(const char *)
#define RBASIC_CLASS(obj)
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
st_table * st_init_strcasetable(void)
#define RARRAY_AREF(a, i)
void rb_str_set_len(VALUE, long)
struct dump_call_arg * dump
void rb_hash_foreach(VALUE hash, rb_foreach_func *func, VALUE farg)
int memcmp(const void *s1, const void *s2, size_t len)
int ruby_marshal_write_long(long x, char *buf)
#define dump_check_funcall(arg, obj, sym, argc, argv)
#define to_be_skipped_id(id)
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
#define StringValueCStr(v)
int link(const char *, const char *)
VALUE rb_ary_clear(VALUE ary)
int st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
int rb_reg_options(VALUE)
unsigned char buf[MIME_BUF_SIZE]
int rb_usascii_encindex(void)
int rb_enc_str_coderange(VALUE)
void rb_mark_hash(st_table *tbl)
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
char str[HTML_ESCAPE_MAX_LEN+1]
#define RREGEXP_SRC_PTR(r)
VALUE rb_struct_s_members(VALUE)
int rb_enc_find_index(const char *name)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define ENC_CODERANGE_7BIT
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
RUBY_EXTERN VALUE rb_cString
VALUE rb_marshal_load(VALUE port)
#define RHASH_SET_IFNONE(h, ifnone)
VALUE(* rb_alloc_func_t)(VALUE)
VALUE rb_ivar_defined(VALUE, ID)
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
void rb_define_const(VALUE, const char *, VALUE)
VALUE rb_io_write(VALUE, VALUE)
#define RSTRUCT_GET(st, idx)
void rb_gc_mark(VALUE ptr)
#define load_funcall(arg, obj, sym, argc, argv)
#define ENCINDEX_US_ASCII
NORETURN(static inline void io_needed(void))
VALUE rb_class_name(VALUE)
VALUE rb_class_path(VALUE)
void rb_mark_tbl(st_table *tbl)
VALUE rb_ivar_set(VALUE, ID, VALUE)
#define dump_funcall(arg, obj, sym, argc, argv)
#define rb_intern_str(string)
int rb_enc_str_asciionly_p(VALUE)
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
int st_lookup(st_table *tab, st_data_t key, st_data_t *value)
RUBY_EXTERN VALUE rb_cRegexp
VALUE rb_marshal_dump(VALUE obj, VALUE port)
void st_free_table(st_table *tab)
VALUE rb_path_to_class(VALUE)
VALUE rb_str_cat(VALUE, const char *, long)
VALUE type(ANYARGS)
ANYARGS-ed function type.
int rb_enc_capable(VALUE obj)
VALUE rb_enc_associate_index(VALUE obj, int idx)
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
Determines if mod inherits arg.
VALUE rb_class_real(VALUE cl)
Looks up the nearest ancestor of cl, skipping singleton classes or module inclusions.