msgpack 0.7.6-x86-mingw32 → 1.0.0-x86-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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcbde438631780821c89ee3c321dcfa32eedb09d
4
- data.tar.gz: 93e0835a1fdf5e0cdc90d45f394eaf315761bd8f
3
+ metadata.gz: 21e348c774582d02431d9bc7f4aa05c613b3d666
4
+ data.tar.gz: 8d5759682e567269c62ec32bedd6f09c33bba599
5
5
  SHA512:
6
- metadata.gz: 1c16d01185539cc87a0c75cd1348ce5e231824d6d34b56d79b080c197533061d16c80802b5b44cd25d3ff2ab8026fda33994b449be74da35a22da6022e2c4a42
7
- data.tar.gz: 53b0bb453d6e2941598c0c82ca209535a61660de38da697e92ada02c977136c98fadba8ec8582695ad3d0e9bd6e1c2c99dbfb185d51813f018a8be508e805ad6
6
+ metadata.gz: cf93dbee6bae73dc955856bbd61199489d125e01a79639a811cb8351261967cf0af10cae3d102a768042b54ef9e110a6ddcbe8e4a594d15b621a48d101f31306
7
+ data.tar.gz: 8f2f8256abcc4cdd6a091c8a58ceddbd9f80d7ad663f63f0d6b5b28119f357fda37b82dee86db8090049759e7c137ca64f9937d93cf3afff850ab11ba38ff4f0
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ 2016-07-08 version 1.0.0:
2
+
3
+ * Fix to be able to pack Symbol with ext types
4
+ * Fix for MRI 2.4 (Integer unification)
5
+
1
6
  2016-05-10 version 0.7.6:
2
7
 
3
8
  * Fix bug to raise IndexOutOfBoundsException for Bignum value with small int in JRuby
@@ -91,6 +91,36 @@ or event-driven style which works well with EventMachine:
91
91
 
