msgpack 1.4.2 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +89 -0
  3. data/README.md +73 -13
  4. data/ext/java/org/msgpack/jruby/Buffer.java +26 -19
  5. data/ext/java/org/msgpack/jruby/Decoder.java +29 -21
  6. data/ext/java/org/msgpack/jruby/Encoder.java +68 -30
  7. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +43 -64
  8. data/ext/java/org/msgpack/jruby/ExtensionValue.java +6 -9
  9. data/ext/java/org/msgpack/jruby/Factory.java +43 -42
  10. data/ext/java/org/msgpack/jruby/Packer.java +37 -40
  11. data/ext/java/org/msgpack/jruby/Unpacker.java +80 -73
  12. data/ext/msgpack/buffer.c +54 -74
  13. data/ext/msgpack/buffer.h +21 -18
  14. data/ext/msgpack/buffer_class.c +161 -52
  15. data/ext/msgpack/buffer_class.h +1 -0
  16. data/ext/msgpack/compat.h +0 -99
  17. data/ext/msgpack/extconf.rb +25 -46
  18. data/ext/msgpack/factory_class.c +143 -87
  19. data/ext/msgpack/packer.c +66 -43
  20. data/ext/msgpack/packer.h +25 -20
  21. data/ext/msgpack/packer_class.c +102 -130
  22. data/ext/msgpack/packer_class.h +11 -0
  23. data/ext/msgpack/packer_ext_registry.c +35 -40
  24. data/ext/msgpack/packer_ext_registry.h +41 -38
  25. data/ext/msgpack/rbinit.c +1 -1
  26. data/ext/msgpack/rmem.c +3 -4
  27. data/ext/msgpack/sysdep.h +5 -2
  28. data/ext/msgpack/unpacker.c +136 -111
  29. data/ext/msgpack/unpacker.h +16 -13
  30. data/ext/msgpack/unpacker_class.c +86 -126
  31. data/ext/msgpack/unpacker_class.h +11 -0
  32. data/ext/msgpack/unpacker_ext_registry.c +40 -28
  33. data/ext/msgpack/unpacker_ext_registry.h +21 -18
  34. data/lib/msgpack/bigint.rb +69 -0
  35. data/lib/msgpack/buffer.rb +9 -0
  36. data/lib/msgpack/factory.rb +140 -10
  37. data/lib/msgpack/packer.rb +10 -1
  38. data/lib/msgpack/symbol.rb +21 -4
  39. data/lib/msgpack/time.rb +1 -1
  40. data/lib/msgpack/unpacker.rb +14 -1
  41. data/lib/msgpack/version.rb +1 -1
  42. data/lib/msgpack.rb +6 -7
  43. data/msgpack.gemspec +8 -5
  44. metadata +37 -82
  45. data/.gitignore +0 -23
  46. data/.rubocop.yml +0 -36
  47. data/.travis.yml +0 -39
  48. data/Gemfile +0 -9
  49. data/Rakefile +0 -71
  50. data/appveyor.yml +0 -18
  51. data/bench/pack.rb +0 -23
  52. data/bench/pack_log.rb +0 -33
  53. data/bench/pack_log_long.rb +0 -65
  54. data/bench/pack_symbols.rb +0 -28
  55. data/bench/run.sh +0 -14
  56. data/bench/run_long.sh +0 -35
  57. data/bench/run_symbols.sh +0 -26
  58. data/bench/unpack.rb +0 -21
  59. data/bench/unpack_log.rb +0 -34
  60. data/bench/unpack_log_long.rb +0 -67
  61. data/doclib/msgpack/buffer.rb +0 -193
  62. data/doclib/msgpack/core_ext.rb +0 -101
  63. data/doclib/msgpack/error.rb +0 -19
  64. data/doclib/msgpack/extension_value.rb +0 -9
  65. data/doclib/msgpack/factory.rb +0 -101
  66. data/doclib/msgpack/packer.rb +0 -208
  67. data/doclib/msgpack/time.rb +0 -22
  68. data/doclib/msgpack/timestamp.rb +0 -44
  69. data/doclib/msgpack/unpacker.rb +0 -183
  70. data/doclib/msgpack.rb +0 -87
  71. data/msgpack.org.md +0 -46
  72. data/spec/cases.json +0 -1
  73. data/spec/cases.msg +0 -0
  74. data/spec/cases_compact.msg +0 -0
  75. data/spec/cases_spec.rb +0 -39
  76. data/spec/cruby/buffer_io_spec.rb +0 -255
  77. data/spec/cruby/buffer_packer.rb +0 -29
  78. data/spec/cruby/buffer_spec.rb +0 -575
  79. data/spec/cruby/buffer_unpacker.rb +0 -19
  80. data/spec/cruby/unpacker_spec.rb +0 -70
  81. data/spec/ext_value_spec.rb +0 -99
  82. data/spec/exttypes.rb +0 -51
  83. data/spec/factory_spec.rb +0 -367
  84. data/spec/format_spec.rb +0 -301
  85. data/spec/jruby/benchmarks/shootout_bm.rb +0 -73
  86. data/spec/jruby/benchmarks/symbolize_keys_bm.rb +0 -25
  87. data/spec/jruby/unpacker_spec.rb +0 -186
  88. data/spec/msgpack_spec.rb +0 -214
  89. data/spec/pack_spec.rb +0 -61
  90. data/spec/packer_spec.rb +0 -557
  91. data/spec/random_compat.rb +0 -24
  92. data/spec/spec_helper.rb +0 -55
  93. data/spec/timestamp_spec.rb +0 -121
  94. data/spec/unpack_spec.rb +0 -57
  95. data/spec/unpacker_spec.rb +0 -819
