forked from rubinius/rubinius
/
marshal.cpp
286 lines (224 loc) · 6.46 KB
/
marshal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <iomanip>
#include "objectmemory.hpp"
#include "marshal.hpp"
#include "object_utils.hpp"
#include <tommath.h>
#include <gdtoa.h>
#include "builtin/array.hpp"
#include "builtin/compiledcode.hpp"
#include "builtin/encoding.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/float.hpp"
#include "builtin/iseq.hpp"
#include "builtin/string.hpp"
#include "builtin/symbol.hpp"
#include "builtin/tuple.hpp"
#include "detection.hpp"
#define STACK_BUF_SZ 1024
namespace rubinius {
Object* UnMarshaller::get_constant() {
char stack_data[STACK_BUF_SZ];
char *malloc_data = NULL;
char *data = stack_data;
size_t count;
stream >> count;
stream.get();
if(count >= STACK_BUF_SZ) {
malloc_data = (char*)malloc(count + 1);
data = malloc_data;
}
stream.read(data, count + 1);
data[count] = 0; // clamp
Object* cls = state->vm()->path2class(data);
if(malloc_data) {
free(malloc_data);
}
return cls;
}
Object* UnMarshaller::get_encoding() {
char stack_data[STACK_BUF_SZ];
char *malloc_data = NULL;
char *data = stack_data;
size_t count;
stream >> count;
stream.get();
if(count >= STACK_BUF_SZ) {
malloc_data = (char*)malloc(count + 1);
data = malloc_data;
}
stream.read(data, count + 1);
data[count] = 0; // clamp
if(count > 0) {
Encoding* enc = Encoding::find(state, data);
if(malloc_data) {
free(malloc_data);
}
return enc;
} else {
return cNil;
}
}
Object* UnMarshaller::get_int() {
std::string data;
stream >> data;
return Bignum::from_string(state, data.c_str(), 16);
}
String* UnMarshaller::get_string() {
size_t count;
Encoding* enc = try_as<Encoding>(unmarshal());
stream >> count;
// String::create adds room for a trailing null on its own
// using pinned here allows later stages to optimize these literal
// strings better.
String* str = String::create(state, NULL, count);
stream.get(); // read off newline
stream.read(reinterpret_cast<char*>(str->byte_address()), count);
stream.get(); // read off newline
if(enc) str->encoding(state, enc);
return str;
}
Symbol* UnMarshaller::get_symbol() {
char stack_data[STACK_BUF_SZ];
char *malloc_data = NULL;
char *data = stack_data;
size_t count;
stream >> count;
stream.get();
if(count >= STACK_BUF_SZ) {
malloc_data = (char*)malloc(count + 1);
data = malloc_data;
}
stream.read(data, count + 1);
data[count] = 0; // clamp
Symbol* sym = state->symbol(data);
if(malloc_data) {
free(malloc_data);
}
return sym;
}
Tuple* UnMarshaller::get_tuple() {
size_t count;
stream >> count;
Tuple* tup = Tuple::create(state, count);
for(size_t i = 0; i < count; i++) {
tup->put(state, i, unmarshal());
}
return tup;
}
#define FLOAT_EXP_OFFSET 58
Float* UnMarshaller::get_float() {
std::string line;
const char* data;
// discard the delimiter
stream.get();
std::getline(stream, line);
if(stream.fail()) {
Exception::type_error(state, "Unable to unmarshal Float: failed to read value");
}
data = line.c_str();
if(data[0] == ' ') {
double x;
long e;
x = ::ruby_strtod(data, NULL);
e = strtol(data+FLOAT_EXP_OFFSET, NULL, 10);
// This is necessary because exp2(1024) yields inf
if(e == 1024) {
double root_exp = ::exp2(512);
return Float::create(state, x * root_exp * root_exp);
} else {
return Float::create(state, x * ::exp2(e));
}
} else {
// avoid compiler warning
double zero = 0.0;
double val = 0.0;
if(!strncasecmp(data, "Infinity", 8U)) {
val = 1.0;
} else if(!strncasecmp(data, "-Infinity", 9U)) {
val = -1.0;
} else if(!strncasecmp(data, "NaN", 3U)) {
val = zero;
} else {
Exception::type_error(state, "Unable to unmarshal Float: invalid format");
}
return Float::create(state, val / zero);
}
}
#define OPCODE_LENGTH 32
InstructionSequence* UnMarshaller::get_iseq() {
size_t count;
long op;
char data[OPCODE_LENGTH];
stream >> count;
// Read off newline
stream.get();
InstructionSequence* iseq = InstructionSequence::create(state, count);
Tuple* ops = iseq->opcodes();
for(size_t i = 0; i < count; i++) {
stream.getline(data, OPCODE_LENGTH);
op = strtol(data, NULL, 10);
ops->put(state, i, Fixnum::from(op));
}
iseq->post_marshal(state);
return iseq;
}
CompiledCode* UnMarshaller::get_compiled_code() {
size_t ver;
stream >> ver;
CompiledCode* code = CompiledCode::create(state);
code->metadata(state, unmarshal());
code->primitive(state, reinterpret_cast<Symbol*>(unmarshal()));
code->name(state, reinterpret_cast<Symbol*>(unmarshal()));
code->iseq(state, reinterpret_cast<InstructionSequence*>(unmarshal()));
code->stack_size(state, reinterpret_cast<Fixnum*>(unmarshal()));
code->local_count(state, reinterpret_cast<Fixnum*>(unmarshal()));
code->required_args(state, reinterpret_cast<Fixnum*>(unmarshal()));
code->post_args(state, reinterpret_cast<Fixnum*>(unmarshal()));
code->total_args(state, reinterpret_cast<Fixnum*>(unmarshal()));
code->splat(state, unmarshal());
code->literals(state, reinterpret_cast<Tuple*>(unmarshal()));
code->lines(state, reinterpret_cast<Tuple*>(unmarshal()));
code->file(state, reinterpret_cast<Symbol*>(unmarshal()));
code->local_names(state, reinterpret_cast<Tuple*>(unmarshal()));
code->post_marshal(state);
return code;
}
Object* UnMarshaller::unmarshal() {
char code;
stream >> code;
switch(code) {
case 'n':
return cNil;
case 't':
return cTrue;
case 'f':
return cFalse;
case 'I':
return get_int();
case 's':
return get_string();
case 'x':
return get_symbol();
case 'p':
return get_tuple();
case 'd':
return get_float();
case 'i':
return get_iseq();
case 'M':
return get_compiled_code();
case 'c':
return get_constant();
case 'E':
return get_encoding();
default:
std::string str = "unknown marshal code: ";
str.append( 1, code );
Exception::type_error(state, str.c_str());
return cNil; // make compiler happy
}
}
}