gitlab-cbor 1.0.0

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.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.gitlab-ci.yml +44 -0
  4. data/.travis.yml +24 -0
  5. data/CHANGELOG.md +30 -0
  6. data/ChangeLog +106 -0
  7. data/Gemfile +11 -0
  8. data/README.rdoc +191 -0
  9. data/Rakefile +97 -0
  10. data/ci/test.rb +31 -0
  11. data/doclib/cbor/buffer.rb +193 -0
  12. data/doclib/cbor/core_ext.rb +133 -0
  13. data/doclib/cbor/error.rb +14 -0
  14. data/doclib/cbor/packer.rb +133 -0
  15. data/doclib/cbor/simple.rb +15 -0
  16. data/doclib/cbor/tagged.rb +16 -0
  17. data/doclib/cbor/unpacker.rb +138 -0
  18. data/doclib/cbor.rb +80 -0
  19. data/ext/cbor/buffer.c +693 -0
  20. data/ext/cbor/buffer.h +484 -0
  21. data/ext/cbor/buffer_class.c +561 -0
  22. data/ext/cbor/buffer_class.h +41 -0
  23. data/ext/cbor/cbor.h +69 -0
  24. data/ext/cbor/compat.h +147 -0
  25. data/ext/cbor/core_ext.c +199 -0
  26. data/ext/cbor/core_ext.h +35 -0
  27. data/ext/cbor/extconf.rb +29 -0
  28. data/ext/cbor/packer.c +169 -0
  29. data/ext/cbor/packer.h +370 -0
  30. data/ext/cbor/packer_class.c +345 -0
  31. data/ext/cbor/packer_class.h +40 -0
  32. data/ext/cbor/rbinit.c +57 -0
  33. data/ext/cbor/renamer.h +56 -0
  34. data/ext/cbor/rmem.c +103 -0
  35. data/ext/cbor/rmem.h +118 -0
  36. data/ext/cbor/sysdep.h +139 -0
  37. data/ext/cbor/sysdep_endian.h +59 -0
  38. data/ext/cbor/sysdep_types.h +55 -0
  39. data/ext/cbor/unpacker.c +787 -0
  40. data/ext/cbor/unpacker.h +135 -0
  41. data/ext/cbor/unpacker_class.c +483 -0
  42. data/ext/cbor/unpacker_class.h +39 -0
  43. data/gitlab-cbor.gemspec +27 -0
  44. data/lib/cbor/version.rb +3 -0
  45. data/lib/cbor.rb +6 -0
  46. data/spec/buffer_io_spec.rb +260 -0
  47. data/spec/buffer_spec.rb +576 -0
  48. data/spec/cases.cbor +0 -0
  49. data/spec/cases.cbor_stream +0 -0
  50. data/spec/cases.json +1 -0
  51. data/spec/cases.msg +0 -0
  52. data/spec/cases_compact.msg +0 -0
  53. data/spec/cases_spec.rb +39 -0
  54. data/spec/format_spec.rb +603 -0
  55. data/spec/packer_spec.rb +127 -0
  56. data/spec/random_compat.rb +24 -0
  57. data/spec/spec_helper.rb +68 -0
  58. data/spec/unpacker_spec.rb +260 -0
  59. metadata +189 -0
