msgpack-ably 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.travis.yml +26 -0
  4. data/ChangeLog +101 -0
  5. data/README.rdoc +129 -0
  6. data/Rakefile +110 -0
  7. data/doclib/msgpack.rb +77 -0
  8. data/doclib/msgpack/buffer.rb +193 -0
  9. data/doclib/msgpack/core_ext.rb +101 -0
  10. data/doclib/msgpack/error.rb +14 -0
  11. data/doclib/msgpack/packer.rb +134 -0
  12. data/doclib/msgpack/unpacker.rb +146 -0
  13. data/ext/msgpack/buffer.c +678 -0
  14. data/ext/msgpack/buffer.h +441 -0
  15. data/ext/msgpack/buffer_class.c +507 -0
  16. data/ext/msgpack/buffer_class.h +32 -0
  17. data/ext/msgpack/compat.h +113 -0
  18. data/ext/msgpack/core_ext.c +129 -0
  19. data/ext/msgpack/core_ext.h +26 -0
  20. data/ext/msgpack/extconf.rb +28 -0
  21. data/ext/msgpack/packer.c +168 -0
  22. data/ext/msgpack/packer.h +429 -0
  23. data/ext/msgpack/packer_class.c +302 -0
  24. data/ext/msgpack/packer_class.h +30 -0
  25. data/ext/msgpack/rbinit.c +33 -0
  26. data/ext/msgpack/rmem.c +94 -0
  27. data/ext/msgpack/rmem.h +109 -0
  28. data/ext/msgpack/sysdep.h +115 -0
  29. data/ext/msgpack/sysdep_endian.h +50 -0
  30. data/ext/msgpack/sysdep_types.h +46 -0
  31. data/ext/msgpack/unpacker.c +781 -0
  32. data/ext/msgpack/unpacker.h +122 -0
  33. data/ext/msgpack/unpacker_class.c +405 -0
  34. data/ext/msgpack/unpacker_class.h +32 -0
  35. data/lib/msgpack.rb +6 -0
  36. data/lib/msgpack/version.rb +3 -0
  37. data/msgpack.gemspec +26 -0
  38. data/msgpack.org.md +49 -0
  39. data/spec/cases.json +1 -0
  40. data/spec/cases.msg +0 -0
  41. data/spec/cases_compact.msg +0 -0
  42. data/spec/cases_spec.rb +39 -0
  43. data/spec/cruby/buffer_io_spec.rb +256 -0
  44. data/spec/cruby/buffer_packer.rb +29 -0
  45. data/spec/cruby/buffer_spec.rb +572 -0
  46. data/spec/cruby/buffer_unpacker.rb +19 -0
  47. data/spec/format_spec.rb +256 -0
  48. data/spec/packer_spec.rb +120 -0
  49. data/spec/random_compat.rb +24 -0
  50. data/spec/spec_helper.rb +21 -0
  51. data/spec/unpacker_spec.rb +305 -0
  52. metadata +195 -0
