2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
marshal.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Thu Apr 27 16:30:01 JST 1995
|
|
|
|
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-25 07:29:39 +04:00
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
|
|
|
|
**********************************************************************/
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2020-05-08 12:31:09 +03:00
|
|
|
#include "ruby/internal/config.h"
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2003-01-15 09:29:05 +03:00
|
|
|
#include <math.h>
|
2003-04-20 19:11:20 +04:00
|
|
|
#ifdef HAVE_FLOAT_H
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
2003-12-22 11:23:55 +03:00
|
|
|
#ifdef HAVE_IEEEFP_H
|
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
2003-01-15 09:29:05 +03:00
|
|
|
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "encindex.h"
|
|
|
|
#include "id_table.h"
|
|
|
|
#include "internal.h"
|
2020-08-14 08:45:23 +03:00
|
|
|
#include "internal/array.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/bignum.h"
|
|
|
|
#include "internal/class.h"
|
|
|
|
#include "internal/encoding.h"
|
|
|
|
#include "internal/error.h"
|
|
|
|
#include "internal/hash.h"
|
2022-08-16 02:14:12 +03:00
|
|
|
#include "internal/numeric.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/object.h"
|
|
|
|
#include "internal/struct.h"
|
2021-09-22 19:36:27 +03:00
|
|
|
#include "internal/symbol.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/util.h"
|
|
|
|
#include "internal/vm.h"
|
|
|
|
#include "ruby/io.h"
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/st.h"
|
|
|
|
#include "ruby/util.h"
|
2021-09-18 16:34:15 +03:00
|
|
|
#include "builtin.h"
|
2022-10-03 18:14:32 +03:00
|
|
|
#include "shape.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
|
2001-08-29 10:28:51 +04:00
|
|
|
#define BITSPERSHORT (2*CHAR_BIT)
|
2000-10-31 11:37:47 +03:00
|
|
|
#define SHORTMASK ((1<<BITSPERSHORT)-1)
|
2010-12-19 13:15:57 +03:00
|
|
|
#define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
|
2000-10-31 11:37:47 +03:00
|
|
|
|
2014-04-13 07:48:17 +04:00
|
|
|
#if SIZEOF_SHORT == SIZEOF_BDIGIT
|
2000-10-31 11:37:47 +03:00
|
|
|
#define SHORTLEN(x) (x)
|
|
|
|
#else
|
2014-04-19 05:11:04 +04:00
|
|
|
static size_t
|
|
|
|
shortlen(size_t len, BDIGIT *ds)
|
2000-10-31 11:37:47 +03:00
|
|
|
{
|
|
|
|
BDIGIT num;
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
num = ds[len-1];
|
|
|
|
while (num) {
|
|
|
|
num = SHORTDN(num);
|
|
|
|
offset++;
|
|
|
|
}
|
2014-04-13 07:48:17 +04:00
|
|
|
return (len - 1)*SIZEOF_BDIGIT/2 + offset;
|
2000-10-31 11:37:47 +03:00
|
|
|
}
|
|
|
|
#define SHORTLEN(x) shortlen((x),d)
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
#define MARSHAL_MAJOR 4
|
* parse.y, compile.c, gc.c, insns.def, intern.h, iseq.c, node.h,
object.c, string.c, variable.c, vm_macro.def: revert private
instance variable feature, which is postponed until next major
release.
* marshal.c: TYPE_SYMBOL2 removed; MARSHAL_MINOR reverted back to
8th version.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11813 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-23 05:49:41 +03:00
|
|
|
#define MARSHAL_MINOR 8
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
#define TYPE_NIL '0'
|
|
|
|
#define TYPE_TRUE 'T'
|
|
|
|
#define TYPE_FALSE 'F'
|
|
|
|
#define TYPE_FIXNUM 'i'
|
|
|
|
|
2002-09-05 13:42:56 +04:00
|
|
|
#define TYPE_EXTENDED 'e'
|
1998-01-16 15:19:09 +03:00
|
|
|
#define TYPE_UCLASS 'C'
|
|
|
|
#define TYPE_OBJECT 'o'
|
2002-09-05 13:42:56 +04:00
|
|
|
#define TYPE_DATA 'd'
|
1998-01-16 15:19:09 +03:00
|
|
|
#define TYPE_USERDEF 'u'
|
2003-04-20 19:11:20 +04:00
|
|
|
#define TYPE_USRMARSHAL 'U'
|
1998-01-16 15:19:09 +03:00
|
|
|
#define TYPE_FLOAT 'f'
|
|
|
|
#define TYPE_BIGNUM 'l'
|
|
|
|
#define TYPE_STRING '"'
|
|
|
|
#define TYPE_REGEXP '/'
|
|
|
|
#define TYPE_ARRAY '['
|
|
|
|
#define TYPE_HASH '{'
|
1999-12-01 12:24:48 +03:00
|
|
|
#define TYPE_HASH_DEF '}'
|
1998-01-16 15:19:09 +03:00
|
|
|
#define TYPE_STRUCT 'S'
|
1999-12-01 12:24:48 +03:00
|
|
|
#define TYPE_MODULE_OLD 'M'
|
|
|
|
#define TYPE_CLASS 'c'
|
|
|
|
#define TYPE_MODULE 'm'
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
#define TYPE_SYMBOL ':'
|
|
|
|
#define TYPE_SYMLINK ';'
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
#define TYPE_IVAR 'I'
|
1998-01-16 15:19:09 +03:00
|
|
|
#define TYPE_LINK '@'
|
|
|
|
|
2003-07-29 22:26:55 +04:00
|
|
|
static ID s_dump, s_load, s_mdump, s_mload;
|
2008-10-01 14:11:51 +04:00
|
|
|
static ID s_dump_data, s_load_data, s_alloc, s_call;
|
2008-01-25 19:37:53 +03:00
|
|
|
static ID s_getbyte, s_read, s_write, s_binmode;
|
2020-01-11 09:04:44 +03:00
|
|
|
static ID s_encoding_short, s_ruby2_keywords_flag;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2014-11-24 06:43:59 +03:00
|
|
|
#define name_s_dump "_dump"
|
|
|
|
#define name_s_load "_load"
|
|
|
|
#define name_s_mdump "marshal_dump"
|
|
|
|
#define name_s_mload "marshal_load"
|
|
|
|
#define name_s_dump_data "_dump_data"
|
|
|
|
#define name_s_load_data "_load_data"
|
|
|
|
#define name_s_alloc "_alloc"
|
|
|
|
#define name_s_call "call"
|
|
|
|
#define name_s_getbyte "getbyte"
|
|
|
|
#define name_s_read "read"
|
|
|
|
#define name_s_write "write"
|
|
|
|
#define name_s_binmode "binmode"
|
2019-08-10 05:38:49 +03:00
|
|
|
#define name_s_encoding_short "E"
|
2020-01-11 09:04:44 +03:00
|
|
|
#define name_s_ruby2_keywords_flag "K"
|
2014-11-24 06:43:59 +03:00
|
|
|
|
2007-09-08 19:07:18 +04:00
|
|
|
typedef struct {
|
|
|
|
VALUE newclass;
|
|
|
|
VALUE oldclass;
|
|
|
|
VALUE (*dumper)(VALUE);
|
|
|
|
VALUE (*loader)(VALUE, VALUE);
|
|
|
|
} marshal_compat_t;
|
|
|
|
|
|
|
|
static st_table *compat_allocator_tbl;
|
2007-09-26 23:12:04 +04:00
|
|
|
static VALUE compat_allocator_tbl_wrapper;
|
2019-06-04 13:06:19 +03:00
|
|
|
static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit);
|
2021-09-18 16:34:15 +03:00
|
|
|
static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze);
|
2007-09-26 23:12:04 +04:00
|
|
|
|
|
|
|
static int
|
2019-08-26 10:06:40 +03:00
|
|
|
mark_marshal_compat_i(st_data_t key, st_data_t value, st_data_t _)
|
2007-09-26 23:12:04 +04:00
|
|
|
{
|
|
|
|
marshal_compat_t *p = (marshal_compat_t *)value;
|
|
|
|
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);
|
|
|
|
}
|
2007-09-08 19:07:18 +04:00
|
|
|
|
2015-12-02 10:27:22 +03:00
|
|
|
static st_table *compat_allocator_table(void);
|
|
|
|
|
2007-09-08 19:07:18 +04:00
|
|
|
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;
|
|
|
|
|
2015-12-02 10:27:22 +03:00
|
|
|
st_insert(compat_allocator_table(), (st_data_t)allocator, (st_data_t)compat);
|
2007-09-08 19:07:18 +04:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
struct dump_arg {
|
2002-10-17 14:20:52 +04:00
|
|
|
VALUE str, dest;
|
2003-08-07 01:50:06 +04:00
|
|
|
st_table *symbols;
|
1998-01-16 15:19:09 +03:00
|
|
|
st_table *data;
|
2007-09-08 19:07:18 +04:00
|
|
|
st_table *compat_tbl;
|
2007-10-19 15:08:16 +04:00
|
|
|
st_table *encodings;
|
2022-08-16 02:14:12 +03:00
|
|
|
unsigned long num_entries;
|
1998-01-16 15:19:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dump_call_arg {
|
|
|
|
VALUE obj;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
int limit;
|
|
|
|
};
|
|
|
|
|
2015-09-25 15:45:10 +03:00
|
|
|
static VALUE
|
|
|
|
check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name)
|
2008-05-22 15:20:56 +04:00
|
|
|
{
|
2009-10-04 14:30:56 +04:00
|
|
|
if (!arg->symbols) {
|
2008-10-01 14:11:51 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
|
2014-11-24 06:43:59 +03:00
|
|
|
name);
|
2008-05-22 15:20:56 +04:00
|
|
|
}
|
2015-09-25 15:45:10 +03:00
|
|
|
return ret;
|
2008-05-22 15:20:56 +04:00
|
|
|
}
|
2016-11-24 14:07:12 +03:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv,
|
|
|
|
struct dump_arg *arg, const char *name)
|
|
|
|
{
|
|
|
|
VALUE ret = rb_funcallv(obj, sym, argc, argv);
|
|
|
|
VALUE klass = CLASS_OF(obj);
|
|
|
|
if (CLASS_OF(ret) == klass) {
|
|
|
|
rb_raise(rb_eRuntimeError, "%"PRIsVALUE"#%s returned same class instance",
|
|
|
|
klass, name);
|
|
|
|
}
|
|
|
|
return check_dump_arg(ret, arg, name);
|
|
|
|
}
|
|
|
|
|
2015-09-25 15:45:10 +03:00
|
|
|
#define dump_funcall(arg, obj, sym, argc, argv) \
|
2016-11-24 14:07:12 +03:00
|
|
|
check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
|
2015-09-25 15:45:10 +03:00
|
|
|
#define dump_check_funcall(arg, obj, sym, argc, argv) \
|
|
|
|
check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
|
2008-05-22 15:20:56 +04:00
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
static void clear_dump_arg(struct dump_arg *arg);
|
|
|
|
|
2007-09-26 23:40:49 +04:00
|
|
|
static void
|
|
|
|
mark_dump_arg(void *ptr)
|
|
|
|
{
|
|
|
|
struct dump_arg *p = ptr;
|
2009-10-07 11:42:30 +04:00
|
|
|
if (!p->symbols)
|
2007-11-18 14:50:25 +03:00
|
|
|
return;
|
2014-07-28 12:15:42 +04:00
|
|
|
rb_mark_set(p->symbols);
|
2007-09-26 23:40:49 +04:00
|
|
|
rb_mark_set(p->data);
|
|
|
|
rb_mark_hash(p->compat_tbl);
|
2009-11-26 01:27:44 +03:00
|
|
|
rb_gc_mark(p->str);
|
2007-09-26 23:40:49 +04:00
|
|
|
}
|
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
static void
|
|
|
|
free_dump_arg(void *ptr)
|
|
|
|
{
|
|
|
|
clear_dump_arg(ptr);
|
|
|
|
xfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
memsize_dump_arg(const void *ptr)
|
|
|
|
{
|
2015-12-10 00:34:01 +03:00
|
|
|
return sizeof(struct dump_arg);
|
2009-10-04 14:30:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t dump_arg_data = {
|
|
|
|
"dump_arg",
|
2010-07-18 11:31:54 +04:00
|
|
|
{mark_dump_arg, free_dump_arg, memsize_dump_arg,},
|
2014-12-01 09:38:04 +03:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-10-04 14:30:56 +04:00
|
|
|
};
|
|
|
|
|
2014-02-05 12:24:37 +04:00
|
|
|
static VALUE
|
2009-08-10 01:55:55 +04:00
|
|
|
must_not_be_anonymous(const char *type, VALUE path)
|
2004-01-16 08:33:39 +03:00
|
|
|
{
|
2006-08-31 14:47:44 +04:00
|
|
|
char *n = RSTRING_PTR(path);
|
2004-01-16 08:33:39 +03:00
|
|
|
|
2009-08-10 01:55:55 +04:00
|
|
|
if (!rb_enc_asciicompat(rb_enc_get(path))) {
|
|
|
|
/* cannot occur? */
|
2014-02-05 12:24:37 +04:00
|
|
|
rb_raise(rb_eTypeError, "can't dump non-ascii %s name % "PRIsVALUE,
|
|
|
|
type, path);
|
2009-08-10 01:55:55 +04:00
|
|
|
}
|
2004-01-17 18:23:59 +03:00
|
|
|
if (n[0] == '#') {
|
2014-02-05 12:24:37 +04:00
|
|
|
rb_raise(rb_eTypeError, "can't dump anonymous %s % "PRIsVALUE,
|
|
|
|
type, path);
|
2004-01-17 18:23:59 +03:00
|
|
|
}
|
2014-02-05 12:24:37 +04:00
|
|
|
return path;
|
2009-08-10 01:55:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
class2path(VALUE klass)
|
|
|
|
{
|
|
|
|
VALUE path = rb_class_path(klass);
|
|
|
|
|
2014-02-05 12:24:37 +04:00
|
|
|
must_not_be_anonymous((RB_TYPE_P(klass, T_CLASS) ? "class" : "module"), path);
|
2009-08-10 01:55:55 +04:00
|
|
|
if (rb_path_to_class(path) != rb_class_real(klass)) {
|
2014-02-05 12:24:37 +04:00
|
|
|
rb_raise(rb_eTypeError, "% "PRIsVALUE" can't be referred to", path);
|
2004-01-16 08:33:39 +03:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-09-27 12:17:28 +03:00
|
|
|
int ruby_marshal_write_long(long x, char *buf);
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 10:32:32 +04:00
|
|
|
static void w_long(long, struct dump_arg*);
|
2019-07-01 10:20:03 +03:00
|
|
|
static int w_encoding(VALUE encname, struct dump_call_arg *arg);
|
2014-02-05 12:32:11 +04:00
|
|
|
static VALUE encoding_name(VALUE obj, struct dump_arg *arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2002-10-17 14:20:52 +04:00
|
|
|
static void
|
2009-05-26 08:58:15 +04:00
|
|
|
w_nbyte(const char *s, long n, struct dump_arg *arg)
|
2002-10-17 14:20:52 +04:00
|
|
|
{
|
2003-03-03 10:20:17 +03:00
|
|
|
VALUE buf = arg->str;
|
|
|
|
rb_str_buf_cat(buf, s, n);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
|
2003-03-03 10:20:17 +03:00
|
|
|
rb_io_write(arg->dest, buf);
|
|
|
|
rb_str_resize(buf, 0);
|
2002-10-17 14:20:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
w_byte(char c, struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2003-03-03 10:20:17 +03:00
|
|
|
w_nbyte(&c, 1, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-26 08:58:15 +04:00
|
|
|
w_bytes(const char *s, long n, struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
w_long(n, arg);
|
2003-03-03 10:20:17 +03:00
|
|
|
w_nbyte(s, n, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2010-12-19 13:15:57 +03:00
|
|
|
#define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
|
2010-05-13 08:30:07 +04:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
w_short(int x, struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2007-03-21 14:38:42 +03:00
|
|
|
w_byte((char)((x >> 0) & 0xff), arg);
|
|
|
|
w_byte((char)((x >> 8) & 0xff), arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
w_long(long x, struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
char buf[sizeof(long)+1];
|
2018-09-27 12:17:28 +03:00
|
|
|
int i = ruby_marshal_write_long(x, buf);
|
|
|
|
if (i < 0) {
|
|
|
|
rb_raise(rb_eTypeError, "long too big to dump");
|
|
|
|
}
|
|
|
|
w_nbyte(buf, i, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ruby_marshal_write_long(long x, char *buf)
|
|
|
|
{
|
2014-12-26 09:20:55 +03:00
|
|
|
int i;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2000-12-05 12:36:54 +03:00
|
|
|
#if SIZEOF_LONG > 4
|
2001-08-29 10:28:51 +04:00
|
|
|
if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
|
2000-12-05 12:36:54 +03:00
|
|
|
/* big long does not fit in 4 bytes */
|
2018-09-27 12:17:28 +03:00
|
|
|
return -1;
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
if (x == 0) {
|
2018-09-27 12:17:28 +03:00
|
|
|
buf[0] = 0;
|
|
|
|
return 1;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2000-12-05 12:36:54 +03:00
|
|
|
if (0 < x && x < 123) {
|
2018-09-27 12:17:28 +03:00
|
|
|
buf[0] = (char)(x + 5);
|
|
|
|
return 1;
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
|
|
|
if (-124 < x && x < 0) {
|
2018-09-27 12:17:28 +03:00
|
|
|
buf[0] = (char)((x - 5)&0xff);
|
|
|
|
return 1;
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
2009-05-26 08:58:15 +04:00
|
|
|
for (i=1;i<(int)sizeof(long)+1;i++) {
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
buf[i] = (char)(x & 0xff);
|
1998-01-16 15:19:09 +03:00
|
|
|
x = RSHIFT(x,8);
|
|
|
|
if (x == 0) {
|
|
|
|
buf[0] = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (x == -1) {
|
|
|
|
buf[0] = -i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 12:17:28 +03:00
|
|
|
return i+1;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2003-04-20 19:11:20 +04:00
|
|
|
#ifdef DBL_MANT_DIG
|
2003-04-22 14:08:57 +04:00
|
|
|
#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
|
|
|
|
|
2003-04-20 19:11:20 +04:00
|
|
|
static double
|
2009-05-26 08:58:15 +04:00
|
|
|
load_mantissa(double d, const char *buf, long len)
|
2003-04-20 19:11:20 +04:00
|
|
|
{
|
2009-05-26 08:58:15 +04:00
|
|
|
if (!len) return d;
|
2003-04-22 14:08:57 +04:00
|
|
|
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) {
|
2019-07-14 08:04:34 +03:00
|
|
|
default: m = *buf++ & 0xff; /* fall through */
|
2003-04-22 14:08:57 +04:00
|
|
|
#if MANT_BITS > 24
|
2019-07-14 08:04:34 +03:00
|
|
|
case 3: m = (m << 8) | (*buf++ & 0xff); /* fall through */
|
2003-04-22 14:08:57 +04:00
|
|
|
#endif
|
|
|
|
#if MANT_BITS > 16
|
2019-07-14 08:04:34 +03:00
|
|
|
case 2: m = (m << 8) | (*buf++ & 0xff); /* fall through */
|
2003-04-22 14:08:57 +04:00
|
|
|
#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);
|
2003-04-20 19:11:20 +04:00
|
|
|
if (s) d = -d;
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define load_mantissa(d, buf, len) (d)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DBL_DIG
|
2003-04-21 17:02:08 +04:00
|
|
|
#define FLOAT_DIG (DBL_DIG+2)
|
2003-04-20 19:11:20 +04:00
|
|
|
#else
|
|
|
|
#define FLOAT_DIG 17
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
w_float(double d, struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2007-09-12 10:19:06 +04:00
|
|
|
char buf[FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2001-11-19 08:03:03 +03:00
|
|
|
if (isinf(d)) {
|
2010-05-13 08:30:07 +04:00
|
|
|
if (d < 0) w_cstr("-inf", arg);
|
|
|
|
else w_cstr("inf", arg);
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
|
|
|
else if (isnan(d)) {
|
2010-05-13 08:30:07 +04:00
|
|
|
w_cstr("nan", arg);
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
2001-11-27 13:00:35 +03:00
|
|
|
else if (d == 0.0) {
|
2018-11-16 04:52:39 +03:00
|
|
|
if (signbit(d)) w_cstr("-0", arg);
|
|
|
|
else w_cstr("0", arg);
|
2001-11-27 13:00:35 +03:00
|
|
|
}
|
2001-11-19 08:03:03 +03:00
|
|
|
else {
|
2010-05-21 07:26:00 +04:00
|
|
|
int decpt, sign, digs, len = 0;
|
|
|
|
char *e, *p = ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
|
|
|
|
if (sign) buf[len++] = '-';
|
|
|
|
digs = (int)(e - p);
|
|
|
|
if (decpt < -3 || decpt > digs) {
|
|
|
|
buf[len++] = p[0];
|
2010-08-05 13:25:39 +04:00
|
|
|
if (--digs > 0) buf[len++] = '.';
|
|
|
|
memcpy(buf + len, p + 1, digs);
|
2010-05-21 07:26:00 +04:00
|
|
|
len += digs;
|
|
|
|
len += snprintf(buf + len, sizeof(buf) - len, "e%d", decpt - 1);
|
|
|
|
}
|
|
|
|
else if (decpt > 0) {
|
|
|
|
memcpy(buf + len, p, decpt);
|
|
|
|
len += decpt;
|
|
|
|
if ((digs -= decpt) > 0) {
|
|
|
|
buf[len++] = '.';
|
|
|
|
memcpy(buf + len, p + decpt, digs);
|
|
|
|
len += digs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf[len++] = '0';
|
|
|
|
buf[len++] = '.';
|
|
|
|
if (decpt) {
|
|
|
|
memset(buf + len, '0', -decpt);
|
|
|
|
len -= decpt;
|
|
|
|
}
|
|
|
|
memcpy(buf + len, p, digs);
|
|
|
|
len += digs;
|
|
|
|
}
|
|
|
|
xfree(p);
|
2010-05-14 02:27:10 +04:00
|
|
|
w_bytes(buf, len, arg);
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-28 12:15:42 +04:00
|
|
|
w_symbol(VALUE sym, struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2003-08-16 18:58:34 +04:00
|
|
|
st_data_t num;
|
2014-02-05 12:32:11 +04:00
|
|
|
VALUE encname;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2014-07-28 12:15:42 +04:00
|
|
|
if (st_lookup(arg->symbols, sym, &num)) {
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_SYMLINK, arg);
|
2003-08-16 18:58:34 +04:00
|
|
|
w_long((long)num, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2014-07-29 07:28:43 +04:00
|
|
|
const VALUE orig_sym = sym;
|
2014-07-28 12:15:42 +04:00
|
|
|
sym = rb_sym2str(sym);
|
2007-09-26 23:12:04 +04:00
|
|
|
if (!sym) {
|
2014-07-28 12:15:42 +04:00
|
|
|
rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, sym);
|
2007-09-26 23:12:04 +04:00
|
|
|
}
|
2014-02-05 12:32:11 +04:00
|
|
|
encname = encoding_name(sym, arg);
|
|
|
|
if (NIL_P(encname) ||
|
2022-05-01 17:13:59 +03:00
|
|
|
is_ascii_string(sym)) {
|
2014-02-05 12:32:11 +04:00
|
|
|
encname = Qnil;
|
2009-08-08 01:18:33 +04:00
|
|
|
}
|
2010-01-08 07:12:29 +03:00
|
|
|
else {
|
2009-08-08 01:18:33 +04:00
|
|
|
w_byte(TYPE_IVAR, arg);
|
|
|
|
}
|
* parse.y, compile.c, gc.c, insns.def, intern.h, iseq.c, node.h,
object.c, string.c, variable.c, vm_macro.def: revert private
instance variable feature, which is postponed until next major
release.
* marshal.c: TYPE_SYMBOL2 removed; MARSHAL_MINOR reverted back to
8th version.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11813 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-23 05:49:41 +03:00
|
|
|
w_byte(TYPE_SYMBOL, arg);
|
2009-08-08 01:18:33 +04:00
|
|
|
w_bytes(RSTRING_PTR(sym), RSTRING_LEN(sym), arg);
|
2014-07-29 07:28:43 +04:00
|
|
|
st_add_direct(arg->symbols, orig_sym, arg->symbols->num_entries);
|
2014-02-05 12:32:11 +04:00
|
|
|
if (!NIL_P(encname)) {
|
2009-08-08 01:18:33 +04:00
|
|
|
struct dump_call_arg c_arg;
|
|
|
|
c_arg.limit = 1;
|
|
|
|
c_arg.arg = arg;
|
2014-02-05 12:32:11 +04:00
|
|
|
w_long(1L, arg);
|
|
|
|
w_encoding(encname, &c_arg);
|
2009-08-08 01:18:33 +04:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-08-10 01:55:55 +04:00
|
|
|
w_unique(VALUE s, struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2009-08-10 01:55:55 +04:00
|
|
|
must_not_be_anonymous("class", s);
|
2014-10-14 11:23:01 +04:00
|
|
|
w_symbol(rb_str_intern(s), arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 10:32:32 +04:00
|
|
|
static void w_object(VALUE,struct dump_arg*,int);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
hash_each(VALUE key, VALUE value, VALUE v)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2019-08-27 05:53:39 +03:00
|
|
|
struct dump_call_arg *arg = (void *)v;
|
2003-10-09 21:45:53 +04:00
|
|
|
w_object(key, arg->arg, arg->limit);
|
|
|
|
w_object(value, arg->arg, arg->limit);
|
1998-01-16 15:19:09 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2013-03-09 06:58:45 +04:00
|
|
|
#define SINGLETON_DUMP_UNABLE_P(klass) \
|
2015-08-12 11:43:55 +03:00
|
|
|
(rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \
|
2023-02-15 17:18:01 +03:00
|
|
|
rb_ivar_count(klass) > 0)
|
2013-03-09 06:58:45 +04:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
w_extended(VALUE klass, struct dump_arg *arg, int check)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2007-03-20 17:00:07 +03:00
|
|
|
if (check && FL_TEST(klass, FL_SINGLETON)) {
|
2013-03-09 06:58:45 +04:00
|
|
|
VALUE origin = RCLASS_ORIGIN(klass);
|
|
|
|
if (SINGLETON_DUMP_UNABLE_P(klass) ||
|
|
|
|
(origin != klass && SINGLETON_DUMP_UNABLE_P(origin))) {
|
2002-08-27 12:31:08 +04:00
|
|
|
rb_raise(rb_eTypeError, "singleton can't be dumped");
|
|
|
|
}
|
2007-09-28 10:21:46 +04:00
|
|
|
klass = RCLASS_SUPER(klass);
|
2002-08-27 12:31:08 +04:00
|
|
|
}
|
2002-09-05 13:42:56 +04:00
|
|
|
while (BUILTIN_TYPE(klass) == T_ICLASS) {
|
Ensure origins for all included, prepended, and refined modules
This fixes various issues when a module is included in or prepended
to a module or class, and then refined, or refined and then included
or prepended to a module or class.
Implement by renaming ensure_origin to rb_ensure_origin, making it
non-static, and calling it when refining a module.
Fix Module#initialize_copy to handle origins correctly. Previously,
Module#initialize_copy did not handle origins correctly. For example,
this code:
```ruby
module B; end
class A
def b; 2 end
prepend B
end
a = A.dup.new
class A
def b; 1 end
end
p a.b
```
Printed 1 instead of 2. This is because the super chain for
a.singleton_class was:
```
a.singleton_class
A.dup
B(iclass)
B(iclass origin)
A(origin) # not A.dup(origin)
```
The B iclasses would not be modified, so the includer entry would be
still be set to A and not A.dup.
This modifies things so that if the class/module has an origin,
all iclasses between the class/module and the origin are duplicated
and have the correct includer entry set, and the correct origin
is created.
This requires other changes to make sure all tests still pass:
* rb_undef_methods_from doesn't automatically handle classes with
origins, so pass it the origin for Comparable when undefing
methods in Complex. This fixed a failure in the Complex tests.
* When adding a method, the method cache was not cleared
correctly if klass has an origin. Clear the method cache for
the klass before switching to the origin of klass. This fixed
failures in the autoload tests related to overridding require,
without breaking the optimization tests. Also clear the method
cache for both the module and origin when removing a method.
* Module#include? is fixed to skip origin iclasses.
* Refinements are fixed to use the origin class of the module that
has an origin.
* RCLASS_REFINED_BY_ANY is removed as it was only used in a single
place and is no longer needed.
* Marshal#dump is fixed to skip iclass origins.
* rb_method_entry_make is fixed to handled overridden optimized
methods for modules that have origins.
Fixes [Bug #16852]
2020-05-24 06:16:27 +03:00
|
|
|
if (!FL_TEST(klass, RICLASS_IS_ORIGIN) ||
|
|
|
|
BUILTIN_TYPE(RBASIC(klass)->klass) != T_MODULE) {
|
|
|
|
VALUE path = rb_class_name(RBASIC(klass)->klass);
|
|
|
|
w_byte(TYPE_EXTENDED, arg);
|
|
|
|
w_unique(path, arg);
|
|
|
|
}
|
|
|
|
klass = RCLASS_SUPER(klass);
|
2002-09-05 13:42:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-10-21 10:46:41 +04:00
|
|
|
w_class(char type, VALUE obj, struct dump_arg *arg, int check)
|
2002-09-05 13:42:56 +04:00
|
|
|
{
|
2009-08-10 01:55:55 +04:00
|
|
|
VALUE path;
|
2007-09-26 23:12:04 +04:00
|
|
|
st_data_t real_obj;
|
2007-09-08 19:07:18 +04:00
|
|
|
VALUE klass;
|
2002-09-05 13:42:56 +04:00
|
|
|
|
2014-10-02 00:29:46 +04:00
|
|
|
if (arg->compat_tbl &&
|
|
|
|
st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) {
|
2007-09-26 23:12:04 +04:00
|
|
|
obj = (VALUE)real_obj;
|
2007-09-08 19:07:18 +04:00
|
|
|
}
|
|
|
|
klass = CLASS_OF(obj);
|
* gc.c (Init_stack): stack region is far smaller than usual if
pthread is used.
* marshal.c (w_extended): singleton methods should not be checked
when dumping via marshal_dump() or _dump(). [ruby-talk:85909]
* file.c (getcwdofdrv): avoid using getcwd() directly, use
my_getcwd() instead.
* merged NeXT, OpenStep, Rhapsody ports patch from Eric Sunshine
<sunshine@sunshineco.com>. [ruby-core:01596]
* marshal.c (w_object): LINK check earlier than anything else,
i.e. do not dump TYPE_IVAR for already dumped objects.
(ruby-bugs PR#1220)
* eval.c (rb_eval): call "inherited" only when a new class is
generated; not on reopening.
* eval.c (eval): prepend error position in evaluating string to
* configure.in: revived NextStep, OpenStep, and Rhapsody ports which
had become unbuildable; enhanced --enable-fat-binary option so that
it accepts a list of desired architectures (rather than assuming a
fixed list), or defaults to a platform-appropriate list if user does
not provide an explicit list; made the default list of architectures
for MAB (fat binary) more comprehensive; now uses -fno-common even
when building the interpreter (in addition to using it for
extensions), thus allowing the interpreter to be embedded into a
plugin module of an external project (in addition to allowing
embedding directly into an application); added checks for
<netinet/in_systm.h> (needed by `socket' extension) and getcwd(); now
ensures that -I/usr/local/include is employed when extensions'
extconf.rb scripts invoke have_header() since extension checks on
NextStep and OpenStep will fail without it if the desired resource
resides in the /usr/local tree; fixed formatting of --help message.
* Makefile.in: $(LIBRUBY_A) rule now deletes the archive before
invoking $(AR) since `ar' on Apple/NeXT can not "update" MAB archives
(see configure's --enable-fat-binary option); added rule for new
missing/getcwd.c.
* defines.h: fixed endian handling during MAB build (see configure's
--enable-fat-binary option) to ensure that all portions of the
project see the correct WORDS_BIGENDIAN value (some extension modules
were getting the wrong endian setting); added missing constants
GETPGRP_VOID, WNOHANG, WUNTRACED, X_OK, and type pid_t for NextStep
and OpenStep; removed unnecessary and problematic HAVE_SYS_WAIT_H
define in NeXT section.
* dir.c: do not allow NAMLEN() macro to trust dirent::d_namlen on
NextStep since, on some installations, this value always resolves
uselessly to zero.
* dln.c: added error reporting to NextStep extension loader since the
previous behavior of failing silently was not useful; now ensures
that NSLINKMODULE_OPTION_BINDNOW compatibility constant is defined
for OpenStep and Rhapsody; no longer includes <mach-o/dyld.h> twice
on Rhapsody since this header lacks multiple-include protection,
which resulted in "redefinition" compilation errors.
* main.c: also create hard reference to objc_msgSend() on NeXT
platforms (in addition to Apple platforms).
* lib/mkmf.rb: now exports XCFLAGS from configure script to extension
makefiles so that extensions can be built MAB (see configure's
--enable-fat-binary option); also utilize XCFLAGS in cc_command()
(but not cpp_command() because MAB flags are incompatible with
direct invocation of `cpp').
* ext/curses/extconf.rb: now additionally checks for presence of these
curses functions which are not present on NextStep or Openstep:
bkgd(), bkgdset(), color(), curs(), getbkgd(), init(), scrl(), set(),
setscrreg(), wattroff(), wattron(), wattrset(), wbkgd(), wbkgdset(),
wscrl(), wsetscrreg()
* ext/curses/curses.c: added appropriate #ifdef's for additional set of
curses functions now checked by extconf.rb; fixed curses_bkgd() and
window_bkgd() to correctly return boolean result rather than numeric
result; fixed window_getbkgd() to correctly signal an error by
returning nil rather than -1.
* ext/etc/etc.c: setup_passwd() and setup_group() now check for null
pointers before invoking rb_tainted_str_new2() upon fields extracted
from `struct passwd' and `struct group' since null pointers in some
fields are common on NextStep/OpenStep (especially so for the
`pw_comment' field) and rb_tainted_str_new2() throws an exception
when it receives a null pointer.
* ext/pty/pty.c: include "util.h" for strdup()/ruby_strdup() for
platforms such as NextStep and OpenStep which lack strdup().
* ext/socket/getaddrinfo.c: cast first argument of getservbyname(),
gethostbyaddr(), and gethostbyname() from (const char*) to non-const
(char*) for older platforms such as NextStep and OpenStep.
* ext/socket/socket.c: include "util.h" for strdup()/ruby_strdup() for
platforms such as NextStep and OpenStep which lack strdup(); include
<netinet/in_systm.h> if present for NextStep and OpenStep; cast first
argument of gethostbyaddr() and getservbyname() from (const char*) to
non-const (char*) for older platforms.
* ext/syslog/syslog.c: include "util.h" for strdup()/ruby_strdup() for
platforms such as NextStep and OpenStep which lack strdup().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5002 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-11-22 07:00:03 +03:00
|
|
|
w_extended(klass, arg, check);
|
2002-09-05 13:42:56 +04:00
|
|
|
w_byte(type, arg);
|
2009-08-10 01:55:55 +04:00
|
|
|
path = class2path(rb_class_real(klass));
|
2002-08-27 12:31:08 +04:00
|
|
|
w_unique(path, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-22 04:17:52 +03:00
|
|
|
w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
|
2002-08-27 12:31:08 +04:00
|
|
|
{
|
|
|
|
VALUE klass = CLASS_OF(obj);
|
|
|
|
|
2009-07-18 12:05:32 +04:00
|
|
|
w_extended(klass, arg, TRUE);
|
2003-08-07 01:50:06 +04:00
|
|
|
klass = rb_class_real(klass);
|
2007-11-22 04:17:52 +03:00
|
|
|
if (klass != super) {
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_UCLASS, arg);
|
2009-08-10 01:55:55 +04:00
|
|
|
w_unique(class2path(klass), arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:53:44 +03:00
|
|
|
static bool
|
|
|
|
rb_hash_ruby2_keywords_p(VALUE obj)
|
|
|
|
{
|
|
|
|
return (RHASH(obj)->basic.flags & RHASH_PASS_AS_KEYWORDS) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_hash_ruby2_keywords(VALUE obj)
|
|
|
|
{
|
|
|
|
RHASH(obj)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:16:27 +03:00
|
|
|
static inline bool
|
|
|
|
to_be_skipped_id(const ID id)
|
|
|
|
{
|
|
|
|
if (id == s_encoding_short) return true;
|
|
|
|
if (id == s_ruby2_keywords_flag) return true;
|
|
|
|
if (id == rb_id_encoding()) return true;
|
|
|
|
return !rb_id2str(id);
|
|
|
|
}
|
2014-02-05 12:32:11 +04:00
|
|
|
|
2019-07-01 09:02:27 +03:00
|
|
|
struct w_ivar_arg {
|
|
|
|
struct dump_call_arg *dump;
|
|
|
|
st_data_t num_ivar;
|
|
|
|
};
|
|
|
|
|
2003-10-04 21:51:11 +04:00
|
|
|
static int
|
2012-03-31 02:40:54 +04:00
|
|
|
w_obj_each(st_data_t key, st_data_t val, st_data_t a)
|
2003-10-04 21:51:11 +04:00
|
|
|
{
|
2012-03-31 02:40:54 +04:00
|
|
|
ID id = (ID)key;
|
|
|
|
VALUE value = (VALUE)val;
|
2019-07-01 09:02:27 +03:00
|
|
|
struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
|
|
|
|
struct dump_call_arg *arg = ivarg->dump;
|
2012-03-31 02:40:54 +04:00
|
|
|
|
2019-08-10 07:18:41 +03:00
|
|
|
if (to_be_skipped_id(id)) {
|
|
|
|
if (id == s_encoding_short) {
|
2020-01-11 10:32:56 +03:00
|
|
|
rb_warn("instance variable `"name_s_encoding_short"' on class %"PRIsVALUE" is not dumped",
|
2019-08-10 07:18:41 +03:00
|
|
|
CLASS_OF(arg->obj));
|
|
|
|
}
|
2020-01-11 09:04:44 +03:00
|
|
|
if (id == s_ruby2_keywords_flag) {
|
|
|
|
rb_warn("instance variable `"name_s_ruby2_keywords_flag"' on class %"PRIsVALUE" is not dumped",
|
|
|
|
CLASS_OF(arg->obj));
|
|
|
|
}
|
2019-08-10 07:18:41 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
2019-07-01 09:02:27 +03:00
|
|
|
--ivarg->num_ivar;
|
2014-07-28 12:15:42 +04:00
|
|
|
w_symbol(ID2SYM(id), arg->arg);
|
2003-10-09 21:45:53 +04:00
|
|
|
w_object(value, arg->arg, arg->limit);
|
2003-10-04 21:51:11 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2014-02-05 12:32:11 +04:00
|
|
|
static int
|
|
|
|
obj_count_ivars(st_data_t key, st_data_t val, st_data_t a)
|
|
|
|
{
|
|
|
|
ID id = (ID)key;
|
2021-09-22 18:04:37 +03:00
|
|
|
if (!to_be_skipped_id(id) && UNLIKELY(!++*(st_index_t *)a)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "too many instance variables");
|
|
|
|
}
|
2014-02-05 12:32:11 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
encoding_name(VALUE obj, struct dump_arg *arg)
|
2000-01-05 07:41:21 +03:00
|
|
|
{
|
2018-06-28 11:35:48 +03:00
|
|
|
if (rb_enc_capable(obj)) {
|
|
|
|
int encidx = rb_enc_get_index(obj);
|
|
|
|
rb_encoding *enc = 0;
|
|
|
|
st_data_t name;
|
2007-10-19 15:08:16 +04:00
|
|
|
|
2018-06-28 11:35:48 +03:00
|
|
|
if (encidx <= 0 || !(enc = rb_enc_from_index(encidx))) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2009-05-27 20:31:51 +04:00
|
|
|
|
2018-06-28 11:35:48 +03:00
|
|
|
/* special treatment for US-ASCII and UTF-8 */
|
|
|
|
if (encidx == rb_usascii_encindex()) {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
else if (encidx == rb_utf8_encindex()) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
2014-02-05 12:32:11 +04:00
|
|
|
|
2018-06-28 11:35:48 +03:00
|
|
|
if (arg->encodings ?
|
|
|
|
!st_lookup(arg->encodings, (st_data_t)rb_enc_name(enc), &name) :
|
|
|
|
(arg->encodings = st_init_strcasetable(), 1)) {
|
|
|
|
name = (st_data_t)rb_str_new_cstr(rb_enc_name(enc));
|
|
|
|
st_insert(arg->encodings, (st_data_t)rb_enc_name(enc), name);
|
|
|
|
}
|
|
|
|
return (VALUE)name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
2014-02-05 12:32:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:20:03 +03:00
|
|
|
static int
|
2014-02-05 12:32:11 +04:00
|
|
|
w_encoding(VALUE encname, struct dump_call_arg *arg)
|
|
|
|
{
|
2016-11-24 13:59:35 +03:00
|
|
|
int limit = arg->limit;
|
|
|
|
if (limit >= 0) ++limit;
|
2014-02-05 12:32:11 +04:00
|
|
|
switch (encname) {
|
|
|
|
case Qfalse:
|
|
|
|
case Qtrue:
|
2019-08-10 05:43:07 +03:00
|
|
|
w_symbol(ID2SYM(s_encoding_short), arg->arg);
|
2016-11-24 13:59:35 +03:00
|
|
|
w_object(encname, arg->arg, limit);
|
2019-07-01 10:20:03 +03:00
|
|
|
return 1;
|
2014-02-05 12:32:11 +04:00
|
|
|
case Qnil:
|
2019-07-01 10:25:56 +03:00
|
|
|
return 0;
|
2009-05-27 20:31:51 +04:00
|
|
|
}
|
2014-07-28 12:15:42 +04:00
|
|
|
w_symbol(ID2SYM(rb_id_encoding()), arg->arg);
|
2016-11-24 13:59:35 +03:00
|
|
|
w_object(encname, arg->arg, limit);
|
2019-07-01 10:20:03 +03:00
|
|
|
return 1;
|
2007-10-19 15:08:16 +04:00
|
|
|
}
|
|
|
|
|
2014-02-05 12:32:11 +04:00
|
|
|
static st_index_t
|
2015-05-30 03:20:15 +03:00
|
|
|
has_ivars(VALUE obj, VALUE encname, VALUE *ivobj)
|
2007-10-19 15:08:16 +04:00
|
|
|
{
|
2021-09-22 18:04:37 +03:00
|
|
|
st_index_t num = !NIL_P(encname);
|
2015-05-30 03:20:15 +03:00
|
|
|
|
|
|
|
if (SPECIAL_CONST_P(obj)) goto generic;
|
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
|
|
|
case T_OBJECT:
|
|
|
|
case T_CLASS:
|
|
|
|
case T_MODULE:
|
|
|
|
break; /* counted elsewhere */
|
2020-01-11 09:04:44 +03:00
|
|
|
case T_HASH:
|
2021-09-22 18:04:37 +03:00
|
|
|
if (rb_hash_ruby2_keywords_p(obj)) ++num;
|
2020-01-11 09:04:44 +03:00
|
|
|
/* fall through */
|
2015-05-30 03:20:15 +03:00
|
|
|
default:
|
|
|
|
generic:
|
|
|
|
rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
|
2021-09-22 18:04:37 +03:00
|
|
|
if (num) *ivobj = obj;
|
2015-05-30 03:20:15 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:04:37 +03:00
|
|
|
return num;
|
2014-02-05 12:32:11 +04:00
|
|
|
}
|
|
|
|
|
2019-07-01 10:18:51 +03:00
|
|
|
static void
|
|
|
|
w_ivar_each(VALUE obj, st_index_t num, struct dump_call_arg *arg)
|
|
|
|
{
|
2022-10-03 18:14:32 +03:00
|
|
|
shape_id_t shape_id = rb_shape_get_shape_id(arg->obj);
|
2019-07-01 10:18:51 +03:00
|
|
|
struct w_ivar_arg ivarg = {arg, num};
|
|
|
|
if (!num) return;
|
|
|
|
rb_ivar_foreach(obj, w_obj_each, (st_data_t)&ivarg);
|
2022-12-06 03:48:47 +03:00
|
|
|
|
2022-10-03 18:14:32 +03:00
|
|
|
if (shape_id != rb_shape_get_shape_id(arg->obj)) {
|
2022-12-06 03:48:47 +03:00
|
|
|
rb_shape_t * expected_shape = rb_shape_get_shape_by_id(shape_id);
|
|
|
|
rb_shape_t * actual_shape = rb_shape_get_shape(arg->obj);
|
|
|
|
|
|
|
|
// If the shape tree got _shorter_ then we probably removed an IV
|
|
|
|
// If the shape tree got longer, then we probably added an IV.
|
|
|
|
// The exception message might not be accurate when someone adds and
|
|
|
|
// removes the same number of IVs, but they will still get an exception
|
|
|
|
if (rb_shape_depth(expected_shape) > rb_shape_depth(actual_shape)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "instance variable removed from %"PRIsVALUE" instance",
|
|
|
|
CLASS_OF(arg->obj));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance",
|
|
|
|
CLASS_OF(arg->obj));
|
|
|
|
}
|
2022-10-03 18:14:32 +03:00
|
|
|
}
|
2019-07-01 10:18:51 +03:00
|
|
|
}
|
|
|
|
|
2014-02-05 12:32:11 +04:00
|
|
|
static void
|
2015-05-30 03:20:15 +03:00
|
|
|
w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg)
|
2014-02-05 12:32:11 +04:00
|
|
|
{
|
|
|
|
w_long(num, arg->arg);
|
2019-07-01 10:20:03 +03:00
|
|
|
num -= w_encoding(encname, arg);
|
2021-09-22 18:53:44 +03:00
|
|
|
if (RB_TYPE_P(ivobj, T_HASH) && rb_hash_ruby2_keywords_p(ivobj)) {
|
2020-01-11 09:04:44 +03:00
|
|
|
int limit = arg->limit;
|
|
|
|
if (limit >= 0) ++limit;
|
|
|
|
w_symbol(ID2SYM(s_ruby2_keywords_flag), arg->arg);
|
|
|
|
w_object(Qtrue, arg->arg, limit);
|
|
|
|
num--;
|
|
|
|
}
|
2022-11-15 07:24:08 +03:00
|
|
|
if (!UNDEF_P(ivobj) && num) {
|
2019-07-01 10:18:51 +03:00
|
|
|
w_ivar_each(ivobj, num, arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 10:21:46 +04:00
|
|
|
static void
|
|
|
|
w_objivar(VALUE obj, struct dump_call_arg *arg)
|
|
|
|
{
|
2015-12-08 08:20:41 +03:00
|
|
|
st_data_t num = 0;
|
2007-09-28 10:21:46 +04:00
|
|
|
|
2015-12-08 08:20:41 +03:00
|
|
|
rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
|
2014-02-05 12:32:11 +04:00
|
|
|
w_long(num, arg->arg);
|
2019-07-01 10:18:51 +03:00
|
|
|
w_ivar_each(obj, num, arg);
|
2007-09-28 10:21:46 +04:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:11:12 +03:00
|
|
|
#if SIZEOF_LONG > 4
|
2022-08-16 02:14:12 +03:00
|
|
|
// Optimized dump for fixnum larger than 31-bits
|
|
|
|
static void
|
|
|
|
w_bigfixnum(VALUE obj, struct dump_arg *arg)
|
|
|
|
{
|
|
|
|
RUBY_ASSERT(FIXNUM_P(obj));
|
|
|
|
|
|
|
|
w_byte(TYPE_BIGNUM, arg);
|
|
|
|
|
|
|
|
#if SIZEOF_LONG == SIZEOF_VALUE
|
|
|
|
long num, slen_num;
|
|
|
|
num = FIX2LONG(obj);
|
|
|
|
#else
|
|
|
|
long long num, slen_num;
|
|
|
|
num = NUM2LL(obj);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char sign = num < 0 ? '-' : '+';
|
|
|
|
w_byte(sign, arg);
|
|
|
|
|
|
|
|
// Guaranteed not to overflow, as FIXNUM is 1-bit less than long
|
|
|
|
if (num < 0) num = -num;
|
|
|
|
|
|
|
|
// calculate the size in shorts
|
|
|
|
int slen = 0;
|
|
|
|
{
|
|
|
|
slen_num = num;
|
|
|
|
while (slen_num) {
|
|
|
|
slen++;
|
|
|
|
slen_num = SHORTDN(slen_num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RUBY_ASSERT(slen > 0 && slen <= SIZEOF_LONG / 2);
|
|
|
|
|
|
|
|
w_long((long)slen, arg);
|
|
|
|
|
|
|
|
for (int i = 0; i < slen; i++) {
|
|
|
|
w_short(num & SHORTMASK, arg);
|
|
|
|
num = SHORTDN(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We aren't adding this object to the link table, but we need to increment
|
|
|
|
// the index.
|
|
|
|
arg->num_entries++;
|
|
|
|
|
|
|
|
RUBY_ASSERT(num == 0);
|
|
|
|
}
|
2022-09-02 16:11:12 +03:00
|
|
|
#endif
|
2022-08-16 02:14:12 +03:00
|
|
|
|
|
|
|
static void
|
|
|
|
w_remember(VALUE obj, struct dump_arg *arg)
|
|
|
|
{
|
|
|
|
st_add_direct(arg->data, obj, arg->num_entries++);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
w_object(VALUE obj, struct dump_arg *arg, int limit)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
struct dump_call_arg c_arg;
|
2015-05-30 03:20:15 +03:00
|
|
|
VALUE ivobj = Qundef;
|
* gc.c (Init_stack): stack region is far smaller than usual if
pthread is used.
* marshal.c (w_extended): singleton methods should not be checked
when dumping via marshal_dump() or _dump(). [ruby-talk:85909]
* file.c (getcwdofdrv): avoid using getcwd() directly, use
my_getcwd() instead.
* merged NeXT, OpenStep, Rhapsody ports patch from Eric Sunshine
<sunshine@sunshineco.com>. [ruby-core:01596]
* marshal.c (w_object): LINK check earlier than anything else,
i.e. do not dump TYPE_IVAR for already dumped objects.
(ruby-bugs PR#1220)
* eval.c (rb_eval): call "inherited" only when a new class is
generated; not on reopening.
* eval.c (eval): prepend error position in evaluating string to
* configure.in: revived NextStep, OpenStep, and Rhapsody ports which
had become unbuildable; enhanced --enable-fat-binary option so that
it accepts a list of desired architectures (rather than assuming a
fixed list), or defaults to a platform-appropriate list if user does
not provide an explicit list; made the default list of architectures
for MAB (fat binary) more comprehensive; now uses -fno-common even
when building the interpreter (in addition to using it for
extensions), thus allowing the interpreter to be embedded into a
plugin module of an external project (in addition to allowing
embedding directly into an application); added checks for
<netinet/in_systm.h> (needed by `socket' extension) and getcwd(); now
ensures that -I/usr/local/include is employed when extensions'
extconf.rb scripts invoke have_header() since extension checks on
NextStep and OpenStep will fail without it if the desired resource
resides in the /usr/local tree; fixed formatting of --help message.
* Makefile.in: $(LIBRUBY_A) rule now deletes the archive before
invoking $(AR) since `ar' on Apple/NeXT can not "update" MAB archives
(see configure's --enable-fat-binary option); added rule for new
missing/getcwd.c.
* defines.h: fixed endian handling during MAB build (see configure's
--enable-fat-binary option) to ensure that all portions of the
project see the correct WORDS_BIGENDIAN value (some extension modules
were getting the wrong endian setting); added missing constants
GETPGRP_VOID, WNOHANG, WUNTRACED, X_OK, and type pid_t for NextStep
and OpenStep; removed unnecessary and problematic HAVE_SYS_WAIT_H
define in NeXT section.
* dir.c: do not allow NAMLEN() macro to trust dirent::d_namlen on
NextStep since, on some installations, this value always resolves
uselessly to zero.
* dln.c: added error reporting to NextStep extension loader since the
previous behavior of failing silently was not useful; now ensures
that NSLINKMODULE_OPTION_BINDNOW compatibility constant is defined
for OpenStep and Rhapsody; no longer includes <mach-o/dyld.h> twice
on Rhapsody since this header lacks multiple-include protection,
which resulted in "redefinition" compilation errors.
* main.c: also create hard reference to objc_msgSend() on NeXT
platforms (in addition to Apple platforms).
* lib/mkmf.rb: now exports XCFLAGS from configure script to extension
makefiles so that extensions can be built MAB (see configure's
--enable-fat-binary option); also utilize XCFLAGS in cc_command()
(but not cpp_command() because MAB flags are incompatible with
direct invocation of `cpp').
* ext/curses/extconf.rb: now additionally checks for presence of these
curses functions which are not present on NextStep or Openstep:
bkgd(), bkgdset(), color(), curs(), getbkgd(), init(), scrl(), set(),
setscrreg(), wattroff(), wattron(), wattrset(), wbkgd(), wbkgdset(),
wscrl(), wsetscrreg()
* ext/curses/curses.c: added appropriate #ifdef's for additional set of
curses functions now checked by extconf.rb; fixed curses_bkgd() and
window_bkgd() to correctly return boolean result rather than numeric
result; fixed window_getbkgd() to correctly signal an error by
returning nil rather than -1.
* ext/etc/etc.c: setup_passwd() and setup_group() now check for null
pointers before invoking rb_tainted_str_new2() upon fields extracted
from `struct passwd' and `struct group' since null pointers in some
fields are common on NextStep/OpenStep (especially so for the
`pw_comment' field) and rb_tainted_str_new2() throws an exception
when it receives a null pointer.
* ext/pty/pty.c: include "util.h" for strdup()/ruby_strdup() for
platforms such as NextStep and OpenStep which lack strdup().
* ext/socket/getaddrinfo.c: cast first argument of getservbyname(),
gethostbyaddr(), and gethostbyname() from (const char*) to non-const
(char*) for older platforms such as NextStep and OpenStep.
* ext/socket/socket.c: include "util.h" for strdup()/ruby_strdup() for
platforms such as NextStep and OpenStep which lack strdup(); include
<netinet/in_systm.h> if present for NextStep and OpenStep; cast first
argument of gethostbyaddr() and getservbyname() from (const char*) to
non-const (char*) for older platforms.
* ext/syslog/syslog.c: include "util.h" for strdup()/ruby_strdup() for
platforms such as NextStep and OpenStep which lack strdup().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5002 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-11-22 07:00:03 +03:00
|
|
|
st_data_t num;
|
2014-02-05 12:32:11 +04:00
|
|
|
st_index_t hasiv = 0;
|
|
|
|
VALUE encname = Qnil;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
if (limit == 0) {
|
2000-02-01 06:12:21 +03:00
|
|
|
rb_raise(rb_eArgError, "exceed depth limit");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2003-08-07 01:50:06 +04:00
|
|
|
|
2021-10-03 16:34:45 +03:00
|
|
|
if (NIL_P(obj)) {
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_NIL, arg);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (obj == Qtrue) {
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_TRUE, arg);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (obj == Qfalse) {
|
1998-01-16 15:19:09 +03:00
|
|
|
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
|
2001-08-23 10:02:15 +04:00
|
|
|
if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_FIXNUM, arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
w_long(FIX2LONG(obj), arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2022-08-16 02:14:12 +03:00
|
|
|
w_bigfixnum(obj, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2000-03-07 11:37:59 +03:00
|
|
|
else if (SYMBOL_P(obj)) {
|
2014-07-28 12:15:42 +04:00
|
|
|
w_symbol(obj, arg);
|
2000-03-07 11:37:59 +03:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
else {
|
2022-08-16 02:14:12 +03:00
|
|
|
if (st_lookup(arg->data, obj, &num)) {
|
|
|
|
w_byte(TYPE_LINK, arg);
|
|
|
|
w_long((long)num, arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit > 0) limit--;
|
|
|
|
c_arg.limit = limit;
|
|
|
|
c_arg.arg = arg;
|
|
|
|
c_arg.obj = obj;
|
|
|
|
|
|
|
|
if (FLONUM_P(obj)) {
|
|
|
|
w_remember(obj, arg);
|
|
|
|
w_byte(TYPE_FLOAT, arg);
|
|
|
|
w_float(RFLOAT_VALUE(obj), arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-04 11:23:25 +04:00
|
|
|
VALUE v;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2014-03-26 06:20:52 +04:00
|
|
|
if (!RBASIC_CLASS(obj)) {
|
2003-07-31 12:42:44 +04:00
|
|
|
rb_raise(rb_eTypeError, "can't dump internal %s",
|
2015-05-30 03:20:15 +03:00
|
|
|
rb_builtin_type_name(BUILTIN_TYPE(obj)));
|
2022-07-21 19:23:58 +03:00
|
|
|
}
|
|
|
|
|
2015-05-30 03:20:15 +03:00
|
|
|
if (rb_obj_respond_to(obj, s_mdump, TRUE)) {
|
2022-08-16 02:14:12 +03:00
|
|
|
w_remember(obj, arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2015-09-25 15:45:10 +03:00
|
|
|
v = dump_funcall(arg, obj, s_mdump, 0, 0);
|
2009-07-18 12:05:32 +04:00
|
|
|
w_class(TYPE_USRMARSHAL, obj, arg, FALSE);
|
2003-10-09 21:45:53 +04:00
|
|
|
w_object(v, arg, limit);
|
1998-01-16 15:19:09 +03:00
|
|
|
return;
|
2022-07-21 19:23:58 +03:00
|
|
|
}
|
2013-01-20 18:55:42 +04:00
|
|
|
if (rb_obj_respond_to(obj, s_dump, TRUE)) {
|
2015-05-30 03:20:15 +03:00
|
|
|
VALUE ivobj2 = Qundef;
|
2014-02-05 12:32:11 +04:00
|
|
|
st_index_t hasiv2;
|
|
|
|
VALUE encname2;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-01-20 18:55:42 +04:00
|
|
|
v = INT2NUM(limit);
|
2015-09-25 15:45:10 +03:00
|
|
|
v = dump_funcall(arg, obj, s_dump, 1, &v);
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(v, T_STRING)) {
|
2001-05-11 09:24:59 +04:00
|
|
|
rb_raise(rb_eTypeError, "_dump() must return string");
|
2022-07-21 19:23:58 +03:00
|
|
|
}
|
2015-05-30 03:20:15 +03:00
|
|
|
hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
|
|
|
|
hasiv2 = has_ivars(v, (encname2 = encoding_name(v, arg)), &ivobj2);
|
2014-02-05 12:32:11 +04:00
|
|
|
if (hasiv2) {
|
|
|
|
hasiv = hasiv2;
|
2015-05-30 03:20:15 +03:00
|
|
|
ivobj = ivobj2;
|
2014-02-05 12:32:11 +04:00
|
|
|
encname = encname2;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2014-02-05 12:32:11 +04:00
|
|
|
if (hasiv) w_byte(TYPE_IVAR, arg);
|
2009-07-18 12:05:32 +04:00
|
|
|
w_class(TYPE_USERDEF, obj, arg, FALSE);
|
2006-08-31 14:47:44 +04:00
|
|
|
w_bytes(RSTRING_PTR(v), RSTRING_LEN(v), arg);
|
2014-02-05 12:32:11 +04:00
|
|
|
if (hasiv) {
|
2015-05-30 03:20:15 +03:00
|
|
|
w_ivar(hasiv, ivobj, encname, &c_arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
}
|
2022-08-16 02:14:12 +03:00
|
|
|
w_remember(obj, arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
return;
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2022-08-16 02:14:12 +03:00
|
|
|
w_remember(obj, arg);
|
2008-04-10 12:41:46 +04:00
|
|
|
|
2015-05-30 03:20:15 +03:00
|
|
|
hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
|
2008-04-10 12:41:46 +04:00
|
|
|
{
|
|
|
|
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);
|
2014-10-02 00:29:46 +04:00
|
|
|
if (!arg->compat_tbl) {
|
2015-01-23 14:01:02 +03:00
|
|
|
arg->compat_tbl = rb_init_identtable();
|
2014-10-02 00:29:46 +04:00
|
|
|
}
|
2008-04-10 12:41:46 +04:00
|
|
|
st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
|
2022-11-15 07:24:08 +03:00
|
|
|
if (obj != real_obj && UNDEF_P(ivobj)) hasiv = 0;
|
2008-04-10 12:41:46 +04:00
|
|
|
}
|
|
|
|
}
|
2010-07-01 03:40:37 +04:00
|
|
|
if (hasiv) w_byte(TYPE_IVAR, arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
|
|
|
case T_CLASS:
|
2001-05-11 09:24:59 +04:00
|
|
|
if (FL_TEST(obj, FL_SINGLETON)) {
|
|
|
|
rb_raise(rb_eTypeError, "singleton class can't be dumped");
|
|
|
|
}
|
1999-12-01 12:24:48 +03:00
|
|
|
w_byte(TYPE_CLASS, arg);
|
|
|
|
{
|
2012-12-04 11:23:20 +04:00
|
|
|
VALUE path = class2path(obj);
|
2006-08-31 14:47:44 +04:00
|
|
|
w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
|
2012-12-04 11:23:20 +04:00
|
|
|
RB_GC_GUARD(path);
|
1999-12-01 12:24:48 +03:00
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-12-01 12:24:48 +03:00
|
|
|
case T_MODULE:
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_MODULE, arg);
|
|
|
|
{
|
2004-01-16 08:33:39 +03:00
|
|
|
VALUE path = class2path(obj);
|
2006-08-31 14:47:44 +04:00
|
|
|
w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
|
2012-12-04 11:23:20 +04:00
|
|
|
RB_GC_GUARD(path);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_FLOAT:
|
|
|
|
w_byte(TYPE_FLOAT, arg);
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
w_float(RFLOAT_VALUE(obj), arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_BIGNUM:
|
|
|
|
w_byte(TYPE_BIGNUM, arg);
|
|
|
|
{
|
2014-02-16 01:17:34 +04:00
|
|
|
char sign = BIGNUM_SIGN(obj) ? '+' : '-';
|
2014-04-19 05:11:04 +04:00
|
|
|
size_t len = BIGNUM_LEN(obj);
|
|
|
|
size_t slen;
|
2018-11-15 10:34:01 +03:00
|
|
|
size_t j;
|
2014-02-16 01:17:34 +04:00
|
|
|
BDIGIT *d = BIGNUM_DIGITS(obj);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2014-04-19 05:11:04 +04:00
|
|
|
slen = SHORTLEN(len);
|
|
|
|
if (LONG_MAX < slen) {
|
|
|
|
rb_raise(rb_eTypeError, "too big Bignum can't be dumped");
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(sign, arg);
|
2014-04-19 05:11:04 +04:00
|
|
|
w_long((long)slen, arg);
|
2018-11-15 10:34:01 +03:00
|
|
|
for (j = 0; j < len; j++) {
|
2014-04-13 07:48:17 +04:00
|
|
|
#if SIZEOF_BDIGIT > SIZEOF_SHORT
|
2000-10-31 11:37:47 +03:00
|
|
|
BDIGIT num = *d;
|
|
|
|
int i;
|
|
|
|
|
2014-04-13 07:48:17 +04:00
|
|
|
for (i=0; i<SIZEOF_BDIGIT; i+=SIZEOF_SHORT) {
|
2000-10-31 11:37:47 +03:00
|
|
|
w_short(num & SHORTMASK, arg);
|
|
|
|
num = SHORTDN(num);
|
2018-11-15 10:34:01 +03:00
|
|
|
if (j == len - 1 && num == 0) break;
|
2000-10-31 11:37:47 +03:00
|
|
|
}
|
|
|
|
#else
|
1998-01-16 15:19:09 +03:00
|
|
|
w_short(*d, arg);
|
2000-10-31 11:37:47 +03:00
|
|
|
#endif
|
1998-01-16 15:19:09 +03:00
|
|
|
d++;
|
|
|
|
}
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_STRING:
|
1999-01-20 07:59:39 +03:00
|
|
|
w_uclass(obj, rb_cString, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_STRING, arg);
|
2006-08-31 14:47:44 +04:00
|
|
|
w_bytes(RSTRING_PTR(obj), RSTRING_LEN(obj), arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_REGEXP:
|
2008-06-28 16:25:45 +04:00
|
|
|
w_uclass(obj, rb_cRegexp, arg);
|
|
|
|
w_byte(TYPE_REGEXP, arg);
|
|
|
|
{
|
|
|
|
int opts = rb_reg_options(obj);
|
|
|
|
w_bytes(RREGEXP_SRC_PTR(obj), RREGEXP_SRC_LEN(obj), arg);
|
|
|
|
w_byte((char)opts, arg);
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_ARRAY:
|
1999-01-20 07:59:39 +03:00
|
|
|
w_uclass(obj, rb_cArray, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_ARRAY, arg);
|
|
|
|
{
|
2008-06-02 19:29:11 +04:00
|
|
|
long i, len = RARRAY_LEN(obj);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
w_long(len, arg);
|
2008-06-02 19:29:11 +04:00
|
|
|
for (i=0; i<RARRAY_LEN(obj); i++) {
|
2013-05-13 13:56:22 +04:00
|
|
|
w_object(RARRAY_AREF(obj, i), arg, limit);
|
2008-06-02 19:29:11 +04:00
|
|
|
if (len != RARRAY_LEN(obj)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "array modified during dump");
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_HASH:
|
1999-01-20 07:59:39 +03:00
|
|
|
w_uclass(obj, rb_cHash, arg);
|
2021-09-22 19:36:27 +03:00
|
|
|
if (rb_hash_compare_by_id_p(obj)) {
|
|
|
|
w_byte(TYPE_UCLASS, arg);
|
|
|
|
w_symbol(rb_sym_intern_ascii_cstr("Hash"), arg);
|
|
|
|
}
|
2009-11-11 03:36:04 +03:00
|
|
|
if (NIL_P(RHASH_IFNONE(obj))) {
|
2002-08-29 13:08:18 +04:00
|
|
|
w_byte(TYPE_HASH, arg);
|
|
|
|
}
|
2019-07-31 04:22:47 +03:00
|
|
|
else if (FL_TEST(obj, RHASH_PROC_DEFAULT)) {
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
rb_raise(rb_eTypeError, "can't dump hash with default proc");
|
1999-12-01 12:24:48 +03:00
|
|
|
}
|
|
|
|
else {
|
2002-08-29 13:08:18 +04:00
|
|
|
w_byte(TYPE_HASH_DEF, arg);
|
1999-12-01 12:24:48 +03:00
|
|
|
}
|
2018-10-31 01:12:12 +03:00
|
|
|
w_long(rb_hash_size_num(obj), arg);
|
2004-09-29 09:15:33 +04:00
|
|
|
rb_hash_foreach(obj, hash_each, (st_data_t)&c_arg);
|
2009-11-11 03:36:04 +03:00
|
|
|
if (!NIL_P(RHASH_IFNONE(obj))) {
|
|
|
|
w_object(RHASH_IFNONE(obj), arg, limit);
|
1999-12-01 12:24:48 +03:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_STRUCT:
|
2009-07-18 12:05:32 +04:00
|
|
|
w_class(TYPE_STRUCT, obj, arg, TRUE);
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2006-02-05 17:40:01 +03:00
|
|
|
long len = RSTRUCT_LEN(obj);
|
1998-01-16 15:19:09 +03:00
|
|
|
VALUE mem;
|
2000-11-20 10:31:55 +03:00
|
|
|
long i;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
w_long(len, arg);
|
2004-09-27 08:46:54 +04:00
|
|
|
mem = rb_struct_members(obj);
|
1998-01-16 15:19:09 +03:00
|
|
|
for (i=0; i<len; i++) {
|
2014-07-28 12:15:42 +04:00
|
|
|
w_symbol(RARRAY_AREF(mem, i), arg);
|
2013-06-21 15:22:18 +04:00
|
|
|
w_object(RSTRUCT_GET(obj, i), arg, limit);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case T_OBJECT:
|
2009-07-18 12:05:32 +04:00
|
|
|
w_class(TYPE_OBJECT, obj, arg, TRUE);
|
2007-09-28 10:21:46 +04:00
|
|
|
w_objivar(obj, &c_arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2003-04-09 09:08:25 +04:00
|
|
|
case T_DATA:
|
|
|
|
{
|
|
|
|
VALUE v;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-01-20 18:55:42 +04:00
|
|
|
if (!rb_obj_respond_to(obj, s_dump_data, TRUE)) {
|
2003-04-09 09:08:25 +04:00
|
|
|
rb_raise(rb_eTypeError,
|
2015-04-16 15:46:07 +03:00
|
|
|
"no _dump_data is defined for class %"PRIsVALUE,
|
|
|
|
rb_obj_class(obj));
|
2003-04-09 09:08:25 +04:00
|
|
|
}
|
2015-09-25 15:45:10 +03:00
|
|
|
v = dump_funcall(arg, obj, s_dump_data, 0, 0);
|
2009-07-18 12:05:32 +04:00
|
|
|
w_class(TYPE_DATA, obj, arg, TRUE);
|
2003-10-09 21:45:53 +04:00
|
|
|
w_object(v, arg, limit);
|
2003-04-09 09:08:25 +04:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
default:
|
2015-04-16 14:43:37 +03:00
|
|
|
rb_raise(rb_eTypeError, "can't dump %"PRIsVALUE,
|
|
|
|
rb_obj_class(obj));
|
1998-01-16 15:19:09 +03:00
|
|
|
break;
|
|
|
|
}
|
2012-12-04 11:23:20 +04:00
|
|
|
RB_GC_GUARD(obj);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2007-10-19 15:08:16 +04:00
|
|
|
if (hasiv) {
|
2015-05-30 03:20:15 +03:00
|
|
|
w_ivar(hasiv, ivobj, encname, &c_arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
static void
|
|
|
|
clear_dump_arg(struct dump_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2009-10-04 14:30:56 +04:00
|
|
|
if (!arg->symbols) return;
|
2003-08-07 01:50:06 +04:00
|
|
|
st_free_table(arg->symbols);
|
2009-10-04 14:30:56 +04:00
|
|
|
arg->symbols = 0;
|
1998-01-16 15:19:09 +03:00
|
|
|
st_free_table(arg->data);
|
2009-10-07 11:42:30 +04:00
|
|
|
arg->data = 0;
|
2022-08-16 02:14:12 +03:00
|
|
|
arg->num_entries = 0;
|
2014-10-02 00:29:46 +04:00
|
|
|
if (arg->compat_tbl) {
|
|
|
|
st_free_table(arg->compat_tbl);
|
|
|
|
arg->compat_tbl = 0;
|
|
|
|
}
|
2009-10-04 14:30:56 +04:00
|
|
|
if (arg->encodings) {
|
|
|
|
st_free_table(arg->encodings);
|
|
|
|
arg->encodings = 0;
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2012-06-22 09:22:46 +04:00
|
|
|
NORETURN(static inline void io_needed(void));
|
|
|
|
static inline void
|
|
|
|
io_needed(void)
|
|
|
|
{
|
|
|
|
rb_raise(rb_eTypeError, "instance of IO needed");
|
|
|
|
}
|
|
|
|
|
2003-12-27 19:07:43 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-02-15 10:24:49 +03:00
|
|
|
* dump( obj [, anIO] , limit=-1 ) -> anIO
|
2003-12-27 19:07:43 +03:00
|
|
|
*
|
2009-11-03 20:46:28 +03:00
|
|
|
* Serializes obj and all descendant objects. If anIO is
|
2003-12-27 19:07:43 +03:00
|
|
|
* 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
|
2011-03-07 11:44:45 +03:00
|
|
|
* def say_hello
|
2003-12-27 19:07:43 +03:00
|
|
|
* @str
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* (produces no output)
|
|
|
|
*
|
|
|
|
* o = Klass.new("hello\n")
|
|
|
|
* data = Marshal.dump(o)
|
|
|
|
* obj = Marshal.load(data)
|
2011-03-07 11:44:45 +03:00
|
|
|
* obj.say_hello #=> "hello\n"
|
2009-11-04 16:42:03 +03:00
|
|
|
*
|
|
|
|
* Marshal can't dump following objects:
|
|
|
|
* * anonymous Class/Module.
|
2013-12-09 12:51:17 +04:00
|
|
|
* * objects which are related to system (ex: Dir, File::Stat, IO, File, Socket
|
2010-05-26 06:50:58 +04:00
|
|
|
* and so on)
|
|
|
|
* * an instance of MatchData, Data, Method, UnboundMethod, Proc, Thread,
|
|
|
|
* ThreadGroup, Continuation
|
2013-12-09 12:51:17 +04:00
|
|
|
* * objects which define singleton methods
|
2003-12-27 19:07:43 +03:00
|
|
|
*/
|
2012-06-03 11:34:03 +04:00
|
|
|
static VALUE
|
2019-08-28 11:48:48 +03:00
|
|
|
marshal_dump(int argc, VALUE *argv, VALUE _)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
VALUE obj, port, a1, a2;
|
|
|
|
int limit = -1;
|
|
|
|
|
2003-04-08 09:40:29 +04:00
|
|
|
port = Qnil;
|
1998-01-16 15:19:09 +03:00
|
|
|
rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
|
|
|
|
if (argc == 3) {
|
2000-04-12 09:06:23 +04:00
|
|
|
if (!NIL_P(a2)) limit = NUM2INT(a2);
|
2012-06-22 09:22:46 +04:00
|
|
|
if (NIL_P(a1)) io_needed();
|
1998-01-16 15:19:09 +03:00
|
|
|
port = a1;
|
|
|
|
}
|
|
|
|
else if (argc == 2) {
|
|
|
|
if (FIXNUM_P(a1)) limit = FIX2INT(a1);
|
2012-06-22 09:22:46 +04:00
|
|
|
else if (NIL_P(a1)) io_needed();
|
1998-01-16 15:19:09 +03:00
|
|
|
else port = a1;
|
|
|
|
}
|
2015-09-25 16:14:16 +03:00
|
|
|
return rb_marshal_dump_limited(obj, port, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_marshal_dump_limited(VALUE obj, VALUE port, int limit)
|
|
|
|
{
|
|
|
|
struct dump_arg *arg;
|
2017-02-13 11:14:19 +03:00
|
|
|
VALUE wrapper; /* used to avoid memory leak in case of exception */
|
2015-09-25 16:14:16 +03:00
|
|
|
|
2017-02-15 03:42:51 +03:00
|
|
|
wrapper = TypedData_Make_Struct(0, struct dump_arg, &dump_arg_data, arg);
|
2009-10-04 14:30:56 +04:00
|
|
|
arg->dest = 0;
|
|
|
|
arg->symbols = st_init_numtable();
|
2015-01-23 14:01:02 +03:00
|
|
|
arg->data = rb_init_identtable();
|
2022-08-16 02:14:12 +03:00
|
|
|
arg->num_entries = 0;
|
2014-10-02 00:29:46 +04:00
|
|
|
arg->compat_tbl = 0;
|
2009-10-04 14:30:56 +04:00
|
|
|
arg->encodings = 0;
|
2009-11-21 10:38:51 +03:00
|
|
|
arg->str = rb_str_buf_new(0);
|
2003-04-08 09:40:29 +04:00
|
|
|
if (!NIL_P(port)) {
|
2008-06-18 07:05:33 +04:00
|
|
|
if (!rb_respond_to(port, s_write)) {
|
2012-06-22 09:22:46 +04:00
|
|
|
io_needed();
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2009-10-04 14:30:56 +04:00
|
|
|
arg->dest = port;
|
2015-09-25 15:45:10 +03:00
|
|
|
dump_check_funcall(arg, port, s_binmode, 0, 0);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2009-10-04 14:30:56 +04:00
|
|
|
port = arg->str;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
w_byte(MARSHAL_MAJOR, arg);
|
|
|
|
w_byte(MARSHAL_MINOR, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
w_object(obj, arg, limit);
|
|
|
|
if (arg->dest) {
|
|
|
|
rb_io_write(arg->dest, arg->str);
|
|
|
|
rb_str_resize(arg->str, 0);
|
|
|
|
}
|
2017-02-15 11:38:01 +03:00
|
|
|
clear_dump_arg(arg);
|
|
|
|
RB_GC_GUARD(wrapper);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct load_arg {
|
2004-10-05 05:37:46 +04:00
|
|
|
VALUE src;
|
2012-11-20 19:17:15 +04:00
|
|
|
char *buf;
|
|
|
|
long buflen;
|
|
|
|
long readable;
|
2004-10-05 05:37:46 +04:00
|
|
|
long offset;
|
2003-08-07 01:50:06 +04:00
|
|
|
st_table *symbols;
|
2008-09-02 06:47:38 +04:00
|
|
|
st_table *data;
|
2021-09-30 17:50:31 +03:00
|
|
|
st_table *partial_objects;
|
1998-01-16 15:19:09 +03:00
|
|
|
VALUE proc;
|
2007-09-08 19:07:18 +04:00
|
|
|
st_table *compat_tbl;
|
2021-09-18 16:34:15 +03:00
|
|
|
bool freeze;
|
1998-01-16 15:19:09 +03:00
|
|
|
};
|
|
|
|
|
2015-09-25 15:45:10 +03:00
|
|
|
static VALUE
|
|
|
|
check_load_arg(VALUE ret, struct load_arg *arg, const char *name)
|
2008-05-22 15:20:56 +04:00
|
|
|
{
|
2009-10-04 14:30:56 +04:00
|
|
|
if (!arg->symbols) {
|
2008-10-01 14:11:51 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
|
2014-11-24 06:43:59 +03:00
|
|
|
name);
|
2008-05-22 15:20:56 +04:00
|
|
|
}
|
2015-09-25 15:45:10 +03:00
|
|
|
return ret;
|
2008-05-22 15:20:56 +04:00
|
|
|
}
|
2015-09-25 15:45:10 +03:00
|
|
|
#define load_funcall(arg, obj, sym, argc, argv) \
|
|
|
|
check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
|
2008-05-22 15:20:56 +04:00
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
static void clear_load_arg(struct load_arg *arg);
|
|
|
|
|
2008-09-02 06:47:38 +04:00
|
|
|
static void
|
|
|
|
mark_load_arg(void *ptr)
|
|
|
|
{
|
|
|
|
struct load_arg *p = ptr;
|
2009-10-07 11:42:30 +04:00
|
|
|
if (!p->symbols)
|
2008-09-02 06:47:38 +04:00
|
|
|
return;
|
2014-07-28 12:15:42 +04:00
|
|
|
rb_mark_tbl(p->symbols);
|
2008-09-02 06:47:38 +04:00
|
|
|
rb_mark_tbl(p->data);
|
2021-09-30 17:50:31 +03:00
|
|
|
rb_mark_tbl(p->partial_objects);
|
2008-09-02 06:47:38 +04:00
|
|
|
rb_mark_hash(p->compat_tbl);
|
|
|
|
}
|
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
static void
|
|
|
|
free_load_arg(void *ptr)
|
|
|
|
{
|
|
|
|
clear_load_arg(ptr);
|
|
|
|
xfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
memsize_load_arg(const void *ptr)
|
|
|
|
{
|
2015-12-10 00:34:01 +03:00
|
|
|
return sizeof(struct load_arg);
|
2009-10-04 14:30:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t load_arg_data = {
|
|
|
|
"load_arg",
|
2010-07-18 11:31:54 +04:00
|
|
|
{mark_load_arg, free_load_arg, memsize_load_arg,},
|
2014-12-01 09:38:04 +03:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-10-04 14:30:56 +04:00
|
|
|
};
|
|
|
|
|
2010-12-19 13:15:57 +03:00
|
|
|
#define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 10:32:32 +04:00
|
|
|
static VALUE r_object(struct load_arg *arg);
|
2014-07-28 12:15:42 +04:00
|
|
|
static VALUE r_symbol(struct load_arg *arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
|
2012-11-20 19:17:15 +04:00
|
|
|
NORETURN(static void too_short(void));
|
|
|
|
static void
|
|
|
|
too_short(void)
|
|
|
|
{
|
|
|
|
rb_raise(rb_eArgError, "marshal data too short");
|
|
|
|
}
|
|
|
|
|
2009-10-17 17:11:05 +04:00
|
|
|
static st_index_t
|
|
|
|
r_prepare(struct load_arg *arg)
|
|
|
|
{
|
|
|
|
st_index_t idx = arg->data->num_entries;
|
|
|
|
|
|
|
|
st_insert(arg->data, (st_data_t)idx, (st_data_t)Qundef);
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2012-11-20 19:17:15 +04:00
|
|
|
static unsigned char
|
|
|
|
r_byte1_buffered(struct load_arg *arg)
|
|
|
|
{
|
|
|
|
if (arg->buflen == 0) {
|
|
|
|
long readable = arg->readable < BUFSIZ ? arg->readable : BUFSIZ;
|
|
|
|
VALUE str, n = LONG2NUM(readable);
|
|
|
|
|
2015-09-25 15:45:10 +03:00
|
|
|
str = load_funcall(arg, arg->src, s_read, 1, &n);
|
2012-11-20 19:17:15 +04:00
|
|
|
if (NIL_P(str)) too_short();
|
|
|
|
StringValue(str);
|
|
|
|
memcpy(arg->buf, RSTRING_PTR(str), RSTRING_LEN(str));
|
|
|
|
arg->offset = 0;
|
|
|
|
arg->buflen = RSTRING_LEN(str);
|
|
|
|
}
|
|
|
|
arg->buflen--;
|
|
|
|
return arg->buf[arg->offset++];
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_byte(struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
int c;
|
|
|
|
|
2012-05-23 11:13:21 +04:00
|
|
|
if (RB_TYPE_P(arg->src, T_STRING)) {
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(arg->src) > arg->offset) {
|
|
|
|
c = (unsigned char)RSTRING_PTR(arg->src)[arg->offset++];
|
2004-10-05 05:37:46 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-11-20 19:17:15 +04:00
|
|
|
too_short();
|
2004-10-05 05:37:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-11-20 19:17:15 +04:00
|
|
|
if (arg->readable >0 || arg->buflen > 0) {
|
|
|
|
c = r_byte1_buffered(arg);
|
|
|
|
}
|
|
|
|
else {
|
2015-09-25 15:45:10 +03:00
|
|
|
VALUE v = load_funcall(arg, arg->src, s_getbyte, 0, 0);
|
2012-11-20 19:17:15 +04:00
|
|
|
if (NIL_P(v)) rb_eof_error();
|
|
|
|
c = (unsigned char)NUM2CHR(v);
|
|
|
|
}
|
2002-10-17 14:20:52 +04:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return c;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 12:44:39 +03:00
|
|
|
NORETURN(static void long_toobig(int size));
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
long_toobig(int size)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2008-07-12 17:17:29 +04:00
|
|
|
rb_raise(rb_eTypeError, "long too big for this architecture (size "
|
|
|
|
STRINGIZE(SIZEOF_LONG)", given %d)", size);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_long(struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
register long x;
|
2016-03-23 05:44:54 +03:00
|
|
|
int c = (signed char)r_byte(arg);
|
2001-08-29 10:28:51 +04:00
|
|
|
long i;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
if (c == 0) return 0;
|
|
|
|
if (c > 0) {
|
2000-12-05 12:36:54 +03:00
|
|
|
if (4 < c && c < 128) {
|
|
|
|
return c - 5;
|
|
|
|
}
|
2009-05-26 08:58:15 +04:00
|
|
|
if (c > (int)sizeof(long)) long_toobig(c);
|
1998-01-16 15:19:09 +03:00
|
|
|
x = 0;
|
|
|
|
for (i=0;i<c;i++) {
|
|
|
|
x |= (long)r_byte(arg) << (8*i);
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
2000-12-05 12:36:54 +03:00
|
|
|
if (-129 < c && c < -4) {
|
|
|
|
return c + 5;
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
c = -c;
|
2009-05-26 08:58:15 +04:00
|
|
|
if (c > (int)sizeof(long)) long_toobig(c);
|
1998-01-16 15:19:09 +03:00
|
|
|
x = -1;
|
|
|
|
for (i=0;i<c;i++) {
|
2001-08-29 10:28:51 +04:00
|
|
|
x &= ~((long)0xff << (8*i));
|
1998-01-16 15:19:09 +03:00
|
|
|
x |= (long)r_byte(arg) << (8*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2018-09-27 12:17:28 +03:00
|
|
|
long
|
|
|
|
ruby_marshal_read_long(const char **buf, long len)
|
|
|
|
{
|
|
|
|
long x;
|
|
|
|
struct RString src;
|
|
|
|
struct load_arg arg;
|
|
|
|
memset(&arg, 0, sizeof(arg));
|
|
|
|
arg.src = rb_setup_fake_str(&src, *buf, len, 0);
|
|
|
|
x = r_long(&arg);
|
|
|
|
*buf += arg.offset;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2012-11-20 19:17:15 +04:00
|
|
|
static VALUE
|
|
|
|
r_bytes1(long len, struct load_arg *arg)
|
|
|
|
{
|
|
|
|
VALUE str, n = LONG2NUM(len);
|
|
|
|
|
2015-09-25 15:45:10 +03:00
|
|
|
str = load_funcall(arg, arg->src, s_read, 1, &n);
|
2012-11-20 19:17:15 +04:00
|
|
|
if (NIL_P(str)) too_short();
|
|
|
|
StringValue(str);
|
|
|
|
if (RSTRING_LEN(str) != len) too_short();
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
r_bytes1_buffered(long len, struct load_arg *arg)
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
if (len <= arg->buflen) {
|
|
|
|
str = rb_str_new(arg->buf+arg->offset, len);
|
|
|
|
arg->offset += len;
|
|
|
|
arg->buflen -= len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
long buflen = arg->buflen;
|
|
|
|
long readable = arg->readable + 1;
|
|
|
|
long tmp_len, read_len, need_len = len - buflen;
|
|
|
|
VALUE tmp, n;
|
|
|
|
|
|
|
|
readable = readable < BUFSIZ ? readable : BUFSIZ;
|
|
|
|
read_len = need_len > readable ? need_len : readable;
|
|
|
|
n = LONG2NUM(read_len);
|
2015-09-25 15:45:10 +03:00
|
|
|
tmp = load_funcall(arg, arg->src, s_read, 1, &n);
|
2012-11-20 19:17:15 +04:00
|
|
|
if (NIL_P(tmp)) too_short();
|
|
|
|
StringValue(tmp);
|
|
|
|
|
|
|
|
tmp_len = RSTRING_LEN(tmp);
|
|
|
|
|
|
|
|
if (tmp_len < need_len) too_short();
|
|
|
|
|
|
|
|
str = rb_str_new(arg->buf+arg->offset, buflen);
|
|
|
|
rb_str_cat(str, RSTRING_PTR(tmp), need_len);
|
|
|
|
|
|
|
|
if (tmp_len > need_len) {
|
|
|
|
buflen = tmp_len - need_len;
|
|
|
|
memcpy(arg->buf, RSTRING_PTR(tmp)+need_len, buflen);
|
|
|
|
arg->buflen = buflen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arg->buflen = 0;
|
|
|
|
}
|
|
|
|
arg->offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-09-04 10:37:39 +04:00
|
|
|
#define r_bytes(arg) r_bytes0(r_long(arg), (arg))
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2002-09-04 10:37:39 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_bytes0(long len, struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2002-09-04 10:37:39 +04:00
|
|
|
VALUE str;
|
|
|
|
|
2004-08-17 13:02:40 +04:00
|
|
|
if (len == 0) return rb_str_new(0, 0);
|
2012-05-23 11:13:21 +04:00
|
|
|
if (RB_TYPE_P(arg->src, T_STRING)) {
|
2007-10-15 06:45:14 +04:00
|
|
|
if (RSTRING_LEN(arg->src) - arg->offset >= len) {
|
2006-08-31 14:47:44 +04:00
|
|
|
str = rb_str_new(RSTRING_PTR(arg->src)+arg->offset, len);
|
2004-10-05 05:37:46 +04:00
|
|
|
arg->offset += len;
|
|
|
|
}
|
|
|
|
else {
|
2012-11-20 19:17:15 +04:00
|
|
|
too_short();
|
2004-10-05 05:37:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-11-20 19:17:15 +04:00
|
|
|
if (arg->readable > 0 || arg->buflen > 0) {
|
|
|
|
str = r_bytes1_buffered(len, arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str = r_bytes1(len, arg);
|
|
|
|
}
|
2002-10-17 14:20:52 +04:00
|
|
|
}
|
2002-09-04 10:37:39 +04:00
|
|
|
return str;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2020-01-11 10:32:56 +03:00
|
|
|
static inline int
|
|
|
|
name_equal(const char *name, size_t nlen, const char *p, long l)
|
|
|
|
{
|
|
|
|
if ((size_t)l != nlen || *p != *name) return 0;
|
|
|
|
return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0;
|
|
|
|
}
|
|
|
|
|
2009-08-08 01:18:33 +04:00
|
|
|
static int
|
2014-07-28 12:15:42 +04:00
|
|
|
sym2encidx(VALUE sym, VALUE val)
|
2009-08-08 01:18:33 +04:00
|
|
|
{
|
2015-03-22 20:03:31 +03:00
|
|
|
static const char name_encoding[8] = "encoding";
|
|
|
|
const char *p;
|
|
|
|
long l;
|
|
|
|
if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1;
|
|
|
|
RSTRING_GETMEM(sym, p, l);
|
|
|
|
if (l <= 0) return -1;
|
2020-01-11 10:32:56 +03:00
|
|
|
if (name_equal(name_encoding, sizeof(name_encoding), p, l)) {
|
2009-10-17 17:11:05 +04:00
|
|
|
int idx = rb_enc_find_index(StringValueCStr(val));
|
|
|
|
return idx;
|
2009-08-08 01:18:33 +04:00
|
|
|
}
|
2020-01-11 10:32:56 +03:00
|
|
|
if (name_equal(name_s_encoding_short, rb_strlen_lit(name_s_encoding_short), p, l)) {
|
2009-08-08 01:18:33 +04:00
|
|
|
if (val == Qfalse) return rb_usascii_encindex();
|
|
|
|
else if (val == Qtrue) return rb_utf8_encindex();
|
|
|
|
/* bogus ignore */
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-01-11 09:04:44 +03:00
|
|
|
static int
|
2021-09-22 18:23:37 +03:00
|
|
|
symname_equal(VALUE sym, const char *name, size_t nlen)
|
2020-01-11 09:04:44 +03:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
long l;
|
2021-09-22 18:04:14 +03:00
|
|
|
if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return 0;
|
2020-01-11 09:04:44 +03:00
|
|
|
RSTRING_GETMEM(sym, p, l);
|
2021-09-22 18:23:37 +03:00
|
|
|
return name_equal(name, nlen, p, l);
|
2020-01-11 09:04:44 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:23:37 +03:00
|
|
|
#define BUILD_ASSERT_POSITIVE(n) \
|
2021-12-25 04:33:49 +03:00
|
|
|
/* make 0 negative to workaround the "zero size array" GCC extension, */ \
|
2021-09-22 18:23:37 +03:00
|
|
|
((sizeof(char [2*(ssize_t)(n)-1])+1)/2) /* assuming no overflow */
|
|
|
|
#define symname_equal_lit(sym, sym_name) \
|
|
|
|
symname_equal(sym, sym_name, BUILD_ASSERT_POSITIVE(rb_strlen_lit(sym_name)))
|
|
|
|
|
2014-07-28 12:15:42 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_symlink(struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2014-07-28 12:15:42 +04:00
|
|
|
st_data_t sym;
|
2000-11-20 10:31:55 +03:00
|
|
|
long num = r_long(arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2014-07-28 12:15:42 +04:00
|
|
|
if (!st_lookup(arg->symbols, num, &sym)) {
|
2012-04-15 04:06:13 +04:00
|
|
|
rb_raise(rb_eArgError, "bad symbol");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2014-07-28 12:15:42 +04:00
|
|
|
return (VALUE)sym;
|
2000-04-10 09:48:43 +04:00
|
|
|
}
|
|
|
|
|
2014-07-28 12:15:42 +04:00
|
|
|
static VALUE
|
2009-08-08 01:18:33 +04:00
|
|
|
r_symreal(struct load_arg *arg, int ivar)
|
2000-04-10 09:48:43 +04:00
|
|
|
{
|
2012-03-28 10:05:20 +04:00
|
|
|
VALUE s = r_bytes(arg);
|
2014-07-28 12:15:42 +04:00
|
|
|
VALUE sym;
|
2009-08-08 01:18:33 +04:00
|
|
|
int idx = -1;
|
2009-10-20 12:57:33 +04:00
|
|
|
st_index_t n = arg->symbols->num_entries;
|
2007-02-04 22:17:33 +03:00
|
|
|
|
2015-03-22 20:03:31 +03:00
|
|
|
if (rb_enc_str_asciionly_p(s)) rb_enc_associate_index(s, ENCINDEX_US_ASCII);
|
|
|
|
st_insert(arg->symbols, (st_data_t)n, (st_data_t)s);
|
2009-08-08 01:18:33 +04:00
|
|
|
if (ivar) {
|
|
|
|
long num = r_long(arg);
|
|
|
|
while (num-- > 0) {
|
2014-07-29 00:36:50 +04:00
|
|
|
sym = r_symbol(arg);
|
|
|
|
idx = sym2encidx(sym, r_object(arg));
|
2009-08-08 01:18:33 +04:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 10:02:44 +03:00
|
|
|
if (idx > 0) {
|
|
|
|
rb_enc_associate_index(s, idx);
|
2022-09-09 20:00:34 +03:00
|
|
|
if (is_broken_string(s)) {
|
2021-09-23 10:02:44 +03:00
|
|
|
rb_raise(rb_eArgError, "invalid byte sequence in %s: %+"PRIsVALUE,
|
|
|
|
rb_enc_name(rb_enc_from_index(idx)), s);
|
|
|
|
}
|
|
|
|
}
|
2007-02-04 22:17:33 +03:00
|
|
|
|
2015-03-22 20:03:31 +03:00
|
|
|
return s;
|
2007-02-04 22:17:33 +03:00
|
|
|
}
|
|
|
|
|
2014-07-28 12:15:42 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_symbol(struct load_arg *arg)
|
2000-04-10 09:48:43 +04:00
|
|
|
{
|
2009-08-08 01:18:33 +04:00
|
|
|
int type, ivar = 0;
|
2007-02-04 22:17:33 +03:00
|
|
|
|
2009-08-08 01:18:33 +04:00
|
|
|
again:
|
2007-02-04 22:17:33 +03:00
|
|
|
switch ((type = r_byte(arg))) {
|
2012-04-15 04:06:13 +04:00
|
|
|
default:
|
|
|
|
rb_raise(rb_eArgError, "dump format error for symbol(0x%x)", type);
|
2009-08-08 01:18:33 +04:00
|
|
|
case TYPE_IVAR:
|
|
|
|
ivar = 1;
|
|
|
|
goto again;
|
2007-02-04 22:17:33 +03:00
|
|
|
case TYPE_SYMBOL:
|
2009-08-08 01:18:33 +04:00
|
|
|
return r_symreal(arg, ivar);
|
2007-02-04 22:17:33 +03:00
|
|
|
case TYPE_SYMLINK:
|
2009-08-08 01:18:33 +04:00
|
|
|
if (ivar) {
|
|
|
|
rb_raise(rb_eArgError, "dump format error (symlink with encoding)");
|
|
|
|
}
|
2000-04-10 09:48:43 +04:00
|
|
|
return r_symlink(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-10 01:55:55 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_unique(struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2015-03-22 20:03:31 +03:00
|
|
|
return r_symbol(arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_string(struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2002-09-04 10:37:39 +04:00
|
|
|
return r_bytes(arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2009-10-17 17:11:05 +04:00
|
|
|
r_entry0(VALUE v, st_index_t num, struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2020-09-05 11:28:06 +03:00
|
|
|
st_data_t real_obj = (st_data_t)v;
|
|
|
|
if (arg->compat_tbl) {
|
|
|
|
/* real_obj is kept if not found */
|
|
|
|
st_lookup(arg->compat_tbl, v, &real_obj);
|
2007-09-08 19:07:18 +04:00
|
|
|
}
|
2020-09-05 11:28:06 +03:00
|
|
|
st_insert(arg->data, num, real_obj);
|
2021-09-30 17:50:31 +03:00
|
|
|
st_insert(arg->partial_objects, (st_data_t)real_obj, Qtrue);
|
1998-01-16 15:19:09 +03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2007-09-08 19:07:18 +04:00
|
|
|
static VALUE
|
2013-04-22 12:09:44 +04:00
|
|
|
r_fixup_compat(VALUE v, struct load_arg *arg)
|
2007-09-08 19:07:18 +04:00
|
|
|
{
|
2007-09-26 23:12:04 +04:00
|
|
|
st_data_t data;
|
2015-12-02 09:50:41 +03:00
|
|
|
st_data_t key = (st_data_t)v;
|
|
|
|
if (arg->compat_tbl && st_delete(arg->compat_tbl, &key, &data)) {
|
2007-09-26 23:12:04 +04:00
|
|
|
VALUE real_obj = (VALUE)data;
|
2007-09-08 19:07:18 +04:00
|
|
|
rb_alloc_func_t allocator = rb_get_alloc_func(CLASS_OF(real_obj));
|
2007-09-26 23:12:04 +04:00
|
|
|
if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
|
|
|
|
marshal_compat_t *compat = (marshal_compat_t*)data;
|
2007-09-08 19:07:18 +04:00
|
|
|
compat->loader(real_obj, v);
|
|
|
|
}
|
2007-09-29 12:17:48 +04:00
|
|
|
v = real_obj;
|
|
|
|
}
|
2013-04-22 12:09:44 +04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
r_post_proc(VALUE v, struct load_arg *arg)
|
|
|
|
{
|
2007-09-29 12:17:48 +04:00
|
|
|
if (arg->proc) {
|
2015-09-25 15:45:10 +03:00
|
|
|
v = load_funcall(arg, arg->proc, s_call, 1, &v);
|
2007-09-08 19:07:18 +04:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-04-22 12:09:44 +04:00
|
|
|
static VALUE
|
2021-09-30 17:50:31 +03:00
|
|
|
r_leave(VALUE v, struct load_arg *arg, bool partial)
|
2013-04-22 12:09:44 +04:00
|
|
|
{
|
|
|
|
v = r_fixup_compat(v, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
if (!partial) {
|
|
|
|
st_data_t data;
|
|
|
|
st_data_t key = (st_data_t)v;
|
|
|
|
st_delete(arg->partial_objects, &key, &data);
|
2021-09-18 16:34:15 +03:00
|
|
|
if (arg->freeze) {
|
|
|
|
if (RB_TYPE_P(v, T_MODULE) || RB_TYPE_P(v, T_CLASS)) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
else if (RB_TYPE_P(v, T_STRING)) {
|
|
|
|
v = rb_str_to_interned_str(v);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
OBJ_FREEZE(v);
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_post_proc(v, arg);
|
|
|
|
}
|
2013-04-22 12:09:44 +04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
copy_ivar_i(st_data_t key, st_data_t val, st_data_t arg)
|
|
|
|
{
|
2013-05-02 12:32:48 +04:00
|
|
|
VALUE obj = (VALUE)arg, value = (VALUE)val;
|
|
|
|
ID vid = (ID)key;
|
|
|
|
|
|
|
|
if (!rb_ivar_defined(obj, vid))
|
|
|
|
rb_ivar_set(obj, vid, value);
|
2013-04-22 12:09:44 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
r_copy_ivar(VALUE v, VALUE data)
|
|
|
|
{
|
|
|
|
rb_ivar_foreach(data, copy_ivar_i, (st_data_t)v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
static void
|
2010-02-11 08:37:43 +03:00
|
|
|
r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
|
2000-01-05 07:41:21 +03:00
|
|
|
{
|
2000-11-20 10:31:55 +03:00
|
|
|
long len;
|
2000-01-05 07:41:21 +03:00
|
|
|
|
|
|
|
len = r_long(arg);
|
|
|
|
if (len > 0) {
|
2009-06-13 04:58:30 +04:00
|
|
|
do {
|
2014-07-28 12:15:42 +04:00
|
|
|
VALUE sym = r_symbol(arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
VALUE val = r_object(arg);
|
2014-07-28 12:15:42 +04:00
|
|
|
int idx = sym2encidx(sym, val);
|
2009-08-08 01:18:33 +04:00
|
|
|
if (idx >= 0) {
|
2018-06-28 11:35:48 +03:00
|
|
|
if (rb_enc_capable(obj)) {
|
|
|
|
rb_enc_associate_index(obj, idx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "%"PRIsVALUE" is not enc_capable", obj);
|
|
|
|
}
|
2010-02-11 08:37:43 +03:00
|
|
|
if (has_encoding) *has_encoding = TRUE;
|
2009-05-27 20:31:51 +04:00
|
|
|
}
|
2021-09-22 18:23:37 +03:00
|
|
|
else if (symname_equal_lit(sym, name_s_ruby2_keywords_flag)) {
|
2020-01-11 09:04:44 +03:00
|
|
|
if (RB_TYPE_P(obj, T_HASH)) {
|
2021-09-22 18:53:44 +03:00
|
|
|
rb_hash_ruby2_keywords(obj);
|
2020-01-11 09:04:44 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "ruby2_keywords flag is given but %"PRIsVALUE" is not a Hash", obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_ivar_set(obj, rb_intern_str(sym), val);
|
2007-11-09 07:37:36 +03:00
|
|
|
}
|
2009-06-13 04:58:30 +04:00
|
|
|
} while (--len > 0);
|
2000-01-05 07:41:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-05 13:42:56 +04:00
|
|
|
static VALUE
|
2009-08-10 01:55:55 +04:00
|
|
|
path2class(VALUE path)
|
2002-09-05 13:42:56 +04:00
|
|
|
{
|
2009-08-10 01:55:55 +04:00
|
|
|
VALUE v = rb_path_to_class(path);
|
2002-09-05 13:42:56 +04:00
|
|
|
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(v, T_CLASS)) {
|
2012-12-04 11:23:34 +04:00
|
|
|
rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to class", path);
|
2002-09-05 13:42:56 +04:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-03-08 19:26:08 +04:00
|
|
|
#define path2module(path) must_be_module(rb_path_to_class(path), path)
|
|
|
|
|
2002-09-05 13:42:56 +04:00
|
|
|
static VALUE
|
2013-03-08 19:26:08 +04:00
|
|
|
must_be_module(VALUE v, VALUE path)
|
2002-09-05 13:42:56 +04:00
|
|
|
{
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(v, T_MODULE)) {
|
2012-12-04 11:23:34 +04:00
|
|
|
rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to module", path);
|
2002-09-05 13:42:56 +04:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2007-09-08 19:07:18 +04:00
|
|
|
static VALUE
|
2012-06-04 06:40:30 +04:00
|
|
|
obj_alloc_by_klass(VALUE klass, struct load_arg *arg, VALUE *oldclass)
|
2007-09-08 19:07:18 +04:00
|
|
|
{
|
2007-09-26 23:12:04 +04:00
|
|
|
st_data_t data;
|
2007-09-08 19:07:18 +04:00
|
|
|
rb_alloc_func_t allocator;
|
|
|
|
|
|
|
|
allocator = rb_get_alloc_func(klass);
|
2007-09-26 23:12:04 +04:00
|
|
|
if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
|
|
|
|
marshal_compat_t *compat = (marshal_compat_t*)data;
|
2007-09-08 19:07:18 +04:00
|
|
|
VALUE real_obj = rb_obj_alloc(klass);
|
|
|
|
VALUE obj = rb_obj_alloc(compat->oldclass);
|
2012-06-04 06:40:30 +04:00
|
|
|
if (oldclass) *oldclass = compat->oldclass;
|
2014-10-02 00:29:46 +04:00
|
|
|
|
|
|
|
if (!arg->compat_tbl) {
|
2015-01-23 14:01:02 +03:00
|
|
|
arg->compat_tbl = rb_init_identtable();
|
2014-10-02 00:29:46 +04:00
|
|
|
}
|
2007-09-08 19:07:18 +04:00
|
|
|
st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_obj_alloc(klass);
|
|
|
|
}
|
|
|
|
|
2012-06-04 06:40:30 +04:00
|
|
|
static VALUE
|
|
|
|
obj_alloc_by_path(VALUE path, struct load_arg *arg)
|
|
|
|
{
|
|
|
|
return obj_alloc_by_klass(path2class(path), arg, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
append_extmod(VALUE obj, VALUE extmod)
|
|
|
|
{
|
|
|
|
long i = RARRAY_LEN(extmod);
|
|
|
|
while (i > 0) {
|
2013-05-13 13:56:22 +04:00
|
|
|
VALUE m = RARRAY_AREF(extmod, --i);
|
2012-06-04 06:40:30 +04:00
|
|
|
rb_extend_object(obj, m);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-02-01 11:35:37 +04:00
|
|
|
#define prohibit_ivar(type, str) do { \
|
|
|
|
if (!ivp || !*ivp) break; \
|
|
|
|
rb_raise(rb_eTypeError, \
|
|
|
|
"can't override instance variable of "type" `%"PRIsVALUE"'", \
|
|
|
|
(str)); \
|
|
|
|
} while (0)
|
|
|
|
|
2021-09-22 19:36:27 +03:00
|
|
|
static VALUE r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int type);
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static VALUE
|
2021-09-30 17:50:31 +03:00
|
|
|
r_object0(struct load_arg *arg, bool partial, int *ivp, VALUE extmod)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
int type = r_byte(arg);
|
2021-09-22 19:36:27 +03:00
|
|
|
return r_object_for(arg, partial, ivp, extmod, type);
|
|
|
|
}
|
|
|
|
|
2023-02-16 14:18:27 +03:00
|
|
|
static int
|
|
|
|
r_move_ivar(st_data_t k, st_data_t v, st_data_t d)
|
|
|
|
{
|
|
|
|
ID key = (ID)k;
|
|
|
|
VALUE value = (VALUE)v;
|
|
|
|
VALUE dest = (VALUE)d;
|
|
|
|
|
|
|
|
if (rb_is_instance_id(key)) {
|
|
|
|
rb_ivar_set(dest, key, value);
|
|
|
|
return ST_DELETE;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:36:27 +03:00
|
|
|
static VALUE
|
|
|
|
r_object_for(struct load_arg *arg, bool partial, int *ivp, VALUE extmod, int type)
|
|
|
|
{
|
|
|
|
VALUE (*hash_new_with_size)(st_index_t) = rb_hash_new_with_size;
|
|
|
|
VALUE v = Qnil;
|
1999-10-20 11:10:23 +04:00
|
|
|
long id;
|
2008-09-02 06:47:38 +04:00
|
|
|
st_data_t link;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_LINK:
|
1999-10-20 11:10:23 +04:00
|
|
|
id = r_long(arg);
|
2008-09-02 06:47:38 +04:00
|
|
|
if (!st_lookup(arg->data, (st_data_t)id, &link)) {
|
1999-10-27 08:20:00 +04:00
|
|
|
rb_raise(rb_eArgError, "dump format error (unlinked)");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2008-09-02 06:47:38 +04:00
|
|
|
v = (VALUE)link;
|
2021-09-30 17:50:31 +03:00
|
|
|
if (!st_lookup(arg->partial_objects, (st_data_t)v, &link)) {
|
|
|
|
v = r_post_proc(v, arg);
|
|
|
|
}
|
2006-10-23 02:24:14 +04:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
case TYPE_IVAR:
|
2003-10-02 12:25:00 +04:00
|
|
|
{
|
2009-07-18 12:05:32 +04:00
|
|
|
int ivar = TRUE;
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_object0(arg, true, &ivar, extmod);
|
2010-02-11 08:37:43 +03:00
|
|
|
if (ivar) r_ivar(v, NULL, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
2003-10-02 12:25:00 +04:00
|
|
|
}
|
2002-08-28 19:58:35 +04:00
|
|
|
break;
|
2000-01-05 07:41:21 +03:00
|
|
|
|
2002-09-05 13:42:56 +04:00
|
|
|
case TYPE_EXTENDED:
|
|
|
|
{
|
2013-03-08 19:26:08 +04:00
|
|
|
VALUE path = r_unique(arg);
|
|
|
|
VALUE m = rb_path_to_class(path);
|
2022-07-25 17:40:45 +03:00
|
|
|
if (NIL_P(extmod)) extmod = rb_ary_hidden_new(0);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-03-08 19:26:08 +04:00
|
|
|
if (RB_TYPE_P(m, T_CLASS)) { /* prepended */
|
|
|
|
VALUE c;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_object0(arg, true, 0, Qnil);
|
2013-03-08 19:26:08 +04:00
|
|
|
c = CLASS_OF(v);
|
|
|
|
if (c != m || FL_TEST(c, FL_SINGLETON)) {
|
|
|
|
rb_raise(rb_eArgError,
|
|
|
|
"prepended class %"PRIsVALUE" differs from class %"PRIsVALUE,
|
|
|
|
path, rb_class_name(c));
|
|
|
|
}
|
|
|
|
c = rb_singleton_class(v);
|
|
|
|
while (RARRAY_LEN(extmod) > 0) {
|
|
|
|
m = rb_ary_pop(extmod);
|
|
|
|
rb_prepend_module(c, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
must_be_module(m, path);
|
|
|
|
rb_ary_push(extmod, m);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_object0(arg, true, 0, extmod);
|
2013-03-08 19:26:08 +04:00
|
|
|
while (RARRAY_LEN(extmod) > 0) {
|
|
|
|
m = rb_ary_pop(extmod);
|
|
|
|
rb_extend_object(v, m);
|
|
|
|
}
|
|
|
|
}
|
2023-02-10 12:31:30 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
2002-09-05 13:42:56 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case TYPE_UCLASS:
|
|
|
|
{
|
2002-09-05 13:42:56 +04:00
|
|
|
VALUE c = path2class(r_unique(arg));
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2002-12-12 10:29:14 +03:00
|
|
|
if (FL_TEST(c, FL_SINGLETON)) {
|
|
|
|
rb_raise(rb_eTypeError, "singleton can't be loaded");
|
|
|
|
}
|
2021-09-22 19:36:27 +03:00
|
|
|
type = r_byte(arg);
|
|
|
|
if ((c == rb_cHash) &&
|
|
|
|
/* Hack for compare_by_identify */
|
|
|
|
(type == TYPE_HASH || type == TYPE_HASH_DEF)) {
|
|
|
|
hash_new_with_size = rb_ident_hash_new_with_size;
|
|
|
|
goto type_hash;
|
|
|
|
}
|
|
|
|
v = r_object_for(arg, partial, 0, extmod, type);
|
2011-09-29 15:07:45 +04:00
|
|
|
if (rb_special_const_p(v) || RB_TYPE_P(v, T_OBJECT) || RB_TYPE_P(v, T_CLASS)) {
|
2020-06-15 10:09:33 +03:00
|
|
|
goto format_error;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(v, T_MODULE) || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
|
2001-10-22 10:48:18 +04:00
|
|
|
VALUE tmp = rb_obj_alloc(c);
|
|
|
|
|
|
|
|
if (TYPE(v) != TYPE(tmp)) goto format_error;
|
2001-10-03 11:19:19 +04:00
|
|
|
}
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
|
|
|
RBASIC_SET_CLASS(v, c);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2002-08-28 19:58:35 +04:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2020-06-15 10:09:33 +03:00
|
|
|
format_error:
|
|
|
|
rb_raise(rb_eArgError, "dump format error (user class)");
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case TYPE_NIL:
|
2002-02-26 09:48:59 +03:00
|
|
|
v = Qnil;
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, false);
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_TRUE:
|
2002-02-26 09:48:59 +03:00
|
|
|
v = Qtrue;
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, false);
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_FALSE:
|
2002-02-26 09:48:59 +03:00
|
|
|
v = Qfalse;
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, false);
|
2002-02-28 09:53:33 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_FIXNUM:
|
|
|
|
{
|
2000-11-20 10:31:55 +03:00
|
|
|
long i = r_long(arg);
|
2002-08-21 19:47:54 +04:00
|
|
|
v = LONG2FIX(i);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, false);
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_FLOAT:
|
|
|
|
{
|
2009-03-12 12:09:15 +03:00
|
|
|
double d;
|
2002-09-04 10:37:39 +04:00
|
|
|
VALUE str = r_bytes(arg);
|
2006-08-31 14:47:44 +04:00
|
|
|
const char *ptr = RSTRING_PTR(str);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2003-04-20 19:11:20 +04:00
|
|
|
if (strcmp(ptr, "nan") == 0) {
|
2018-01-20 06:16:59 +03:00
|
|
|
d = nan("");
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
2003-04-20 19:11:20 +04:00
|
|
|
else if (strcmp(ptr, "inf") == 0) {
|
2018-01-19 04:45:36 +03:00
|
|
|
d = HUGE_VAL;
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
2003-04-20 19:11:20 +04:00
|
|
|
else if (strcmp(ptr, "-inf") == 0) {
|
2018-01-19 04:45:36 +03:00
|
|
|
d = -HUGE_VAL;
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2003-04-20 19:11:20 +04:00
|
|
|
char *e;
|
|
|
|
d = strtod(ptr, &e);
|
2006-08-31 14:47:44 +04:00
|
|
|
d = load_mantissa(d, e, RSTRING_LEN(str) - (e - ptr));
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
2008-09-05 22:24:21 +04:00
|
|
|
v = DBL2NUM(d);
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, false);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_BIGNUM:
|
|
|
|
{
|
2000-11-20 10:31:55 +03:00
|
|
|
long len;
|
2012-12-04 11:23:20 +04:00
|
|
|
VALUE data;
|
2013-06-08 18:14:46 +04:00
|
|
|
int sign;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2013-06-08 18:14:46 +04:00
|
|
|
sign = r_byte(arg);
|
2000-10-31 11:37:47 +03:00
|
|
|
len = r_long(arg);
|
2022-08-16 02:14:12 +03:00
|
|
|
|
|
|
|
if (SIZEOF_VALUE >= 8 && len <= 4) {
|
|
|
|
// Representable within uintptr, likely FIXNUM
|
|
|
|
VALUE num = 0;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
num |= (VALUE)r_byte(arg) << (i * 16);
|
|
|
|
num |= (VALUE)r_byte(arg) << (i * 16 + 8);
|
|
|
|
}
|
|
|
|
#if SIZEOF_VALUE == SIZEOF_LONG
|
|
|
|
v = ULONG2NUM(num);
|
|
|
|
#else
|
|
|
|
v = ULL2NUM(num);
|
|
|
|
#endif
|
|
|
|
if (sign == '-') {
|
|
|
|
v = rb_int_uminus(v);
|
|
|
|
}
|
2022-09-02 08:49:42 +03:00
|
|
|
}
|
|
|
|
else {
|
2022-08-16 02:14:12 +03:00
|
|
|
data = r_bytes0(len * 2, arg);
|
|
|
|
v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
|
|
|
|
INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
|
|
|
|
rb_str_resize(data, 0L);
|
|
|
|
}
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, false);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_STRING:
|
2003-10-02 12:25:00 +04:00
|
|
|
v = r_entry(r_string(arg), arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_REGEXP:
|
|
|
|
{
|
2012-12-04 11:23:20 +04:00
|
|
|
VALUE str = r_bytes(arg);
|
2002-09-04 10:37:39 +04:00
|
|
|
int options = r_byte(arg);
|
2010-02-11 08:37:43 +03:00
|
|
|
int has_encoding = FALSE;
|
2010-02-24 17:16:59 +03:00
|
|
|
st_index_t idx = r_prepare(arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2010-02-11 08:37:43 +03:00
|
|
|
if (ivp) {
|
|
|
|
r_ivar(str, &has_encoding, arg);
|
|
|
|
*ivp = FALSE;
|
|
|
|
}
|
|
|
|
if (!has_encoding) {
|
2010-02-11 08:43:19 +03:00
|
|
|
/* 1.8 compatibility; remove escapes undefined in 1.8 */
|
|
|
|
char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
|
|
|
|
long len = RSTRING_LEN(str);
|
|
|
|
long bs = 0;
|
|
|
|
for (; len-- > 0; *dst++ = *src++) {
|
|
|
|
switch (*src) {
|
|
|
|
case '\\': bs++; break;
|
|
|
|
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
|
|
|
|
case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
|
|
|
|
case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
|
|
|
|
case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
|
|
|
|
case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
|
|
|
|
if (bs & 1) --dst;
|
2019-07-14 08:03:44 +03:00
|
|
|
/* fall through */
|
2010-02-11 08:43:19 +03:00
|
|
|
default: bs = 0; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb_str_set_len(str, dst - ptr);
|
2009-07-09 18:47:22 +04:00
|
|
|
}
|
2023-02-16 14:18:27 +03:00
|
|
|
VALUE regexp = rb_reg_new_str(str, options);
|
|
|
|
rb_ivar_foreach(str, r_move_ivar, regexp);
|
|
|
|
|
|
|
|
v = r_entry0(regexp, idx, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
{
|
2014-04-12 16:58:04 +04:00
|
|
|
long len = r_long(arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
v = rb_ary_new2(len);
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable += len - 1;
|
1998-01-16 15:19:09 +03:00
|
|
|
while (len--) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(v, r_object(arg));
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable--;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable++;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_HASH:
|
1999-12-01 12:24:48 +03:00
|
|
|
case TYPE_HASH_DEF:
|
2021-09-22 19:36:27 +03:00
|
|
|
type_hash:
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2000-11-20 10:31:55 +03:00
|
|
|
long len = r_long(arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2021-09-22 19:36:27 +03:00
|
|
|
v = hash_new_with_size(len);
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable += (len - 1) * 2;
|
1998-01-16 15:19:09 +03:00
|
|
|
while (len--) {
|
|
|
|
VALUE key = r_object(arg);
|
|
|
|
VALUE value = r_object(arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_hash_aset(v, key, value);
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable -= 2;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable += 2;
|
1999-12-02 09:58:52 +03:00
|
|
|
if (type == TYPE_HASH_DEF) {
|
2013-05-26 16:37:11 +04:00
|
|
|
RHASH_SET_IFNONE(v, r_object(arg));
|
1999-12-01 12:24:48 +03:00
|
|
|
}
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_STRUCT:
|
|
|
|
{
|
2009-10-17 17:11:05 +04:00
|
|
|
VALUE mem, values;
|
2014-04-12 16:58:04 +04:00
|
|
|
long i;
|
2014-07-28 12:15:42 +04:00
|
|
|
VALUE slot;
|
2009-10-17 17:11:05 +04:00
|
|
|
st_index_t idx = r_prepare(arg);
|
|
|
|
VALUE klass = path2class(r_unique(arg));
|
|
|
|
long len = r_long(arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2007-09-09 20:23:27 +04:00
|
|
|
v = rb_obj_alloc(klass);
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(v, T_STRUCT)) {
|
2014-07-28 12:15:42 +04:00
|
|
|
rb_raise(rb_eTypeError, "class %"PRIsVALUE" not a struct", rb_class_name(klass));
|
2007-09-09 20:23:27 +04:00
|
|
|
}
|
|
|
|
mem = rb_struct_s_members(klass);
|
2007-09-08 19:07:18 +04:00
|
|
|
if (RARRAY_LEN(mem) != len) {
|
2014-07-28 12:15:42 +04:00
|
|
|
rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (struct size differs)",
|
|
|
|
rb_class_name(klass));
|
2007-09-08 19:07:18 +04:00
|
|
|
}
|
|
|
|
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable += (len - 1) * 2;
|
2009-10-17 17:11:05 +04:00
|
|
|
v = r_entry0(v, idx, arg);
|
2007-09-08 19:07:18 +04:00
|
|
|
values = rb_ary_new2(len);
|
2018-01-05 14:44:31 +03:00
|
|
|
{
|
2018-01-18 04:27:45 +03:00
|
|
|
VALUE keywords = Qfalse;
|
|
|
|
if (RTEST(rb_struct_s_keyword_init(klass))) {
|
2018-01-05 14:44:31 +03:00
|
|
|
keywords = rb_hash_new();
|
|
|
|
rb_ary_push(values, keywords);
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2018-01-05 14:44:31 +03:00
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
|
|
|
|
slot = r_symbol(arg);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2018-01-05 14:44:31 +03:00
|
|
|
if (!rb_str_equal(n, slot)) {
|
|
|
|
rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
|
|
|
|
rb_class_name(klass),
|
|
|
|
slot, n);
|
|
|
|
}
|
2018-01-18 04:27:45 +03:00
|
|
|
if (keywords) {
|
2018-01-05 14:44:31 +03:00
|
|
|
rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_ary_push(values, r_object(arg));
|
|
|
|
}
|
|
|
|
arg->readable -= 2;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
2007-09-08 20:19:13 +04:00
|
|
|
rb_struct_initialize(v, values);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable += 2;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_USERDEF:
|
|
|
|
{
|
2015-04-16 17:25:33 +03:00
|
|
|
VALUE name = r_unique(arg);
|
|
|
|
VALUE klass = path2class(name);
|
2003-10-02 12:25:00 +04:00
|
|
|
VALUE data;
|
2015-12-02 10:27:22 +03:00
|
|
|
st_data_t d;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2013-01-20 18:55:42 +04:00
|
|
|
if (!rb_obj_respond_to(klass, s_load, TRUE)) {
|
2015-04-16 17:25:33 +03:00
|
|
|
rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method `_load'",
|
|
|
|
name);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2013-01-20 18:55:42 +04:00
|
|
|
data = r_string(arg);
|
|
|
|
if (ivp) {
|
|
|
|
r_ivar(data, NULL, arg);
|
|
|
|
*ivp = FALSE;
|
|
|
|
}
|
2015-09-25 15:45:10 +03:00
|
|
|
v = load_funcall(arg, klass, s_load, 1, &data);
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2015-12-02 10:27:22 +03:00
|
|
|
if (st_lookup(compat_allocator_tbl, (st_data_t)rb_get_alloc_func(klass), &d)) {
|
|
|
|
marshal_compat_t *compat = (marshal_compat_t*)d;
|
|
|
|
v = compat->loader(klass, v);
|
|
|
|
}
|
2021-09-30 17:50:31 +03:00
|
|
|
if (!partial) v = r_post_proc(v, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-07-29 22:26:55 +04:00
|
|
|
case TYPE_USRMARSHAL:
|
|
|
|
{
|
2015-04-16 17:28:29 +03:00
|
|
|
VALUE name = r_unique(arg);
|
|
|
|
VALUE klass = path2class(name);
|
2012-06-04 06:40:30 +04:00
|
|
|
VALUE oldclass = 0;
|
2003-10-02 12:25:00 +04:00
|
|
|
VALUE data;
|
2003-07-29 22:26:55 +04:00
|
|
|
|
2012-06-04 06:40:30 +04:00
|
|
|
v = obj_alloc_by_klass(klass, arg, &oldclass);
|
2007-03-20 17:00:07 +03:00
|
|
|
if (!NIL_P(extmod)) {
|
2012-06-04 06:40:30 +04:00
|
|
|
/* for the case marshal_load is overridden */
|
|
|
|
append_extmod(v, extmod);
|
2022-07-21 19:23:58 +03:00
|
|
|
}
|
2013-01-20 18:55:42 +04:00
|
|
|
if (!rb_obj_respond_to(v, s_mload, TRUE)) {
|
2015-04-16 17:28:29 +03:00
|
|
|
rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method `marshal_load'",
|
2022-07-21 19:23:58 +03:00
|
|
|
name);
|
|
|
|
}
|
2013-01-20 18:55:42 +04:00
|
|
|
v = r_entry(v, arg);
|
|
|
|
data = r_object(arg);
|
2015-09-25 15:45:10 +03:00
|
|
|
load_funcall(arg, v, s_mload, 1, &data);
|
2013-04-22 12:09:44 +04:00
|
|
|
v = r_fixup_compat(v, arg);
|
|
|
|
v = r_copy_ivar(v, data);
|
|
|
|
v = r_post_proc(v, arg);
|
2007-03-20 17:00:07 +03:00
|
|
|
if (!NIL_P(extmod)) {
|
2012-06-04 06:40:30 +04:00
|
|
|
if (oldclass) append_extmod(v, extmod);
|
|
|
|
rb_ary_clear(extmod);
|
2003-10-15 06:27:56 +04:00
|
|
|
}
|
2003-07-29 22:26:55 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case TYPE_OBJECT:
|
|
|
|
{
|
2009-10-17 17:11:05 +04:00
|
|
|
st_index_t idx = r_prepare(arg);
|
2007-09-08 19:07:18 +04:00
|
|
|
v = obj_alloc_by_path(r_unique(arg), arg);
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(v, T_OBJECT)) {
|
2001-10-03 11:19:19 +04:00
|
|
|
rb_raise(rb_eArgError, "dump format error");
|
|
|
|
}
|
2009-10-17 17:11:05 +04:00
|
|
|
v = r_entry0(v, idx, arg);
|
2010-02-11 08:37:43 +03:00
|
|
|
r_ivar(v, NULL, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
case TYPE_DATA:
|
2012-06-04 01:13:36 +04:00
|
|
|
{
|
2015-04-16 17:25:19 +03:00
|
|
|
VALUE name = r_unique(arg);
|
|
|
|
VALUE klass = path2class(name);
|
2012-06-04 06:40:30 +04:00
|
|
|
VALUE oldclass = 0;
|
2012-12-01 14:01:40 +04:00
|
|
|
VALUE r;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2012-06-04 06:40:30 +04:00
|
|
|
v = obj_alloc_by_klass(klass, arg, &oldclass);
|
2012-06-04 01:13:36 +04:00
|
|
|
if (!RB_TYPE_P(v, T_DATA)) {
|
|
|
|
rb_raise(rb_eArgError, "dump format error");
|
|
|
|
}
|
|
|
|
v = r_entry(v, arg);
|
2013-01-20 18:55:42 +04:00
|
|
|
if (!rb_obj_respond_to(v, s_load_data, TRUE)) {
|
2012-06-04 01:13:36 +04:00
|
|
|
rb_raise(rb_eTypeError,
|
2015-04-16 17:25:19 +03:00
|
|
|
"class %"PRIsVALUE" needs to have instance method `_load_data'",
|
|
|
|
name);
|
2012-06-04 01:13:36 +04:00
|
|
|
}
|
2021-09-30 17:50:31 +03:00
|
|
|
r = r_object0(arg, partial, 0, extmod);
|
2015-09-25 15:45:10 +03:00
|
|
|
load_funcall(arg, v, s_load_data, 1, &r);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
2012-06-04 01:13:36 +04:00
|
|
|
}
|
|
|
|
break;
|
2002-04-24 08:54:16 +04:00
|
|
|
|
1999-12-01 12:24:48 +03:00
|
|
|
case TYPE_MODULE_OLD:
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2012-12-04 11:23:20 +04:00
|
|
|
VALUE str = r_bytes(arg);
|
2002-09-05 13:42:56 +04:00
|
|
|
|
2009-08-10 01:55:55 +04:00
|
|
|
v = rb_path_to_class(str);
|
2013-02-01 11:35:37 +04:00
|
|
|
prohibit_ivar("class/module", str);
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-12-01 12:24:48 +03:00
|
|
|
case TYPE_CLASS:
|
|
|
|
{
|
2012-12-04 11:23:20 +04:00
|
|
|
VALUE str = r_bytes(arg);
|
2002-09-05 13:42:56 +04:00
|
|
|
|
2009-08-10 01:55:55 +04:00
|
|
|
v = path2class(str);
|
2013-02-01 11:35:37 +04:00
|
|
|
prohibit_ivar("class", str);
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
1999-12-01 12:24:48 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
1999-12-01 12:24:48 +03:00
|
|
|
|
|
|
|
case TYPE_MODULE:
|
|
|
|
{
|
2012-12-04 11:23:20 +04:00
|
|
|
VALUE str = r_bytes(arg);
|
2002-09-05 13:42:56 +04:00
|
|
|
|
2009-08-10 01:55:55 +04:00
|
|
|
v = path2module(str);
|
2013-02-01 11:35:37 +04:00
|
|
|
prohibit_ivar("module", str);
|
2006-10-23 02:24:14 +04:00
|
|
|
v = r_entry(v, arg);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
1999-12-01 12:24:48 +03:00
|
|
|
}
|
2002-02-26 09:48:59 +03:00
|
|
|
break;
|
2000-04-10 09:48:43 +04:00
|
|
|
|
2000-03-07 11:37:59 +03:00
|
|
|
case TYPE_SYMBOL:
|
2009-08-08 01:18:33 +04:00
|
|
|
if (ivp) {
|
2014-07-28 12:15:42 +04:00
|
|
|
v = r_symreal(arg, *ivp);
|
2009-08-08 01:18:33 +04:00
|
|
|
*ivp = FALSE;
|
|
|
|
}
|
|
|
|
else {
|
2014-07-28 12:15:42 +04:00
|
|
|
v = r_symreal(arg, 0);
|
2009-08-08 01:18:33 +04:00
|
|
|
}
|
2015-03-22 20:03:31 +03:00
|
|
|
v = rb_str_intern(v);
|
2021-09-30 17:50:31 +03:00
|
|
|
v = r_leave(v, arg, partial);
|
2002-02-27 07:52:21 +03:00
|
|
|
break;
|
2000-04-10 09:48:43 +04:00
|
|
|
|
|
|
|
case TYPE_SYMLINK:
|
2015-03-22 20:03:31 +03:00
|
|
|
v = rb_str_intern(r_symlink(arg));
|
2006-10-28 02:57:19 +04:00
|
|
|
break;
|
1999-12-01 12:24:48 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "dump format error(0x%x)", type);
|
1998-01-16 15:19:09 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-03-17 01:18:12 +03:00
|
|
|
|
2022-11-15 07:24:08 +03:00
|
|
|
if (UNDEF_P(v)) {
|
2016-03-17 01:18:12 +03:00
|
|
|
rb_raise(rb_eArgError, "dump format error (bad link)");
|
|
|
|
}
|
|
|
|
|
2002-02-26 09:48:59 +03:00
|
|
|
return v;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2002-08-28 19:58:35 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
r_object(struct load_arg *arg)
|
2002-08-28 19:58:35 +04:00
|
|
|
{
|
2021-09-30 17:50:31 +03:00
|
|
|
return r_object0(arg, false, 0, Qnil);
|
2002-08-28 19:58:35 +04:00
|
|
|
}
|
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
static void
|
|
|
|
clear_load_arg(struct load_arg *arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
2012-11-20 19:17:15 +04:00
|
|
|
if (arg->buf) {
|
|
|
|
xfree(arg->buf);
|
|
|
|
arg->buf = 0;
|
|
|
|
}
|
|
|
|
arg->buflen = 0;
|
|
|
|
arg->offset = 0;
|
|
|
|
arg->readable = 0;
|
2009-10-04 14:30:56 +04:00
|
|
|
if (!arg->symbols) return;
|
2003-08-07 01:50:06 +04:00
|
|
|
st_free_table(arg->symbols);
|
2009-10-04 14:30:56 +04:00
|
|
|
arg->symbols = 0;
|
2008-09-02 06:47:38 +04:00
|
|
|
st_free_table(arg->data);
|
2009-10-07 11:42:30 +04:00
|
|
|
arg->data = 0;
|
2021-09-30 17:50:31 +03:00
|
|
|
st_free_table(arg->partial_objects);
|
|
|
|
arg->partial_objects = 0;
|
2014-10-02 00:29:46 +04:00
|
|
|
if (arg->compat_tbl) {
|
|
|
|
st_free_table(arg->compat_tbl);
|
|
|
|
arg->compat_tbl = 0;
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2015-09-25 16:14:16 +03:00
|
|
|
VALUE
|
2021-09-18 16:34:15 +03:00
|
|
|
rb_marshal_load_with_proc(VALUE port, VALUE proc, bool freeze)
|
2015-09-25 16:14:16 +03:00
|
|
|
{
|
2019-09-25 06:59:12 +03:00
|
|
|
int major, minor;
|
2009-10-10 19:14:40 +04:00
|
|
|
VALUE v;
|
2017-02-13 11:14:19 +03:00
|
|
|
VALUE wrapper; /* used to avoid memory leak in case of exception */
|
2009-10-04 14:30:56 +04:00
|
|
|
struct load_arg *arg;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2008-05-28 07:52:44 +04:00
|
|
|
v = rb_check_string_type(port);
|
|
|
|
if (!NIL_P(v)) {
|
|
|
|
port = v;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2008-06-18 07:05:33 +04:00
|
|
|
else if (rb_respond_to(port, s_getbyte) && rb_respond_to(port, s_read)) {
|
2012-12-04 11:23:25 +04:00
|
|
|
rb_check_funcall(port, s_binmode, 0, 0);
|
2002-10-17 14:20:52 +04:00
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
else {
|
2012-06-22 09:22:46 +04:00
|
|
|
io_needed();
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2017-02-15 03:42:51 +03:00
|
|
|
wrapper = TypedData_Make_Struct(0, struct load_arg, &load_arg_data, arg);
|
2009-10-04 14:30:56 +04:00
|
|
|
arg->src = port;
|
|
|
|
arg->offset = 0;
|
|
|
|
arg->symbols = st_init_numtable();
|
2015-01-23 14:01:02 +03:00
|
|
|
arg->data = rb_init_identtable();
|
2021-09-30 17:50:31 +03:00
|
|
|
arg->partial_objects = rb_init_identtable();
|
2014-10-02 00:29:46 +04:00
|
|
|
arg->compat_tbl = 0;
|
2009-10-04 14:30:56 +04:00
|
|
|
arg->proc = 0;
|
2012-11-20 19:17:15 +04:00
|
|
|
arg->readable = 0;
|
2021-09-18 16:34:15 +03:00
|
|
|
arg->freeze = freeze;
|
2012-11-20 19:17:15 +04:00
|
|
|
|
|
|
|
if (NIL_P(v))
|
|
|
|
arg->buf = xmalloc(BUFSIZ);
|
|
|
|
else
|
|
|
|
arg->buf = 0;
|
2009-10-04 14:30:56 +04:00
|
|
|
|
|
|
|
major = r_byte(arg);
|
|
|
|
minor = r_byte(arg);
|
2000-11-21 17:31:11 +03:00
|
|
|
if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
|
2009-10-04 14:30:56 +04:00
|
|
|
clear_load_arg(arg);
|
2000-11-20 10:31:55 +03:00
|
|
|
rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
|
|
|
|
\tformat version %d.%d required; %d.%d given",
|
|
|
|
MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2001-07-31 10:24:45 +04:00
|
|
|
if (RTEST(ruby_verbose) && minor != MARSHAL_MINOR) {
|
2000-11-20 10:31:55 +03:00
|
|
|
rb_warn("incompatible marshal file format (can be read)\n\
|
|
|
|
\tformat version %d.%d required; %d.%d given",
|
|
|
|
MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
2009-10-04 14:30:56 +04:00
|
|
|
if (!NIL_P(proc)) arg->proc = proc;
|
|
|
|
v = r_object(arg);
|
2017-02-15 11:38:01 +03:00
|
|
|
clear_load_arg(arg);
|
|
|
|
RB_GC_GUARD(wrapper);
|
2000-11-20 10:31:55 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2022-08-06 04:13:20 +03:00
|
|
|
static VALUE
|
|
|
|
marshal_load(rb_execution_context_t *ec, VALUE mod, VALUE source, VALUE proc, VALUE freeze)
|
2021-09-18 16:34:15 +03:00
|
|
|
{
|
|
|
|
return rb_marshal_load_with_proc(source, proc, RTEST(freeze));
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "marshal.rbinc"
|
|
|
|
|
2003-12-27 19:07:43 +03:00
|
|
|
/*
|
|
|
|
* The marshaling library converts collections of Ruby objects into a
|
|
|
|
* byte stream, allowing them to be stored outside the currently
|
|
|
|
* active script. This data may subsequently be read and the original
|
|
|
|
* objects reconstituted.
|
2010-05-26 06:50:58 +04:00
|
|
|
*
|
2003-12-27 19:07:43 +03:00
|
|
|
* Marshaled data has major and minor version numbers stored along
|
|
|
|
* with the object information. In normal use, marshaling can only
|
|
|
|
* load data written with the same major version number and an equal
|
|
|
|
* or lower minor version number. If Ruby's ``verbose'' flag is set
|
|
|
|
* (normally using -d, -v, -w, or --verbose) the major and minor
|
|
|
|
* numbers must match exactly. Marshal versioning is independent of
|
|
|
|
* Ruby's version numbers. You can extract the version by reading the
|
|
|
|
* first two bytes of marshaled data.
|
|
|
|
*
|
|
|
|
* str = Marshal.dump("thing")
|
2008-03-09 04:04:46 +03:00
|
|
|
* RUBY_VERSION #=> "1.9.0"
|
|
|
|
* str[0].ord #=> 4
|
|
|
|
* str[1].ord #=> 8
|
2003-12-27 19:07:43 +03:00
|
|
|
*
|
|
|
|
* Some objects cannot be dumped: if the objects to be dumped include
|
|
|
|
* bindings, procedure or method objects, instances of class IO, or
|
|
|
|
* singleton objects, a TypeError will be raised.
|
2010-05-26 06:50:58 +04:00
|
|
|
*
|
2003-12-27 19:07:43 +03:00
|
|
|
* If your class has special serialization needs (for example, if you
|
|
|
|
* want to serialize in some specific format), or if it contains
|
|
|
|
* objects that would otherwise not be serializable, you can implement
|
2010-05-26 06:50:58 +04:00
|
|
|
* your own serialization strategy.
|
|
|
|
*
|
|
|
|
* There are two methods of doing this, your object can define either
|
|
|
|
* marshal_dump and marshal_load or _dump and _load. marshal_dump will take
|
|
|
|
* precedence over _dump if both are defined. marshal_dump may result in
|
|
|
|
* smaller Marshal strings.
|
|
|
|
*
|
2013-02-01 19:46:10 +04:00
|
|
|
* == Security considerations
|
|
|
|
*
|
|
|
|
* By design, Marshal.load can deserialize almost any class loaded into the
|
|
|
|
* Ruby process. In many cases this can lead to remote code execution if the
|
|
|
|
* Marshal data is loaded from an untrusted source.
|
|
|
|
*
|
|
|
|
* As a result, Marshal.load is not suitable as a general purpose serialization
|
|
|
|
* format and you should never unmarshal user supplied input or other untrusted
|
|
|
|
* data.
|
|
|
|
*
|
|
|
|
* If you need to deserialize untrusted data, use JSON or another serialization
|
|
|
|
* format that is only able to load simple, 'primitive' types such as String,
|
|
|
|
* Array, Hash, etc. Never allow user input to specify arbitrary types to
|
|
|
|
* deserialize into.
|
|
|
|
*
|
2010-05-26 06:50:58 +04:00
|
|
|
* == marshal_dump and marshal_load
|
|
|
|
*
|
|
|
|
* When dumping an object the method marshal_dump will be called.
|
|
|
|
* marshal_dump must return a result containing the information necessary for
|
|
|
|
* marshal_load to reconstitute the object. The result can be any object.
|
|
|
|
*
|
|
|
|
* When loading an object dumped using marshal_dump the object is first
|
|
|
|
* allocated then marshal_load is called with the result from marshal_dump.
|
|
|
|
* marshal_load must recreate the object from the information in the result.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* class MyObj
|
|
|
|
* def initialize name, version, data
|
|
|
|
* @name = name
|
|
|
|
* @version = version
|
|
|
|
* @data = data
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* def marshal_dump
|
|
|
|
* [@name, @version]
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* def marshal_load array
|
|
|
|
* @name, @version = array
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* == _dump and _load
|
|
|
|
*
|
|
|
|
* Use _dump and _load when you need to allocate the object you're restoring
|
|
|
|
* yourself.
|
|
|
|
*
|
|
|
|
* When dumping an object the instance method _dump is called with an Integer
|
|
|
|
* which indicates the maximum depth of objects to dump (a value of -1 implies
|
|
|
|
* that you should disable depth checking). _dump must return a String
|
|
|
|
* containing the information necessary to reconstitute the object.
|
|
|
|
*
|
|
|
|
* The class method _load should take a String and use it to return an object
|
|
|
|
* of the same class.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* class MyObj
|
|
|
|
* def initialize name, version, data
|
|
|
|
* @name = name
|
|
|
|
* @version = version
|
|
|
|
* @data = data
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* def _dump level
|
|
|
|
* [@name, @version].join ':'
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* def self._load args
|
|
|
|
* new(*args.split(':'))
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
2014-02-13 10:59:33 +04:00
|
|
|
* Since Marshal.dump outputs a string you can have _dump return a Marshal
|
2010-05-26 06:50:58 +04:00
|
|
|
* string which is Marshal.loaded in _load for complex objects.
|
2003-12-27 19:07:43 +03:00
|
|
|
*/
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
Init_marshal(void)
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_mMarshal = rb_define_module("Marshal");
|
2014-11-24 06:43:59 +03:00
|
|
|
#define set_id(sym) sym = rb_intern_const(name_##sym)
|
|
|
|
set_id(s_dump);
|
|
|
|
set_id(s_load);
|
|
|
|
set_id(s_mdump);
|
|
|
|
set_id(s_mload);
|
|
|
|
set_id(s_dump_data);
|
|
|
|
set_id(s_load_data);
|
|
|
|
set_id(s_alloc);
|
|
|
|
set_id(s_call);
|
|
|
|
set_id(s_getbyte);
|
|
|
|
set_id(s_read);
|
|
|
|
set_id(s_write);
|
|
|
|
set_id(s_binmode);
|
2019-08-10 05:38:49 +03:00
|
|
|
set_id(s_encoding_short);
|
2020-01-11 09:04:44 +03:00
|
|
|
set_id(s_ruby2_keywords_flag);
|
2002-10-17 14:20:52 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
|
2001-07-31 10:24:45 +04:00
|
|
|
|
2013-06-02 07:12:04 +04:00
|
|
|
/* major version */
|
2001-07-31 12:33:17 +04:00
|
|
|
rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
|
2013-06-02 07:12:04 +04:00
|
|
|
/* minor version */
|
2001-07-31 12:33:17 +04:00
|
|
|
rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
|
2015-12-02 10:27:22 +03:00
|
|
|
}
|
2007-09-08 19:07:18 +04:00
|
|
|
|
2015-12-02 10:27:22 +03:00
|
|
|
static st_table *
|
|
|
|
compat_allocator_table(void)
|
|
|
|
{
|
|
|
|
if (compat_allocator_tbl) return compat_allocator_tbl;
|
2007-09-08 19:07:18 +04:00
|
|
|
compat_allocator_tbl = st_init_numtable();
|
2014-09-28 19:27:24 +04:00
|
|
|
#undef RUBY_UNTYPED_DATA_WARNING
|
|
|
|
#define RUBY_UNTYPED_DATA_WARNING 0
|
2007-09-26 23:12:04 +04:00
|
|
|
compat_allocator_tbl_wrapper =
|
2017-12-04 10:37:21 +03:00
|
|
|
Data_Wrap_Struct(0, mark_marshal_compat_t, 0, compat_allocator_tbl);
|
* gc.c, include/ruby/ruby.h: rename rb_register_mark_object()
to rb_gc_register_mark_object().
* eval.c, vm.c: initialize vm->mark_object_ary at
Init_top_self().
* bignum.c, complex.c, encoding.c, ext/win32ole/win32ole.c,
io.c, load.c, marshal.c, rational.c, ruby.c, vm.c:
use rb_gc_register_mark_object() instead of
rb_global_variable() or rb_gc_register_address().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19365 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-15 18:59:14 +04:00
|
|
|
rb_gc_register_mark_object(compat_allocator_tbl_wrapper);
|
2015-12-02 10:27:22 +03:00
|
|
|
return compat_allocator_tbl;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2001-07-03 11:29:00 +04:00
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_marshal_dump(VALUE obj, VALUE port)
|
2001-07-03 11:29:00 +04:00
|
|
|
{
|
2015-09-25 16:14:16 +03:00
|
|
|
return rb_marshal_dump_limited(obj, port, -1);
|
2001-07-03 11:29:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_marshal_load(VALUE port)
|
2001-07-03 11:29:00 +04:00
|
|
|
{
|
2021-09-18 16:34:15 +03:00
|
|
|
return rb_marshal_load_with_proc(port, Qnil, false);
|
2001-07-03 11:29:00 +04:00
|
|
|
}
|