data/ext/cbor/compat.h ADDED
@@ -0,0 +1,147 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_COMPAT_H__
28
+ #define MSGPACK_RUBY_COMPAT_H__
29
+
30
+ #include "ruby.h"
31
+
32
+ #if defined(HAVE_RUBY_ST_H)
33
+ # include "ruby/st.h" /* ruby hash on Ruby 1.9 */
34
+ #elif defined(HAVE_ST_H)
35
+ # include "st.h" /* ruby hash on Ruby 1.8 */
36
+ #endif
37
+
38
+
39
+ /*
40
+ * COMPAT_HAVE_ENCODING
41
+ */
42
+ #ifdef HAVE_RUBY_ENCODING_H
43
+ # include "ruby/encoding.h"
44
+ # define COMPAT_HAVE_ENCODING
45
+ #endif
46
+
47
+ #if defined(__MACRUBY__) /* MacRuby */
48
+ # undef COMPAT_HAVE_ENCODING
49
+ #endif
50
+
51
+
52
+ /*
53
+ * define STR_DUP_LIKELY_DOES_COPY
54
+ * check rb_str_dup actually copies the string or not
55
+ */
56
+ #if defined(RUBY_VM) && defined(FL_ALL) && defined(FL_USER1) && defined(FL_USER3) /* MRI 1.9 */
57
+ # define STR_DUP_LIKELY_DOES_COPY(str) FL_ALL(str, FL_USER1|FL_USER3) /* same as STR_ASSOC_P(str) */
58
+
59
+ #elif defined(FL_TEST) && defined(ELTS_SHARED) /* MRI 1.8 */
60
+ # define STR_DUP_LIKELY_DOES_COPY(str) (!FL_TEST(str, ELTS_SHARED))
61
+
62
+ //#elif defined(RUBINIUS) || defined(JRUBY) /* Rubinius and JRuby */
63
+ #else
64
+ # define STR_DUP_LIKELY_DOES_COPY(str) (1)
65
+
66
+ #endif
67
+
68
+
69
+ /*
70
+ * SIZET2NUM
71
+ */
72
+ #ifndef SIZET2NUM /* MRI 1.8 */
73
+ # define SIZET2NUM(v) ULL2NUM(v)
74
+ #endif
75
+
76
+
77
+ /*
78
+ * rb_errinfo()
79
+ */
80
+ #if defined(RUBY_VM) /* MRI 1.9 */
81
+ # define COMPAT_RERAISE rb_exc_raise(rb_errinfo())
82
+
83
+ #elif defined(JRUBY) /* JRuby */
84
+ # define COMPAT_RERAISE rb_exc_raise(rb_gv_get("$!"))
85
+
86
+ #else /* MRI 1.8 and Rubinius */
87
+ # define COMPAT_RERAISE rb_exc_raise(ruby_errinfo)
88
+ #endif
89
+
90
+
91
+ /*
92
+ * RBIGNUM_POSITIVE_P
93
+ */
94
+ #ifndef RBIGNUM_POSITIVE_P
95
+ # if defined(RUBINIUS) /* Rubinius <= v1.2.3 */
96
+ # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
97
+
98
+ # elif defined(JRUBY) /* JRuby */
99
+ # define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue)
100
+ # define rb_big2ull(b) rb_num2ull(b)
101
+ /*#define rb_big2ll(b) rb_num2ll(b)*/
102
+
103
+ # else /* MRI 1.8 */
104
+ # define RBIGNUM_POSITIVE_P(b) (RBIGNUM(b)->sign)
105
+ # endif
106
+ #endif
107
+
108
+
109
+ #ifndef HAVE_RB_INTEGER_UNPACK
110
+
111
+ /* More MRI 1.8 */
112
+ #ifndef RBIGNUM_LEN
113
+ #define RBIGNUM_LEN(b) (RBIGNUM(b)->len)
114
+ #endif
115
+ #ifndef RBIGNUM_DIGITS
116
+ #ifndef RBIGNUM
117
+ #define CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
118
+ #endif
119
+ #define RBIGNUM_DIGITS(b) (RBIGNUM(b)->digits)
120
+ #endif
121
+ #ifndef HAVE_RB_BIG_NEW
122
+ /* not really worth fixing any more... */
123
+ #define CANT_DO_BIGNUMS_FAST_ON_THIS_PLATFORM
124
+ /* gross 1.8.7 hack thanks to Mathieu Bouchard <matju@artengine.ca> */
125
+ #define rb_big_new(len, sign) rb_funcall(INT2FIX(1),rb_intern("<<"),1,INT2FIX(len > 0 ? ((len) * SIZEOF_BDIGITS * 8) - 1 : 0));
126
+ #endif
127
+
128
+ #endif
129
+
130
+ #ifndef RB_TYPE_P
131
+ #define RB_TYPE_P(obj, type) (TYPE(obj) == (type))
132
+ #endif
133
+
134
+ /*
135
+ * RSTRING_PTR, RSTRING_LEN
136
+ */
137
+ #ifndef RSTRING_PTR /* MRI 1.8.5 */
138
+ # define RSTRING_PTR(s) (RSTRING(s)->ptr)
139
+ #endif
140
+
141
+ #ifndef RSTRING_LEN /* MRI 1.8.5 */
142
+ # define RSTRING_LEN(s) (RSTRING(s)->len)
143
+ #endif
144
+
145
+
146
+ #endif
147
+
@@ -0,0 +1,199 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+
28
+ #include "core_ext.h"
29
+ #include "packer.h"
30
+ #include "packer_class.h"
31
+
32
+ static inline VALUE delegete_to_pack(int argc, VALUE* argv, VALUE self)
33
+ {
34
+ if(argc == 0) {
35
+ return MessagePack_pack(1, &self);
36
+ } else if(argc == 1) {
37
+ /* write to io */
38
+ VALUE argv2[2];
39
+ argv2[0] = self;
40
+ argv2[1] = argv[0];
41
+ return MessagePack_pack(2, argv2);
42
+ } else {
43
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
44
+ }
45
+ }
46
+
47
+ #define ENSURE_PACKER(argc, argv, packer, pk) \
48
+ if(argc != 1 || rb_class_of(argv[0]) != cMessagePack_Packer) { \
49
+ return delegete_to_pack(argc, argv, self); \
50
+ } \
51
+ VALUE packer = argv[0]; \
52
+ msgpack_packer_t *pk = MessagePack_Packer_get(packer);
53
+
54
+ static VALUE NilClass_to_msgpack(int argc, VALUE* argv, VALUE self)
55
+ {
56
+ ENSURE_PACKER(argc, argv, packer, pk);
57
+ msgpack_packer_write_nil(pk);
58
+ return packer;
59
+ }
60
+
61
+ static VALUE TrueClass_to_msgpack(int argc, VALUE* argv, VALUE self)
62
+ {
63
+ ENSURE_PACKER(argc, argv, packer, pk);
64
+ msgpack_packer_write_true(pk);
65
+ return packer;
66
+ }
67
+
68
+ static VALUE FalseClass_to_msgpack(int argc, VALUE* argv, VALUE self)
69
+ {
70
+ ENSURE_PACKER(argc, argv, packer, pk);
71
+ msgpack_packer_write_false(pk);
72
+ return packer;
73
+ }
74
+
75
+ #ifdef RUBY_INTEGER_UNIFICATION
76
+
77
+ static VALUE Integer_to_msgpack(int argc, VALUE* argv, VALUE self)
78
+ {
79
+ ENSURE_PACKER(argc, argv, packer, pk);
80
+ if (FIXNUM_P(self))
81
+ msgpack_packer_write_fixnum_value(pk, self);
82
+ else
83
+ msgpack_packer_write_bignum_value(pk, self);
84
+ return packer;
85
+ }
86
+
87
+ #else
88
+
89
+ static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
90
+ {
91
+ ENSURE_PACKER(argc, argv, packer, pk);
92
+ msgpack_packer_write_fixnum_value(pk, self);
93
+ return packer;
94
+ }
95
+
96
+ static VALUE Bignum_to_msgpack(int argc, VALUE* argv, VALUE self)
97
+ {
98
+ ENSURE_PACKER(argc, argv, packer, pk);
99
+ msgpack_packer_write_bignum_value(pk, self);
100
+ return packer;
101
+ }
102
+
103
+ #endif
104
+
105
+ static VALUE Float_to_msgpack(int argc, VALUE* argv, VALUE self)
106
+ {
107
+ ENSURE_PACKER(argc, argv, packer, pk);
108
+ msgpack_packer_write_float_value(pk, self);
109
+ return packer;
110
+ }
111
+
112
+ static VALUE String_to_msgpack(int argc, VALUE* argv, VALUE self)
113
+ {
114
+ ENSURE_PACKER(argc, argv, packer, pk);
115
+ msgpack_packer_write_string_value(pk, self);
116
+ return packer;
117
+ }
118
+
119
+ static VALUE Array_to_msgpack(int argc, VALUE* argv, VALUE self)
120
+ {
121
+ ENSURE_PACKER(argc, argv, packer, pk);
122
+ msgpack_packer_write_array_value(pk, self);
123
+ return packer;
124
+ }
125
+
126
+ static VALUE Hash_to_msgpack(int argc, VALUE* argv, VALUE self)
127
+ {
128
+ ENSURE_PACKER(argc, argv, packer, pk);
129
+ msgpack_packer_write_hash_value(pk, self);
130
+ return packer;
131
+ }
132
+
133
+ static VALUE Symbol_to_msgpack(int argc, VALUE* argv, VALUE self)
134
+ {
135
+ ENSURE_PACKER(argc, argv, packer, pk);
136
+ msgpack_packer_write_symbol_value(pk, self);
137
+ return packer;
138
+ }
139
+
140
+ static VALUE Simple_to_msgpack(int argc, VALUE* argv, VALUE self)
141
+ {
142
+ ENSURE_PACKER(argc, argv, packer, pk);
143
+ msgpack_packer_write_simple_value(pk, self);
144
+ return packer;
145
+ }
146
+
147
+ static VALUE Tagged_to_msgpack(int argc, VALUE* argv, VALUE self)
148
+ {
149
+ ENSURE_PACKER(argc, argv, packer, pk);
150
+ msgpack_packer_write_tagged_value(pk, self);
151
+ return packer;
152
+ }
153
+
154
+ static VALUE Regexp_to_msgpack(int argc, VALUE* argv, VALUE self)
155
+ {
156
+ ENSURE_PACKER(argc, argv, packer, pk);
157
+ msgpack_packer_write_processed_value(pk, self, rb_intern("source"), TAG_RE);
158
+ return packer;
159
+ }
160
+
161
+ static VALUE URI_to_msgpack(int argc, VALUE* argv, VALUE self)
162
+ {
163
+ ENSURE_PACKER(argc, argv, packer, pk);
164
+ msgpack_packer_write_processed_value(pk, self, rb_intern("to_s"), TAG_URI);
165
+ return packer;
166
+ }
167
+
168
+ static VALUE Time_to_msgpack(int argc, VALUE* argv, VALUE self)
169
+ {
170
+ ENSURE_PACKER(argc, argv, packer, pk);
171
+ msgpack_packer_write_processed_value(pk, self, rb_intern("to_i"), TAG_TIME_EPOCH);
172
+ return packer;
173
+ }
174
+
175
+ void MessagePack_core_ext_module_init()
176
+ {
177
+ rb_define_method(rb_cNilClass, "to_cbor", NilClass_to_msgpack, -1);
178
+ rb_define_method(rb_cTrueClass, "to_cbor", TrueClass_to_msgpack, -1);
179
+ rb_define_method(rb_cFalseClass, "to_cbor", FalseClass_to_msgpack, -1);
180
+ #ifdef RUBY_INTEGER_UNIFICATION
181
+ rb_define_method(rb_cInteger, "to_cbor", Integer_to_msgpack, -1);
182
+ #else
183
+ rb_define_method(rb_cFixnum, "to_cbor", Fixnum_to_msgpack, -1);
184
+ rb_define_method(rb_cBignum, "to_cbor", Bignum_to_msgpack, -1);
185
+ #endif
186
+ rb_define_method(rb_cFloat, "to_cbor", Float_to_msgpack, -1);
187
+ rb_define_method(rb_cString, "to_cbor", String_to_msgpack, -1);
188
+ rb_define_method(rb_cArray, "to_cbor", Array_to_msgpack, -1);
189
+ rb_define_method(rb_cHash, "to_cbor", Hash_to_msgpack, -1);
190
+ rb_define_method(rb_cSymbol, "to_cbor", Symbol_to_msgpack, -1);
191
+ rb_define_method(rb_cTime, "to_cbor", Time_to_msgpack, -1);
192
+ rb_define_method(rb_cRegexp, "to_cbor", Regexp_to_msgpack, -1);
193
+ if (rb_const_defined(rb_cObject, rb_intern("URI"))) {
194
+ rb_define_method(rb_const_get(rb_cObject, rb_intern("URI")),
195
+ "to_cbor", URI_to_msgpack, -1);
196
+ }
197
+ rb_define_method(rb_cCBOR_Simple, "to_cbor", Simple_to_msgpack, -1);
198
+ rb_define_method(rb_cCBOR_Tagged, "to_cbor", Tagged_to_msgpack, -1);
199
+ }
@@ -0,0 +1,35 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+ #ifndef MSGPACK_RUBY_CORE_EXT_H__
28
+ #define MSGPACK_RUBY_CORE_EXT_H__
29
+
30
+ #include "compat.h"
31
+
32
+ void MessagePack_core_ext_module_init();
33
+
34
+ #endif
35
+
@@ -0,0 +1,29 @@
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_big_new", ["ruby.h"])
7
+ have_func("rb_intern_str", ["ruby.h"])
8
+ have_func("rb_sym2str", ["ruby.h"])
9
+ have_func("rb_str_intern", ["ruby.h"])
10
+ have_func("rb_integer_unpack", ["ruby.h"])
11
+
12
+ append_cflags(%w[-I.. -Wall -O3 -g -std=c99])
13
+ #$CFLAGS << %[ -DDISABLE_RMEM]
14
+ #$CFLAGS << %[ -DDISABLE_RMEM_REUSE_INTERNAL_FRAGMENT]
15
+ #$CFLAGS << %[ -DDISABLE_BUFFER_READ_REFERENCE_OPTIMIZE]
16
+ #$CFLAGS << %[ -DDISABLE_BUFFER_READ_TO_S_OPTIMIZE]
17
+
18
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
19
+ # msgpack-ruby doesn't modify data came from RSTRING_PTR(str)
20
+ append_cflags('-DRSTRING_NOT_MODIFIED')
21
+ # Rubinius C extensions don't grab GVL while rmem is not thread safe
22
+ append_cflags('-DDISABLE_RMEM')
23
+ end
24
+
25
+ if warnflags = CONFIG['warnflags']
26
+ warnflags.slice!(/ -Wdeclaration-after-statement/)
27
+ end
28
+
29
+ create_makefile('cbor/cbor')
data/ext/cbor/packer.c ADDED
@@ -0,0 +1,169 @@
1
+ /*
2
+ * CBOR for Ruby
3
+ *
4
+ * Copyright (C) 2013 Carsten Bormann
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License").
7
+ *
8
+ * Based on:
9
+ ***********/
10
+ /*
11
+ * MessagePack for Ruby
12
+ *
13
+ * Copyright (C) 2008-2013 Sadayuki Furuhashi
14
+ *
15
+ * Licensed under the Apache License, Version 2.0 (the "License");
16
+ * you may not use this file except in compliance with the License.
17
+ * You may obtain a copy of the License at
18
+ *
19
+ * http://www.apache.org/licenses/LICENSE-2.0
20
+ *
21
+ * Unless required by applicable law or agreed to in writing, software
22
+ * distributed under the License is distributed on an "AS IS" BASIS,
23
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ * See the License for the specific language governing permissions and
25
+ * limitations under the License.
26
+ */
27
+
28
+ #include "packer.h"
29
+
30
+ #ifdef RUBINIUS
31
+ static ID s_to_iter;
32
+ static ID s_next;
33
+ static ID s_key;
34
+ static ID s_value;
35
+ #endif
36
+
37
+ void msgpack_packer_static_init()
38
+ {
39
+ #ifdef RUBINIUS
40
+ s_to_iter = rb_intern("to_iter");
41
+ s_next = rb_intern("next");
42
+ s_key = rb_intern("key");
43
+ s_value = rb_intern("value");
44
+ #endif
45
+ }
46
+
47
+ void msgpack_packer_static_destroy()
48
+ { }
49
+
50
+ void msgpack_packer_init(msgpack_packer_t* pk)
51
+ {
52
+ memset(pk, 0, sizeof(msgpack_packer_t));
53
+
54
+ msgpack_buffer_init(PACKER_BUFFER_(pk));
55
+
56
+ pk->io = Qnil;
57
+ }
58
+
59
+ void msgpack_packer_destroy(msgpack_packer_t* pk)
60
+ {
61
+ msgpack_buffer_destroy(PACKER_BUFFER_(pk));
62
+ }
63
+
64
+ void msgpack_packer_mark(msgpack_packer_t* pk)
65
+ {
66
+ rb_gc_mark(pk->io);
67
+
68
+ /* See MessagePack_Buffer_wrap */
69
+ /* msgpack_buffer_mark(PACKER_BUFFER_(pk)); */
70
+ rb_gc_mark(pk->buffer_ref);
71
+ }
72
+
73
+ void msgpack_packer_reset(msgpack_packer_t* pk)
74
+ {
75
+ msgpack_buffer_clear(PACKER_BUFFER_(pk));
76
+
77
+ pk->io = Qnil;
78
+ pk->io_write_all_method = 0;
79
+ pk->buffer_ref = Qnil;
80
+ }
81
+
82
+
83
+ void msgpack_packer_write_array_value(msgpack_packer_t* pk, VALUE v)
84
+ {
85
+ /* actual return type of RARRAY_LEN is long */
86
+ unsigned long len = RARRAY_LEN(v);
87
+ msgpack_packer_write_array_header(pk, len);
88
+
89
+ unsigned long i;
90
+ for(i=0; i < len; ++i) {
91
+ VALUE e = rb_ary_entry(v, i);
92
+ msgpack_packer_write_value(pk, e);
93
+ }
94
+ }
95
+
96
+ static int write_hash_foreach(VALUE key, VALUE value, VALUE pk_value)
97
+ {
98
+ if (key == Qundef) {
99
+ return ST_CONTINUE;
100
+ }
101
+ msgpack_packer_t* pk = (msgpack_packer_t*) pk_value;
102
+ msgpack_packer_write_value(pk, key);
103
+ msgpack_packer_write_value(pk, value);
104
+ return ST_CONTINUE;
105
+ }
106
+
107
+ void msgpack_packer_write_hash_value(msgpack_packer_t* pk, VALUE v)
108
+ {
109
+ /* actual return type of RHASH_SIZE is long (if SIZEOF_LONG == SIZEOF_VOIDP
110
+ * or long long (if SIZEOF_LONG_LONG == SIZEOF_VOIDP. See st.h. */
111
+ unsigned long len = RHASH_SIZE(v);
112
+ msgpack_packer_write_map_header(pk, len);
113
+
114
+ #ifdef RUBINIUS
115
+ VALUE iter = rb_funcall(v, s_to_iter, 0);
116
+ VALUE entry = Qnil;
117
+ while(RTEST(entry = rb_funcall(iter, s_next, 1, entry))) {
118
+ VALUE key = rb_funcall(entry, s_key, 0);
119
+ VALUE val = rb_funcall(entry, s_value, 0);
120
+ write_hash_foreach(key, val, (VALUE) pk);
121
+ }
122
+ #else
123
+ rb_hash_foreach(v, write_hash_foreach, (VALUE) pk);
124
+ #endif
125
+ }
126
+
127
+ static void _msgpack_packer_write_other_value(msgpack_packer_t* pk, VALUE v)
128
+ {
129
+ rb_funcall(v, pk->to_msgpack_method, 1, pk->to_msgpack_arg);
130
+ }
131
+
132
+ void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v)
133
+ {
134
+ switch(rb_type(v)) {
135
+ case T_NIL:
136
+ msgpack_packer_write_nil(pk);
137
+ break;
138
+ case T_TRUE:
139
+ msgpack_packer_write_true(pk);
140
+ break;
141
+ case T_FALSE:
142
+ msgpack_packer_write_false(pk);
143
+ break;
144
+ case T_FIXNUM:
145
+ msgpack_packer_write_fixnum_value(pk, v);
146
+ break;
147
+ case T_SYMBOL:
148
+ msgpack_packer_write_symbol_value(pk, v);
149
+ break;
150
+ case T_STRING:
151
+ msgpack_packer_write_string_value(pk, v);
152
+ break;
153
+ case T_ARRAY:
154
+ msgpack_packer_write_array_value(pk, v);
155
+ break;
156
+ case T_HASH:
157
+ msgpack_packer_write_hash_value(pk, v);
158
+ break;
159
+ case T_BIGNUM:
160
+ msgpack_packer_write_bignum_value(pk, v);
161
+ break;
162
+ case T_FLOAT:
163
+ msgpack_packer_write_float_value(pk, v);
164
+ break;
165
+ default:
166
+ _msgpack_packer_write_other_value(pk, v);
167
+ }
168
+ }
169
+