msgpack 1.3.3 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yaml +57 -0
  3. data/.rubocop.yml +2 -2
  4. data/ChangeLog +74 -0
  5. data/Gemfile +1 -1
  6. data/README.md +266 -0
  7. data/Rakefile +1 -9
  8. data/bench/bench.rb +78 -0
  9. data/bin/console +8 -0
  10. data/doclib/msgpack/factory.rb +47 -3
  11. data/doclib/msgpack/packer.rb +5 -4
  12. data/doclib/msgpack/unpacker.rb +2 -2
  13. data/ext/java/org/msgpack/jruby/Buffer.java +23 -16
  14. data/ext/java/org/msgpack/jruby/Decoder.java +46 -23
  15. data/ext/java/org/msgpack/jruby/Encoder.java +68 -30
  16. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +37 -49
  17. data/ext/java/org/msgpack/jruby/ExtensionValue.java +5 -8
  18. data/ext/java/org/msgpack/jruby/Factory.java +47 -7
  19. data/ext/java/org/msgpack/jruby/Packer.java +29 -17
  20. data/ext/java/org/msgpack/jruby/Unpacker.java +72 -37
  21. data/ext/msgpack/buffer.c +42 -68
  22. data/ext/msgpack/buffer.h +59 -14
  23. data/ext/msgpack/buffer_class.c +90 -52
  24. data/ext/msgpack/compat.h +1 -111
  25. data/ext/msgpack/extconf.rb +45 -19
  26. data/ext/msgpack/factory_class.c +133 -43
  27. data/ext/msgpack/packer.c +60 -36
  28. data/ext/msgpack/packer.h +27 -25
  29. data/ext/msgpack/packer_class.c +84 -77
  30. data/ext/msgpack/packer_class.h +11 -0
  31. data/ext/msgpack/packer_ext_registry.c +24 -32
  32. data/ext/msgpack/packer_ext_registry.h +40 -33
  33. data/ext/msgpack/sysdep.h +5 -2
  34. data/ext/msgpack/unpacker.c +132 -115
  35. data/ext/msgpack/unpacker.h +23 -10
  36. data/ext/msgpack/unpacker_class.c +83 -78
  37. data/ext/msgpack/unpacker_class.h +11 -0
  38. data/ext/msgpack/unpacker_ext_registry.c +42 -18
  39. data/ext/msgpack/unpacker_ext_registry.h +23 -16
  40. data/lib/msgpack/bigint.rb +69 -0
  41. data/lib/msgpack/factory.rb +103 -0
  42. data/lib/msgpack/symbol.rb +21 -4
  43. data/lib/msgpack/time.rb +1 -1
  44. data/lib/msgpack/version.rb +4 -8
  45. data/lib/msgpack.rb +6 -12
  46. data/msgpack.gemspec +4 -6
  47. data/spec/bigint_spec.rb +26 -0
  48. data/spec/cruby/buffer_spec.rb +17 -0
  49. data/spec/factory_spec.rb +351 -12
  50. data/spec/msgpack_spec.rb +1 -1
  51. data/spec/packer_spec.rb +18 -0
  52. data/spec/spec_helper.rb +37 -3
  53. data/spec/timestamp_spec.rb +38 -0
  54. data/spec/unpacker_spec.rb +157 -4
  55. metadata +31 -61
  56. data/.travis.yml +0 -43
  57. data/README.rdoc +0 -225
  58. data/bench/pack.rb +0 -23
  59. data/bench/pack_log.rb +0 -33
  60. data/bench/pack_log_long.rb +0 -65
  61. data/bench/pack_symbols.rb +0 -28
  62. data/bench/run.sh +0 -14
  63. data/bench/run_long.sh +0 -35
  64. data/bench/run_symbols.sh +0 -26
  65. data/bench/unpack.rb +0 -21
  66. data/bench/unpack_log.rb +0 -34
  67. data/bench/unpack_log_long.rb +0 -67
@@ -21,6 +21,7 @@ import org.jcodings.Encoding;
21
21
 
22
22
  @JRubyClass(name="MessagePack::Buffer")
23
23
  public class Buffer extends RubyObject {
24
+ private static final long serialVersionUID = 8441244627425629412L;
24
25
  private IRubyObject io;
25
26
  private ByteBuffer buffer;
26
27
  private boolean writeMode;
@@ -49,7 +50,7 @@ public class Buffer extends RubyObject {
49
50
  }
50
51
  this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
51
52
  this.writeMode = true;
52
- this.binaryEncoding = ctx.getRuntime().getEncodingService().getAscii8bitEncoding();
53
+ this.binaryEncoding = ctx.runtime.getEncodingService().getAscii8bitEncoding();
53
54
  return this;
54
55
  }
