forked from pgriess/node-msgpack
/
msgpack.cc
323 lines (266 loc) · 9.46 KB
/
msgpack.cc
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* Bindings for http://msgpack.sourceforge.net/ */
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include <msgpack.h>
#include <math.h>
using namespace v8;
using namespace node;
static Persistent<String> msgpack_tag_symbol;
static Persistent<String> msgpack_bytes_remaining_symbol;
static Persistent<FunctionTemplate> msgpack_unpack_template;
// An exception class that wraps a textual message
class MsgpackException {
public:
MsgpackException(const char *str) :
msg(String::New(str)) {
}
Handle<Value> getThrownException() {
return ThrowException(Exception::TypeError(msg));
}
private:
const Handle<String> msg;
};
// A holder for a msgpack_zone object; ensures destruction on scope exit
class MsgpackZone {
public:
msgpack_zone _mz;
MsgpackZone(size_t sz = 1024) {
msgpack_zone_init(&this->_mz, sz);
}
~MsgpackZone() {
msgpack_zone_destroy(&this->_mz);
}
};
// A holder for a msgpack_sbuffer object; ensures destruction on scope exit
class MsgpackSbuffer {
public:
msgpack_sbuffer _sbuf;
MsgpackSbuffer() {
msgpack_sbuffer_init(&this->_sbuf);
}
~MsgpackSbuffer() {
msgpack_sbuffer_destroy(&this->_sbuf);
}
};
#define CHECK_CIRCULAR_REFS(o, t) \
do { \
Local<Value> ov = (o)->GetHiddenValue(msgpack_tag_symbol); \
if (!ov.IsEmpty() && ov->Equals(t)) { \
throw MsgpackException( \
"Cowardly refusing to pack object with circular reference" \
); \
} \
(o)->SetHiddenValue(msgpack_tag_symbol, (t)); \
} while(0)
#define DBG_PRINT_BUF(buf, name) \
do { \
fprintf(stderr, "Buffer %s has %lu bytes:\n", \
(name), (buf)->length() \
); \
for (uint32_t i = 0; i * 16 < (buf)->length(); i++) { \
fprintf(stderr, " "); \
for (uint32_t ii = 0; \
ii < 16 && (i * 16) + ii < (buf)->length(); \
ii++) { \
fprintf(stderr, "%s%2.2hhx", \
(ii > 0 && (ii % 2 == 0)) ? " " : "", \
(buf)->data()[i * 16 + ii] \
); \
} \
fprintf(stderr, "\n"); \
} \
} while (0)
// Convert a V8 object to a MessagePack object.
//
// This method is recursive. It will probably blow out the stack on objects
// with extremely deep nesting.
//
// This method can detect circular references in provided objects. It utilizes
// the v8::Object::SetHiddenValue() facility to tag each encountered object
// with a value unique to this serialization run. We leave the tags attached to
// each object for ease-of-implementation (otherwise we'd have to track each
// tagged v8::Object instance and un-tag at the end). This decision can be
// revisited in the future if this proves problematic. I'd expect most objects
// being packed to be short-lived anyway.
//
// If a circular reference is detected, an exception is thrown.
static void
v8_to_msgpack(Handle<Value> v8obj, msgpack_object *mo, msgpack_zone *mz, Handle<Value> tag) {
if (v8obj->IsUndefined() || v8obj->IsNull()) {
mo->type = MSGPACK_OBJECT_NIL;
} else if (v8obj->IsBoolean()) {
mo->type = MSGPACK_OBJECT_BOOLEAN;
mo->via.boolean = v8obj->BooleanValue();
} else if (v8obj->IsNumber()) {
double d = v8obj->NumberValue();
if (trunc(d) != d) {
mo->type = MSGPACK_OBJECT_DOUBLE;
mo->via.dec = d;
} else if (d > 0) {
mo->type = MSGPACK_OBJECT_POSITIVE_INTEGER;
mo->via.u64 = d;
} else {
mo->type = MSGPACK_OBJECT_NEGATIVE_INTEGER;
mo->via.i64 = d;
}
} else if (v8obj->IsString()) {
mo->type = MSGPACK_OBJECT_RAW;
mo->via.raw.size = DecodeBytes(v8obj, UTF8);
mo->via.raw.ptr = (char*) msgpack_zone_malloc(mz, mo->via.raw.size);
DecodeWrite((char*) mo->via.raw.ptr, mo->via.raw.size, v8obj, UTF8);
} else if (v8obj->IsArray()) {
Local<Object> o = v8obj->ToObject();
Local<Array> a = Local<Array>::Cast(o);
CHECK_CIRCULAR_REFS(o, tag);
mo->type = MSGPACK_OBJECT_ARRAY;
mo->via.array.size = a->Length();
mo->via.array.ptr = (msgpack_object*) msgpack_zone_malloc(
mz,
sizeof(msgpack_object) * mo->via.array.size
);
for (uint32_t i = 0; i < a->Length(); i++) {
Local<Value> v = a->Get(i);
v8_to_msgpack(v, &mo->via.array.ptr[i], mz, tag);
}
} else {
Local<Object> o = v8obj->ToObject();
Local<Array> a = o->GetPropertyNames();
CHECK_CIRCULAR_REFS(o, tag);
mo->type = MSGPACK_OBJECT_MAP;
mo->via.map.size = a->Length();
mo->via.map.ptr = (msgpack_object_kv*) msgpack_zone_malloc(
mz,
sizeof(msgpack_object_kv) * mo->via.map.size
);
for (uint32_t i = 0; i < a->Length(); i++) {
Local<Value> k = a->Get(i);
v8_to_msgpack(k, &mo->via.map.ptr[i].key, mz, tag);
v8_to_msgpack(o->Get(k), &mo->via.map.ptr[i].val, mz, tag);
}
}
}
// Convert a MessagePack object to a V8 object.
//
// This method is recursive. It will probably blow out the stack on objects
// with extremely deep nesting.
static Handle<Value>
msgpack_to_v8(msgpack_object *mo) {
switch (mo->type) {
case MSGPACK_OBJECT_NIL:
return Null();
case MSGPACK_OBJECT_BOOLEAN:
return (mo->via.boolean) ?
True() :
False();
case MSGPACK_OBJECT_POSITIVE_INTEGER:
return Integer::NewFromUnsigned(mo->via.u64);
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
return Integer::New(mo->via.i64);
case MSGPACK_OBJECT_DOUBLE:
return Number::New(mo->via.dec);
case MSGPACK_OBJECT_ARRAY: {
Local<Array> a = Array::New(mo->via.array.size);
for (uint32_t i = 0; i < mo->via.array.size; i++) {
a->Set(i, msgpack_to_v8(&mo->via.array.ptr[i]));
}
return a;
}
case MSGPACK_OBJECT_RAW:
return String::New(mo->via.raw.ptr, mo->via.raw.size);
case MSGPACK_OBJECT_MAP: {
Local<Object> o = Object::New();
for (uint32_t i = 0; i < mo->via.map.size; i++) {
o->Set(
msgpack_to_v8(&mo->via.map.ptr[i].key),
msgpack_to_v8(&mo->via.map.ptr[i].val)
);
}
return o;
}
default:
throw MsgpackException("Encountered unknown MesssagePack object type");
}
}
// var buf = msgpack.pack(obj[, obj ...]);
//
// Returns a Buffer object representing the serialized state of the provided
// JavaScript object. If more arguments are provided, their serialized state
// will be accumulated to the end of the previous value(s).
//
// Any number of objects can be provided as arguments, and all will be
// serialized to the same bytestream, back-ty-back.
static Handle<Value>
pack(const Arguments &args) {
static int64_t tag_i = 0;
HandleScope scope;
msgpack_packer pk;
MsgpackZone mz;
MsgpackSbuffer sb;
Local<Value> tag = Integer::New(++tag_i);
msgpack_packer_init(&pk, &sb._sbuf, msgpack_sbuffer_write);
for (int i = 0; i < args.Length(); i++) {
msgpack_object mo;
try {
v8_to_msgpack(args[0], &mo, &mz._mz, tag);
} catch (MsgpackException e) {
return e.getThrownException();
}
if (msgpack_pack_object(&pk, mo)) {
return ThrowException(Exception::Error(
String::New("Error serializaing object")));
}
}
Buffer *bp = Buffer::New(sb._sbuf.size);
memcpy(bp->data(), sb._sbuf.data, sb._sbuf.size);
return scope.Close(bp->handle_);
}
// var o = msgpack.unpack(buf);
//
// Return the JavaScript object resulting from unpacking the contents of the
// specified buffer.
static Handle<Value>
unpack(const Arguments &args) {
HandleScope scope;
if (args.Length() < 0 || !Buffer::HasInstance(args[0])) {
return ThrowException(Exception::TypeError(
String::New("First argument must be a Buffer")));
}
Buffer *buf = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
MsgpackZone mz;
msgpack_object mo;
size_t off = 0;
switch (msgpack_unpack(buf->data(), buf->length(), &off, &mz._mz, &mo)) {
case MSGPACK_UNPACK_EXTRA_BYTES:
case MSGPACK_UNPACK_SUCCESS:
try {
msgpack_unpack_template->GetFunction()->Set(
msgpack_bytes_remaining_symbol,
Integer::New(buf->length() - off)
);
return scope.Close(msgpack_to_v8(&mo));
} catch (MsgpackException e) {
return e.getThrownException();
}
default:
return ThrowException(Exception::Error(
String::New("Error de-serializing object")));
}
}
extern "C" void
init(Handle<Object> target) {
HandleScope scope;
NODE_SET_METHOD(target, "pack", pack);
// Go through this mess rather than call NODE_SET_METHOD so that we can set
// a field on the function for 'bytes_remaining'.
msgpack_unpack_template = Persistent<FunctionTemplate>::New(
FunctionTemplate::New(unpack)
);
target->Set(
String::NewSymbol("unpack"),
msgpack_unpack_template->GetFunction()
);
msgpack_tag_symbol = NODE_PSYMBOL("msgpack::tag");
msgpack_bytes_remaining_symbol = NODE_PSYMBOL("bytes_remaining");
}
// vim:ts=4 sw=4 et