msgpack 1.4.2 → 1.7.2

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 (95) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +85 -0
  3. data/README.md +52 -1
  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 +126 -108
  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
@@ -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
  }
@@ -27,53 +27,65 @@ import static org.jruby.runtime.Visibility.PRIVATE;
27
27
 
28
28
  @JRubyClass(name="MessagePack::Packer")
29
29
  public class Packer extends RubyObject {
30
- public ExtensionRegistry registry;
30
+ private static final long serialVersionUID = 8451274621499362492L;
31
+ public transient ExtensionRegistry registry;
31
32
  private Buffer buffer;
32
- private Encoder encoder;
33
+ private transient Encoder encoder;
33
34
  private boolean hasSymbolExtType;
34
- private Encoding binaryEncoding;
35
+ private boolean hasBigintExtType;
36
+ private transient Encoding binaryEncoding;
35
37
 
36
- public Packer(Ruby runtime, RubyClass type, ExtensionRegistry registry, boolean hasSymbolExtType) {
38
+ public Packer(Ruby runtime, RubyClass type, ExtensionRegistry registry, boolean hasSymbolExtType, boolean hasBigintExtType) {
37
39
  super(runtime, type);
38
40
  this.registry = registry;
39
41
  this.hasSymbolExtType = hasSymbolExtType;
42
+ this.hasBigintExtType = hasBigintExtType;
40
43
  }
41
44
 
42
45
  static class PackerAllocator implements ObjectAllocator {
43
46
  public IRubyObject allocate(Ruby runtime, RubyClass type) {
44
- return new Packer(runtime, type, null, false);
47
+ return new Packer(runtime, type, null, false, false);
45
48
  }
46
49
  }
47
50
 
48
51
  @JRubyMethod(name = "initialize", optional = 2)
49
52
  public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
50
53
  boolean compatibilityMode = false;
51
- if (args.length > 0 && args[args.length - 1] instanceof RubyHash) {
52
- RubyHash options = (RubyHash) args[args.length - 1];
53
- IRubyObject mode = options.fastARef(ctx.getRuntime().newSymbol("compatibility_mode"));
54
- compatibilityMode = (mode != null) && mode.isTrue();
54
+ Ruby runtime = ctx.runtime;
55
+ if (args.length > 0) {
56
+ RubyHash options = null;
57
+ if (args[args.length - 1] instanceof RubyHash) {
58
+ options = (RubyHash) args[args.length - 1];
59
+ } else if (args.length > 1 && args[args.length - 2] instanceof RubyHash) {
60
+ options = (RubyHash) args[args.length - 2];
61
+ }
62
+
63
+ if (options != null) {
64
+ IRubyObject mode = options.fastARef(runtime.newSymbol("compatibility_mode"));
65
+ compatibilityMode = (mode != null) && mode.isTrue();
66
+ }
55
67
  }
56
68
  if (registry == null) {
57
69
  // registry is null when allocate -> initialize
58
70
  // registry is already initialized (and somthing might be registered) when newPacker from Factory
59
71
  this.registry = new ExtensionRegistry();
60
72
  }
61
- this.encoder = new Encoder(ctx.getRuntime(), compatibilityMode, registry, hasSymbolExtType);
62
- this.buffer = new Buffer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Buffer"));
73
+ this.encoder = new Encoder(runtime, this, compatibilityMode, registry, hasSymbolExtType, hasBigintExtType);
74
+ this.buffer = new Buffer(runtime, runtime.getModule("MessagePack").getClass("Buffer"));
63
75
  this.buffer.initialize(ctx, args);
64
- this.binaryEncoding = ctx.getRuntime().getEncodingService().getAscii8bitEncoding();
76
+ this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
65
77
  return this;
66
78
  }
67
79
 
68
- public static Packer newPacker(ThreadContext ctx, ExtensionRegistry extRegistry, boolean hasSymbolExtType, IRubyObject[] args) {
69
- Packer packer = new Packer(ctx.getRuntime(), ctx.getRuntime().getModule("MessagePack").getClass("Packer"), extRegistry, hasSymbolExtType);
80
+ public static Packer newPacker(ThreadContext ctx, ExtensionRegistry extRegistry, boolean hasSymbolExtType, boolean hasBigintExtType, IRubyObject[] args) {
81
+ Packer packer = new Packer(ctx.runtime, ctx.runtime.getModule("MessagePack").getClass("Packer"), extRegistry, hasSymbolExtType, hasBigintExtType);
70
82
  packer.initialize(ctx, args);
71
83
  return packer;
72
84
  }
73
85
 
74
86
  @JRubyMethod(name = "compatibility_mode?")
75
87
  public IRubyObject isCompatibilityMode(ThreadContext ctx) {
76
- return encoder.isCompatibilityMode() ? ctx.getRuntime().getTrue() : ctx.getRuntime().getFalse();
88
+ return encoder.isCompatibilityMode() ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
77
89
  }
78
90
 
79
91
  @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
@@ -81,26 +93,11 @@ public class Packer extends RubyObject {
81
93
  return registry.toInternalPackerRegistry(ctx);
82
94
  }
83
95
 
84
- @JRubyMethod(name = "register_type", required = 2, optional = 1)
85
- public IRubyObject registerType(ThreadContext ctx, IRubyObject[] args, final Block block) {
86
- Ruby runtime = ctx.getRuntime();
87
- IRubyObject type = args[0];
88
- IRubyObject mod = args[1];
89
-
90
- IRubyObject arg;
91
- IRubyObject proc;
92
- if (args.length == 2) {
93
- if (! block.isGiven()) {
94
- throw runtime.newLocalJumpErrorNoBlock();
95
- }
96
- proc = block.getProcObject();
97
- arg = proc;
98
- } else if (args.length == 3) {
99
- arg = args[2];
100
- proc = arg.callMethod(ctx, "to_proc");
101
- } else {
102
- throw runtime.newArgumentError(String.format("wrong number of arguments (%d for 2..3)", 2 + args.length));
103
- }
96
+ @JRubyMethod(name = "register_type_internal", required = 3, visibility = PRIVATE)
97
+ public IRubyObject registerType(ThreadContext ctx, IRubyObject type, IRubyObject mod, IRubyObject proc) {
98
+ testFrozen("MessagePack::Packer");
99
+
100
+ Ruby runtime = ctx.runtime;
104
101
 
105
102
  long typeId = ((RubyFixnum) type).getLongValue();
106
103
  if (typeId < -128 || typeId > 127) {
@@ -112,9 +109,9 @@ public class Packer extends RubyObject {
112
109
  }
113
110
  RubyModule extModule = (RubyModule) mod;
114
111
 
115
- registry.put(extModule, (int) typeId, proc, arg, null, null);
112
+ registry.put(extModule, (int) typeId, false, proc, null);
116
113
 
117
- if (extModule == runtime.getSymbol()) {
114
+ if (extModule == runtime.getSymbol() && !proc.isNil()) {
118
115
  encoder.hasSymbolExtType = true;
119
116
  }
120
117
 
@@ -182,12 +179,12 @@ public class Packer extends RubyObject {
182
179
 
183
180
  @JRubyMethod(name = "write_true")
184
181
  public IRubyObject writeTrue(ThreadContext ctx) {
185
- return write(ctx, ctx.getRuntime().getTrue());
182
+ return write(ctx, ctx.runtime.getTrue());
186
183
  }
187
184
 
188
185
  @JRubyMethod(name = "write_false")
189
186
  public IRubyObject writeFalse(ThreadContext ctx) {
190
- return write(ctx, ctx.getRuntime().getFalse());
187
+ return write(ctx, ctx.runtime.getFalse());
191
188
  }
192
189
 
193
190
  @JRubyMethod(name = "write_nil")
@@ -255,7 +252,7 @@ public class Packer extends RubyObject {
255
252
  return buffer.size(ctx);
256
253
  }
257
254
 
258
- @JRubyMethod(name = "clear")
255
+ @JRubyMethod(name = "clear", alias = { "reset" })
259
256
  public IRubyObject clear(ThreadContext ctx) {
260
257
  return buffer.clear(ctx);
261
258
  }