55
56
 
@@ -87,17 +88,17 @@ public class Buffer extends RubyObject {
87
88
  writeMode = true;
88
89
  }
89
90
  buffer.clear();
90
- return ctx.getRuntime().getNil();
91
+ return ctx.runtime.getNil();
91
92
  }
92
93
 
93
94
  @JRubyMethod(name = "size")
94
95
  public IRubyObject size(ThreadContext ctx) {
95
- return ctx.getRuntime().newFixnum(rawSize());
96
+ return ctx.runtime.newFixnum(rawSize());
96
97
  }
97
98
 
98
99
  @JRubyMethod(name = "empty?")
99
100
  public IRubyObject isEmpty(ThreadContext ctx) {
100
- return rawSize() == 0 ? ctx.getRuntime().getTrue() : ctx.getRuntime().getFalse();
101
+ return rawSize() == 0 ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
101
102
  }
102
103
 
103
104
  private IRubyObject bufferWrite(ThreadContext ctx, IRubyObject str) {
@@ -105,7 +106,7 @@ public class Buffer extends RubyObject {
105
106
  int length = bytes.length();
106
107
  ensureRemainingCapacity(length);
107
108
  buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
108
- return ctx.getRuntime().newFixnum(length);
109
+ return ctx.runtime.newFixnum(length);
109
110
 
110
111
  }
111
112
 
@@ -131,19 +132,19 @@ public class Buffer extends RubyObject {
131
132
  length = (int) args[0].convertToInteger().getLongValue();
132
133
  }
133
134
  if (raiseOnUnderflow && rawSize() < length) {
134
- throw ctx.getRuntime().newEOFError();
135
+ throw ctx.runtime.newEOFError();
135
136
  }
136
137
  int readLength = Math.min(length, rawSize());
137
138
  if (readLength == 0 && length > 0) {
138
- return ctx.getRuntime().getNil();
139
+ return ctx.runtime.getNil();
139
140
  } else if (readLength == 0) {
140
- return ctx.getRuntime().newString();
141
+ return ctx.runtime.newString();
141
142
  } else {
142
143
  ensureReadMode();
143
144
  byte[] bytes = new byte[readLength];
144
145
  buffer.get(bytes);
145
146
  ByteList byteList = new ByteList(bytes, binaryEncoding);
146
- return ctx.getRuntime().newString(byteList);
147
+ return ctx.runtime.newString(byteList);
147
148
  }
148
149
  }
149
150
 
@@ -161,12 +162,12 @@ public class Buffer extends RubyObject {
161
162
  feed(ctx);
162
163
  int length = (int) _length.convertToInteger().getLongValue();
163
164
  if (raiseOnUnderflow && rawSize() < length) {
164
- throw ctx.getRuntime().newEOFError();
165
+ throw ctx.runtime.newEOFError();
165
166
  }
166
167
  ensureReadMode();
167
168
  int skipLength = Math.min(length, rawSize());
168
169
  buffer.position(buffer.position() + skipLength);
169
- return ctx.getRuntime().newFixnum(skipLength);
170
+ return ctx.runtime.newFixnum(skipLength);
170
171
  }
171
172
 
172
173
  @JRubyMethod(name = "skip")
@@ -188,23 +189,23 @@ public class Buffer extends RubyObject {
188
189
  ensureReadMode();
189
190
  int length = buffer.limit() - buffer.position();
190
191
  ByteList str = new ByteList(buffer.array(), buffer.position(), length, binaryEncoding, true);
191
- return ctx.getRuntime().newString(str);
192
+ return ctx.runtime.newString(str);
192
193
  }
193
194
 
194
195
  @JRubyMethod(name = "to_a")
195
196
  public IRubyObject toA(ThreadContext ctx) {
196
- return ctx.getRuntime().newArray(toS(ctx));
197
+ return ctx.runtime.newArray(toS(ctx));
197
198
  }
198
199
 
199
200
  @JRubyMethod(name = "io")
200
201
  public IRubyObject getIo(ThreadContext ctx) {
201
- return io == null ? ctx.getRuntime().getNil() : io;
202
+ return io == null ? ctx.runtime.getNil() : io;
202
203
  }
203
204
 
204
205
  @JRubyMethod(name = "flush")