@@ -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
+
@@ -0,0 +1,129 @@
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 "core_ext.h"
20
+ #include "packer.h"
21
+ #include "packer_class.h"
22
+
23
+ static inline VALUE delegete_to_pack(int argc, VALUE* argv, VALUE self)
24
+ {
25
+ if(argc == 0) {
26
+ return MessagePack_pack(1, &self);
27
+ } else if(argc == 1) {
28
+ /* write to io */
29
+ VALUE argv2[2];
30
+ argv2[0] = self;
31
+ argv2[1] = argv[0];
32
+ return MessagePack_pack(2, argv2);
33
+ } else {
34
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
35
+ }
36
+ }
37
+
38
+ #define ENSURE_PACKER(argc, argv, packer, pk) \
39
+ if(argc != 1 || rb_class_of(argv[0]) != cMessagePack_Packer) { \
40
+ return delegete_to_pack(argc, argv, self); \
41
+ } \
42
+ VALUE packer = argv[0]; \
43
+ msgpack_packer_t *pk; \
44
+ Data_Get_Struct(packer, msgpack_packer_t, pk);
45
+
46
+ static VALUE NilClass_to_msgpack(int argc, VALUE* argv, VALUE self)
47
+ {
48
+ ENSURE_PACKER(argc, argv, packer, pk);
49
+ msgpack_packer_write_nil(pk);
50
+ return packer;
51
+ }
52
+
53
+ static VALUE TrueClass_to_msgpack(int argc, VALUE* argv, VALUE self)
54
+ {
55
+ ENSURE_PACKER(argc, argv, packer, pk);
56
+ msgpack_packer_write_true(pk);
57
+ return packer;
58
+ }
59
+
60
+ static VALUE FalseClass_to_msgpack(int argc, VALUE* argv, VALUE self)
61
+ {
62
+ ENSURE_PACKER(argc, argv, packer, pk);
63
+ msgpack_packer_write_false(pk);
64
+ return packer;
65
+ }
66
+
67
+ static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
68
+ {
69
+ ENSURE_PACKER(argc, argv, packer, pk);
70
+ msgpack_packer_write_fixnum_value(pk, self);
71
+ return packer;
72
+ }
73
+
74
+ static VALUE Bignum_to_msgpack(int argc, VALUE* argv, VALUE self)
75
+ {
76
+ ENSURE_PACKER(argc, argv, packer, pk);
77
+ msgpack_packer_write_bignum_value(pk, self);
78
+ return packer;
79
+ }
80
+
81
+ static VALUE Float_to_msgpack(int argc, VALUE* argv, VALUE self)
82
+ {
83
+ ENSURE_PACKER(argc, argv, packer, pk);
84
+ msgpack_packer_write_float_value(pk, self);
85
+ return packer;
86
+ }
87
+
88
+ static VALUE String_to_msgpack(int argc, VALUE* argv, VALUE self)
89
+ {
90
+ ENSURE_PACKER(argc, argv, packer, pk);
91
+ msgpack_packer_write_string_value(pk, self);
92
+ return packer;
93
+ }
94
+
95
+ static VALUE Array_to_msgpack(int argc, VALUE* argv, VALUE self)
96
+ {
97
+ ENSURE_PACKER(argc, argv, packer, pk);
98
+ msgpack_packer_write_array_value(pk, self);
99
+ return packer;
100
+ }
101
+
102
+ static VALUE Hash_to_msgpack(int argc, VALUE* argv, VALUE self)
103
+ {
104
+ ENSURE_PACKER(argc, argv, packer, pk);
105
+ msgpack_packer_write_hash_value(pk, self);
106
+ return packer;
107
+ }
108
+
109
+ static VALUE Symbol_to_msgpack(int argc, VALUE* argv, VALUE self)
110
+ {
111
+ ENSURE_PACKER(argc, argv, packer, pk);
112
+ msgpack_packer_write_symbol_value(pk, self);
113
+ return packer;
114
+ }
115
+
116
+ void MessagePack_core_ext_module_init()
117
+ {
118
+ rb_define_method(rb_cNilClass, "to_msgpack", NilClass_to_msgpack, -1);
119
+ rb_define_method(rb_cTrueClass, "to_msgpack", TrueClass_to_msgpack, -1);
120
+ rb_define_method(rb_cFalseClass, "to_msgpack", FalseClass_to_msgpack, -1);
121
+ rb_define_method(rb_cFixnum, "to_msgpack", Fixnum_to_msgpack, -1);
122
+ rb_define_method(rb_cBignum, "to_msgpack", Bignum_to_msgpack, -1);
123
+ rb_define_method(rb_cFloat, "to_msgpack", Float_to_msgpack, -1);
124
+ rb_define_method(rb_cString, "to_msgpack", String_to_msgpack, -1);
125
+ rb_define_method(rb_cArray, "to_msgpack", Array_to_msgpack, -1);
126
+ rb_define_method(rb_cHash, "to_msgpack", Hash_to_msgpack, -1);
127
+ rb_define_method(rb_cSymbol, "to_msgpack", Symbol_to_msgpack, -1);
128
+ }
129
+
@@ -0,0 +1,26 @@
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_CORE_EXT_H__
19
+ #define MSGPACK_RUBY_CORE_EXT_H__
20
+
21
+ #include "compat.h"
22
+
23
+ void MessagePack_core_ext_module_init();
24
+
25
+ #endif
26
+
@@ -0,0 +1,28 @@
1
+ require 'mkmf'
2
+
3
+ have_header("ruby/st.h")
4
+ have_header("st.h")
5
+ have_func("rb_str_replace", ["ruby.h"])
6
+ have_func("rb_intern_str", ["ruby.h"])
7
+ have_func("rb_sym2str", ["ruby.h"])
8
+ have_func("rb_str_intern", ["ruby.h"])
9
+
10
+ $CFLAGS << %[ -I.. -Wall -O3 -g -std=c99]
11
+ #$CFLAGS << %[ -DDISABLE_RMEM]
12
+ #$CFLAGS << %[ -DDISABLE_RMEM_REUSE_INTERNAL_FRAGMENT]
13
+ #$CFLAGS << %[ -DDISABLE_BUFFER_READ_REFERENCE_OPTIMIZE]
14
+ #$CFLAGS << %[ -DDISABLE_BUFFER_READ_TO_S_OPTIMIZE]
15
+
16
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
17
+ # msgpack-ruby doesn't modify data came from RSTRING_PTR(str)
18
+ $CFLAGS << %[ -DRSTRING_NOT_MODIFIED]
19
+ # Rubinius C extensions don't grab GVL while rmem is not thread safe
20
+ $CFLAGS << %[ -DDISABLE_RMEM]
21
+ end
22
+
23
+ if warnflags = CONFIG['warnflags']
24
+ warnflags.slice!(/ -Wdeclaration-after-statement/)
25
+ end
26
+
27
+ create_makefile('msgpack/msgpack')
28
+
@@ -0,0 +1,168 @@
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 "packer.h"
20
+
21
+ #ifdef RUBINIUS
22
+ static ID s_to_iter;
23
+ static ID s_next;
24
+ static ID s_key;
25
+ static ID s_value;
26
+ #endif
27
+
28
+ void msgpack_packer_static_init()
29
+ {
30
+ #ifdef RUBINIUS
31
+ s_to_iter = rb_intern("to_iter");
32
+ s_next = rb_intern("next");
33
+ s_key = rb_intern("key");
34
+ s_value = rb_intern("value");
35
+ #endif
36
+ }
37
+
38
+ void msgpack_packer_static_destroy()
39
+ { }
40
+
41
+ void msgpack_packer_init(msgpack_packer_t* pk)
42
+ {
43
+ memset(pk, 0, sizeof(msgpack_packer_t));
44
+
45
+ msgpack_buffer_init(PACKER_BUFFER_(pk));
46
+
47
+ pk->io = Qnil;
48
+ }
49
+
50
+ void msgpack_packer_destroy(msgpack_packer_t* pk)
51
+ {
52
+ msgpack_buffer_destroy(PACKER_BUFFER_(pk));
53
+ }
54
+
55
+ void msgpack_packer_mark(msgpack_packer_t* pk)
56
+ {
57
+ rb_gc_mark(pk->io);
58
+
59
+ /* See MessagePack_Buffer_wrap */
60
+ /* msgpack_buffer_mark(PACKER_BUFFER_(pk)); */
61
+ rb_gc_mark(pk->buffer_ref);
62
+ }
63
+
64
+ void msgpack_packer_reset(msgpack_packer_t* pk)
65
+ {
66
+ msgpack_buffer_clear(PACKER_BUFFER_(pk));
67
+
68
+ pk->io = Qnil;
69
+ pk->io_write_all_method = 0;
70
+ pk->buffer_ref = Qnil;
71
+ }
72
+
73
+
74
+ void msgpack_packer_write_array_value(msgpack_packer_t* pk, VALUE v)
75
+ {
76
+ /* actual return type of RARRAY_LEN is long */
77
+ unsigned long len = RARRAY_LEN(v);
78
+ if(len > 0xffffffffUL) {
79
+ rb_raise(rb_eArgError, "size of array is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
80
+ }
81
+ unsigned int len32 = (unsigned int)len;
82
+ msgpack_packer_write_array_header(pk, len32);
83
+
84
+ unsigned int i;
85
+ for(i=0; i < len32; ++i) {
86
+ VALUE e = rb_ary_entry(v, i);
87
+ msgpack_packer_write_value(pk, e);
88
+ }
89
+ }
90
+
91
+ static int write_hash_foreach(VALUE key, VALUE value, VALUE pk_value)
92
+ {
93
+ if (key == Qundef) {
94
+ return ST_CONTINUE;
95
+ }
96
+ msgpack_packer_t* pk = (msgpack_packer_t*) pk_value;
97
+ msgpack_packer_write_value(pk, key);
98
+ msgpack_packer_write_value(pk, value);
99
+ return ST_CONTINUE;
100
+ }
101
+
102
+ void msgpack_packer_write_hash_value(msgpack_packer_t* pk, VALUE v)
103
+ {
104
+ /* actual return type of RHASH_SIZE is long (if SIZEOF_LONG == SIZEOF_VOIDP
105
+ * or long long (if SIZEOF_LONG_LONG == SIZEOF_VOIDP. See st.h. */
106
+ unsigned long len = RHASH_SIZE(v);
107
+ if(len > 0xffffffffUL) {
108
+ rb_raise(rb_eArgError, "size of array is too long to pack: %ld bytes should be <= %lu", len, 0xffffffffUL);
109
+ }
110
+ unsigned int len32 = (unsigned int)len;
111
+ msgpack_packer_write_map_header(pk, len32);
112
+
113
+ #ifdef RUBINIUS
114
+ VALUE iter = rb_funcall(v, s_to_iter, 0);
115
+ VALUE entry = Qnil;
116
+ while(RTEST(entry = rb_funcall(iter, s_next, 1, entry))) {
117
+ VALUE key = rb_funcall(entry, s_key, 0);
118
+ VALUE val = rb_funcall(entry, s_value, 0);
119
+ write_hash_foreach(key, val, (VALUE) pk);
120
+ }
121
+ #else
122
+ rb_hash_foreach(v, write_hash_foreach, (VALUE) pk);
123
+ #endif
124
+ }
125
+
126
+ static void _msgpack_packer_write_other_value(msgpack_packer_t* pk, VALUE v)
127
+ {
128
+ rb_funcall(v, pk->to_msgpack_method, 1, pk->to_msgpack_arg);
129
+ }
130
+
131
+ void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v)
132
+ {
133
+ switch(rb_type(v)) {
134
+ case T_NIL:
135
+ msgpack_packer_write_nil(pk);
136
+ break;
137
+ case T_TRUE:
138
+ msgpack_packer_write_true(pk);
139
+ break;
140
+ case T_FALSE:
141
+ msgpack_packer_write_false(pk);
142
+ break;
143
+ case T_FIXNUM:
144
+ msgpack_packer_write_fixnum_value(pk, v);
145
+ break;
146
+ case T_SYMBOL:
147
+ msgpack_packer_write_symbol_value(pk, v);
148
+ break;
149
+ case T_STRING:
150
+ msgpack_packer_write_string_value(pk, v);
151
+ break;
152
+ case T_ARRAY:
153
+ msgpack_packer_write_array_value(pk, v);
154
+ break;
155
+ case T_HASH:
156
+ msgpack_packer_write_hash_value(pk, v);
157
+ break;
158
+ case T_BIGNUM:
159
+ msgpack_packer_write_bignum_value(pk, v);
160
+ break;
161
+ case T_FLOAT:
162
+ msgpack_packer_write_float_value(pk, v);
163
+ break;
164
+ default:
165
+ _msgpack_packer_write_other_value(pk, v);
166
+ }
167
+ }
168
+