1998-01-16 15:19:09 +03:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
marshal.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Thu Apr 27 16:30:01 JST 1995
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
1999-01-20 07:59:39 +03:00
|
|
|
#include "rubyio.h"
|
1998-01-16 15:19:09 +03:00
|
|
|
#include "st.h"
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifndef atof
|
|
|
|
double strtod();
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
#define MARSHAL_MAJOR 4
|
2000-03-07 11:37:59 +03:00
|
|
|
#define MARSHAL_MINOR 4
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
#define TYPE_NIL '0'
|
|
|
|
#define TYPE_TRUE 'T'
|
|
|
|
#define TYPE_FALSE 'F'
|
|
|
|
#define TYPE_FIXNUM 'i'
|
|
|
|
|
|
|
|
#define TYPE_UCLASS 'C'
|
|
|
|
#define TYPE_OBJECT 'o'
|
|
|
|
#define TYPE_USERDEF 'u'
|
|
|
|
#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 '@'
|
|
|
|
|
|
|
|
static ID s_dump, s_load;
|
|
|
|
|
|
|
|
struct dump_arg {
|
|
|
|
VALUE obj;
|
|
|
|
FILE *fp;
|
|
|
|
VALUE str;
|
|
|
|
st_table *symbol;
|
|
|
|
st_table *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dump_call_arg {
|
|
|
|
VALUE obj;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
int limit;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void w_long _((long, struct dump_arg*));
|
|
|
|
|
|
|
|
static void
|
|
|
|
w_byte(c, arg)
|
|
|
|
char c;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
if (arg->fp) putc(c, arg->fp);
|
1999-01-20 07:59:39 +03:00
|
|
|
else rb_str_cat(arg->str, &c, 1);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w_bytes(s, n, arg)
|
|
|
|
char *s;
|
|
|
|
int n;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
w_long(n, arg);
|
|
|
|
if (arg->fp) {
|
|
|
|
fwrite(s, 1, n, arg->fp);
|
|
|
|
}
|
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_str_cat(arg->str, s, n);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w_short(x, arg)
|
|
|
|
int x;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
for (i=0; i<sizeof(short); i++) {
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte((x >> (i*8)) & 0xff, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w_long(x, arg)
|
|
|
|
long x;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
char buf[sizeof(long)+1];
|
|
|
|
int i, len = 0;
|
|
|
|
|
|
|
|
if (x == 0) {
|
|
|
|
w_byte(0, arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i=1;i<sizeof(long)+1;i++) {
|
|
|
|
buf[i] = x & 0xff;
|
|
|
|
x = RSHIFT(x,8);
|
|
|
|
if (x == 0) {
|
|
|
|
buf[0] = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (x == -1) {
|
|
|
|
buf[0] = -i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
len = i;
|
|
|
|
for (i=0;i<=len;i++) {
|
|
|
|
w_byte(buf[i], arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w_float(d, arg)
|
|
|
|
double d;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
sprintf(buf, "%.12g", d);
|
|
|
|
w_bytes(buf, strlen(buf), arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w_symbol(id, arg)
|
|
|
|
ID id;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
char *sym = rb_id2name(id);
|
|
|
|
int num;
|
|
|
|
|
|
|
|
if (st_lookup(arg->symbol, id, &num)) {
|
|
|
|
w_byte(TYPE_SYMLINK, arg);
|
|
|
|
w_long(num, arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
w_byte(TYPE_SYMBOL, arg);
|
|
|
|
w_bytes(sym, strlen(sym), arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
st_add_direct(arg->symbol, id, arg->symbol->num_entries);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
w_unique(s, arg)
|
|
|
|
char *s;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
w_symbol(rb_intern(s), arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void w_object _((VALUE,struct dump_arg*,int));
|
|
|
|
|
|
|
|
static int
|
1999-08-13 09:45:20 +04:00
|
|
|
hash_each(key, value, arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
VALUE key, value;
|
|
|
|
struct dump_call_arg *arg;
|
|
|
|
{
|
|
|
|
w_object(key, arg->arg, arg->limit);
|
|
|
|
w_object(value, arg->arg, arg->limit);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-08-13 09:45:20 +04:00
|
|
|
obj_each(id, value, arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
ID id;
|
|
|
|
VALUE value;
|
|
|
|
struct dump_call_arg *arg;
|
|
|
|
{
|
|
|
|
w_symbol(id, arg->arg);
|
|
|
|
w_object(value, arg->arg, arg->limit);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-01-20 07:59:39 +03:00
|
|
|
w_uclass(obj, klass, arg)
|
|
|
|
VALUE obj, klass;
|
1998-01-16 15:19:09 +03:00
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (CLASS_OF(obj) != klass) {
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_UCLASS, arg);
|
|
|
|
w_unique(rb_class2name(CLASS_OF(obj)), arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
static void
|
|
|
|
w_ivar(tbl, arg)
|
|
|
|
st_table *tbl;
|
|
|
|
struct dump_call_arg *arg;
|
|
|
|
{
|
|
|
|
struct dump_call_arg c_arg;
|
|
|
|
|
|
|
|
if (tbl) {
|
|
|
|
w_long(tbl->num_entries, arg->arg);
|
|
|
|
st_foreach(tbl, obj_each, arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
w_long(0, arg->arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static void
|
|
|
|
w_object(obj, arg, limit)
|
|
|
|
VALUE obj;
|
|
|
|
struct dump_arg *arg;
|
|
|
|
int limit;
|
|
|
|
{
|
|
|
|
struct dump_call_arg c_arg;
|
2000-01-17 11:37:53 +03:00
|
|
|
st_table *ivtbl = 0;
|
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
|
|
|
}
|
|
|
|
if (obj == Qnil) {
|
|
|
|
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
|
1999-01-20 07:59:39 +03:00
|
|
|
if (RSHIFT((long)obj, 32) == 0 || RSHIFT((long)obj, 32) == -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 {
|
1999-01-20 07:59:39 +03:00
|
|
|
w_object(rb_int2big(FIX2LONG(obj)), arg, limit);
|
|
|
|
return;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2000-03-07 11:37:59 +03:00
|
|
|
else if (SYMBOL_P(obj)) {
|
2000-04-10 09:48:43 +04:00
|
|
|
w_symbol(SYM2ID(obj), arg);
|
2000-03-07 11:37:59 +03:00
|
|
|
return;
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
else {
|
|
|
|
int num;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
limit--;
|
|
|
|
c_arg.limit = limit;
|
|
|
|
c_arg.arg = arg;
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
if (st_lookup(arg->data, obj, &num)) {
|
|
|
|
w_byte(TYPE_LINK, arg);
|
|
|
|
w_long(num, arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
st_add_direct(arg->data, obj, arg->data->num_entries);
|
1998-01-16 15:19:09 +03:00
|
|
|
if (rb_respond_to(obj, s_dump)) {
|
|
|
|
VALUE v;
|
|
|
|
|
|
|
|
w_byte(TYPE_USERDEF, arg);
|
|
|
|
w_unique(rb_class2name(CLASS_OF(obj)), arg);
|
1999-08-13 09:45:20 +04:00
|
|
|
v = rb_funcall(obj, s_dump, 1, INT2NUM(limit));
|
1998-01-16 15:19:09 +03:00
|
|
|
if (TYPE(v) != T_STRING) {
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eTypeError, "_dump() must return String");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
w_bytes(RSTRING(v)->ptr, RSTRING(v)->len, arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
if (ivtbl = rb_generic_ivar_table(obj)) {
|
|
|
|
w_byte(TYPE_IVAR, arg);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
|
|
|
case T_CLASS:
|
1999-12-01 12:24:48 +03:00
|
|
|
w_byte(TYPE_CLASS, arg);
|
|
|
|
{
|
|
|
|
VALUE path = rb_class_path(obj);
|
|
|
|
w_bytes(RSTRING(path)->ptr, RSTRING(path)->len, arg);
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
1999-12-01 12:24:48 +03:00
|
|
|
|
|
|
|
case T_MODULE:
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_MODULE, arg);
|
|
|
|
{
|
|
|
|
VALUE path = rb_class_path(obj);
|
|
|
|
w_bytes(RSTRING(path)->ptr, RSTRING(path)->len, arg);
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
w_byte(TYPE_FLOAT, arg);
|
|
|
|
w_float(RFLOAT(obj)->value, arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case T_BIGNUM:
|
|
|
|
w_byte(TYPE_BIGNUM, arg);
|
|
|
|
{
|
|
|
|
char sign = RBIGNUM(obj)->sign?'+':'-';
|
|
|
|
int len = RBIGNUM(obj)->len;
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned short *d = RBIGNUM(obj)->digits;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
w_byte(sign, arg);
|
|
|
|
w_long(len, arg);
|
|
|
|
while (len--) {
|
|
|
|
w_short(*d, arg);
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
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);
|
|
|
|
w_bytes(RSTRING(obj)->ptr, RSTRING(obj)->len, arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case T_REGEXP:
|
1999-01-20 07:59:39 +03:00
|
|
|
w_uclass(obj, rb_cRegexp, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
w_byte(TYPE_REGEXP, arg);
|
|
|
|
w_bytes(RREGEXP(obj)->str, RREGEXP(obj)->len, arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
w_byte(rb_reg_options(obj), arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
break;
|
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);
|
|
|
|
{
|
|
|
|
int len = RARRAY(obj)->len;
|
|
|
|
VALUE *ptr = RARRAY(obj)->ptr;
|
|
|
|
|
|
|
|
w_long(len, arg);
|
|
|
|
while (len--) {
|
|
|
|
w_object(*ptr, arg, limit);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_HASH:
|
1999-01-20 07:59:39 +03:00
|
|
|
w_uclass(obj, rb_cHash, arg);
|
1999-12-01 12:24:48 +03:00
|
|
|
if (!NIL_P(RHASH(obj)->ifnone)) {
|
|
|
|
w_byte(TYPE_HASH_DEF, arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
w_byte(TYPE_HASH, arg);
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
w_long(RHASH(obj)->tbl->num_entries, arg);
|
1999-08-13 09:45:20 +04:00
|
|
|
st_foreach(RHASH(obj)->tbl, hash_each, &c_arg);
|
1999-12-01 12:24:48 +03:00
|
|
|
if (!NIL_P(RHASH(obj)->ifnone)) {
|
|
|
|
w_object(RHASH(obj)->ifnone, arg, limit);
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRUCT:
|
|
|
|
w_byte(TYPE_STRUCT, arg);
|
|
|
|
{
|
|
|
|
int len = RSTRUCT(obj)->len;
|
|
|
|
char *path = rb_class2name(CLASS_OF(obj));
|
|
|
|
VALUE mem;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
w_unique(path, arg);
|
|
|
|
w_long(len, arg);
|
|
|
|
mem = rb_ivar_get(CLASS_OF(obj), rb_intern("__member__"));
|
|
|
|
if (mem == Qnil) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "non-initialized struct");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
for (i=0; i<len; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
w_symbol(FIX2LONG(RARRAY(mem)->ptr[i]), arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
w_object(RSTRUCT(obj)->ptr[i], arg, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OBJECT:
|
|
|
|
w_byte(TYPE_OBJECT, arg);
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass = CLASS_OF(obj);
|
1998-01-16 15:19:09 +03:00
|
|
|
char *path;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FL_TEST(klass, FL_SINGLETON)) {
|
|
|
|
rb_raise(rb_eTypeError, "singleton can't be dumped");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
path = rb_class2name(klass);
|
1998-01-16 15:19:09 +03:00
|
|
|
w_unique(path, arg);
|
2000-01-17 11:37:53 +03:00
|
|
|
w_ivar(ROBJECT(obj)->iv_tbl, &c_arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "can't dump %s",
|
|
|
|
rb_class2name(CLASS_OF(obj)));
|
1998-01-16 15:19:09 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
if (ivtbl) {
|
|
|
|
w_ivar(ivtbl, &c_arg);
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump(arg)
|
|
|
|
struct dump_call_arg *arg;
|
|
|
|
{
|
|
|
|
w_object(arg->obj, arg->arg, arg->limit);
|
1999-01-20 07:59:39 +03:00
|
|
|
return 0;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump_ensure(arg)
|
|
|
|
struct dump_arg *arg;
|
|
|
|
{
|
|
|
|
st_free_table(arg->symbol);
|
|
|
|
st_free_table(arg->data);
|
1999-01-20 07:59:39 +03:00
|
|
|
return 0;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
marshal_dump(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE* argv;
|
|
|
|
{
|
|
|
|
VALUE obj, port, a1, a2;
|
|
|
|
int limit = -1;
|
|
|
|
struct dump_arg arg;
|
|
|
|
struct dump_call_arg c_arg;
|
|
|
|
|
|
|
|
port = 0;
|
|
|
|
rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
|
|
|
|
if (argc == 3) {
|
|
|
|
limit = NUM2INT(a2);
|
|
|
|
port = a1;
|
|
|
|
}
|
|
|
|
else if (argc == 2) {
|
|
|
|
if (FIXNUM_P(a1)) limit = FIX2INT(a1);
|
|
|
|
else port = a1;
|
|
|
|
}
|
|
|
|
if (port) {
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_obj_is_kind_of(port, rb_cIO)) {
|
1998-01-16 15:19:09 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_binmode(port);
|
1998-01-16 15:19:09 +03:00
|
|
|
GetOpenFile(port, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_writable(fptr);
|
1998-01-16 15:19:09 +03:00
|
|
|
arg.fp = (fptr->f2) ? fptr->f2 : fptr->f;
|
|
|
|
}
|
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "instance of IO needed");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arg.fp = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
port = rb_str_new(0, 0);
|
1998-01-16 15:19:09 +03:00
|
|
|
arg.str = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg.symbol = st_init_numtable();
|
|
|
|
arg.data = st_init_numtable();
|
|
|
|
c_arg.obj = obj;
|
|
|
|
c_arg.arg = &arg;
|
|
|
|
c_arg.limit = limit;
|
|
|
|
|
|
|
|
w_byte(MARSHAL_MAJOR, &arg);
|
|
|
|
w_byte(MARSHAL_MINOR, &arg);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ensure(dump, (VALUE)&c_arg, dump_ensure, (VALUE)&arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct load_arg {
|
|
|
|
FILE *fp;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *ptr, *end;
|
1998-01-16 15:19:09 +03:00
|
|
|
st_table *symbol;
|
1999-10-20 11:10:23 +04:00
|
|
|
VALUE data;
|
1998-01-16 15:19:09 +03:00
|
|
|
VALUE proc;
|
|
|
|
};
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
static VALUE r_object _((struct load_arg *arg));
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static int
|
|
|
|
r_byte(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (arg->fp) {
|
|
|
|
c = rb_getc(arg->fp);
|
|
|
|
if (c == EOF) rb_eof_error();
|
|
|
|
}
|
|
|
|
else if (arg->ptr < arg->end) {
|
|
|
|
c = *(unsigned char*)arg->ptr++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "marshal data too short");
|
|
|
|
}
|
|
|
|
return c;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static unsigned short
|
1998-01-16 15:19:09 +03:00
|
|
|
r_short(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned short x;
|
1998-01-16 15:19:09 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
x = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
for (i=0; i<sizeof(short); i++) {
|
1998-01-16 15:19:09 +03:00
|
|
|
x |= r_byte(arg)<<(i*8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
long_toobig(size)
|
|
|
|
int size;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "long too big for this architecture (size %d, given %d)",
|
|
|
|
sizeof(long), size);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
r_long(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
register long x;
|
1999-01-20 07:59:39 +03:00
|
|
|
int c = (char)r_byte(arg);
|
|
|
|
int i;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
if (c == 0) return 0;
|
|
|
|
if (c > 0) {
|
|
|
|
if (c > sizeof(long)) long_toobig((int)c);
|
|
|
|
x = 0;
|
|
|
|
for (i=0;i<c;i++) {
|
|
|
|
x |= (long)r_byte(arg) << (8*i);
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
1998-01-16 15:19:09 +03:00
|
|
|
c = -c;
|
|
|
|
if (c > sizeof(long)) long_toobig((int)c);
|
|
|
|
x = -1;
|
|
|
|
for (i=0;i<c;i++) {
|
|
|
|
x &= ~(0xff << (8*i));
|
|
|
|
x |= (long)r_byte(arg) << (8*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define r_bytes2(s, len, arg) do { \
|
|
|
|
(len) = r_long(arg); \
|
|
|
|
(s) = ALLOCA_N(char,(len)+1); \
|
|
|
|
r_bytes0((s),(len),(arg)); \
|
|
|
|
} while (0)
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define r_bytes(s, arg) do { \
|
|
|
|
int r_bytes_len; \
|
|
|
|
r_bytes2((s), r_bytes_len, (arg)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static void
|
|
|
|
r_bytes0(s, len, arg)
|
|
|
|
char *s;
|
1998-01-16 15:19:09 +03:00
|
|
|
int len;
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
if (arg->fp) {
|
|
|
|
len = fread(s, 1, len, arg->fp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (arg->ptr + len > arg->end) {
|
|
|
|
len = arg->end - arg->ptr;
|
|
|
|
}
|
|
|
|
memcpy(s, arg->ptr, len);
|
|
|
|
arg->ptr += len;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
s[len] = '\0';
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static ID
|
2000-04-10 09:48:43 +04:00
|
|
|
r_symlink(arg)
|
1998-01-16 15:19:09 +03:00
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
ID id;
|
2000-04-10 09:48:43 +04:00
|
|
|
int num = r_long(arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2000-04-10 09:48:43 +04:00
|
|
|
if (st_lookup(arg->symbol, num, &id)) {
|
|
|
|
return id;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
2000-04-10 09:48:43 +04:00
|
|
|
rb_raise(rb_eTypeError, "bad symbol");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ID
|
|
|
|
r_symreal(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
ID id;
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
r_bytes(buf, arg);
|
|
|
|
id = rb_intern(buf);
|
|
|
|
st_insert(arg->symbol, arg->symbol->num_entries, id);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2000-04-10 09:48:43 +04:00
|
|
|
static ID
|
|
|
|
r_symbol(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
if (r_byte(arg) == TYPE_SYMLINK) {
|
|
|
|
return r_symlink(arg);
|
|
|
|
}
|
|
|
|
return r_symreal(arg);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static char*
|
|
|
|
r_unique(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
return rb_id2name(r_symbol(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
r_string(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
char *buf;
|
1999-01-20 07:59:39 +03:00
|
|
|
int len;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
r_bytes2(buf, len, arg);
|
|
|
|
return rb_str_new(buf, len);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
r_regist(v, arg)
|
|
|
|
VALUE v;
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJ_TAINT(v);
|
1998-01-16 15:19:09 +03:00
|
|
|
if (arg->proc) {
|
|
|
|
rb_funcall(arg->proc, rb_intern("call"), 1, v);
|
|
|
|
}
|
1999-10-20 11:10:23 +04:00
|
|
|
rb_hash_aset(arg->data, INT2FIX(RHASH(arg->data)->tbl->num_entries), v);
|
1998-01-16 15:19:09 +03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
static void
|
|
|
|
r_ivar(obj, arg)
|
|
|
|
VALUE obj;
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = r_long(arg);
|
|
|
|
if (len > 0) {
|
|
|
|
while (len--) {
|
|
|
|
ID id = r_symbol(arg);
|
|
|
|
VALUE val = r_object(arg);
|
|
|
|
rb_ivar_set(obj, id, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
static VALUE
|
|
|
|
r_object(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
VALUE v;
|
|
|
|
int type = r_byte(arg);
|
1999-10-20 11:10:23 +04:00
|
|
|
long id;
|
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);
|
1999-10-27 08:20:00 +04:00
|
|
|
v = rb_hash_aref(arg->data, INT2FIX(id));
|
|
|
|
if (NIL_P(v)) {
|
|
|
|
rb_raise(rb_eArgError, "dump format error (unlinked)");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
1999-10-27 08:20:00 +04:00
|
|
|
return v;
|
1999-10-20 11:10:23 +04:00
|
|
|
break;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
case TYPE_IVAR:
|
|
|
|
v = r_object(arg);
|
|
|
|
r_ivar(v, arg);
|
|
|
|
return v;
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
case TYPE_UCLASS:
|
|
|
|
{
|
|
|
|
VALUE c = rb_path2class(r_unique(arg));
|
|
|
|
v = r_object(arg);
|
|
|
|
if (rb_special_const_p(v)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "dump format error (user class)");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
RBASIC(v)->klass = c;
|
1998-01-16 15:19:09 +03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_NIL:
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
case TYPE_TRUE:
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_FALSE:
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
case TYPE_FIXNUM:
|
|
|
|
{
|
|
|
|
int i = r_long(arg);
|
|
|
|
return INT2FIX(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_FLOAT:
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
r_bytes(buf, arg);
|
1999-08-13 09:45:20 +04:00
|
|
|
v = rb_float_new(strtod(buf, 0));
|
1998-01-16 15:19:09 +03:00
|
|
|
return r_regist(v, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_BIGNUM:
|
|
|
|
{
|
|
|
|
int len;
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned short *digits;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
NEWOBJ(big, struct RBignum);
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJSETUP(big, rb_cBignum, T_BIGNUM);
|
1998-01-16 15:19:09 +03:00
|
|
|
big->sign = (r_byte(arg) == '+');
|
|
|
|
big->len = len = r_long(arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
big->digits = digits = ALLOC_N(unsigned short, len);
|
1998-01-16 15:19:09 +03:00
|
|
|
while (len--) {
|
|
|
|
*digits++ = r_short(arg);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
big = RBIGNUM(rb_big_norm((VALUE)big));
|
1998-01-16 15:19:09 +03:00
|
|
|
if (TYPE(big) == T_BIGNUM) {
|
1999-01-20 07:59:39 +03:00
|
|
|
r_regist((VALUE)big, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
return (VALUE)big;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_STRING:
|
|
|
|
return r_regist(r_string(arg), arg);
|
|
|
|
|
|
|
|
case TYPE_REGEXP:
|
|
|
|
{
|
|
|
|
char *buf;
|
1999-01-20 07:59:39 +03:00
|
|
|
int len;
|
|
|
|
int options;
|
|
|
|
|
|
|
|
r_bytes2(buf, len, arg);
|
|
|
|
options = r_byte(arg);
|
|
|
|
return r_regist(rb_reg_new(buf, len, options), arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
{
|
1999-08-24 12:21:56 +04:00
|
|
|
volatile int len = r_long(arg); /* gcc 2.7.2.3 -O2 bug?? */
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
v = rb_ary_new2(len);
|
1998-01-16 15:19:09 +03:00
|
|
|
r_regist(v, arg);
|
|
|
|
while (len--) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(v, r_object(arg));
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_HASH:
|
1999-12-01 12:24:48 +03:00
|
|
|
case TYPE_HASH_DEF:
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
int len = r_long(arg);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
v = rb_hash_new();
|
1998-01-16 15:19:09 +03:00
|
|
|
r_regist(v, arg);
|
|
|
|
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);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
1999-12-02 09:58:52 +03:00
|
|
|
if (type == TYPE_HASH_DEF) {
|
1999-12-01 12:24:48 +03:00
|
|
|
RHASH(v)->ifnone = r_object(arg);
|
|
|
|
}
|
1998-01-16 15:19:09 +03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_STRUCT:
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass, mem, values;
|
1998-01-16 15:19:09 +03:00
|
|
|
volatile int i; /* gcc 2.7.2.3 -O2 bug?? */
|
|
|
|
int len;
|
|
|
|
ID slot;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
klass = rb_path2class(r_unique(arg));
|
|
|
|
mem = rb_ivar_get(klass, rb_intern("__member__"));
|
1998-01-16 15:19:09 +03:00
|
|
|
if (mem == Qnil) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "non-initialized struct");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
len = r_long(arg);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
values = rb_ary_new2(len);
|
1998-01-16 15:19:09 +03:00
|
|
|
for (i=0; i<len; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(values, Qnil);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
v = rb_struct_alloc(klass, values);
|
1998-01-16 15:19:09 +03:00
|
|
|
r_regist(v, arg);
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
slot = r_symbol(arg);
|
|
|
|
|
|
|
|
if (RARRAY(mem)->ptr[i] != INT2FIX(slot)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "struct %s not compatible (:%s for :%s)",
|
|
|
|
rb_class2name(klass),
|
|
|
|
rb_id2name(slot),
|
|
|
|
rb_id2name(FIX2INT(RARRAY(mem)->ptr[i])));
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_struct_aset(v, INT2FIX(i), r_object(arg));
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_USERDEF:
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
klass = rb_path2class(r_unique(arg));
|
|
|
|
if (rb_respond_to(klass, s_load)) {
|
|
|
|
v = rb_funcall(klass, s_load, 1, r_string(arg));
|
1998-01-16 15:19:09 +03:00
|
|
|
return r_regist(v, arg);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eTypeError, "class %s needs to have method `_load'",
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_class2name(klass));
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_OBJECT:
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass;
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
klass = rb_path2class(r_unique(arg));
|
|
|
|
v = rb_obj_alloc(klass);
|
1998-01-16 15:19:09 +03:00
|
|
|
r_regist(v, arg);
|
2000-01-05 07:41:21 +03:00
|
|
|
r_ivar(v, arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1999-12-01 12:24:48 +03:00
|
|
|
case TYPE_MODULE_OLD:
|
1998-01-16 15:19:09 +03:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
r_bytes(buf, arg);
|
1999-10-27 08:20:00 +04:00
|
|
|
return r_regist(rb_path2class(buf), arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
1999-12-01 12:24:48 +03:00
|
|
|
case TYPE_CLASS:
|
|
|
|
{
|
|
|
|
VALUE c;
|
|
|
|
|
|
|
|
char *buf;
|
|
|
|
r_bytes(buf, arg);
|
|
|
|
c = rb_path2class(buf);
|
|
|
|
if (TYPE(c) != T_CLASS) {
|
|
|
|
rb_raise(rb_eTypeError, "%s is not a class", buf);
|
|
|
|
}
|
|
|
|
return r_regist(c, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
case TYPE_MODULE:
|
|
|
|
{
|
|
|
|
VALUE m;
|
|
|
|
|
|
|
|
char *buf;
|
|
|
|
r_bytes(buf, arg);
|
|
|
|
m = rb_path2class(buf);
|
|
|
|
if (TYPE(m) != T_CLASS) {
|
|
|
|
rb_raise(rb_eTypeError, "%s is not a module", buf);
|
|
|
|
}
|
|
|
|
return r_regist(m, arg);
|
|
|
|
}
|
2000-04-10 09:48:43 +04:00
|
|
|
|
2000-03-07 11:37:59 +03:00
|
|
|
case TYPE_SYMBOL:
|
2000-04-10 09:48:43 +04:00
|
|
|
return ID2SYM(r_symreal(arg));
|
|
|
|
|
|
|
|
case TYPE_SYMLINK:
|
|
|
|
return ID2SYM(r_symlink(arg));
|
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;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
load(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
return r_object(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
load_ensure(arg)
|
|
|
|
struct load_arg *arg;
|
|
|
|
{
|
|
|
|
st_free_table(arg->symbol);
|
1999-01-20 07:59:39 +03:00
|
|
|
return 0;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
marshal_load(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE port, proc;
|
|
|
|
int major;
|
|
|
|
VALUE v;
|
|
|
|
OpenFile *fptr;
|
|
|
|
struct load_arg arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &port, &proc);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_obj_is_kind_of(port, rb_cIO)) {
|
|
|
|
rb_io_binmode(port);
|
|
|
|
GetOpenFile(port, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
arg.fp = fptr->f;
|
|
|
|
}
|
|
|
|
else if (rb_respond_to(port, rb_intern("to_str"))) {
|
|
|
|
int len;
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
arg.fp = 0;
|
2000-01-05 07:41:21 +03:00
|
|
|
arg.ptr = rb_str2cstr(port, &len);
|
1999-01-20 07:59:39 +03:00
|
|
|
arg.end = arg.ptr + len;
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "instance of IO needed");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
major = r_byte(&arg);
|
|
|
|
if (major == MARSHAL_MAJOR) {
|
1999-10-20 11:10:23 +04:00
|
|
|
volatile VALUE hash; /* protect from GC */
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
if (r_byte(&arg) != MARSHAL_MINOR) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_warn("Old marshal file format (can be read)");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
arg.symbol = st_init_numtable();
|
1999-10-20 11:10:23 +04:00
|
|
|
arg.data = hash = rb_hash_new();
|
1998-01-16 15:19:09 +03:00
|
|
|
if (NIL_P(proc)) arg.proc = 0;
|
|
|
|
else arg.proc = proc;
|
1999-01-20 07:59:39 +03:00
|
|
|
v = rb_ensure(load, (VALUE)&arg, load_ensure, (VALUE)&arg);
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eTypeError, "old marshal file format (can't read)");
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
1998-01-16 15:19:09 +03:00
|
|
|
Init_marshal()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_mMarshal = rb_define_module("Marshal");
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
s_dump = rb_intern("_dump");
|
|
|
|
s_load = rb_intern("_load");
|
|
|
|
rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
|
|
|
|
rb_define_module_function(rb_mMarshal, "load", marshal_load, -1);
|
|
|
|
rb_define_module_function(rb_mMarshal, "restore", marshal_load, 1);
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_provide("marshal.so"); /* for backward compatibility */
|
1998-01-16 15:19:09 +03:00
|
|
|
}
|