205
206
  public IRubyObject flush(ThreadContext ctx) {
206
207
  if (io == null) {
207
- return ctx.getRuntime().getNil();
208
+ return ctx.runtime.getNil();
208
209
  } else {
209
210
  return io.callMethod(ctx, "flush");
210
211
  }
@@ -213,7 +214,7 @@ public class Buffer extends RubyObject {
213
214
  @JRubyMethod(name = "close")
214
215
  public IRubyObject close(ThreadContext ctx) {
215
216
  if (io == null) {
216
- return ctx.getRuntime().getNil();
217
+ return ctx.runtime.getNil();
217
218
  } else {
218
219
  return io.callMethod(ctx, "close");
219
220
  }
@@ -223,4 +224,10 @@ public class Buffer extends RubyObject {
223
224
  public IRubyObject writeTo(ThreadContext ctx, IRubyObject io) {
224
225
  return io.callMethod(ctx, "write", readCommon(ctx, null, false));
225
226
  }
227
+
228
+ public ByteList getBytes() {
229
+ byte[] bytes = new byte[rawSize()];
230
+ buffer.get(bytes);
231
+ return new ByteList(bytes, binaryEncoding);
232
+ }
226
233
  }
@@ -14,6 +14,7 @@ import org.jruby.RubyBignum;
14
14
  import org.jruby.RubyString;
15
15
  import org.jruby.RubyArray;
16
16
  import org.jruby.RubyHash;
17
+ import org.jruby.RubyInteger;
17
18
  import org.jruby.exceptions.RaiseException;
18
19
  import org.jruby.runtime.builtin.IRubyObject;
19
20
  import org.jruby.util.ByteList;
@@ -35,39 +36,41 @@ public class Decoder implements Iterator<IRubyObject> {
35
36
  private final RubyClass unexpectedTypeErrorClass;
36
37
  private final RubyClass unknownExtTypeErrorClass;
37
38
 
38
- private ExtensionRegistry registry;
39
+ private Unpacker unpacker;
39
40
  private ByteBuffer buffer;
40
41
  private boolean symbolizeKeys;
42
+ private boolean freeze;
41
43
  private boolean allowUnknownExt;
42
44
 
43
45
  public Decoder(Ruby runtime) {
44
- this(runtime, null, new byte[] {}, 0, 0, false, false);
46
+ this(runtime, null, new byte[] {}, 0, 0, false, false, false);
45
47
  }
46
48
 
47
- public Decoder(Ruby runtime, ExtensionRegistry registry) {
48
- this(runtime, registry, new byte[] {}, 0, 0, false, false);
49
+ public Decoder(Ruby runtime, Unpacker unpacker) {
50
+ this(runtime, unpacker, new byte[] {}, 0, 0, false, false, false);
49
51
  }
50
52
 
51
53
  public Decoder(Ruby runtime, byte[] bytes) {
52
- this(runtime, null, bytes, 0, bytes.length, false, false);
54
+ this(runtime, null, bytes, 0, bytes.length, false, false, false);
53
55
  }
54
56
 
55
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes) {
56
- this(runtime, registry, bytes, 0, bytes.length, false, false);
57
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes) {
58
+ this(runtime, unpacker, bytes, 0, bytes.length, false, false, false);
57
59
  }
58
60
 
59
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, boolean symbolizeKeys, boolean allowUnknownExt) {
60
- this(runtime, registry, bytes, 0, bytes.length, symbolizeKeys, allowUnknownExt);
61
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
62
+ this(runtime, unpacker, bytes, 0, bytes.length, symbolizeKeys, freeze, allowUnknownExt);
61
63
  }
62
64
 
63
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, int offset, int length) {
64
- this(runtime, registry, bytes, offset, length, false, false);
65
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, int offset, int length) {
66
+ this(runtime, unpacker, bytes, offset, length, false, false, false);
65
67
  }
66
68
 
67
- public Decoder(Ruby runtime, ExtensionRegistry registry, byte[] bytes, int offset, int length, boolean symbolizeKeys, boolean allowUnknownExt) {
69
+ public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, int offset, int length, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
68
70
  this.runtime = runtime;
69
- this.registry = registry;
71
+ this.unpacker = unpacker;
70
72
  this.symbolizeKeys = symbolizeKeys;
73
+ this.freeze = freeze;
71
74
  this.allowUnknownExt = allowUnknownExt;
72
75
  this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
73
76
  this.utf8Encoding = UTF8Encoding.INSTANCE;
@@ -118,7 +121,11 @@ public class Decoder implements Iterator<IRubyObject> {
118
121
  private IRubyObject consumeString(int size, Encoding encoding) {
119
122
  byte[] bytes = readBytes(size);
120
123
  ByteList byteList = new ByteList(bytes, encoding);
121
- return runtime.newString(byteList);
124
+ RubyString string = runtime.newString(byteList);
125
+ if (this.freeze) {
126
+ string = runtime.freezeAndDedupString(string);
127
+ }
128
+ return string;
122
129
  }
123
130
 
124
131
  private IRubyObject consumeArray(int size) {
@@ -133,9 +140,14 @@ public class Decoder implements Iterator<IRubyObject> {
133
140
  RubyHash hash = RubyHash.newHash(runtime);
134
141
  for (int i = 0; i < size; i++) {
135
142
  IRubyObject key = next();
136
- if (this.symbolizeKeys && key instanceof RubyString) {
143
+ if (key instanceof RubyString) {
144
+ if (this.symbolizeKeys) {
137
145
  key = ((RubyString) key).intern();
146
+ } else {
147
+ key = runtime.freezeAndDedupString((RubyString) key);
148
+ }
138
149
  }
150
+
139
151
  hash.fastASet(key, next());
140
152
  }
141
153
  return hash;
@@ -143,18 +155,21 @@ public class Decoder implements Iterator<IRubyObject> {
143
155
 
144
156
  private IRubyObject consumeExtension(int size) {
145
157
  int type = buffer.get();
146
- byte[] payload = readBytes(size);
147
-
148
- if (registry != null) {
149
- IRubyObject proc = registry.lookupUnpackerByTypeId(type);
150
- if (proc != null) {
151
- ByteList byteList = new ByteList(payload, runtime.getEncodingService().getAscii8bitEncoding());
152
- return proc.callMethod(runtime.getCurrentContext(), "call", runtime.newString(byteList));
158
+ if (unpacker != null) {
159
+ ExtensionRegistry.ExtensionEntry entry = unpacker.lookupExtensionByTypeId(type);
160
+ if (entry != null) {
161
+ IRubyObject proc = entry.getUnpackerProc();
162
+ if (entry.isRecursive()) {
163
+ return proc.callMethod(runtime.getCurrentContext(), "call", unpacker);
164
+ } else {
165
+ ByteList byteList = new ByteList(readBytes(size), runtime.getEncodingService().getAscii8bitEncoding());
166
+ return proc.callMethod(runtime.getCurrentContext(), "call", runtime.newString(byteList));
167
+ }
153
168
  }
154
169
  }
155
170
 
156
171
  if (this.allowUnknownExt) {
157
- return ExtensionValue.newExtensionValue(runtime, type, payload);
172
+ return ExtensionValue.newExtensionValue(runtime, type, readBytes(size));
158
173
  }
159
174
 
160
175
  throw runtime.newRaiseException(unknownExtTypeErrorClass, "unexpected extension type");
@@ -220,6 +235,14 @@ public class Decoder implements Iterator<IRubyObject> {
220
235
 
221
236
  @Override
222
237
  public IRubyObject next() {
238
+ IRubyObject next = consumeNext();
239
+ if (freeze) {
240
+ next.setFrozen(true);
241
+ }
242
+ return next;
243
+ }
244
+
245
+ private IRubyObject consumeNext() {
223
246
  int position = buffer.position();
224
247
  try {
225
248
  byte b = buffer.get();
@@ -38,12 +38,16 @@ public class Encoder {
38
38
  private final Encoding utf8Encoding;
39
39
  private final boolean compatibilityMode;
40
40
  private final ExtensionRegistry registry;
41
+ private final Packer packer;
41
42
 
42
43
  public boolean hasSymbolExtType;
44
+ private boolean hasBigintExtType;
45
+ private boolean recursiveExtension;
43
46
 
44
47
  private ByteBuffer buffer;
45
48
 
46
- public Encoder(Ruby runtime, boolean compatibilityMode, ExtensionRegistry registry, boolean hasSymbolExtType) {
49
+ public Encoder(Ruby runtime, Packer packer, boolean compatibilityMode, ExtensionRegistry registry, boolean hasSymbolExtType, boolean hasBigintExtType) {
50
+ this.packer = packer;
47
51
  this.runtime = runtime;
48
52
  this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
49
53
  this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
@@ -51,6 +55,7 @@ public class Encoder {
51
55
  this.compatibilityMode = compatibilityMode;
52
56
  this.registry = registry;
53
57
  this.hasSymbolExtType = hasSymbolExtType;
58
+ this.hasBigintExtType = hasBigintExtType;
54
59
  }
55
60
 
56
61
  public boolean isCompatibilityMode() {
@@ -66,9 +71,17 @@ public class Encoder {
66
71
  }
67
72
 
68
73
  private IRubyObject readRubyString() {
69
- IRubyObject str = runtime.newString(new ByteList(buffer.array(), 0, buffer.position(), binaryEncoding, false));
70
- buffer.clear();
71
- return str;
74
+ if (recursiveExtension) {
75
+ // If recursiveExtension is true, it means we re-entered encode, so we MUST NOT flush the buffer.
76
+ // Instead we return an empty string to act as a null object for the caller. The buffer will actually
77
+ // be flushed once we're done serializing the recursive extension.
78
+ // All other method that consume the buffer should do so through readRubyString or implement the same logic.
79
+ return runtime.newString();
80
+ } else {
81
+ IRubyObject str = runtime.newString(new ByteList(buffer.array(), 0, buffer.position(), binaryEncoding, false));
82
+ buffer.clear();
83
+ return str;
84
+ }
72
85
  }
73
86
 
74
87
  public IRubyObject encode(IRubyObject object) {
@@ -119,7 +132,9 @@ public class Encoder {
119
132
  } else if (object instanceof RubyFloat) {
120
133
  appendFloat((RubyFloat) object);
121
134
  } else if (object instanceof RubyString) {
122
- appendString((RubyString) object);
135
+ if (object.getType() == runtime.getString() || !tryAppendWithExtTypeLookup(object)) {
136
+ appendString((RubyString) object);
137
+ }
123
138
  } else if (object instanceof RubySymbol) {
124
139
  if (hasSymbolExtType) {
125
140
  appendOther(object, destination);
@@ -127,9 +142,13 @@ public class Encoder {
127
142
  appendString(((RubySymbol) object).asString());
128
143
  }
129
144
  } else if (object instanceof RubyArray) {
130
- appendArray((RubyArray) object);
145
+ if (object.getType() == runtime.getArray() || !tryAppendWithExtTypeLookup(object)) {
146
+ appendArray((RubyArray) object);
147
+ }
131
148
  } else if (object instanceof RubyHash) {
132
- appendHash((RubyHash) object);
149
+ if (object.getType() == runtime.getHash() || !tryAppendWithExtTypeLookup(object)) {
150
+ appendHash((RubyHash) object);
151
+ }
133
152
  } else if (object instanceof ExtensionValue) {
134
153
  appendExtensionValue((ExtensionValue) object);
135
154
  } else {
@@ -141,7 +160,10 @@ public class Encoder {
141
160
  BigInteger value = object.getBigIntegerValue();
142
161
  if (value.compareTo(RubyBignum.LONG_MIN) < 0 || value.compareTo(RubyBignum.LONG_MAX) > 0) {
143
162
  if (value.bitLength() > 64 || (value.bitLength() > 63 && value.signum() < 0)) {
144
- throw runtime.newArgumentError(String.format("Cannot pack big integer: %s", value));
163
+ if (hasBigintExtType && tryAppendWithExtTypeLookup(object)) {
164
+ return;
165
+ }
166
+ throw runtime.newRangeError(String.format("Cannot pack big integer: %s", value));
145
167
  }
146
168
  ensureRemainingCapacity(9);
147
169
  buffer.put(value.signum() < 0 ? INT64 : UINT64);
@@ -153,7 +175,7 @@ public class Encoder {
153
175
  }
154
176
 
155
177
  private void appendInteger(RubyInteger object) {
156
- long value = ((RubyInteger) object).getLongValue();
178
+ long value = object.getLongValue();
157
179
  if (value < 0) {
158
180
  if (value < Short.MIN_VALUE) {
159
181
  if (value < Integer.MIN_VALUE) {
@@ -241,7 +263,7 @@ public class Encoder {
241
263
  } else {
242
264
  ensureRemainingCapacity(5 + length);
243
265
  buffer.put(binary ? BIN32 : STR32);
244
- buffer.putInt((int) length);
266
+ buffer.putInt(length);
245
267
  }
246
268
  }
247
269
 
@@ -249,7 +271,7 @@ public class Encoder {
249
271
  Encoding encoding = object.getEncoding();
250
272
  boolean binary = !compatibilityMode && encoding == binaryEncoding;
251
273
  if (encoding != utf8Encoding && encoding != binaryEncoding) {
252
- object = (RubyString) ((RubyString) object).encode(runtime.getCurrentContext(), runtime.getEncodingService().getEncoding(utf8Encoding));
274
+ object = (RubyString)(object).encode(runtime.getCurrentContext(), runtime.getEncodingService().getEncoding(utf8Encoding));
253
275
  }
254
276
  ByteList bytes = object.getByteList();
255
277
  int length = bytes.length();
@@ -257,12 +279,12 @@ public class Encoder {
257
279
  buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
258
280
  }
259
281
 
260
- private void appendArray(RubyArray object) {
282
+ private void appendArray(RubyArray<?> object) {
261
283
  appendArrayHeader(object);
262
284
  appendArrayElements(object);
263
285
  }
264
286
 
265
- private void appendArrayHeader(RubyArray object) {
287
+ private void appendArrayHeader(RubyArray<?> object) {
266
288
  appendArrayHeader(object.size());
267
289
  }
268
290
 
@@ -281,7 +303,7 @@ public class Encoder {
281
303
  }
282
304
  }
283
305
 
284
- private void appendArrayElements(RubyArray object) {
306
+ private void appendArrayElements(RubyArray<?> object) {
285
307
  int size = object.size();
286
308
  for (int i = 0; i < size; i++) {
287
309
  appendObject(object.eltOk(i));
@@ -383,25 +405,41 @@ public class Encoder {
383
405
  appendExt((int) type, payloadBytes);
384
406
  }
385
407
 
386
- private void appendOther(IRubyObject object, IRubyObject destination) {
408
+ private boolean tryAppendWithExtTypeLookup(IRubyObject object) {
387
409
  if (registry != null) {
388
- RubyModule lookupClass;
389
-
390
- if (object.getType() == runtime.getSymbol()) {
391
- lookupClass = object.getType();
392
- } else {
393
- lookupClass = object.getSingletonClass();
394
- }
395
-
396
- IRubyObject[] pair = registry.lookupPackerForObject(object);
397
- if (pair != null) {
398
- RubyString bytes = pair[0].callMethod(runtime.getCurrentContext(), "call", object).asString();
399
- int type = (int) ((RubyFixnum) pair[1]).getLongValue();
400
- appendExt(type, bytes.getByteList());
401
- return;
410
+ ExtensionRegistry.ExtensionEntry entry = registry.lookupExtensionForObject(object);
411
+ if (entry != null) {
412
+ IRubyObject proc = entry.getPackerProc();
413
+ int type = entry.getTypeId();
414
+
415
+ if (entry.isRecursive()) {
416
+ ByteBuffer oldBuffer = buffer;
417
+ buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
418
+ boolean previousRecursiveExtension = recursiveExtension;
419
+ recursiveExtension = true;
420
+
421
+ ByteList payload;
422
+ try {
423
+ IRubyObject args[] = { object, packer };
424
+ proc.callMethod(runtime.getCurrentContext(), "call", args);
425
+ payload = new ByteList(buffer.array(), 0, buffer.position(), binaryEncoding, false);
426
+ } finally {
427
+ recursiveExtension = previousRecursiveExtension;
428
+ buffer = oldBuffer;
429
+ }
430
+ appendExt(type, payload);
431
+ } else {
432
+ RubyString bytes = proc.callMethod(runtime.getCurrentContext(), "call", object).asString();
433
+ appendExt(type, bytes.getByteList());
434
+ }
435
+ return true;
402
436
  }
403
437
  }
404
- appendCustom(object, destination);
438
+ return false;
439
+ }
440
+
441
+ private void appendOther(IRubyObject object, IRubyObject destination) {
442
+ if (!tryAppendWithExtTypeLookup(object)) { appendCustom(object, destination); }
405
443
  }
406
444
 
407
445
  private void appendCustom(IRubyObject object, IRubyObject destination) {
@@ -37,7 +37,7 @@ public class ExtensionRegistry {
37
37
  }
38
38
 
39
39
  public IRubyObject toInternalPackerRegistry(ThreadContext ctx) {
40
- RubyHash hash = RubyHash.newHash(ctx.getRuntime());
40
+ RubyHash hash = RubyHash.newHash(ctx.runtime);
41
41
  for (RubyModule extensionModule : extensionsByModule.keySet()) {
42
42
  ExtensionEntry entry = extensionsByModule.get(extensionModule);
43
43
  if (entry.hasPacker()) {
@@ -48,75 +48,57 @@ public class ExtensionRegistry {
48
48
  }
49
49
 
50
50
  public IRubyObject toInternalUnpackerRegistry(ThreadContext ctx) {
51
- RubyHash hash = RubyHash.newHash(ctx.getRuntime());
51
+ RubyHash hash = RubyHash.newHash(ctx.runtime);
52
52
  for (int typeIdIndex = 0 ; typeIdIndex < 256 ; typeIdIndex++) {
53
53
  ExtensionEntry entry = extensionsByTypeId[typeIdIndex];
54
54
  if (entry != null && entry.hasUnpacker()) {
55
- IRubyObject typeId = RubyFixnum.newFixnum(ctx.getRuntime(), typeIdIndex - 128);
55
+ IRubyObject typeId = RubyFixnum.newFixnum(ctx.runtime, typeIdIndex - 128);
56
56
  hash.put(typeId, entry.toUnpackerTuple(ctx));
57
57
  }
58
58
  }
59
59
  return hash;
60
60
  }
61
61
 
62
- public void put(RubyModule mod, int typeId, IRubyObject packerProc, IRubyObject packerArg, IRubyObject unpackerProc, IRubyObject unpackerArg) {
63
- ExtensionEntry entry = new ExtensionEntry(mod, typeId, packerProc, packerArg, unpackerProc, unpackerArg);
62
+ public void put(RubyModule mod, int typeId, boolean recursive, IRubyObject packerProc, IRubyObject packerArg, IRubyObject unpackerProc, IRubyObject unpackerArg) {
63
+ ExtensionEntry entry = new ExtensionEntry(mod, typeId, recursive, packerProc, packerArg, unpackerProc, unpackerArg);
64
64
  extensionsByModule.put(mod, entry);
65
65
  extensionsByTypeId[typeId + 128] = entry;
66
66
  extensionsByAncestor.clear();
67
67
  }
68
68
 
69
- public IRubyObject lookupUnpackerByTypeId(int typeId) {
69
+ public ExtensionEntry lookupExtensionByTypeId(int typeId) {
70
70
  ExtensionEntry e = extensionsByTypeId[typeId + 128];
71
71
  if (e != null && e.hasUnpacker()) {
72
- return e.getUnpackerProc();
73
- } else {
74
- return null;
72
+ return e;
75
73
  }
74
+ return null;
76
75
  }
77
76
 
78
- public IRubyObject[] lookupPackerForObject(IRubyObject object) {
77
+ public ExtensionEntry lookupExtensionForObject(IRubyObject object) {
79
78
  RubyModule lookupClass = null;
80
- IRubyObject[] pair;
79
+ ExtensionEntry entry = null;
81
80
  /*
82
81
  * Objects of type Integer (Fixnum, Bignum), Float, Symbol and frozen
83
82
  * String have no singleton class and raise a TypeError when trying to get
84
83
  * it.
85
- *
86
- * Since all but symbols are already filtered out when reaching this code
87
- * only symbols are checked here.
88
84
  */
89
- if (!(object instanceof RubySymbol)) {
90
- lookupClass = object.getSingletonClass();
91
- pair = fetchEntryByModule(lookupClass);
92
- if (pair != null) {
93
- return pair;
94
- }
85
+ lookupClass = object.getMetaClass();
86
+ entry = extensionsByModule.get(lookupClass);
87
+ if (entry != null && entry.hasPacker()) {
88
+ return entry;
95
89
  }
96
90
 
97
- pair = fetchEntryByModule(object.getType());
98
- if (pair != null) {
99
- return pair;
91
+ RubyModule realClass = object.getType();
92
+ if (realClass != lookupClass) {
93
+ entry = extensionsByModule.get(realClass);
94
+ if (entry != null && entry.hasPacker()) {
95
+ return entry;
96
+ }
100
97
  }
101
98
 
102
- if (lookupClass == null) {
103
- lookupClass = object.getType(); // only for Symbol
104
- }
105
- ExtensionEntry e = findEntryByModuleOrAncestor(lookupClass);
106
- if (e != null && e.hasPacker()) {
107
- extensionsByAncestor.put(e.getExtensionModule(), e);
108
- return e.toPackerProcTypeIdPair(lookupClass.getRuntime().getCurrentContext());
109
- }
110
- return null;
111
- }
112
-
113
- private IRubyObject[] fetchEntryByModule(final RubyModule mod) {
114
- ExtensionEntry e = extensionsByModule.get(mod);
115
- if (e == null) {
116
- e = extensionsByAncestor.get(mod);
117
- }
118
- if (e != null && e.hasPacker()) {
119
- return e.toPackerProcTypeIdPair(mod.getRuntime().getCurrentContext());
99
+ entry = findEntryByModuleOrAncestor(lookupClass);
100
+ if (entry != null && entry.hasPacker()) {
101
+ return entry;
120
102
  }
121
103
  return null;
122
104
  }
@@ -124,7 +106,7 @@ public class ExtensionRegistry {
124
106
  private ExtensionEntry findEntryByModuleOrAncestor(final RubyModule mod) {
125
107
  ThreadContext ctx = mod.getRuntime().getCurrentContext();
126
108
  for (RubyModule extensionModule : extensionsByModule.keySet()) {
127
- RubyArray ancestors = (RubyArray) mod.callMethod(ctx, "ancestors");
109
+ RubyArray<?> ancestors = (RubyArray)mod.callMethod(ctx, "ancestors");
128
110
  if (ancestors.callMethod(ctx, "include?", extensionModule).isTrue()) {
129
111
  return extensionsByModule.get(extensionModule);
130
112
  }
@@ -132,17 +114,19 @@ public class ExtensionRegistry {
132
114
  return null;
133
115
  }
134
116
 
135
- private static class ExtensionEntry {
117
+ public static class ExtensionEntry {
136
118
  private final RubyModule mod;
137
119
  private final int typeId;
120
+ private final boolean recursive;
138
121
  private final IRubyObject packerProc;
139
122
  private final IRubyObject packerArg;
140
123
  private final IRubyObject unpackerProc;
141
124
  private final IRubyObject unpackerArg;
142
125
 
143
- public ExtensionEntry(RubyModule mod, int typeId, IRubyObject packerProc, IRubyObject packerArg, IRubyObject unpackerProc, IRubyObject unpackerArg) {
126
+ public ExtensionEntry(RubyModule mod, int typeId, boolean recursive, IRubyObject packerProc, IRubyObject packerArg, IRubyObject unpackerProc, IRubyObject unpackerArg) {
144
127
  this.mod = mod;
145
128
  this.typeId = typeId;
129
+ this.recursive = recursive;
146
130
  this.packerProc = packerProc;
147
131
  this.packerArg = packerArg;
148
132
  this.unpackerProc = unpackerProc;
@@ -157,6 +141,10 @@ public class ExtensionRegistry {
157
141
  return typeId;
158
142
  }
159
143
 
144
+ public boolean isRecursive() {
145
+ return recursive;
146
+ }
147
+
160
148
  public boolean hasPacker() {
161
149
  return packerProc != null;
162
150
  }
@@ -173,16 +161,16 @@ public class ExtensionRegistry {
173
161
  return unpackerProc;
174
162
  }
175
163
 
176
- public RubyArray toPackerTuple(ThreadContext ctx) {
177
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {RubyFixnum.newFixnum(ctx.getRuntime(), typeId), packerProc, packerArg});
164
+ public RubyArray<?> toPackerTuple(ThreadContext ctx) {
165
+ return ctx.runtime.newArray(new IRubyObject[] {ctx.runtime.newFixnum(typeId), packerProc, packerArg});
178
166
  }
179
167
 
180
- public RubyArray toUnpackerTuple(ThreadContext ctx) {
181
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {mod, unpackerProc, unpackerArg});
168
+ public RubyArray<?> toUnpackerTuple(ThreadContext ctx) {
169
+ return ctx.runtime.newArray(new IRubyObject[] {mod, unpackerProc, unpackerArg});
182
170
  }
183
171
 
184
172
  public IRubyObject[] toPackerProcTypeIdPair(ThreadContext ctx) {
185
- return new IRubyObject[] {packerProc, RubyFixnum.newFixnum(ctx.getRuntime(), typeId)};
173
+ return new IRubyObject[] {packerProc, ctx.runtime.newFixnum(typeId)};
186
174
  }
187
175
  }
188
176
  }
@@ -25,6 +25,7 @@ import static org.msgpack.jruby.Types.*;
25
25
 
26
26
  @JRubyClass(name="MessagePack::ExtensionValue")
27
27
  public class ExtensionValue extends RubyObject {
28
+ private static final long serialVersionUID = 8451274621449322492L;
28
29
  private final Encoding binaryEncoding;
29
30
 
30
31
  private RubyFixnum type;
@@ -77,12 +78,10 @@ public class ExtensionValue extends RubyObject {
77
78
  }
78
79
  if (o instanceof ExtensionValue) {
79
80
  ExtensionValue other = (ExtensionValue) o;
80
- if (!this.type.eql_p(other.type).isTrue())
81
+ if (!this.type.eql_p(other.type).isTrue()) {
81
82
  return runtime.getFalse();
82
- if (runtime.is1_8()) {
83
- return this.payload.str_eql_p(ctx, other.payload);
84
83
  } else {
85
- return this.payload.str_eql_p19(ctx, other.payload);
84
+ return this.payload.str_eql_p(ctx, other.payload);
86
85
  }
87
86
  }
88
87
  return runtime.getFalse();
@@ -96,12 +95,10 @@ public class ExtensionValue extends RubyObject {
96
95
  }
97
96
  if (o instanceof ExtensionValue) {
98
97
  ExtensionValue other = (ExtensionValue) o;
99
- if (!this.type.op_equal(ctx, other.type).isTrue())
98
+ if (!this.type.op_equal(ctx, other.type).isTrue()) {
100
99
  return runtime.getFalse();
101
- if (runtime.is1_8()) {
102
- return this.payload.op_equal(ctx, other.payload);
103
100
  } else {
104
- return this.payload.op_equal19(ctx, other.payload);
101
+ return this.payload.op_equal(ctx, other.payload);
105
102
  }
106
103
  }
107
104
  return runtime.getFalse();