Ruby
2.7.2p137(2020-10-01revision5445e0435260b449decf2ac16f9d09bae3cafe72)
|
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, s_ruby2_keywords_flag;
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"
101 #define name_s_ruby2_keywords_flag "K"
110 static st_table *compat_allocator_tbl;
111 static VALUE compat_allocator_tbl_wrapper;
125 mark_marshal_compat_t(
void *tbl)
131 static st_table *compat_allocator_table(
void);
188 return check_dump_arg(ret,
arg,
name);
191 #define dump_funcall(arg, obj, sym, argc, argv) \
192 check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
193 #define dump_check_funcall(arg, obj, sym, argc, argv) \
194 check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
199 mark_dump_arg(
void *
ptr)
211 free_dump_arg(
void *
ptr)
218 memsize_dump_arg(
const void *
ptr)
225 {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
259 static void w_long(
long,
struct dump_arg*);
287 #define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
292 w_byte((
char)((x >> 0) & 0xff),
arg);
293 w_byte((
char)((x >> 8) & 0xff),
arg);
323 if (0 < x && x < 123) {
327 if (-124 < x && x < 0) {
331 for (
i=1;
i<(
int)
sizeof(
long)+1;
i++) {
347 #define DECIMAL_MANT (53-16)
349 #if DBL_MANT_DIG > 32
351 #elif DBL_MANT_DIG > 24
353 #elif DBL_MANT_DIG > 16
363 if (--
len > 0 && !*
buf++) {
364 int e, s = d < 0, dig = 0;
371 default: m = *
buf++ & 0xff;
373 case 3: m = (m << 8) | (*
buf++ & 0xff);
376 case 2: m = (m << 8) | (*
buf++ & 0xff);
379 case 1: m = (m << 8) | (*
buf++ & 0xff);
383 d +=
ldexp((
double)m, dig);
384 }
while ((
len -= MANT_BITS / 8) > 0);
385 d =
ldexp(d, e - DECIMAL_MANT);
391 #define load_mantissa(d, buf, len) (d)
395 #define FLOAT_DIG (DBL_DIG+2)
417 int decpt, sign, digs,
len = 0;
418 char *e, *p =
ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
419 if (sign)
buf[
len++] =
'-';
421 if (decpt < -3 || decpt > digs) {
423 if (--digs > 0)
buf[
len++] =
'.';
428 else if (decpt > 0) {
431 if ((digs -= decpt) > 0) {
460 w_long((
long)num,
arg);
468 encname = encoding_name(
sym,
arg);
469 if (
NIL_P(encname) ||
479 if (!
NIL_P(encname)) {
484 w_encoding(encname, &c_arg);
492 must_not_be_anonymous(
"class", s);
503 w_object(value,
arg->arg,
arg->limit);
507 #define SINGLETON_DUMP_UNABLE_P(klass) \
508 (rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \
509 (RCLASS_IV_TBL(klass) && RCLASS_IV_TBL(klass)->num_entries > 1))
537 if (
arg->compat_tbl &&
555 if (
klass != super) {
561 #define to_be_skipped_id(id) (id == rb_id_encoding() || id == s_encoding_short || id == s_ruby2_keywords_flag || !rb_id2str(id))
577 if (
id == s_encoding_short) {
581 if (
id == s_ruby2_keywords_flag) {
593 w_object(value,
arg->arg,
arg->limit);
646 w_symbol(
ID2SYM(s_encoding_short),
arg->arg);
676 if (ruby2_keywords_flag || num) *ivobj =
obj;
679 return num + enc + ruby2_keywords_flag;
697 w_long(num,
arg->arg);
698 num -= w_encoding(encname,
arg);
702 w_symbol(
ID2SYM(s_ruby2_keywords_flag),
arg->arg);
706 if (ivobj !=
Qundef && num) {
707 w_ivar_each(ivobj, num,
arg);
717 w_long(num,
arg->arg);
718 w_ivar_each(
obj, num,
arg);
741 w_long((
long)num,
arg);
802 hasiv = has_ivars(
obj, (encname = encoding_name(
obj,
arg)), &ivobj);
803 hasiv2 = has_ivars(
v, (encname2 = encoding_name(
v,
arg)), &ivobj2);
813 w_ivar(hasiv, ivobj, encname, &c_arg);
821 hasiv = has_ivars(
obj, (encname = encoding_name(
obj,
arg)), &ivobj);
831 if (!
arg->compat_tbl) {
835 if (
obj != real_obj && ivobj ==
Qundef) hasiv = 0;
882 w_long((
long)slen,
arg);
883 for (j = 0; j <
len; j++) {
884 #if SIZEOF_BDIGIT > SIZEOF_SHORT
891 if (j ==
len - 1 && num == 0)
break;
913 w_byte((
char)opts,
arg);
969 w_objivar(
obj, &c_arg);
978 "no _dump_data is defined for class %"PRIsVALUE,
995 w_ivar(hasiv, ivobj, encname, &c_arg);
1002 if (!
arg->symbols)
return;
1007 if (
arg->compat_tbl) {
1009 arg->compat_tbl = 0;
1011 if (
arg->encodings) {
1068 if (
NIL_P(a1)) io_needed();
1071 else if (
argc == 2) {
1073 else if (
NIL_P(a1)) io_needed();
1076 return rb_marshal_dump_limited(
obj, port,
limit);
1089 arg->compat_tbl = 0;
1111 clear_dump_arg(
arg);
1132 if (!
arg->symbols) {
1138 #define load_funcall(arg, obj, sym, argc, argv) \
1139 check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
1144 mark_load_arg(
void *
ptr)
1155 free_load_arg(
void *
ptr)
1157 clear_load_arg(
ptr);
1162 memsize_load_arg(
const void *
ptr)
1169 {mark_load_arg, free_load_arg, memsize_load_arg,},
1173 #define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
1195 static unsigned char
1198 if (
arg->buflen == 0) {
1210 return arg->buf[
arg->offset++];
1227 if (
arg->readable >0 ||
arg->buflen > 0) {
1228 c = r_byte1_buffered(
arg);
1242 long_toobig(
int size)
1252 int c = (
signed char)r_byte(
arg);
1255 if (c == 0)
return 0;
1257 if (4 < c && c < 128) {
1260 if (c > (
int)
sizeof(
long)) long_toobig(c);
1267 if (-129 < c && c < -4) {
1271 if (c > (
int)
sizeof(
long)) long_toobig(c);
1274 x &= ~((
long)0xff << (8*
i));
1312 if (len <= arg->buflen) {
1318 long buflen =
arg->buflen;
1319 long readable =
arg->readable + 1;
1320 long tmp_len, read_len, need_len =
len - buflen;
1324 read_len = need_len > readable ? need_len : readable;
1327 if (
NIL_P(tmp)) too_short();
1332 if (tmp_len < need_len) too_short();
1337 if (tmp_len > need_len) {
1338 buflen = tmp_len - need_len;
1340 arg->buflen = buflen;
1351 #define r_bytes(arg) r_bytes0(r_long(arg), (arg))
1369 if (
arg->readable > 0 ||
arg->buflen > 0) {
1380 name_equal(
const char *
name,
size_t nlen,
const char *p,
long l)
1382 if ((
size_t)l != nlen || *p != *
name)
return 0;
1383 return nlen == 1 ||
memcmp(p+1,
name+1, nlen-1) == 0;
1389 static const char name_encoding[8] =
"encoding";
1394 if (l <= 0)
return -1;
1395 if (name_equal(name_encoding,
sizeof(name_encoding), p, l)) {
1408 ruby2_keywords_flag_check(
VALUE sym)
1413 if (l <= 0)
return 0;
1424 long num = r_long(
arg);
1443 long num = r_long(
arg);
1446 idx = sym2encidx(
sym, r_object(
arg));
1460 switch ((
type = r_byte(
arg))) {
1467 return r_symreal(
arg, ivar);
1472 return r_symlink(
arg);
1479 return r_symbol(
arg);
1530 v = r_fixup_compat(
v,
arg);
1531 v = r_post_proc(
v,
arg);
1563 int idx = sym2encidx(
sym, val);
1571 if (has_encoding) *has_encoding =
TRUE;
1573 else if (ruby2_keywords_flag_check(
sym)) {
1584 }
while (--
len > 0);
1599 #define path2module(path) must_be_module(rb_path_to_class(path), path)
1621 if (oldclass) *oldclass = compat->
oldclass;
1623 if (!
arg->compat_tbl) {
1636 return obj_alloc_by_klass(path2class(
path),
arg, 0);
1650 #define prohibit_ivar(type, str) do { \
1651 if (!ivp || !*ivp) break; \
1652 rb_raise(rb_eTypeError, \
1653 "can't override instance variable of "type" `%"PRIsVALUE"'", \
1672 v = r_post_proc(
v,
arg);
1679 v = r_object0(
arg, &ivar, extmod);
1707 must_be_module(m,
path);
1710 v = r_object0(
arg, 0, extmod);
1721 VALUE c = path2class(r_unique(
arg));
1726 v = r_object0(
arg, 0, extmod);
1734 if (
TYPE(
v) !=
TYPE(tmp))
goto format_error;
1742 v = r_leave(
v,
arg);
1747 v = r_leave(
v,
arg);
1752 v = r_leave(
v,
arg);
1757 long i = r_long(
arg);
1760 v = r_leave(
v,
arg);
1785 v = r_leave(
v,
arg);
1797 data = r_bytes0(
len * 2,
arg);
1802 v = r_leave(
v,
arg);
1808 v = r_leave(
v,
arg);
1814 int options = r_byte(
arg);
1815 int has_encoding =
FALSE;
1819 r_ivar(
str, &has_encoding,
arg);
1822 if (!has_encoding) {
1827 for (;
len-- > 0; *dst++ = *
src++) {
1829 case '\\': bs++;
break;
1830 case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
1831 case 'm':
case 'o':
case 'p':
case 'q':
case 'u':
case 'y':
1832 case 'E':
case 'F':
case 'H':
case 'I':
case 'J':
case 'K':
1833 case 'L':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
1834 case 'S':
case 'T':
case 'U':
case 'V':
case 'X':
case 'Y':
1837 default: bs = 0;
break;
1843 v = r_leave(
v,
arg);
1853 arg->readable +=
len - 1;
1858 v = r_leave(
v,
arg);
1870 arg->readable += (
len - 1) * 2;
1881 v = r_leave(
v,
arg);
1904 arg->readable += (
len - 1) * 2;
1905 v = r_entry0(
v, idx,
arg);
1916 slot = r_symbol(
arg);
1933 v = r_leave(
v,
arg);
1949 data = r_string(
arg);
1960 v = r_post_proc(
v,
arg);
1971 v = obj_alloc_by_klass(
klass,
arg, &oldclass);
1972 if (!
NIL_P(extmod)) {
1974 append_extmod(
v, extmod);
1981 data = r_object(
arg);
1983 v = r_fixup_compat(
v,
arg);
1984 v = r_copy_ivar(
v, data);
1985 v = r_post_proc(
v,
arg);
1986 if (!
NIL_P(extmod)) {
1987 if (oldclass) append_extmod(
v, extmod);
1996 v = obj_alloc_by_path(r_unique(
arg),
arg);
2000 v = r_entry0(
v, idx,
arg);
2002 v = r_leave(
v,
arg);
2013 v = obj_alloc_by_klass(
klass,
arg, &oldclass);
2020 "class %"PRIsVALUE" needs to have instance method `_load_data'",
2023 r = r_object0(
arg, 0, extmod);
2025 v = r_leave(
v,
arg);
2036 v = r_leave(
v,
arg);
2044 v = path2class(
str);
2047 v = r_leave(
v,
arg);
2058 v = r_leave(
v,
arg);
2064 v = r_symreal(
arg, *ivp);
2068 v = r_symreal(
arg, 0);
2071 v = r_leave(
v,
arg);
2093 return r_object0(
arg, 0,
Qnil);
2106 if (!
arg->symbols)
return;
2111 if (
arg->compat_tbl) {
2113 arg->compat_tbl = 0;
2139 return rb_marshal_load_with_proc(port, proc);
2143 rb_marshal_load_with_proc(
VALUE port,
VALUE proc)
2165 arg->compat_tbl = 0;
2177 clear_load_arg(
arg);
2179 \tformat version %d.%d required; %d.%d given",
2183 rb_warn(
"incompatible marshal file format (can be read)\n\
2184 \tformat version %d.%d required; %d.%d given",
2188 if (!
NIL_P(proc))
arg->proc = proc;
2190 clear_load_arg(
arg);
2311 #define rb_intern(str) rb_intern_const(str)
2314 #define set_id(sym) sym = rb_intern_const(name_##sym)
2327 set_id(s_encoding_short);
2328 set_id(s_ruby2_keywords_flag);
2341 compat_allocator_table(
void)
2343 if (compat_allocator_tbl)
return compat_allocator_tbl;
2345 #undef RUBY_UNTYPED_DATA_WARNING
2346 #define RUBY_UNTYPED_DATA_WARNING 0
2347 compat_allocator_tbl_wrapper =
2350 return compat_allocator_tbl;
2356 return rb_marshal_dump_limited(
obj, port, -1);
2362 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)
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)
#define RARRAY_AREF(a, i)
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)
void rb_str_set_len(VALUE, long)
#define name_s_encoding_short
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)
#define name_s_ruby2_keywords_flag
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)
#define rb_strlen_lit(str)
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.