@@ -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) {
@@ -18,26 +18,21 @@ public class ExtensionRegistry {
18
18
  private final ExtensionEntry[] extensionsByTypeId;
19
19
 
20
20
  public ExtensionRegistry() {
21
- this(new HashMap<RubyModule, ExtensionEntry>());
21
+ this(new HashMap<RubyModule, ExtensionEntry>(), new ExtensionEntry[256]);
22
22
  }
23
23
 
24
- private ExtensionRegistry(Map<RubyModule, ExtensionEntry> extensionsByModule) {
24
+ private ExtensionRegistry(Map<RubyModule, ExtensionEntry> extensionsByModule, ExtensionEntry[] extensionsByTypeId) {
25
25
  this.extensionsByModule = new HashMap<RubyModule, ExtensionEntry>(extensionsByModule);
26
26
  this.extensionsByAncestor = new HashMap<RubyModule, ExtensionEntry>();
27
- this.extensionsByTypeId = new ExtensionEntry[256];
28
- for (ExtensionEntry entry : extensionsByModule.values()) {
29
- if (entry.hasUnpacker()) {
30
- extensionsByTypeId[entry.getTypeId() + 128] = entry;
31
- }
32
- }
27
+ this.extensionsByTypeId = extensionsByTypeId.clone();
33
28
  }
34
29
 
35
30
  public ExtensionRegistry dup() {
36
- return new ExtensionRegistry(extensionsByModule);
31
+ return new ExtensionRegistry(extensionsByModule, extensionsByTypeId);
37
32
  }
38
33
 
39
34
  public IRubyObject toInternalPackerRegistry(ThreadContext ctx) {
40
- RubyHash hash = RubyHash.newHash(ctx.getRuntime());
35
+ RubyHash hash = RubyHash.newHash(ctx.runtime);
41
36
  for (RubyModule extensionModule : extensionsByModule.keySet()) {
42
37
  ExtensionEntry entry = extensionsByModule.get(extensionModule);
43
38
  if (entry.hasPacker()) {
@@ -48,75 +43,57 @@ public class ExtensionRegistry {
48
43
  }
49
44
 
50
45
  public IRubyObject toInternalUnpackerRegistry(ThreadContext ctx) {
51
- RubyHash hash = RubyHash.newHash(ctx.getRuntime());
46
+ RubyHash hash = RubyHash.newHash(ctx.runtime);
52
47
  for (int typeIdIndex = 0 ; typeIdIndex < 256 ; typeIdIndex++) {
53
48
  ExtensionEntry entry = extensionsByTypeId[typeIdIndex];
54
49
  if (entry != null && entry.hasUnpacker()) {
55
- IRubyObject typeId = RubyFixnum.newFixnum(ctx.getRuntime(), typeIdIndex - 128);
50
+ IRubyObject typeId = RubyFixnum.newFixnum(ctx.runtime, typeIdIndex - 128);
56
51
  hash.put(typeId, entry.toUnpackerTuple(ctx));
57
52
  }
58
53
  }
59
54
  return hash;
60
55
  }
61
56
 
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);
57
+ public void put(RubyModule mod, int typeId, boolean recursive, IRubyObject packerProc, IRubyObject unpackerProc) {
58
+ ExtensionEntry entry = new ExtensionEntry(mod, typeId, recursive, packerProc, unpackerProc);
64
59
  extensionsByModule.put(mod, entry);
65
60
  extensionsByTypeId[typeId + 128] = entry;
66
61
  extensionsByAncestor.clear();
67
62
  }
68
63
 
69
- public IRubyObject lookupUnpackerByTypeId(int typeId) {
64
+ public ExtensionEntry lookupExtensionByTypeId(int typeId) {
70
65
  ExtensionEntry e = extensionsByTypeId[typeId + 128];
71
66
  if (e != null && e.hasUnpacker()) {
72
- return e.getUnpackerProc();
73
- } else {
74
- return null;
67
+ return e;
75
68
  }
69
+ return null;
76
70
  }
77
71
 
78
- public IRubyObject[] lookupPackerForObject(IRubyObject object) {
72
+ public ExtensionEntry lookupExtensionForObject(IRubyObject object) {
79
73
  RubyModule lookupClass = null;
80
- IRubyObject[] pair;
74
+ ExtensionEntry entry = null;
81
75
  /*
82
76
  * Objects of type Integer (Fixnum, Bignum), Float, Symbol and frozen
83
77
  * String have no singleton class and raise a TypeError when trying to get
84
78
  * it.
85
- *
86
- * Since all but symbols are already filtered out when reaching this code
87
- * only symbols are checked here.
88
79
  */
89
- if (!(object instanceof RubySymbol)) {
90
- lookupClass = object.getSingletonClass();
91
- pair = fetchEntryByModule(lookupClass);
92
- if (pair != null) {
93
- return pair;
94
- }
80
+ lookupClass = object.getMetaClass();
81
+ entry = extensionsByModule.get(lookupClass);
82
+ if (entry != null && entry.hasPacker()) {
83
+ return entry;
95
84
  }
96
85
 
97
- pair = fetchEntryByModule(object.getType());
98
- if (pair != null) {
99
- return pair;
100
- }
101
-
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());
86
+ RubyModule realClass = object.getType();
87
+ if (realClass != lookupClass) {
88
+ entry = extensionsByModule.get(realClass);
89
+ if (entry != null && entry.hasPacker()) {
90
+ return entry;
91
+ }
109
92
  }
110
- return null;
111
- }
112
93
 
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());
94
+ entry = findEntryByModuleOrAncestor(lookupClass);
95
+ if (entry != null && entry.hasPacker()) {
96
+ return entry;
120
97
  }
121
98
  return null;
122
99
  }
@@ -124,7 +101,7 @@ public class ExtensionRegistry {
124
101
  private ExtensionEntry findEntryByModuleOrAncestor(final RubyModule mod) {
125
102
  ThreadContext ctx = mod.getRuntime().getCurrentContext();
126
103
  for (RubyModule extensionModule : extensionsByModule.keySet()) {
127
- RubyArray ancestors = (RubyArray) mod.callMethod(ctx, "ancestors");
104
+ RubyArray<?> ancestors = (RubyArray)mod.callMethod(ctx, "ancestors");
128
105
  if (ancestors.callMethod(ctx, "include?", extensionModule).isTrue()) {
129
106
  return extensionsByModule.get(extensionModule);
130
107
  }
@@ -132,21 +109,19 @@ public class ExtensionRegistry {
132
109
  return null;
133
110
  }
134
111
 
135
- private static class ExtensionEntry {
112
+ public static class ExtensionEntry {
136
113
  private final RubyModule mod;
137
114
  private final int typeId;
115
+ private final boolean recursive;
138
116
  private final IRubyObject packerProc;
139
- private final IRubyObject packerArg;
140
117
  private final IRubyObject unpackerProc;
141
- private final IRubyObject unpackerArg;
142
118
 
143
- public ExtensionEntry(RubyModule mod, int typeId, IRubyObject packerProc, IRubyObject packerArg, IRubyObject unpackerProc, IRubyObject unpackerArg) {
119
+ public ExtensionEntry(RubyModule mod, int typeId, boolean recursive, IRubyObject packerProc, IRubyObject unpackerProc) {
144
120
  this.mod = mod;
145
121
  this.typeId = typeId;
122
+ this.recursive = recursive;
146
123
  this.packerProc = packerProc;
147
- this.packerArg = packerArg;
148
124
  this.unpackerProc = unpackerProc;
149
- this.unpackerArg = unpackerArg;
150
125
  }
151
126
 
152
127
  public RubyModule getExtensionModule() {
@@ -157,12 +132,16 @@ public class ExtensionRegistry {
157
132
  return typeId;
158
133
  }
159
134
 
135
+ public boolean isRecursive() {
136
+ return recursive;
137
+ }
138
+
160
139
  public boolean hasPacker() {
161
- return packerProc != null;
140
+ return packerProc != null && !packerProc.isNil();
162
141
  }
163
142
 
164
143
  public boolean hasUnpacker() {
165
- return unpackerProc != null;
144
+ return unpackerProc != null && !unpackerProc.isNil();
166
145
  }
167
146
 
168
147
  public IRubyObject getPackerProc() {
@@ -173,16 +152,16 @@ public class ExtensionRegistry {
173
152
  return unpackerProc;
174
153
  }
175
154
 
176
- public RubyArray toPackerTuple(ThreadContext ctx) {
177
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {RubyFixnum.newFixnum(ctx.getRuntime(), typeId), packerProc, packerArg});
155
+ public RubyArray<?> toPackerTuple(ThreadContext ctx) {
156
+ return ctx.runtime.newArray(new IRubyObject[] {ctx.runtime.newFixnum(typeId), packerProc});
178
157
  }
179
158
 
180
- public RubyArray toUnpackerTuple(ThreadContext ctx) {
181
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {mod, unpackerProc, unpackerArg});
159
+ public RubyArray<?> toUnpackerTuple(ThreadContext ctx) {
160
+ return ctx.runtime.newArray(new IRubyObject[] {mod, unpackerProc});
182
161
  }
183
162
 
184
163
  public IRubyObject[] toPackerProcTypeIdPair(ThreadContext ctx) {
185
- return new IRubyObject[] {packerProc, RubyFixnum.newFixnum(ctx.getRuntime(), typeId)};
164
+ return new IRubyObject[] {packerProc, ctx.runtime.newFixnum(typeId)};
186
165
  }
187
166
  }
188
167
  }
@@ -25,7 +25,8 @@ import static org.msgpack.jruby.Types.*;
25
25
 
26
26
  @JRubyClass(name="MessagePack::ExtensionValue")
27
27
  public class ExtensionValue extends RubyObject {
28
- private final Encoding binaryEncoding;
28
+ private static final long serialVersionUID = 8451274621449322492L;
29
+ private transient final Encoding binaryEncoding;
29
30
 
30
31
  private RubyFixnum type;
31
32
  private RubyString payload;
@@ -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();
@@ -12,6 +12,8 @@ import org.jruby.RubyInteger;
12
12
  import org.jruby.RubyFixnum;
13
13
  import org.jruby.RubyString;
14
14
  import org.jruby.RubySymbol;
15
+ import org.jruby.RubyProc;
16
+ import org.jruby.RubyMethod;
15
17
  import org.jruby.runtime.builtin.IRubyObject;
16
18
  import org.jruby.anno.JRubyClass;
17
19
  import org.jruby.anno.JRubyMethod;
@@ -23,15 +25,18 @@ import static org.jruby.runtime.Visibility.PRIVATE;
23
25
 
24
26
  @JRubyClass(name="MessagePack::Factory")
25
27
  public class Factory extends RubyObject {
26
- private final Ruby runtime;
27
- private final ExtensionRegistry extensionRegistry;
28
+ private static final long serialVersionUID = 8441284623445322492L;
29
+ private transient final Ruby runtime;
30
+ private transient ExtensionRegistry extensionRegistry;
28
31
  private boolean hasSymbolExtType;
32
+ private boolean hasBigIntExtType;
29
33
 
30
34
  public Factory(Ruby runtime, RubyClass type) {
31
35
  super(runtime, type);
32
36
  this.runtime = runtime;
33
37
  this.extensionRegistry = new ExtensionRegistry();
34
38
  this.hasSymbolExtType = false;
39
+ this.hasBigIntExtType = false;
35
40
  }
36
41
 
37
42
  static class FactoryAllocator implements ObjectAllocator {
@@ -49,9 +54,17 @@ public class Factory extends RubyObject {
49
54
  return this;
50
55
  }
51
56
 
52
- @JRubyMethod(name = "packer", optional = 1)
57
+ @JRubyMethod(name = "dup")
58
+ public IRubyObject dup() {
59
+ Factory clone = (Factory)super.dup();
60
+ clone.extensionRegistry = extensionRegistry();
61
+ clone.hasSymbolExtType = hasSymbolExtType;
62
+ return clone;
63
+ }
64
+
65
+ @JRubyMethod(name = "packer", optional = 2)
53
66
  public Packer packer(ThreadContext ctx, IRubyObject[] args) {
54
- return Packer.newPacker(ctx, extensionRegistry(), hasSymbolExtType, args);
67
+ return Packer.newPacker(ctx, extensionRegistry(), hasSymbolExtType, hasBigIntExtType, args);
55
68
  }
56
69
 
57
70
  @JRubyMethod(name = "unpacker", optional = 2)
@@ -61,39 +74,21 @@ public class Factory extends RubyObject {
61
74
 
62
75
  @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
63
76
  public IRubyObject registeredTypesInternal(ThreadContext ctx) {
64
- return RubyArray.newArray(ctx.getRuntime(), new IRubyObject[] {
77
+ return RubyArray.newArray(ctx.runtime, new IRubyObject[] {
65
78
  extensionRegistry.toInternalPackerRegistry(ctx),
66
79
  extensionRegistry.toInternalUnpackerRegistry(ctx)
67
80
  });
68
81
  }
69
82
 
70
- @JRubyMethod(name = "register_type", required = 2, optional = 1)
71
- public IRubyObject registerType(ThreadContext ctx, IRubyObject[] args) {
72
- Ruby runtime = ctx.getRuntime();
73
- IRubyObject type = args[0];
74
- IRubyObject mod = args[1];
83
+ @JRubyMethod(name = "register_type_internal", required = 3, visibility = PRIVATE)
84
+ public IRubyObject registerTypeInternal(ThreadContext ctx, IRubyObject type, IRubyObject mod, IRubyObject opts) {
85
+ testFrozen("MessagePack::Factory");
75
86
 
76
- IRubyObject packerArg;
77
- IRubyObject unpackerArg;
87
+ Ruby runtime = ctx.runtime;
88
+ RubyHash options = (RubyHash) opts;
78
89
 
79
- if (isFrozen()) {
80
- throw runtime.newRuntimeError("can't modify frozen Factory");
81
- }
82
-
83
- if (args.length == 2) {
84
- packerArg = runtime.newSymbol("to_msgpack_ext");
85
- unpackerArg = runtime.newSymbol("from_msgpack_ext");
86
- } else if (args.length == 3) {
87
- if (args[args.length - 1] instanceof RubyHash) {
88
- RubyHash options = (RubyHash) args[args.length - 1];
89
- packerArg = options.fastARef(runtime.newSymbol("packer"));
90
- unpackerArg = options.fastARef(runtime.newSymbol("unpacker"));
91
- } else {
92
- throw runtime.newArgumentError(String.format("expected Hash but found %s.", args[args.length - 1].getType().getName()));
93
- }
94
- } else {
95
- throw runtime.newArgumentError(String.format("wrong number of arguments (%d for 2..3)", 2 + args.length));
96
- }
90
+ IRubyObject packerProc = options.fastARef(runtime.newSymbol("packer"));
91
+ IRubyObject unpackerProc = options.fastARef(runtime.newSymbol("unpacker"));
97
92
 
98
93
  long typeId = ((RubyFixnum) type).getLongValue();
99
94
  if (typeId < -128 || typeId > 127) {
@@ -105,25 +100,31 @@ public class Factory extends RubyObject {
105
100
  }
106
101
  RubyModule extModule = (RubyModule) mod;
107
102
 
108
- IRubyObject packerProc = runtime.getNil();
109
- IRubyObject unpackerProc = runtime.getNil();
110
- if (packerArg != null) {
111
- packerProc = packerArg.callMethod(ctx, "to_proc");
112
- }
113
- if (unpackerArg != null) {
114
- if (unpackerArg instanceof RubyString || unpackerArg instanceof RubySymbol) {
115
- unpackerProc = extModule.method(unpackerArg.callMethod(ctx, "to_sym"));
116
- } else {
117
- unpackerProc = unpackerArg.callMethod(ctx, "method", runtime.newSymbol("call"));
103
+ boolean recursive = false;
104
+ if (options != null) {
105
+ IRubyObject recursiveExtensionArg = options.fastARef(runtime.newSymbol("recursive"));
106
+ if (recursiveExtensionArg != null && recursiveExtensionArg.isTrue()) {
107
+ recursive = true;
118
108
  }
119
109
  }
120
110
 
121
- extensionRegistry.put(extModule, (int) typeId, packerProc, packerArg, unpackerProc, unpackerArg);
111
+ extensionRegistry.put(extModule, (int) typeId, recursive, packerProc, unpackerProc);
122
112
 
123
- if (extModule == runtime.getSymbol()) {
113
+ if (extModule == runtime.getSymbol() && !packerProc.isNil()) {
124
114
  hasSymbolExtType = true;
125
115
  }
126
116
 
117
+ if (options != null) {
118
+ IRubyObject oversizedIntegerExtensionArg = options.fastARef(runtime.newSymbol("oversized_integer_extension"));
119
+ if (oversizedIntegerExtensionArg != null && oversizedIntegerExtensionArg.isTrue()) {
120
+ if (extModule == runtime.getModule("Integer")) {
121
+ hasBigIntExtType = true;
122
+ } else {
123
+ throw runtime.newArgumentError("oversized_integer_extension: true is only for Integer class");
124
+ }
125
+ }
126
+ }
127
+
127
128
  return runtime.getNil();
128
129
  }
129
130
  }