92
92
  See {API reference}[http://ruby.msgpack.org/MessagePack/Unpacker.html] for details.
93
93
 
94
+ = Serializing and deserializing symbols
95
+
96
+ By default, symbols are serialized as strings:
97
+
98
+ packed = :symbol.to_msgpack # => "\xA6symbol"
99
+ MessagePack.unpack(packed) # => "symbol"
100
+
101
+ This can be customized by registering an extension type for them:
102
+
103
+ MessagePack::DefaultFactory.register_type(0x00, Symbol)
104
+
105
+ # symbols now survive round trips
106
+ packed = :symbol.to_msgpack # => "\xc7\x06\x00symbol"
107
+ MessagePack.unpack(packed) # => :symbol
108
+
109
+ The extension type for symbols is configurable like any other extension type.
110
+ For example, to customize how symbols are packed you can just redefine
111
+ Symbol#to_msgpack_ext. Doing this gives you an option to prevent symbols from
112
+ being serialized altogether by throwing an exception:
113
+
114
+ class Symbol
115
+ def to_msgpack_ext
116
+ raise "Serialization of symbols prohibited"
117
+ end
118
+ end
119
+
120
+ MessagePack::DefaultFactory.register_type(0x00, Symbol)
121
+
122
+ [1, :symbol, 'string'].to_msgpack # => RuntimeError: Serialization of symbols prohibited
123
+
94
124
  = Extension Types
95
125
 
96
126
  Packer and Unpacker support {Extension types of MessagePack}[https://github.com/msgpack/msgpack/blob/master/spec.md#types-extension-type].
@@ -0,0 +1,28 @@
1
+ require 'viiite'
2
+ require 'msgpack'
3
+
4
+ data = :symbol
5
+
6
+ Viiite.bench do |b|
7
+ b.variation_point :branch, `git rev-parse --abbrev-ref HEAD`
8
+
9
+ b.range_over([:symbol, :none], :reg_type) do |reg_type|
10
+ packer = MessagePack::Packer.new
11
+ packer.register_type(0x00, Symbol, :to_msgpack_ext) if reg_type == :symbol
12
+
13
+ b.range_over([100_000, 1_000_000, 10_000_000], :count) do |count|
14
+ packer.clear
15
+ b.report(:multi_run) do
16
+ count.times do
17
+ packer.pack(data)
18
+ end
19
+ end
20
+
21
+ packer.clear
22
+ items_data = [].fill(data, 0, count)
23
+ b.report(:large_run) do
24
+ packer.pack(items_data)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ #!/bin/sh
2
+
3
+ # so master and this branch have the benchmark file in any case
4
+ cp bench/pack_symbols.rb bench/pack_symbols_tmp.rb
5
+
6
+ benchmark=""
7
+ current_branch=`git rev-parse --abbrev-ref HEAD`
8
+
9
+ for branch in master $current_branch; do
10
+ echo "Testing branch $branch"
11
+ git checkout $branch
12
+
13
+ echo "Installing gem..."
14
+ rake install
15
+
16
+ echo "Running benchmark..."
17
+ if [ "$benchmark" ]; then
18
+ benchmark+=$'\n'
19
+ fi
20
+ benchmark+=$(viiite run bench/pack_symbols_tmp.rb)
21
+ echo
22
+ done
23
+
24
+ rm bench/pack_symbols_tmp.rb
25
+
26
+ echo "$benchmark" | viiite report --regroup bench,reg_type,count,branch
@@ -1,101 +1,101 @@
1
1
 
2
2
  class NilClass
3
3
  #
4
- # Same as MessagePack.to_msgpack(self[, io]).
4
+ # Same as MessagePack.to_msgpack(self[, packer]).
5
5
  #
6
6
  # @return [String] serialized data
7
7
  #
8
- def to_msgpack(io=nil)
8
+ def to_msgpack(packer=nil)
9
9
  end
10
10
  end
11
11
 
12
12
  class TrueClass
13
13
  #
14
- # Same as MessagePack.to_msgpack(self[, io]).
14
+ # Same as MessagePack.to_msgpack(self[, packer]).
15
15
  #
16
16
  # @return [String] serialized data
17
17
  #
18
- def to_msgpack(io=nil)
18
+ def to_msgpack(packer=nil)
19
19
  end
20
20
  end
21
21
 
22
22
  class FalseClass
23
23
  #
24
- # Same as MessagePack.to_msgpack(self[, io]).
24
+ # Same as MessagePack.to_msgpack(self[, packer]).
25
25
  #
26
26
  # @return [String] serialized data
27
27
  #
28
- def to_msgpack(io=nil)
28
+ def to_msgpack(packer=nil)
29
29
  end
30
30
  end
31
31
 
32
32
  class Fixnum < Integer
33
33
  #
34
- # Same as MessagePack.to_msgpack(self[, io]).
34
+ # Same as MessagePack.to_msgpack(self[, packer]).
35
35
  #
36
36
  # @return [String] serialized data
37
37
  #
38
- def to_msgpack(io=nil)
38
+ def to_msgpack(packer=nil)
39
39
  end
40
40
  end
41
41
 
42
42
  class Bignum < Integer
43
43
  #
44
- # Same as MessagePack.to_msgpack(self[, io]).
44
+ # Same as MessagePack.to_msgpack(self[, packer]).
45
45
  #
46
46
  # @return [String] serialized data
47
47
  #
48
- def to_msgpack(io=nil)
48
+ def to_msgpack(packer=nil)
49
49
  end
50
50
  end
51
51
 
52
52
  class Float < Numeric
53
53
  #
54
- # Same as MessagePack.to_msgpack(self[, io]).
54
+ # Same as MessagePack.to_msgpack(self[, packer]).
55
55
  #
56
56
  # @return [String] serialized data
57
57
  #
58
- def to_msgpack(io=nil)
58
+ def to_msgpack(packer=nil)
59
59
  end
60
60
  end
61
61
 
62
62
  class String
63
63
  #
64
- # Same as MessagePack.to_msgpack(self[, io]).
64
+ # Same as MessagePack.to_msgpack(self[, packer]).
65
65
  #
66
66
  # @return [String] serialized data
67
67
  #
68
- def to_msgpack(io=nil)
68
+ def to_msgpack(packer=nil)
69
69
  end
70
70
  end
71
71
 
72
72
  class Array
73
73
  #
74
- # Same as MessagePack.to_msgpack(self[, io]).
74
+ # Same as MessagePack.to_msgpack(self[, packer]).
75
75
  #
76
76
  # @return [String] serialized data
77
77
  #
78
- def to_msgpack(io=nil)
78
+ def to_msgpack(packer=nil)
79
79
  end
80
80
  end
81
81
 
82
82
  class Hash
83
83
  #
84
- # Same as MessagePack.to_msgpack(self[, io]).
84
+ # Same as MessagePack.to_msgpack(self[, packer]).
85
85
  #
86
86
  # @return [String] serialized data
87
87
  #
88
- def to_msgpack(io=nil)
88
+ def to_msgpack(packer=nil)
89
89
  end
90
90
  end
91
91
 
92
92
  class Symbol
93
93
  #
94
- # Same as MessagePack.to_msgpack(self[, io]).
94
+ # Same as MessagePack.to_msgpack(self[, packer]).
95
95
  #
96
96
  # @return [String] serialized data
97
97
  #
98
- def to_msgpack(io=nil)
98
+ def to_msgpack(packer=nil)
99
99
  end
100
100
  end
101
101
 
@@ -38,15 +38,18 @@ public class Encoder {
38
38
  private final boolean compatibilityMode;
39
39
  private final ExtensionRegistry registry;
40
40
 
41
+ public boolean hasSymbolExtType;
42
+
41
43
  private ByteBuffer buffer;
42
44
 
43
- public Encoder(Ruby runtime, boolean compatibilityMode, ExtensionRegistry registry) {
45
+ public Encoder(Ruby runtime, boolean compatibilityMode, ExtensionRegistry registry, boolean hasSymbolExtType) {
44
46
  this.runtime = runtime;
45
47
  this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
46
48
  this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
47
49
  this.utf8Encoding = UTF8Encoding.INSTANCE;
48
50
  this.compatibilityMode = compatibilityMode;
49
51
  this.registry = registry;
52
+ this.hasSymbolExtType = hasSymbolExtType;
50
53
  }
51
54
 
52
55
  public boolean isCompatibilityMode() {
@@ -112,7 +115,11 @@ public class Encoder {
112
115
  } else if (object instanceof RubyString) {
113
116
  appendString((RubyString) object);
114
117
  } else if (object instanceof RubySymbol) {
115
- appendString(((RubySymbol) object).asString());
118
+ if (hasSymbolExtType) {
119
+ appendOther(object, destination);
120
+ } else {
121
+ appendString(((RubySymbol) object).asString());
122
+ }
116
123
  } else if (object instanceof RubyArray) {
117
124
  appendArray((RubyArray) object);
118
125
  } else if (object instanceof RubyHash) {
@@ -24,11 +24,13 @@ import static org.jruby.runtime.Visibility.PRIVATE;
24
24
  public class Factory extends RubyObject {
25
25
  private final Ruby runtime;
26
26
  private final ExtensionRegistry extensionRegistry;
27
+ private boolean hasSymbolExtType;
27
28
 
28
29
  public Factory(Ruby runtime, RubyClass type) {
29
30
  super(runtime, type);
30
31
  this.runtime = runtime;
31
32
  this.extensionRegistry = new ExtensionRegistry();
33
+ this.hasSymbolExtType = false;
32
34
  }
33
35
 
34
36
  static class FactoryAllocator implements ObjectAllocator {
@@ -48,7 +50,7 @@ public class Factory extends RubyObject {
48
50
 
49
51
  @JRubyMethod(name = "packer", optional = 1)
50
52
  public Packer packer(ThreadContext ctx, IRubyObject[] args) {
51
- return Packer.newPacker(ctx, extensionRegistry(), args);
53
+ return Packer.newPacker(ctx, extensionRegistry(), hasSymbolExtType, args);
52
54
  }
53
55
 
54
56
  @JRubyMethod(name = "unpacker", optional = 1)
@@ -112,6 +114,10 @@ public class Factory extends RubyObject {
112
114
 
113
115
  extensionRegistry.put(extClass, (int) typeId, packerProc, packerArg, unpackerProc, unpackerArg);
114
116
 
117
+ if (extClass == runtime.getSymbol()) {
118
+ hasSymbolExtType = true;
119
+ }
120
+
115
121
  return runtime.getNil();
116
122
  }
117
123
  }
@@ -25,15 +25,17 @@ public class Packer extends RubyObject {
25
25
  public ExtensionRegistry registry;
26
26
  private Buffer buffer;
27
27
  private Encoder encoder;
28
+ private boolean hasSymbolExtType;
28
29
 
29
- public Packer(Ruby runtime, RubyClass type, ExtensionRegistry registry) {
30
+ public Packer(Ruby runtime, RubyClass type, ExtensionRegistry registry, boolean hasSymbolExtType) {
30
31
  super(runtime, type);
31
32
  this.registry = registry;
33
+ this.hasSymbolExtType = hasSymbolExtType;
32
34
  }
33
35
 
34
36
  static class PackerAllocator implements ObjectAllocator {
35
37
  public IRubyObject allocate(Ruby runtime, RubyClass type) {
36
- return new Packer(runtime, type, null);
38
+ return new Packer(runtime, type, null, false);
37
39
  }
38
40
  }
39
41
 
@@ -45,15 +47,19 @@ public class Packer extends RubyObject {
45
47
  IRubyObject mode = options.fastARef(ctx.getRuntime().newSymbol("compatibility_mode"));
46
48
  compatibilityMode = (mode != null) && mode.isTrue();
47
49
  }
48
- this.encoder = new Encoder(ctx.getRuntime(), compatibilityMode, registry);
50
+ if (registry == null) {
51
+ // registry is null when allocate -> initialize
52
+ // registry is already initialized (and somthing might be registered) when newPacker from Factory
53
+ this.registry = new ExtensionRegistry();
54
+ }
55
+ this.encoder = new Encoder(ctx.getRuntime(), compatibilityMode, registry, hasSymbolExtType);
49
56
  this.buffer = new Buffer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Buffer"));
50
57
  this.buffer.initialize(ctx, args);
51
- this.registry = new ExtensionRegistry();
52
58
  return this;
53
59
  }
54
60
 
55
- public static Packer newPacker(ThreadContext ctx, ExtensionRegistry extRegistry, IRubyObject[] args) {
56
- Packer packer = new Packer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Packer"), extRegistry);
61
+ public static Packer newPacker(ThreadContext ctx, ExtensionRegistry extRegistry, boolean hasSymbolExtType, IRubyObject[] args) {
62
+ Packer packer = new Packer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Packer"), extRegistry, hasSymbolExtType);
57
63
  packer.initialize(ctx, args);
58
64
  return packer;
59
65
  }
@@ -100,6 +106,11 @@ public class Packer extends RubyObject {
100
106
  RubyClass extClass = (RubyClass) klass;
101
107
 
102
108
  registry.put(extClass, (int) typeId, proc, arg, null, null);
109
+
110
+ if (extClass == runtime.getSymbol()) {
111
+ encoder.hasSymbolExtType = true;
112
+ }
113
+
103
114
  return runtime.getNil();
104
115
  }
105
116
 
@@ -65,6 +65,17 @@ static VALUE FalseClass_to_msgpack(int argc, VALUE* argv, VALUE self)
65
65
  return packer;
66
66
  }
67
67
 
68
+ static VALUE Integer_to_msgpack(int argc, VALUE* argv, VALUE self)
69
+ {
70
+ ENSURE_PACKER(argc, argv, packer, pk);
71
+ if (FIXNUM_P(self)) {
72
+ msgpack_packer_write_fixnum_value(pk, self);
73
+ } else {
74
+ msgpack_packer_write_bignum_value(pk, self);
75
+ }
76
+ return packer;
77
+ }
78
+
68
79
  static VALUE Fixnum_to_msgpack(int argc, VALUE* argv, VALUE self)
69
80
  {
70
81
  ENSURE_PACKER(argc, argv, packer, pk);
@@ -132,8 +143,12 @@ void MessagePack_core_ext_module_init()
132
143
  rb_define_method(rb_cNilClass, "to_msgpack", NilClass_to_msgpack, -1);
133
144
  rb_define_method(rb_cTrueClass, "to_msgpack", TrueClass_to_msgpack, -1);
134
145
  rb_define_method(rb_cFalseClass, "to_msgpack", FalseClass_to_msgpack, -1);
146
+ #ifdef RUBY_INTEGER_UNIFICATION
147
+ rb_define_method(rb_cInteger, "to_msgpack", Integer_to_msgpack, -1);
148
+ #else
135
149
  rb_define_method(rb_cFixnum, "to_msgpack", Fixnum_to_msgpack, -1);
136
150
  rb_define_method(rb_cBignum, "to_msgpack", Bignum_to_msgpack, -1);
151
+ #endif
137
152
  rb_define_method(rb_cFloat, "to_msgpack", Float_to_msgpack, -1);
138
153
  rb_define_method(rb_cString, "to_msgpack", String_to_msgpack, -1);
139
154
  rb_define_method(rb_cArray, "to_msgpack", Array_to_msgpack, -1);
@@ -32,6 +32,7 @@ typedef struct msgpack_factory_t msgpack_factory_t;
32
32
  struct msgpack_factory_t {
33
33
  msgpack_packer_ext_registry_t pkrg;
34
34
  msgpack_unpacker_ext_registry_t ukrg;
35
+ bool has_symbol_ext_type;
35
36
  };
36
37
 
37
38
  #define FACTORY(from, name) \
@@ -72,6 +73,8 @@ static VALUE Factory_initialize(int argc, VALUE* argv, VALUE self)
72
73
  {
73
74
  FACTORY(self, fc);
74
75
 
76
+ fc->has_symbol_ext_type = false;
77
+
75
78
  switch (argc) {
76
79
  case 0:
77
80
  break;
@@ -95,6 +98,7 @@ VALUE MessagePack_Factory_packer(int argc, VALUE* argv, VALUE self)
95
98
 
96
99
  msgpack_packer_ext_registry_destroy(&pk->ext_registry);
97
100
  msgpack_packer_ext_registry_dup(&fc->pkrg, &pk->ext_registry);
101
+ pk->has_symbol_ext_type = fc->has_symbol_ext_type;
98
102
 
99
103
  return packer;
100
104
  }
@@ -188,6 +192,10 @@ static VALUE Factory_register_type(int argc, VALUE* argv, VALUE self)
188
192
 
189
193
  msgpack_packer_ext_registry_put(&fc->pkrg, ext_class, ext_type, packer_proc, packer_arg);
190
194
 
195
+ if (ext_class == rb_cSymbol) {
196
+ fc->has_symbol_ext_type = true;
197
+ }
198
+
191
199
  msgpack_unpacker_ext_registry_put(&fc->ukrg, ext_class, ext_type, unpacker_proc, unpacker_arg);
192
200
 
193
201
  return Qnil;
@@ -121,7 +121,7 @@ void msgpack_packer_write_hash_value(msgpack_packer_t* pk, VALUE v)
121
121
  #endif
122
122
  }
123
123
 
124
- static void _msgpack_packer_write_other_value(msgpack_packer_t* pk, VALUE v)
124
+ void msgpack_packer_write_other_value(msgpack_packer_t* pk, VALUE v)
125
125
  {
126
126
  int ext_type;
127
127
  VALUE proc = msgpack_packer_ext_registry_lookup(&pk->ext_registry,
@@ -169,7 +169,7 @@ void msgpack_packer_write_value(msgpack_packer_t* pk, VALUE v)
169
169
  msgpack_packer_write_float_value(pk, v);
170
170
  break;
171
171
  default:
172
- _msgpack_packer_write_other_value(pk, v);
172
+ msgpack_packer_write_other_value(pk, v);
173
173
  }
174
174
  }
175
175
 
@@ -32,6 +32,7 @@ struct msgpack_packer_t {
32
32
  msgpack_buffer_t buffer;
33
33
 
34
34
  bool compatibility_mode;
35
+ bool has_symbol_ext_type;
35
36
 
36
37
  ID to_msgpack_method;
37
38
  VALUE to_msgpack_arg;
@@ -448,7 +449,7 @@ static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE
448
449
  #endif
449
450
  }
450
451
 
451
- static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE v)
452
+ static inline void msgpack_packer_write_symbol_string_value(msgpack_packer_t* pk, VALUE v)
452
453
  {
453
454
  #ifdef HAVE_RB_SYM2STR
454
455
  /* rb_sym2str is added since MRI 2.2.0 */
@@ -462,6 +463,17 @@ static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE
462
463
  #endif
463
464
  }
464
465
 
466
+ void msgpack_packer_write_other_value(msgpack_packer_t* pk, VALUE v);
467
+
468
+ static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE v)
469
+ {
470
+ if (pk->has_symbol_ext_type) {
471
+ msgpack_packer_write_other_value(pk, v);
472
+ } else {
473
+ msgpack_packer_write_symbol_string_value(pk, v);
474
+ }
475
+ }
476
+
465
477
  static inline void msgpack_packer_write_fixnum_value(msgpack_packer_t* pk, VALUE v)
466
478
  {
467
479
  #ifdef JRUBY
@@ -286,6 +286,10 @@ static VALUE Packer_register_type(int argc, VALUE* argv, VALUE self)
286
286
 
287
287
  msgpack_packer_ext_registry_put(&pk->ext_registry, ext_class, ext_type, proc, arg);
288
288
 
289
+ if (ext_class == rb_cSymbol) {
290
+ pk->has_symbol_ext_type = true;
291
+ }
292
+
289
293
  return Qnil;
290
294
  }
291
295
 
@@ -15,8 +15,8 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- #ifndef MSGPACK_RUBY_UNPACKER_PACKER_EXT_REGISTRY_H__
19
- #define MSGPACK_RUBY_UNPACKER_PACKER_EXT_REGISTRY_H__
18
+ #ifndef MSGPACK_RUBY_PACKER_EXT_REGISTRY_H__
19
+ #define MSGPACK_RUBY_PACKER_EXT_REGISTRY_H__
20
20
 
21
21
  #include "compat.h"
22
22
  #include "ruby.h"
@@ -15,8 +15,8 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- #ifndef MSGPACK_RUBY_PACKER_PACKER_EXT_REGISTRY_H__
19
- #define MSGPACK_RUBY_PACKER_PACKER_EXT_REGISTRY_H__
18
+ #ifndef MSGPACK_RUBY_UNPACKER_EXT_REGISTRY_H__
19
+ #define MSGPACK_RUBY_UNPACKER_EXT_REGISTRY_H__
20
20
 
21
21
  #include "compat.h"
22
22
  #include "ruby.h"
@@ -15,3 +15,4 @@ end
15
15
  require "msgpack/packer"
16
16
  require "msgpack/unpacker"
17
17
  require "msgpack/factory"
18
+ require "msgpack/symbol"
@@ -0,0 +1,9 @@
1
+ class Symbol
2
+ def to_msgpack_ext
3
+ [to_s].pack('A*')
4
+ end
5
+
6
+ def self.from_msgpack_ext(data)
7
+ data.unpack('A*').first.to_sym
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module MessagePack
2
- VERSION = "0.7.6"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -210,6 +210,66 @@ describe MessagePack::Factory do
210
210
  end
211
211
  end
212
212
 
213
+ describe 'the special treatment of symbols with ext type' do
214
+ let(:packer) { subject.packer }
215
+ let(:unpacker) { subject.unpacker }
216
+
217
+ def symbol_after_roundtrip
218
+ packed_symbol = packer.pack(:symbol).to_s
219
+ unpacker.feed(packed_symbol).unpack
220
+ end
221
+
222
+ context 'if no ext type is registered for symbols' do
223
+ it 'converts symbols to string' do
224
+ expect(symbol_after_roundtrip).to eq 'symbol'
225
+ end
226
+ end
227
+
228
+ context 'if an ext type is registered for symbols' do
229
+ context 'if using the default serializer' do
230
+ before { subject.register_type(0x00, ::Symbol) }
231
+
232
+ it 'lets symbols survive a roundtrip' do
233
+ expect(symbol_after_roundtrip).to be :symbol
234
+ end
235
+ end
236
+
237
+ context 'if using a custom serializer' do
238
+ before do
239
+ class Symbol
240
+ alias_method :to_msgpack_ext_orig, :to_msgpack_ext
241
+ def to_msgpack_ext
242
+ self.to_s.codepoints.to_a.pack('n*')
243
+ end
244
+ end
245
+
246
+ class << Symbol
247
+ alias_method :from_msgpack_ext_orig, :from_msgpack_ext
248
+ def from_msgpack_ext(data)
249
+ data.unpack('n*').map(&:chr).join.to_sym
250
+ end
251
+ end
252
+ end
253
+
254
+ before { subject.register_type(0x00, ::Symbol) }
255
+
256
+ it 'lets symbols survive a roundtrip' do
257
+ expect(symbol_after_roundtrip).to be :symbol
258
+ end
259
+
260
+ after do
261
+ class Symbol
262
+ alias_method :to_msgpack_ext, :to_msgpack_ext_orig
263
+ end
264
+
265
+ class << Symbol
266
+ alias_method :from_msgpack_ext, :from_msgpack_ext_orig
267
+ end
268
+ end
269
+ end
270
+ end
271
+ end
272
+
213
273
  describe 'DefaultFactory' do
214
274
  it 'is a factory' do
215
275
  MessagePack::DefaultFactory.should be_kind_of(MessagePack::Factory)
@@ -323,6 +323,42 @@ describe MessagePack::Packer do
323
323
  expect(two[:class]).to eq(ValueTwo)
324
324
  expect(two[:packer]).to eq(:to_msgpack_ext)
325
325
  end
326
+
327
+ context 'when registering a type for symbols' do
328
+ before { packer.register_type(0x00, ::Symbol, :to_msgpack_ext) }
329
+
330
+ it 'packs symbols in an ext type' do
331
+ expect(packer.pack(:symbol).to_s).to eq "\xc7\x06\x00symbol"
332
+ end
333
+ end
334
+ end
335
+
336
+ describe "fixnum and bignum" do
337
+ it "fixnum.to_msgpack" do
338
+ 23.to_msgpack.should == "\x17"
339
+ end
340
+
341
+ it "fixnum.to_msgpack(packer)" do
342
+ 23.to_msgpack(packer)
343
+ packer.to_s.should == "\x17"
344
+ end
345
+
346
+ it "bignum.to_msgpack" do
347
+ -4294967296.to_msgpack.should == "\xD3\xFF\xFF\xFF\xFF\x00\x00\x00\x00"
348
+ end
349
+
350
+ it "bignum.to_msgpack(packer)" do
351
+ -4294967296.to_msgpack(packer)
352
+ packer.to_s.should == "\xD3\xFF\xFF\xFF\xFF\x00\x00\x00\x00"
353
+ end
354
+
355
+ it "unpack(fixnum)" do
356
+ MessagePack.unpack("\x17").should == 23
357
+ end
358
+
359
+ it "unpack(bignum)" do
360
+ MessagePack.unpack("\xD3\xFF\xFF\xFF\xFF\x00\x00\x00\x00").should == -4294967296
361
+ end
326
362
  end
327
363
 
328
364
  describe "ext formats" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.6
4
+ version: 1.0.0
5
5
  platform: x86-mingw32
6
6
  authors:
7
7
  - Sadayuki Furuhashi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-05-10 00:00:00.000000000 Z
13
+ date: 2016-07-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -133,8 +133,10 @@ files:
133
133
  - bench/pack.rb
134
134
  - bench/pack_log.rb
135
135
  - bench/pack_log_long.rb
136
+ - bench/pack_symbols.rb
136
137
  - bench/run.sh
137
138
  - bench/run_long.sh
139
+ - bench/run_symbols.sh
138
140
  - bench/unpack.rb
139
141
  - bench/unpack_log.rb
140
142
  - bench/unpack_log_long.rb
@@ -194,6 +196,7 @@ files:
194
196
  - lib/msgpack/2.3/msgpack.so
195
197
  - lib/msgpack/factory.rb
196
198
  - lib/msgpack/packer.rb
199
+ - lib/msgpack/symbol.rb
197
200
  - lib/msgpack/unpacker.rb
198
201
  - lib/msgpack/version.rb
199
202
  - msgpack.gemspec