summaryrefslogtreecommitdiff
path: root/marshal.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
commit210367ec889f5910e270d6ea2c7ddb8a8d939e61 (patch)
treefeb35473da45947378fbc02defe39bcd79ef600e /marshal.c
parent9c5b1986a36c7a700b4c76817e35aa874ba7907c (diff)
This commit was generated by cvs2svn to compensate for changes in r372,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'marshal.c')
-rw-r--r--marshal.c289
1 files changed, 146 insertions, 143 deletions
diff --git a/marshal.c b/marshal.c
index ac95e43..6a68b04 100644
--- a/marshal.c
+++ b/marshal.c
@@ -3,14 +3,13 @@
marshal.c -
$Author$
- $Revision$
$Date$
created at: Thu Apr 27 16:30:01 JST 1995
************************************************/
#include "ruby.h"
-#include "io.h"
+#include "rubyio.h"
#include "st.h"
#define MARSHAL_MAJOR 4
@@ -38,12 +37,7 @@
#define TYPE_LINK '@'
-extern VALUE cString;
-extern VALUE cRegexp;
-extern VALUE cArray;
-extern VALUE cHash;
-
-VALUE rb_path2class();
+VALUE rb_path2class _((char*));
static ID s_dump, s_load;
@@ -69,7 +63,7 @@ w_byte(c, arg)
struct dump_arg *arg;
{
if (arg->fp) putc(c, arg->fp);
- else str_cat(arg->str, (UCHAR*)&c, 1);
+ else rb_str_cat(arg->str, &c, 1);
}
static void
@@ -83,7 +77,7 @@ w_bytes(s, n, arg)
fwrite(s, 1, n, arg->fp);
}
else {
- str_cat(arg->str, s, n);
+ rb_str_cat(arg->str, s, n);
}
}
@@ -94,7 +88,7 @@ w_short(x, arg)
{
int i;
- for (i=0; i<sizeof(USHORT); i++) {
+ for (i=0; i<sizeof(short); i++) {
w_byte((x >> (i*8)) & 0xff, arg);
}
}
@@ -155,7 +149,7 @@ w_symbol(id, arg)
else {
w_byte(TYPE_SYMBOL, arg);
w_bytes(sym, strlen(sym), arg);
- st_insert(arg->symbol, id, arg->symbol->num_entries);
+ st_add_direct(arg->symbol, id, arg->symbol->num_entries);
}
}
@@ -168,10 +162,9 @@ w_unique(s, arg)
}
static void w_object _((VALUE,struct dump_arg*,int));
-extern VALUE cIO, cBignum, cStruct;
static int
-hash_each(key, value, arg)
+rb_hash_each(key, value, arg)
VALUE key, value;
struct dump_call_arg *arg;
{
@@ -181,7 +174,7 @@ hash_each(key, value, arg)
}
static int
-obj_each(id, value, arg)
+rb_obj_each(id, value, arg)
ID id;
VALUE value;
struct dump_call_arg *arg;
@@ -192,11 +185,11 @@ obj_each(id, value, arg)
}
static void
-w_uclass(obj, class, arg)
- VALUE obj, class;
+w_uclass(obj, klass, arg)
+ VALUE obj, klass;
struct dump_arg *arg;
{
- if (CLASS_OF(obj) != class) {
+ if (CLASS_OF(obj) != klass) {
w_byte(TYPE_UCLASS, arg);
w_unique(rb_class2name(CLASS_OF(obj)), arg);
}
@@ -208,23 +201,18 @@ w_object(obj, arg, limit)
struct dump_arg *arg;
int limit;
{
- int n;
struct dump_call_arg c_arg;
if (limit == 0) {
- Fail("exceed depth limit");
+ rb_raise(rb_eRuntimeError, "exceed depth limit");
}
- limit--;
- c_arg.limit = limit;
- c_arg.arg = arg;
-
if (obj == Qnil) {
w_byte(TYPE_NIL, arg);
}
- else if (obj == TRUE) {
+ else if (obj == Qtrue) {
w_byte(TYPE_TRUE, arg);
}
- else if (obj == FALSE) {
+ else if (obj == Qfalse) {
w_byte(TYPE_FALSE, arg);
}
else if (FIXNUM_P(obj)) {
@@ -232,26 +220,30 @@ w_object(obj, arg, limit)
w_byte(TYPE_FIXNUM, arg);
w_long(FIX2INT(obj), arg);
#else
- if (RSHIFT(obj, 32) == 0 || RSHIFT(obj, 32) == -1) {
+ if (RSHIFT((long)obj, 32) == 0 || RSHIFT((long)obj, 32) == -1) {
w_byte(TYPE_FIXNUM, arg);
- w_long(FIX2INT(obj), arg);
+ w_long(FIX2LONG(obj), arg);
}
else {
- obj = int2big(FIX2INT(obj));
- goto write_bignum;
+ w_object(rb_int2big(FIX2LONG(obj)), arg, limit);
+ return;
}
#endif
}
else {
int num;
+ limit--;
+ c_arg.limit = limit;
+ c_arg.arg = arg;
+
if (st_lookup(arg->data, obj, &num)) {
w_byte(TYPE_LINK, arg);
w_long(num, arg);
return;
}
- st_insert(arg->data, obj, arg->data->num_entries);
+ st_add_direct(arg->data, obj, arg->data->num_entries);
if (rb_respond_to(obj, s_dump)) {
VALUE v;
@@ -259,7 +251,7 @@ w_object(obj, arg, limit)
w_unique(rb_class2name(CLASS_OF(obj)), arg);
v = rb_funcall(obj, s_dump, 1, limit);
if (TYPE(v) != T_STRING) {
- TypeError("_dump_to must return String");
+ rb_raise(rb_eTypeError, "_dump_to must return String");
}
w_bytes(RSTRING(v)->ptr, RSTRING(v)->len, arg);
return;
@@ -281,12 +273,11 @@ w_object(obj, arg, limit)
return;
case T_BIGNUM:
- write_bignum:
w_byte(TYPE_BIGNUM, arg);
{
char sign = RBIGNUM(obj)->sign?'+':'-';
int len = RBIGNUM(obj)->len;
- USHORT *d = RBIGNUM(obj)->digits;
+ unsigned short *d = RBIGNUM(obj)->digits;
w_byte(sign, arg);
w_long(len, arg);
@@ -298,20 +289,20 @@ w_object(obj, arg, limit)
return;
case T_STRING:
- w_uclass(obj, cString, arg);
+ w_uclass(obj, rb_cString, arg);
w_byte(TYPE_STRING, arg);
w_bytes(RSTRING(obj)->ptr, RSTRING(obj)->len, arg);
return;
case T_REGEXP:
- w_uclass(obj, cRegexp, arg);
+ w_uclass(obj, rb_cRegexp, arg);
w_byte(TYPE_REGEXP, arg);
w_bytes(RREGEXP(obj)->str, RREGEXP(obj)->len, arg);
- w_byte(FL_TEST(obj, FL_USER1), arg);
+ w_byte(rb_reg_options(obj), arg);
return;
case T_ARRAY:
- w_uclass(obj, cArray, arg);
+ w_uclass(obj, rb_cArray, arg);
w_byte(TYPE_ARRAY, arg);
{
int len = RARRAY(obj)->len;
@@ -326,10 +317,10 @@ w_object(obj, arg, limit)
break;
case T_HASH:
- w_uclass(obj, cHash, arg);
+ w_uclass(obj, rb_cHash, arg);
w_byte(TYPE_HASH, arg);
w_long(RHASH(obj)->tbl->num_entries, arg);
- st_foreach(RHASH(obj)->tbl, hash_each, &c_arg);
+ st_foreach(RHASH(obj)->tbl, rb_hash_each, &c_arg);
break;
case T_STRUCT:
@@ -344,10 +335,10 @@ w_object(obj, arg, limit)
w_long(len, arg);
mem = rb_ivar_get(CLASS_OF(obj), rb_intern("__member__"));
if (mem == Qnil) {
- Fatal("non-initialized struct");
+ rb_raise(rb_eTypeError, "non-initialized struct");
}
for (i=0; i<len; i++) {
- w_symbol(FIX2INT(RARRAY(mem)->ptr[i]), arg);
+ w_symbol(FIX2LONG(RARRAY(mem)->ptr[i]), arg);
w_object(RSTRUCT(obj)->ptr[i], arg, limit);
}
}
@@ -356,17 +347,17 @@ w_object(obj, arg, limit)
case T_OBJECT:
w_byte(TYPE_OBJECT, arg);
{
- VALUE class = CLASS_OF(obj);
+ VALUE klass = CLASS_OF(obj);
char *path;
- if (FL_TEST(class, FL_SINGLETON)) {
- TypeError("singleton can't be dumped");
+ if (FL_TEST(klass, FL_SINGLETON)) {
+ rb_raise(rb_eTypeError, "singleton can't be dumped");
}
- path = rb_class2name(class);
+ path = rb_class2name(klass);
w_unique(path, arg);
if (ROBJECT(obj)->iv_tbl) {
w_long(ROBJECT(obj)->iv_tbl->num_entries, arg);
- st_foreach(ROBJECT(obj)->iv_tbl, obj_each, &c_arg);
+ st_foreach(ROBJECT(obj)->iv_tbl, rb_obj_each, &c_arg);
}
else {
w_long(0, arg);
@@ -375,7 +366,8 @@ w_object(obj, arg, limit)
break;
default:
- TypeError("can't dump %s", rb_class2name(CLASS_OF(obj)));
+ rb_raise(rb_eTypeError, "can't dump %s",
+ rb_class2name(CLASS_OF(obj)));
break;
}
}
@@ -386,6 +378,7 @@ dump(arg)
struct dump_call_arg *arg;
{
w_object(arg->obj, arg->arg, arg->limit);
+ return 0;
}
static VALUE
@@ -394,6 +387,7 @@ dump_ensure(arg)
{
st_free_table(arg->symbol);
st_free_table(arg->data);
+ return 0;
}
static VALUE
@@ -403,7 +397,6 @@ marshal_dump(argc, argv)
{
VALUE obj, port, a1, a2;
int limit = -1;
- extern VALUE cIO;
struct dump_arg arg;
struct dump_call_arg c_arg;
@@ -418,21 +411,21 @@ marshal_dump(argc, argv)
else port = a1;
}
if (port) {
- if (obj_is_kind_of(port, cIO)) {
+ if (rb_obj_is_kind_of(port, rb_cIO)) {
OpenFile *fptr;
- io_binmode(port);
+ rb_io_binmode(port);
GetOpenFile(port, fptr);
- io_writable(fptr);
+ rb_io_check_writable(fptr);
arg.fp = (fptr->f2) ? fptr->f2 : fptr->f;
}
else {
- TypeError("instance of IO needed");
+ rb_raise(rb_eTypeError, "instance of IO needed");
}
}
else {
arg.fp = 0;
- port = str_new(0, 0);
+ port = rb_str_new(0, 0);
arg.str = port;
}
@@ -445,14 +438,14 @@ marshal_dump(argc, argv)
w_byte(MARSHAL_MAJOR, &arg);
w_byte(MARSHAL_MINOR, &arg);
- rb_ensure(dump, &c_arg, dump_ensure, &arg);
+ rb_ensure(dump, (VALUE)&c_arg, dump_ensure, (VALUE)&arg);
return port;
}
struct load_arg {
FILE *fp;
- UCHAR *ptr, *end;
+ char *ptr, *end;
st_table *symbol;
st_table *data;
VALUE proc;
@@ -463,19 +456,19 @@ r_byte(arg)
struct load_arg *arg;
{
if (arg->fp) return getc(arg->fp);
- if (arg->ptr < arg->end) return *arg->ptr++;
+ if (arg->ptr < arg->end) return *(unsigned char*)arg->ptr++;
return EOF;
}
-static USHORT
+static unsigned short
r_short(arg)
struct load_arg *arg;
{
- USHORT x;
+ unsigned short x;
int i;
x = 0;
- for (i=0; i<sizeof(USHORT); i++) {
+ for (i=0; i<sizeof(short); i++) {
x |= r_byte(arg)<<(i*8);
}
@@ -486,16 +479,17 @@ static void
long_toobig(size)
int size;
{
- TypeError("long too big for this architecture (size %d, given %d)",
- sizeof(long), size);
+ rb_raise(rb_eTypeError, "long too big for this architecture (size %d, given %d)",
+ sizeof(long), size);
}
static long
r_long(arg)
struct load_arg *arg;
{
- int c = r_byte(arg), i;
register long x;
+ int c = (char)r_byte(arg);
+ int i;
if (c == 0) return 0;
if (c > 0) {
@@ -505,7 +499,7 @@ r_long(arg)
x |= (long)r_byte(arg) << (8*i);
}
}
- else if (c < 0) {
+ else {
c = -c;
if (c > sizeof(long)) long_toobig((int)c);
x = -1;
@@ -517,12 +511,20 @@ r_long(arg)
return x;
}
-#define r_bytes(s, arg) \
- (s = (char*)r_long(arg), r_bytes0(&s,ALLOCA_N(char,(long)s),(long)s,arg))
+#define r_bytes2(s, len, arg) do { \
+ (len) = r_long(arg); \
+ (s) = ALLOCA_N(char,(len)+1); \
+ r_bytes0((s),(len),(arg)); \
+} while (0)
-static int
-r_bytes0(sp, s, len, arg)
- char **sp, *s;
+#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;
int len;
struct load_arg *arg;
{
@@ -536,11 +538,7 @@ r_bytes0(sp, s, len, arg)
memcpy(s, arg->ptr, len);
arg->ptr += len;
}
-
- (s)[len] = '\0';
- *sp = s;
-
- return len;
+ s[len] = '\0';
}
static ID
@@ -549,7 +547,6 @@ r_symbol(arg)
{
char *buf;
ID id;
- char type;
if (r_byte(arg) == TYPE_SYMLINK) {
int num = r_long(arg);
@@ -557,7 +554,7 @@ r_symbol(arg)
if (st_lookup(arg->symbol, num, &id)) {
return id;
}
- TypeError("bad symbol");
+ rb_raise(rb_eTypeError, "bad symbol");
}
r_bytes(buf, arg);
id = rb_intern(buf);
@@ -578,9 +575,10 @@ r_string(arg)
struct load_arg *arg;
{
char *buf;
- int len = r_bytes(buf, arg);
+ int len;
- return str_taint(str_new(buf, len));
+ r_bytes2(buf, len, arg);
+ return rb_str_new(buf, len);
}
static VALUE
@@ -588,6 +586,7 @@ r_regist(v, arg)
VALUE v;
struct load_arg *arg;
{
+ OBJ_TAINT(v);
if (arg->proc) {
rb_funcall(arg->proc, rb_intern("call"), 1, v);
}
@@ -604,14 +603,14 @@ r_object(arg)
switch (type) {
case EOF:
- eof_error();
+ rb_eof_error();
return Qnil;
case TYPE_LINK:
if (st_lookup(arg->data, r_long(arg), &v)) {
return v;
}
- ArgError("dump format error (unlinked)");
+ rb_raise(rb_eArgError, "dump format error (unlinked)");
break;
case TYPE_UCLASS:
@@ -619,9 +618,9 @@ r_object(arg)
VALUE c = rb_path2class(r_unique(arg));
v = r_object(arg);
if (rb_special_const_p(v)) {
- ArgError("dump format error (user class)");
+ rb_raise(rb_eArgError, "dump format error (user class)");
}
- RBASIC(v)->class = c;
+ RBASIC(v)->klass = c;
return v;
}
@@ -629,10 +628,10 @@ r_object(arg)
return Qnil;
case TYPE_TRUE:
- return TRUE;
+ return Qtrue;
case TYPE_FALSE:
- return FALSE;
+ return Qfalse;
case TYPE_FIXNUM:
{
@@ -648,26 +647,26 @@ r_object(arg)
char *buf;
r_bytes(buf, arg);
- v = float_new(atof(buf));
+ v = rb_float_new(atof(buf));
return r_regist(v, arg);
}
case TYPE_BIGNUM:
{
int len;
- USHORT *digits;
+ unsigned short *digits;
NEWOBJ(big, struct RBignum);
- OBJSETUP(big, cBignum, T_BIGNUM);
+ OBJSETUP(big, rb_cBignum, T_BIGNUM);
big->sign = (r_byte(arg) == '+');
big->len = len = r_long(arg);
- big->digits = digits = ALLOC_N(USHORT, len);
+ big->digits = digits = ALLOC_N(unsigned short, len);
while (len--) {
*digits++ = r_short(arg);
}
- big = RBIGNUM(big_norm((VALUE)big));
+ big = RBIGNUM(rb_big_norm((VALUE)big));
if (TYPE(big) == T_BIGNUM) {
- r_regist(big, arg);
+ r_regist((VALUE)big, arg);
}
return (VALUE)big;
}
@@ -678,18 +677,21 @@ r_object(arg)
case TYPE_REGEXP:
{
char *buf;
- int len = r_bytes(buf, arg);
- int ci = r_byte(arg);
- return r_regist(reg_new(buf, len, ci), arg);
+ int len;
+ int options;
+
+ r_bytes2(buf, len, arg);
+ options = r_byte(arg);
+ return r_regist(rb_reg_new(buf, len, options), arg);
}
case TYPE_ARRAY:
{
volatile int len = r_long(arg);
- v = ary_new2(len);
+ v = rb_ary_new2(len);
r_regist(v, arg);
while (len--) {
- ary_push(v, r_object(arg));
+ rb_ary_push(v, r_object(arg));
}
return v;
}
@@ -698,46 +700,46 @@ r_object(arg)
{
int len = r_long(arg);
- v = hash_new();
+ v = rb_hash_new();
r_regist(v, arg);
while (len--) {
VALUE key = r_object(arg);
VALUE value = r_object(arg);
- hash_aset(v, key, value);
+ rb_hash_aset(v, key, value);
}
return v;
}
case TYPE_STRUCT:
{
- VALUE class, mem, values;
+ VALUE klass, mem, values;
volatile int i; /* gcc 2.7.2.3 -O2 bug?? */
int len;
ID slot;
- class = rb_path2class(r_unique(arg));
- mem = rb_ivar_get(class, rb_intern("__member__"));
+ klass = rb_path2class(r_unique(arg));
+ mem = rb_ivar_get(klass, rb_intern("__member__"));
if (mem == Qnil) {
- Fatal("non-initialized struct");
+ rb_raise(rb_eTypeError, "non-initialized struct");
}
len = r_long(arg);
- values = ary_new2(len);
+ values = rb_ary_new2(len);
for (i=0; i<len; i++) {
- ary_push(values, Qnil);
+ rb_ary_push(values, Qnil);
}
- v = struct_alloc(class, values);
+ v = rb_struct_alloc(klass, values);
r_regist(v, arg);
for (i=0; i<len; i++) {
slot = r_symbol(arg);
if (RARRAY(mem)->ptr[i] != INT2FIX(slot)) {
- TypeError("struct %s not compatible (:%s for :%s)",
- rb_class2name(class),
- rb_id2name(slot),
- rb_id2name(FIX2INT(RARRAY(mem)->ptr[i])));
+ rb_raise(rb_eTypeError, "struct %s not compatible (:%s for :%s)",
+ rb_class2name(klass),
+ rb_id2name(slot),
+ rb_id2name(FIX2INT(RARRAY(mem)->ptr[i])));
}
- struct_aset(v, INT2FIX(i), r_object(arg));
+ rb_struct_aset(v, INT2FIX(i), r_object(arg));
}
return v;
}
@@ -745,27 +747,26 @@ r_object(arg)
case TYPE_USERDEF:
{
- VALUE class;
- int len;
+ VALUE klass;
- class = rb_path2class(r_unique(arg));
- if (rb_respond_to(class, s_load)) {
- v = rb_funcall(class, s_load, 1, r_string(arg));
+ klass = rb_path2class(r_unique(arg));
+ if (rb_respond_to(klass, s_load)) {
+ v = rb_funcall(klass, s_load, 1, r_string(arg));
return r_regist(v, arg);
}
- TypeError("class %s needs to have method `_load_from'",
- rb_class2name(class));
+ rb_raise(rb_eTypeError, "class %s needs to have method `_load_from'",
+ rb_class2name(klass));
}
break;
case TYPE_OBJECT:
{
- VALUE class;
+ VALUE klass;
int len;
- class = rb_path2class(r_unique(arg));
+ klass = rb_path2class(r_unique(arg));
len = r_long(arg);
- v = obj_alloc(class);
+ v = rb_obj_alloc(klass);
r_regist(v, arg);
if (len > 0) {
while (len--) {
@@ -786,9 +787,10 @@ r_object(arg)
}
default:
- ArgError("dump format error(0x%x)", type);
+ rb_raise(rb_eArgError, "dump format error(0x%x)", type);
break;
}
+ return Qnil; /* not reached */
}
static VALUE
@@ -804,6 +806,7 @@ load_ensure(arg)
{
st_free_table(arg->symbol);
st_free_table(arg->data);
+ return 0;
}
static VALUE
@@ -812,57 +815,57 @@ marshal_load(argc, argv)
VALUE *argv;
{
VALUE port, proc;
- FILE *fp;
int major;
VALUE v;
OpenFile *fptr;
struct load_arg arg;
rb_scan_args(argc, argv, "11", &port, &proc);
- if (TYPE(port) == T_STRING) {
+ 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;
+
arg.fp = 0;
- arg.ptr = RSTRING(port)->ptr;
- arg.end = arg.ptr + RSTRING(port)->len;
+ arg.ptr = str2cstr(port, &len);
+ arg.end = arg.ptr + len;
}
else {
- if (obj_is_kind_of(port, cIO)) {
- io_binmode(port);
- GetOpenFile(port, fptr);
- io_readable(fptr);
- arg.fp = fptr->f;
- }
- else {
- TypeError("instance of IO needed");
- }
+ rb_raise(rb_eTypeError, "instance of IO needed");
}
major = r_byte(&arg);
if (major == MARSHAL_MAJOR) {
if (r_byte(&arg) != MARSHAL_MINOR) {
- Warning("Old marshal file format (can be read)");
+ rb_warn("Old marshal file format (can be read)");
}
arg.symbol = st_init_numtable();
arg.data = st_init_numtable();
if (NIL_P(proc)) arg.proc = 0;
else arg.proc = proc;
- v = rb_ensure(load, &arg, load_ensure, &arg);
+ v = rb_ensure(load, (VALUE)&arg, load_ensure, (VALUE)&arg);
}
else {
- TypeError("Old marshal file format (can't read)");
+ rb_raise(rb_eTypeError, "Old marshal file format (can't read)");
}
return v;
}
+void
Init_marshal()
{
- VALUE mMarshal = rb_define_module("Marshal");
+ VALUE rb_mMarshal = rb_define_module("Marshal");
- s_dump = rb_intern("_dump_to");
- s_load = rb_intern("_load_from");
- rb_define_module_function(mMarshal, "dump", marshal_dump, -1);
- rb_define_module_function(mMarshal, "load", marshal_load, -1);
- rb_define_module_function(mMarshal, "restore", marshal_load, 1);
+ 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);
- rb_provide("marshal.o"); /* for backward compatibility */
+ rb_provide("marshal.so"); /* for backward compatibility */
}