msgpack 0.6.0pre1-x64-mingw32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +26 -0
- data/ChangeLog +117 -0
- data/Dockerfile +30 -0
- data/Gemfile +4 -0
- data/LICENSE +177 -0
- data/README.rdoc +129 -0
- data/Rakefile +114 -0
- data/bench/pack.rb +23 -0
- data/bench/pack_log.rb +33 -0
- data/bench/pack_log_long.rb +65 -0
- data/bench/run.sh +14 -0
- data/bench/run_long.sh +35 -0
- data/bench/unpack.rb +21 -0
- data/bench/unpack_log.rb +34 -0
- data/bench/unpack_log_long.rb +67 -0
- data/cross-build.sh +9 -0
- data/doclib/msgpack/buffer.rb +193 -0
- data/doclib/msgpack/core_ext.rb +101 -0
- data/doclib/msgpack/error.rb +14 -0
- data/doclib/msgpack/packer.rb +134 -0
- data/doclib/msgpack/unpacker.rb +146 -0
- data/doclib/msgpack.rb +77 -0
- data/ext/java/org/msgpack/jruby/Buffer.java +221 -0
- data/ext/java/org/msgpack/jruby/Decoder.java +201 -0
- data/ext/java/org/msgpack/jruby/Encoder.java +308 -0
- data/ext/java/org/msgpack/jruby/ExtensionValue.java +136 -0
- data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +107 -0
- data/ext/java/org/msgpack/jruby/Packer.java +78 -0
- data/ext/java/org/msgpack/jruby/Types.java +37 -0
- data/ext/java/org/msgpack/jruby/Unpacker.java +170 -0
- data/ext/msgpack/buffer.c +695 -0
- data/ext/msgpack/buffer.h +447 -0
- data/ext/msgpack/buffer_class.c +507 -0
- data/ext/msgpack/buffer_class.h +32 -0
- data/ext/msgpack/compat.h +113 -0
- data/ext/msgpack/core_ext.c +129 -0
- data/ext/msgpack/core_ext.h +26 -0
- data/ext/msgpack/extconf.rb +28 -0
- data/ext/msgpack/packer.c +168 -0
- data/ext/msgpack/packer.h +441 -0
- data/ext/msgpack/packer_class.c +302 -0
- data/ext/msgpack/packer_class.h +30 -0
- data/ext/msgpack/rbinit.c +33 -0
- data/ext/msgpack/rmem.c +94 -0
- data/ext/msgpack/rmem.h +109 -0
- data/ext/msgpack/sysdep.h +115 -0
- data/ext/msgpack/sysdep_endian.h +50 -0
- data/ext/msgpack/sysdep_types.h +46 -0
- data/ext/msgpack/unpacker.c +771 -0
- data/ext/msgpack/unpacker.h +122 -0
- data/ext/msgpack/unpacker_class.c +405 -0
- data/ext/msgpack/unpacker_class.h +32 -0
- data/lib/msgpack/msgpack.so +0 -0
- data/lib/msgpack/version.rb +3 -0
- data/lib/msgpack.rb +13 -0
- data/msgpack.gemspec +31 -0
- data/msgpack.org.md +46 -0
- data/spec/cases.json +1 -0
- data/spec/cases.msg +0 -0
- data/spec/cases_compact.msg +0 -0
- data/spec/cases_spec.rb +39 -0
- data/spec/cruby/buffer_io_spec.rb +256 -0
- data/spec/cruby/buffer_packer.rb +29 -0
- data/spec/cruby/buffer_spec.rb +572 -0
- data/spec/cruby/buffer_unpacker.rb +19 -0
- data/spec/cruby/packer_spec.rb +120 -0
- data/spec/cruby/unpacker_spec.rb +305 -0
- data/spec/format_spec.rb +282 -0
- data/spec/jruby/benchmarks/shootout_bm.rb +73 -0
- data/spec/jruby/benchmarks/symbolize_keys_bm.rb +25 -0
- data/spec/jruby/msgpack/unpacker_spec.rb +290 -0
- data/spec/jruby/msgpack_spec.rb +142 -0
- data/spec/pack_spec.rb +67 -0
- data/spec/random_compat.rb +24 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/unpack_spec.rb +60 -0
- metadata +209 -0
@@ -0,0 +1,507 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
|
19
|
+
#include "compat.h"
|
20
|
+
#include "ruby.h"
|
21
|
+
#include "buffer.h"
|
22
|
+
#include "buffer_class.h"
|
23
|
+
|
24
|
+
VALUE cMessagePack_Buffer;
|
25
|
+
|
26
|
+
static ID s_read;
|
27
|
+
static ID s_readpartial;
|
28
|
+
static ID s_write;
|
29
|
+
static ID s_append;
|
30
|
+
static ID s_close;
|
31
|
+
|
32
|
+
#define BUFFER(from, name) \
|
33
|
+
msgpack_buffer_t *name = NULL; \
|
34
|
+
Data_Get_Struct(from, msgpack_buffer_t, name); \
|
35
|
+
if(name == NULL) { \
|
36
|
+
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
37
|
+
}
|
38
|
+
|
39
|
+
#define CHECK_STRING_TYPE(value) \
|
40
|
+
value = rb_check_string_type(value); \
|
41
|
+
if( NIL_P(value) ) { \
|
42
|
+
rb_raise(rb_eTypeError, "instance of String needed"); \
|
43
|
+
}
|
44
|
+
|
45
|
+
static void Buffer_free(void* data)
|
46
|
+
{
|
47
|
+
if(data == NULL) {
|
48
|
+
return;
|
49
|
+
}
|
50
|
+
msgpack_buffer_t* b = (msgpack_buffer_t*) data;
|
51
|
+
msgpack_buffer_destroy(b);
|
52
|
+
free(b);
|
53
|
+
}
|
54
|
+
|
55
|
+
static VALUE Buffer_alloc(VALUE klass)
|
56
|
+
{
|
57
|
+
msgpack_buffer_t* b = ALLOC_N(msgpack_buffer_t, 1);
|
58
|
+
msgpack_buffer_init(b);
|
59
|
+
|
60
|
+
return Data_Wrap_Struct(klass, msgpack_buffer_mark, Buffer_free, b);
|
61
|
+
}
|
62
|
+
|
63
|
+
static ID get_partial_read_method(VALUE io)
|
64
|
+
{
|
65
|
+
if(rb_respond_to(io, s_readpartial)) {
|
66
|
+
return s_readpartial;
|
67
|
+
} else if(rb_respond_to(io, s_read)) {
|
68
|
+
return s_read;
|
69
|
+
} else {
|
70
|
+
return s_read;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
static ID get_write_all_method(VALUE io)
|
75
|
+
{
|
76
|
+
if(rb_respond_to(io, s_write)) {
|
77
|
+
return s_write;
|
78
|
+
} else if(rb_respond_to(io, s_append)) {
|
79
|
+
return s_append;
|
80
|
+
} else {
|
81
|
+
return s_write;
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
void MessagePack_Buffer_initialize(msgpack_buffer_t* b, VALUE io, VALUE options)
|
86
|
+
{
|
87
|
+
b->io = io;
|
88
|
+
b->io_partial_read_method = get_partial_read_method(io);
|
89
|
+
b->io_write_all_method = get_write_all_method(io);
|
90
|
+
|
91
|
+
if(options != Qnil) {
|
92
|
+
VALUE v;
|
93
|
+
|
94
|
+
v = rb_hash_aref(options, ID2SYM(rb_intern("read_reference_threshold")));
|
95
|
+
if(v != Qnil) {
|
96
|
+
msgpack_buffer_set_read_reference_threshold(b, NUM2ULONG(v));
|
97
|
+
}
|
98
|
+
|
99
|
+
v = rb_hash_aref(options, ID2SYM(rb_intern("write_reference_threshold")));
|
100
|
+
if(v != Qnil) {
|
101
|
+
msgpack_buffer_set_write_reference_threshold(b, NUM2ULONG(v));
|
102
|
+
}
|
103
|
+
|
104
|
+
v = rb_hash_aref(options, ID2SYM(rb_intern("io_buffer_size")));
|
105
|
+
if(v != Qnil) {
|
106
|
+
msgpack_buffer_set_io_buffer_size(b, NUM2ULONG(v));
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner)
|
112
|
+
{
|
113
|
+
b->owner = owner;
|
114
|
+
return Data_Wrap_Struct(cMessagePack_Buffer, msgpack_buffer_mark, NULL, b);
|
115
|
+
}
|
116
|
+
|
117
|
+
static VALUE Buffer_initialize(int argc, VALUE* argv, VALUE self)
|
118
|
+
{
|
119
|
+
VALUE io = Qnil;
|
120
|
+
VALUE options = Qnil;
|
121
|
+
|
122
|
+
if(argc == 0 || (argc == 1 && argv[0] == Qnil)) {
|
123
|
+
/* Qnil */
|
124
|
+
|
125
|
+
} else if(argc == 1) {
|
126
|
+
VALUE v = argv[0];
|
127
|
+
if(rb_type(v) == T_HASH) {
|
128
|
+
options = v;
|
129
|
+
} else {
|
130
|
+
io = v;
|
131
|
+
}
|
132
|
+
|
133
|
+
} else if(argc == 2) {
|
134
|
+
io = argv[0];
|
135
|
+
options = argv[1];
|
136
|
+
if(rb_type(options) != T_HASH) {
|
137
|
+
rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(io));
|
138
|
+
}
|
139
|
+
|
140
|
+
} else {
|
141
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
|
142
|
+
}
|
143
|
+
|
144
|
+
BUFFER(self, b);
|
145
|
+
|
146
|
+
MessagePack_Buffer_initialize(b, io, options);
|
147
|
+
|
148
|
+
return self;
|
149
|
+
}
|
150
|
+
|
151
|
+
static VALUE Buffer_clear(VALUE self)
|
152
|
+
{
|
153
|
+
BUFFER(self, b);
|
154
|
+
msgpack_buffer_clear(b);
|
155
|
+
return Qnil;
|
156
|
+
}
|
157
|
+
|
158
|
+
static VALUE Buffer_size(VALUE self)
|
159
|
+
{
|
160
|
+
BUFFER(self, b);
|
161
|
+
size_t size = msgpack_buffer_all_readable_size(b);
|
162
|
+
return SIZET2NUM(size);
|
163
|
+
}
|
164
|
+
|
165
|
+
static VALUE Buffer_empty_p(VALUE self)
|
166
|
+
{
|
167
|
+
BUFFER(self, b);
|
168
|
+
if(msgpack_buffer_top_readable_size(b) == 0) {
|
169
|
+
return Qtrue;
|
170
|
+
} else {
|
171
|
+
return Qfalse;
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
static VALUE Buffer_write(VALUE self, VALUE string_or_buffer)
|
176
|
+
{
|
177
|
+
BUFFER(self, b);
|
178
|
+
|
179
|
+
VALUE string = string_or_buffer; // TODO optimize if string_or_buffer is a Buffer
|
180
|
+
StringValue(string);
|
181
|
+
|
182
|
+
size_t length = msgpack_buffer_append_string(b, string);
|
183
|
+
|
184
|
+
return SIZET2NUM(length);
|
185
|
+
}
|
186
|
+
|
187
|
+
static VALUE Buffer_append(VALUE self, VALUE string_or_buffer)
|
188
|
+
{
|
189
|
+
BUFFER(self, b);
|
190
|
+
|
191
|
+
VALUE string = string_or_buffer; // TODO optimize if string_or_buffer is a Buffer
|
192
|
+
StringValue(string);
|
193
|
+
|
194
|
+
msgpack_buffer_append_string(b, string);
|
195
|
+
|
196
|
+
return self;
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
#define MAKE_EMPTY_STRING(orig) \
|
201
|
+
if(orig == Qnil) { \
|
202
|
+
orig = rb_str_buf_new(0); \
|
203
|
+
} else { \
|
204
|
+
rb_str_resize(orig, 0); \
|
205
|
+
}
|
206
|
+
|
207
|
+
static VALUE read_until_eof_rescue(VALUE args)
|
208
|
+
{
|
209
|
+
msgpack_buffer_t* b = (void*) ((VALUE*) args)[0];
|
210
|
+
VALUE out = ((VALUE*) args)[1];
|
211
|
+
unsigned long max = ((VALUE*) args)[2];
|
212
|
+
size_t* sz = (void*) ((VALUE*) args)[3];
|
213
|
+
|
214
|
+
while(true) {
|
215
|
+
size_t rl;
|
216
|
+
if(max == 0) {
|
217
|
+
if(out == Qnil) {
|
218
|
+
rl = msgpack_buffer_skip(b, b->io_buffer_size);
|
219
|
+
} else {
|
220
|
+
rl = msgpack_buffer_read_to_string(b, out, b->io_buffer_size);
|
221
|
+
}
|
222
|
+
if(rl == 0) {
|
223
|
+
break;
|
224
|
+
}
|
225
|
+
*sz += rl;
|
226
|
+
|
227
|
+
} else {
|
228
|
+
if(out == Qnil) {
|
229
|
+
rl = msgpack_buffer_skip(b, max);
|
230
|
+
} else {
|
231
|
+
rl = msgpack_buffer_read_to_string(b, out, max);
|
232
|
+
}
|
233
|
+
if(rl == 0) {
|
234
|
+
break;
|
235
|
+
}
|
236
|
+
*sz += rl;
|
237
|
+
if(max <= rl) {
|
238
|
+
break;
|
239
|
+
} else {
|
240
|
+
max -= rl;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
return Qnil;
|
246
|
+
}
|
247
|
+
|
248
|
+
static VALUE read_until_eof_error(VALUE args)
|
249
|
+
{
|
250
|
+
/* ignore EOFError */
|
251
|
+
UNUSED(args);
|
252
|
+
return Qnil;
|
253
|
+
}
|
254
|
+
|
255
|
+
static inline size_t read_until_eof(msgpack_buffer_t* b, VALUE out, unsigned long max)
|
256
|
+
{
|
257
|
+
if(msgpack_buffer_has_io(b)) {
|
258
|
+
size_t sz = 0;
|
259
|
+
VALUE args[4] = { (VALUE)(void*) b, out, (VALUE) max, (VALUE)(void*) &sz };
|
260
|
+
rb_rescue2(read_until_eof_rescue, (VALUE)(void*) args,
|
261
|
+
read_until_eof_error, (VALUE)(void*) args,
|
262
|
+
rb_eEOFError, NULL);
|
263
|
+
return sz;
|
264
|
+
|
265
|
+
} else {
|
266
|
+
if(max == 0) {
|
267
|
+
max = ULONG_MAX;
|
268
|
+
}
|
269
|
+
if(out == Qnil) {
|
270
|
+
return msgpack_buffer_skip_nonblock(b, max);
|
271
|
+
} else {
|
272
|
+
return msgpack_buffer_read_to_string_nonblock(b, out, max);
|
273
|
+
}
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
static inline VALUE read_all(msgpack_buffer_t* b, VALUE out)
|
278
|
+
{
|
279
|
+
#ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
|
280
|
+
if(out == Qnil && !msgpack_buffer_has_io(b)) {
|
281
|
+
/* same as to_s && clear; optimize */
|
282
|
+
VALUE str = msgpack_buffer_all_as_string(b);
|
283
|
+
msgpack_buffer_clear(b);
|
284
|
+
return str;
|
285
|
+
}
|
286
|
+
#endif
|
287
|
+
MAKE_EMPTY_STRING(out);
|
288
|
+
read_until_eof(b, out, 0);
|
289
|
+
return out;
|
290
|
+
}
|
291
|
+
|
292
|
+
static VALUE Buffer_skip(VALUE self, VALUE sn)
|
293
|
+
{
|
294
|
+
BUFFER(self, b);
|
295
|
+
|
296
|
+
unsigned long n = FIX2ULONG(sn);
|
297
|
+
|
298
|
+
/* do nothing */
|
299
|
+
if(n == 0) {
|
300
|
+
return ULONG2NUM(0);
|
301
|
+
}
|
302
|
+
|
303
|
+
size_t sz = read_until_eof(b, Qnil, n);
|
304
|
+
return ULONG2NUM(sz);
|
305
|
+
}
|
306
|
+
|
307
|
+
static VALUE Buffer_skip_all(VALUE self, VALUE sn)
|
308
|
+
{
|
309
|
+
BUFFER(self, b);
|
310
|
+
|
311
|
+
unsigned long n = FIX2ULONG(sn);
|
312
|
+
|
313
|
+
/* do nothing */
|
314
|
+
if(n == 0) {
|
315
|
+
return self;
|
316
|
+
}
|
317
|
+
|
318
|
+
if(!msgpack_buffer_ensure_readable(b, n)) {
|
319
|
+
rb_raise(rb_eEOFError, "end of buffer reached");
|
320
|
+
}
|
321
|
+
|
322
|
+
msgpack_buffer_skip_nonblock(b, n);
|
323
|
+
|
324
|
+
return self;
|
325
|
+
}
|
326
|
+
|
327
|
+
static VALUE Buffer_read_all(int argc, VALUE* argv, VALUE self)
|
328
|
+
{
|
329
|
+
VALUE out = Qnil;
|
330
|
+
unsigned long n = 0;
|
331
|
+
bool all = false;
|
332
|
+
|
333
|
+
switch(argc) {
|
334
|
+
case 2:
|
335
|
+
out = argv[1];
|
336
|
+
/* pass through */
|
337
|
+
case 1:
|
338
|
+
n = FIX2ULONG(argv[0]);
|
339
|
+
break;
|
340
|
+
case 0:
|
341
|
+
all = true;
|
342
|
+
break;
|
343
|
+
default:
|
344
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
|
345
|
+
}
|
346
|
+
|
347
|
+
BUFFER(self, b);
|
348
|
+
|
349
|
+
if(out != Qnil) {
|
350
|
+
CHECK_STRING_TYPE(out);
|
351
|
+
}
|
352
|
+
|
353
|
+
if(all) {
|
354
|
+
return read_all(b, out);
|
355
|
+
}
|
356
|
+
|
357
|
+
if(n == 0) {
|
358
|
+
/* do nothing */
|
359
|
+
MAKE_EMPTY_STRING(out);
|
360
|
+
return out;
|
361
|
+
}
|
362
|
+
|
363
|
+
if(!msgpack_buffer_ensure_readable(b, n)) {
|
364
|
+
rb_raise(rb_eEOFError, "end of buffer reached");
|
365
|
+
}
|
366
|
+
|
367
|
+
MAKE_EMPTY_STRING(out);
|
368
|
+
msgpack_buffer_read_to_string_nonblock(b, out, n);
|
369
|
+
|
370
|
+
return out;
|
371
|
+
}
|
372
|
+
|
373
|
+
static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
|
374
|
+
{
|
375
|
+
VALUE out = Qnil;
|
376
|
+
unsigned long n = -1;
|
377
|
+
bool all = false;
|
378
|
+
|
379
|
+
switch(argc) {
|
380
|
+
case 2:
|
381
|
+
out = argv[1];
|
382
|
+
/* pass through */
|
383
|
+
case 1:
|
384
|
+
n = FIX2ULONG(argv[0]);
|
385
|
+
break;
|
386
|
+
case 0:
|
387
|
+
all = true;
|
388
|
+
break;
|
389
|
+
default:
|
390
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
|
391
|
+
}
|
392
|
+
|
393
|
+
BUFFER(self, b);
|
394
|
+
|
395
|
+
if(out != Qnil) {
|
396
|
+
CHECK_STRING_TYPE(out);
|
397
|
+
}
|
398
|
+
|
399
|
+
if(all) {
|
400
|
+
return read_all(b, out);
|
401
|
+
}
|
402
|
+
|
403
|
+
if(n == 0) {
|
404
|
+
/* do nothing */
|
405
|
+
MAKE_EMPTY_STRING(out);
|
406
|
+
return out;
|
407
|
+
}
|
408
|
+
|
409
|
+
#ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
|
410
|
+
if(!msgpack_buffer_has_io(b) && out == Qnil &&
|
411
|
+
msgpack_buffer_all_readable_size(b) <= n) {
|
412
|
+
/* same as to_s && clear; optimize */
|
413
|
+
VALUE str = msgpack_buffer_all_as_string(b);
|
414
|
+
msgpack_buffer_clear(b);
|
415
|
+
|
416
|
+
if(RSTRING_LEN(str) == 0) {
|
417
|
+
return Qnil;
|
418
|
+
} else {
|
419
|
+
return str;
|
420
|
+
}
|
421
|
+
}
|
422
|
+
#endif
|
423
|
+
|
424
|
+
MAKE_EMPTY_STRING(out);
|
425
|
+
read_until_eof(b, out, n);
|
426
|
+
|
427
|
+
if(RSTRING_LEN(out) == 0) {
|
428
|
+
return Qnil;
|
429
|
+
} else {
|
430
|
+
return out;
|
431
|
+
}
|
432
|
+
}
|
433
|
+
|
434
|
+
static VALUE Buffer_to_str(VALUE self)
|
435
|
+
{
|
436
|
+
BUFFER(self, b);
|
437
|
+
return msgpack_buffer_all_as_string(b);
|
438
|
+
}
|
439
|
+
|
440
|
+
static VALUE Buffer_to_a(VALUE self)
|
441
|
+
{
|
442
|
+
BUFFER(self, b);
|
443
|
+
return msgpack_buffer_all_as_string_array(b);
|
444
|
+
}
|
445
|
+
|
446
|
+
static VALUE Buffer_flush(VALUE self)
|
447
|
+
{
|
448
|
+
BUFFER(self, b);
|
449
|
+
msgpack_buffer_flush(b);
|
450
|
+
return self;
|
451
|
+
}
|
452
|
+
|
453
|
+
static VALUE Buffer_io(VALUE self)
|
454
|
+
{
|
455
|
+
BUFFER(self, b);
|
456
|
+
return b->io;
|
457
|
+
}
|
458
|
+
|
459
|
+
static VALUE Buffer_close(VALUE self)
|
460
|
+
{
|
461
|
+
BUFFER(self, b);
|
462
|
+
if(b->io != Qnil) {
|
463
|
+
return rb_funcall(b->io, s_close, 0);
|
464
|
+
}
|
465
|
+
return Qnil;
|
466
|
+
}
|
467
|
+
|
468
|
+
static VALUE Buffer_write_to(VALUE self, VALUE io)
|
469
|
+
{
|
470
|
+
BUFFER(self, b);
|
471
|
+
size_t sz = msgpack_buffer_flush_to_io(b, io, s_write, true);
|
472
|
+
return ULONG2NUM(sz);
|
473
|
+
}
|
474
|
+
|
475
|
+
void MessagePack_Buffer_module_init(VALUE mMessagePack)
|
476
|
+
{
|
477
|
+
s_read = rb_intern("read");
|
478
|
+
s_readpartial = rb_intern("readpartial");
|
479
|
+
s_write = rb_intern("write");
|
480
|
+
s_append = rb_intern("<<");
|
481
|
+
s_close = rb_intern("close");
|
482
|
+
|
483
|
+
msgpack_buffer_static_init();
|
484
|
+
|
485
|
+
cMessagePack_Buffer = rb_define_class_under(mMessagePack, "Buffer", rb_cObject);
|
486
|
+
|
487
|
+
rb_define_alloc_func(cMessagePack_Buffer, Buffer_alloc);
|
488
|
+
|
489
|
+
rb_define_method(cMessagePack_Buffer, "initialize", Buffer_initialize, -1);
|
490
|
+
rb_define_method(cMessagePack_Buffer, "clear", Buffer_clear, 0);
|
491
|
+
rb_define_method(cMessagePack_Buffer, "size", Buffer_size, 0);
|
492
|
+
rb_define_method(cMessagePack_Buffer, "empty?", Buffer_empty_p, 0);
|
493
|
+
rb_define_method(cMessagePack_Buffer, "write", Buffer_write, 1);
|
494
|
+
rb_define_method(cMessagePack_Buffer, "<<", Buffer_append, 1);
|
495
|
+
rb_define_method(cMessagePack_Buffer, "skip", Buffer_skip, 1);
|
496
|
+
rb_define_method(cMessagePack_Buffer, "skip_all", Buffer_skip_all, 1);
|
497
|
+
rb_define_method(cMessagePack_Buffer, "read", Buffer_read, -1);
|
498
|
+
rb_define_method(cMessagePack_Buffer, "read_all", Buffer_read_all, -1);
|
499
|
+
rb_define_method(cMessagePack_Buffer, "io", Buffer_io, 0);
|
500
|
+
rb_define_method(cMessagePack_Buffer, "flush", Buffer_flush, 0);
|
501
|
+
rb_define_method(cMessagePack_Buffer, "close", Buffer_close, 0);
|
502
|
+
rb_define_method(cMessagePack_Buffer, "write_to", Buffer_write_to, 1);
|
503
|
+
rb_define_method(cMessagePack_Buffer, "to_str", Buffer_to_str, 0);
|
504
|
+
rb_define_alias(cMessagePack_Buffer, "to_s", "to_str");
|
505
|
+
rb_define_method(cMessagePack_Buffer, "to_a", Buffer_to_a, 0);
|
506
|
+
}
|
507
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#ifndef MSGPACK_RUBY_BUFFER_CLASS_H__
|
19
|
+
#define MSGPACK_RUBY_BUFFER_CLASS_H__
|
20
|
+
|
21
|
+
#include "buffer.h"
|
22
|
+
|
23
|
+
extern VALUE cMessagePack_Buffer;
|
24
|
+
|
25
|
+
void MessagePack_Buffer_module_init(VALUE mMessagePack);
|
26
|
+
|
27
|
+
VALUE MessagePack_Buffer_wrap(msgpack_buffer_t* b, VALUE owner);
|
28
|
+
|
29
|
+
void MessagePack_Buffer_initialize(msgpack_buffer_t* b, VALUE io, VALUE options);
|
30
|
+
|
31
|
+
#endif
|
32
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#ifndef MSGPACK_RUBY_COMPAT_H__
|
19
|
+
#define MSGPACK_RUBY_COMPAT_H__
|
20
|
+
|
21
|
+
#include "ruby.h"
|
22
|
+
|
23
|
+
#if defined(HAVE_RUBY_ST_H)
|
24
|
+
# include "ruby/st.h" /* ruby hash on Ruby 1.9 */
|
25
|
+
#elif defined(HAVE_ST_H)
|
26
|
+
# include "st.h" /* ruby hash on Ruby 1.8 */
|
27
|
+
#endif
|
28
|
+
|
29
|
+
|
30
|
+
/*
|
31
|
+
* COMPAT_HAVE_ENCODING
|
32
|
+
*/
|
33
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
34
|
+
# include "ruby/encoding.h"
|
35
|
+
# define COMPAT_HAVE_ENCODING
|
36
|
+
#endif
|
37
|
+
|
38
|
+
#if defined(__MACRUBY__) /* MacRuby */
|
39
|
+
# undef COMPAT_HAVE_ENCODING
|
40
|
+
#endif
|
41
|
+
|
42
|
+
|
43
|
+
/*
|
44
|
+
* define STR_DUP_LIKELY_DOES_COPY
|
45
|
+
* check rb_str_dup actually copies the string or not
|
46
|
+
*/
|
47
|
+
#if defined(RUBY_VM) && defined(FL_ALL) && defined(FL_USER1) && defined(FL_USER3) /* MRI 1.9 */
|
48
|
+
# define STR_DUP_LIKELY_DOES_COPY(str) FL_ALL(str, FL_USER1|FL_USER3) /* same as STR_ASSOC_P(str) */
|
49
|
+
|
50
|
+
#elif defined(FL_TEST) && defined(ELTS_SHARED) /* MRI 1.8 */
|
51
|
+
# define STR_DUP_LIKELY_DOES_COPY(str) (!FL_TEST(str, ELTS_SHARED))
|
52
|
+
|
53
|
+
//#elif defined(RUBINIUS) || defined(JRUBY) /* Rubinius and JRuby */
|
54
|
+
#else
|
55
|
+
# define STR_DUP_LIKELY_DOES_COPY(str) (1)
|
56
|
+
|
57
|
+
#endif
|
58
|
+
|
59
|
+
|
60
|
+
/*
|
61
|
+
* SIZET2NUM
|
62
|
+
*/
|
63
|
+
#ifndef SIZET2NUM /* MRI 1.8 */
|
64
|
+
# define SIZET2NUM(v) ULL2NUM(v)
|
65
|
+
#endif
|
66
|
+
|
67
|
+
|
68
|
+
/*
|
69
|
+
* rb_errinfo()
|
70
|
+
*/
|
71
|
+
#if defined(RUBY_VM) /* MRI 1.9 */
|
72
|
+
# define COMPAT_RERAISE rb_exc_raise(rb_errinfo())
|
73
|
+
|
74
|
+
#elif defined(JRUBY) /* JRuby */
|
75
|
+
# define COMPAT_RERAISE rb_exc_raise(rb_gv_get("$!"))
|
76
|
+
|
77
|
+
#else /* MRI 1.8 and Rubinius */
|
78
|
+
# define COMPAT_RERAISE rb_exc_raise(ruby_errinfo)
|
79
|
+
#endif
|
80
|
+
|
81
|
+
|
82
|
+
/*
|
83
|
+
* RBIGNUM_POSITIVE_P
|
84
|
+
*/
|
85
|
+
#ifndef RBIGNUM_POSITIVE_P
|
86
|
+
# if defined(RUBINIUS) /* Rubinius <= v1.2.3 */
|
87
|
+
# define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
|
88
|
+
|
89
|
+
# elif defined(JRUBY) /* JRuby */
|
90
|
+
# define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
|
91
|
+
# define rb_big2ull(b) rb_num2ull(b)
|
92
|
+
/*#define rb_big2ll(b) rb_num2ll(b)*/
|
93
|
+
|
94
|
+
# else /* MRI 1.8 */
|
95
|
+
# define RBIGNUM_POSITIVE_P(b) (RBIGNUM(b)->sign)
|
96
|
+
# endif
|
97
|
+
#endif
|
98
|
+
|
99
|
+
|
100
|
+
/*
|
101
|
+
* RSTRING_PTR, RSTRING_LEN
|
102
|
+
*/
|
103
|
+
#ifndef RSTRING_PTR /* MRI 1.8.5 */
|
104
|
+
# define RSTRING_PTR(s) (RSTRING(s)->ptr)
|
105
|
+
#endif
|
106
|
+
|
107
|
+
#ifndef RSTRING_LEN /* MRI 1.8.5 */
|
108
|
+
# define RSTRING_LEN(s) (RSTRING(s)->len)
|
109
|
+
#endif
|
110
|
+
|
111
|
+
|
112
|
+
#endif
|
113
|
+
|