ed-precompiled_msgpack 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/ChangeLog +368 -0
  3. data/LICENSE +177 -0
  4. data/README.md +302 -0
  5. data/ext/java/org/msgpack/jruby/Buffer.java +233 -0
  6. data/ext/java/org/msgpack/jruby/Decoder.java +307 -0
  7. data/ext/java/org/msgpack/jruby/Encoder.java +456 -0
  8. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
  9. data/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
  10. data/ext/java/org/msgpack/jruby/Factory.java +130 -0
  11. data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
  12. data/ext/java/org/msgpack/jruby/Packer.java +266 -0
  13. data/ext/java/org/msgpack/jruby/Types.java +37 -0
  14. data/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
  15. data/ext/msgpack/buffer.c +669 -0
  16. data/ext/msgpack/buffer.h +604 -0
  17. data/ext/msgpack/buffer_class.c +616 -0
  18. data/ext/msgpack/buffer_class.h +33 -0
  19. data/ext/msgpack/compat.h +26 -0
  20. data/ext/msgpack/extconf.rb +53 -0
  21. data/ext/msgpack/extension_value_class.c +34 -0
  22. data/ext/msgpack/extension_value_class.h +31 -0
  23. data/ext/msgpack/factory_class.c +276 -0
  24. data/ext/msgpack/factory_class.h +33 -0
  25. data/ext/msgpack/packer.c +199 -0
  26. data/ext/msgpack/packer.h +513 -0
  27. data/ext/msgpack/packer_class.c +442 -0
  28. data/ext/msgpack/packer_class.h +43 -0
  29. data/ext/msgpack/packer_ext_registry.c +74 -0
  30. data/ext/msgpack/packer_ext_registry.h +140 -0
  31. data/ext/msgpack/rbinit.c +35 -0
  32. data/ext/msgpack/rmem.c +93 -0
  33. data/ext/msgpack/rmem.h +109 -0
  34. data/ext/msgpack/sysdep.h +118 -0
  35. data/ext/msgpack/sysdep_endian.h +50 -0
  36. data/ext/msgpack/sysdep_types.h +46 -0
  37. data/ext/msgpack/unpacker.c +986 -0
  38. data/ext/msgpack/unpacker.h +152 -0
  39. data/ext/msgpack/unpacker_class.c +447 -0
  40. data/ext/msgpack/unpacker_class.h +43 -0
  41. data/ext/msgpack/unpacker_ext_registry.c +74 -0
  42. data/ext/msgpack/unpacker_ext_registry.h +62 -0
  43. data/lib/msgpack/bigint.rb +69 -0
  44. data/lib/msgpack/buffer.rb +9 -0
  45. data/lib/msgpack/core_ext.rb +139 -0
  46. data/lib/msgpack/factory.rb +211 -0
  47. data/lib/msgpack/packer.rb +37 -0
  48. data/lib/msgpack/symbol.rb +26 -0
  49. data/lib/msgpack/time.rb +29 -0
  50. data/lib/msgpack/timestamp.rb +76 -0
  51. data/lib/msgpack/unpacker.rb +41 -0
  52. data/lib/msgpack/version.rb +6 -0
  53. data/lib/msgpack.rb +53 -0
  54. data/msgpack.gemspec +41 -0
  55. metadata +216 -0
@@ -0,0 +1,336 @@
1
+ package org.msgpack.jruby;
2
+
3
+ import java.util.Arrays;
4
+
5
+ import org.jruby.Ruby;
6
+ import org.jruby.RubyModule;
7
+ import org.jruby.RubyClass;
8
+ import org.jruby.RubyString;
9
+ import org.jruby.RubyObject;
10
+ import org.jruby.RubyArray;
11
+ import org.jruby.RubyHash;
12
+ import org.jruby.RubyNumeric;
13
+ import org.jruby.RubyFixnum;
14
+ import org.jruby.RubyProc;
15
+ import org.jruby.RubyIO;
16
+ import org.jruby.exceptions.RaiseException;
17
+ import org.jruby.runtime.builtin.IRubyObject;
18
+ import org.jruby.runtime.Block;
19
+ import org.jruby.runtime.ObjectAllocator;
20
+ import org.jruby.runtime.ThreadContext;
21
+ import org.jruby.anno.JRubyClass;
22
+ import org.jruby.anno.JRubyMethod;
23
+ import org.jruby.util.ByteList;
24
+
25
+ import static org.jruby.runtime.Visibility.PRIVATE;
26
+
27
+ @JRubyClass(name="MessagePack::Unpacker")
28
+ public class Unpacker extends RubyObject {
29
+ private static final long serialVersionUID = 8451264671199362492L;
30
+ private transient final ExtensionRegistry registry;
31
+
32
+ private transient IRubyObject stream;
33
+ private transient IRubyObject data;
34
+ private transient Decoder decoder;
35
+ private final RubyClass underflowErrorClass;
36
+ private boolean symbolizeKeys;
37
+ private boolean freeze;
38
+ private boolean allowUnknownExt;
39
+
40
+ public Unpacker(Ruby runtime, RubyClass type) {
41
+ this(runtime, type, new ExtensionRegistry());
42
+ }
43
+
44
+ public Unpacker(Ruby runtime, RubyClass type, ExtensionRegistry registry) {
45
+ super(runtime, type);
46
+ this.registry = registry;
47
+ this.underflowErrorClass = runtime.getModule("MessagePack").getClass("UnderflowError");
48
+ }
49
+
50
+ static class UnpackerAllocator implements ObjectAllocator {
51
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
52
+ return new Unpacker(runtime, klass);
53
+ }
54
+ }
55
+
56
+ @JRubyMethod(name = "initialize", optional = 2, visibility = PRIVATE)
57
+ public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
58
+ Ruby runtime = ctx.runtime;
59
+
60
+ symbolizeKeys = false;
61
+ allowUnknownExt = false;
62
+ freeze = false;
63
+
64
+ IRubyObject io = null;
65
+ RubyHash options = null;
66
+
67
+ if (args.length >= 1) {
68
+ io = args[0];
69
+ }
70
+
71
+ if (args.length >= 2 && args[1] != runtime.getNil()) {
72
+ options = (RubyHash)args[1];
73
+ }
74
+
75
+ if (options == null && io != null && io instanceof RubyHash) {
76
+ options = (RubyHash)io;
77
+ io = null;
78
+ }
79
+
80
+ if (options != null) {
81
+ IRubyObject sk = options.fastARef(runtime.newSymbol("symbolize_keys"));
82
+ if (sk != null) {
83
+ symbolizeKeys = sk.isTrue();
84
+ }
85
+ IRubyObject f = options.fastARef(runtime.newSymbol("freeze"));
86
+ if (f != null) {
87
+ freeze = f.isTrue();
88
+ }
89
+ IRubyObject au = options.fastARef(runtime.newSymbol("allow_unknown_ext"));
90
+ if (au != null) {
91
+ allowUnknownExt = au.isTrue();
92
+ }
93
+
94
+ }
95
+
96
+ if (io != null && io != runtime.getNil()) {
97
+ setStream(ctx, io);
98
+ }
99
+
100
+ return this;
101
+ }
102
+
103
+ public static Unpacker newUnpacker(ThreadContext ctx, ExtensionRegistry extRegistry, IRubyObject[] args) {
104
+ Unpacker unpacker = new Unpacker(ctx.runtime, ctx.runtime.getModule("MessagePack").getClass("Unpacker"), extRegistry);
105
+ unpacker.initialize(ctx, args);
106
+ return unpacker;
107
+ }
108
+
109
+ @JRubyMethod(name = "symbolize_keys?")
110
+ public IRubyObject isSymbolizeKeys(ThreadContext ctx) {
111
+ return symbolizeKeys ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
112
+ }
113
+
114
+ @JRubyMethod(name = "freeze?")
115
+ public IRubyObject isFreeze(ThreadContext ctx) {
116
+ return freeze ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
117
+ }
118
+
119
+ @JRubyMethod(name = "allow_unknown_ext?")
120
+ public IRubyObject isAllowUnknownExt(ThreadContext ctx) {
121
+ return allowUnknownExt ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
122
+ }
123
+
124
+ @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
125
+ public IRubyObject registeredTypesInternal(ThreadContext ctx) {
126
+ return registry.toInternalUnpackerRegistry(ctx);
127
+ }
128
+
129
+ @JRubyMethod(name = "register_type_internal", required = 3, visibility = PRIVATE)
130
+ public IRubyObject registerTypeInternal(ThreadContext ctx, IRubyObject type, IRubyObject mod, IRubyObject proc) {
131
+ testFrozen("MessagePack::Unpacker");
132
+
133
+ Ruby runtime = ctx.runtime;
134
+
135
+ long typeId = ((RubyFixnum) type).getLongValue();
136
+ if (typeId < -128 || typeId > 127) {
137
+ throw runtime.newRangeError(String.format("integer %d too big to convert to `signed char'", typeId));
138
+ }
139
+
140
+ RubyModule extModule = null;
141
+ if (mod != runtime.getNil()) {
142
+ extModule = (RubyModule)mod;
143
+ }
144
+
145
+ registry.put(extModule, (int) typeId, false, null, proc);
146
+ return runtime.getNil();
147
+ }
148
+
149
+ @JRubyMethod(required = 2)
150
+ public IRubyObject execute(ThreadContext ctx, IRubyObject data, IRubyObject offset) {
151
+ return executeLimit(ctx, data, offset, null);
152
+ }
153
+
154
+ @JRubyMethod(name = "execute_limit", required = 3)
155
+ public IRubyObject executeLimit(ThreadContext ctx, IRubyObject str, IRubyObject off, IRubyObject lim) {
156
+ RubyString input = str.asString();
157
+ int offset = RubyNumeric.fix2int(off);
158
+ int limit = lim == null || lim.isNil() ? -1 : RubyNumeric.fix2int(lim);
159
+ ByteList byteList = input.getByteList();
160
+ if (limit == -1) {
161
+ limit = byteList.length() - offset;
162
+ }
163
+ Decoder decoder = new Decoder(ctx.runtime, this, byteList.unsafeBytes(), byteList.begin() + offset, limit, symbolizeKeys, freeze, allowUnknownExt);
164
+ try {
165
+ data = null;
166
+ data = decoder.next();
167
+ } catch (RaiseException re) {
168
+ if (re.getException().getType() != underflowErrorClass) {
169
+ throw re;
170
+ }
171
+ }
172
+ return ctx.runtime.newFixnum(decoder.offset());
173
+ }
174
+
175
+ @JRubyMethod(name = "data")
176
+ public IRubyObject getData(ThreadContext ctx) {
177
+ if (data == null) {
178
+ return ctx.runtime.getNil();
179
+ } else {
180
+ return data;
181
+ }
182
+ }
183
+
184
+ @JRubyMethod(name = "finished?")
185
+ public IRubyObject finished_p(ThreadContext ctx) {
186
+ return data == null ? ctx.runtime.getFalse() : ctx.runtime.getTrue();
187
+ }
188
+
189
+ @JRubyMethod(required = 1, name = "feed", alias = { "feed_reference" })
190
+ public IRubyObject feed(ThreadContext ctx, IRubyObject data) {
191
+ ByteList byteList = data.asString().getByteList();
192
+ if (decoder == null) {
193
+ decoder = new Decoder(ctx.runtime, this, byteList.unsafeBytes(), byteList.begin(), byteList.length(), symbolizeKeys, freeze, allowUnknownExt);
194
+ } else {
195
+ decoder.feed(byteList.unsafeBytes(), byteList.begin(), byteList.length());
196
+ }
197
+ return this;
198
+ }
199
+
200
+ @JRubyMethod(name = "full_unpack")
201
+ public IRubyObject fullUnpack(ThreadContext ctx) {
202
+ return decoder.next();
203
+ }
204
+
205
+ @JRubyMethod(name = "feed_each", required = 1)
206
+ public IRubyObject feedEach(ThreadContext ctx, IRubyObject data, Block block) {
207
+ feed(ctx, data);
208
+ if (block.isGiven()) {
209
+ each(ctx, block);
210
+ return ctx.runtime.getNil();
211
+ } else {
212
+ return callMethod(ctx, "to_enum");
213
+ }
214
+ }
215
+
216
+ @JRubyMethod
217
+ public IRubyObject each(ThreadContext ctx, Block block) {
218
+ if (block.isGiven()) {
219
+ if (decoder != null) {
220
+ try {
221
+ while (decoder.hasNext()) {
222
+ block.yield(ctx, decoder.next());
223
+ }
224
+ } catch (RaiseException re) {
225
+ if (re.getException().getType() != underflowErrorClass) {
226
+ throw re;
227
+ }
228
+ }
229
+ }
230
+ return this;
231
+ } else {
232
+ return callMethod(ctx, "to_enum");
233
+ }
234
+ }
235
+
236
+ @JRubyMethod
237
+ public IRubyObject fill(ThreadContext ctx) {
238
+ return ctx.runtime.getNil();
239
+ }
240
+
241
+ @JRubyMethod
242
+ public IRubyObject reset(ThreadContext ctx) {
243
+ if (decoder != null) {
244
+ decoder.reset();
245
+ }
246
+ return ctx.runtime.getNil();
247
+ }
248
+
249
+ @JRubyMethod(name = "read", alias = { "unpack" })
250
+ public IRubyObject read(ThreadContext ctx) {
251
+ if (decoder == null) {
252
+ throw ctx.runtime.newEOFError();
253
+ }
254
+ try {
255
+ return decoder.next();
256
+ } catch (RaiseException re) {
257
+ if (re.getException().getType() != underflowErrorClass) {
258
+ throw re;
259
+ } else {
260
+ throw ctx.runtime.newEOFError();
261
+ }
262
+ }
263
+ }
264
+
265
+ @JRubyMethod(name = "skip")
266
+ public IRubyObject skip(ThreadContext ctx) {
267
+ throw ctx.runtime.newNotImplementedError("Not supported yet in JRuby implementation");
268
+ }
269
+
270
+ @JRubyMethod(name = "skip_nil")
271
+ public IRubyObject skipNil(ThreadContext ctx) {
272
+ throw ctx.runtime.newNotImplementedError("Not supported yet in JRuby implementation");
273
+ }
274
+
275
+ @JRubyMethod
276
+ public IRubyObject read_array_header(ThreadContext ctx) {
277
+ if (decoder != null) {
278
+ try {
279
+ return decoder.read_array_header();
280
+ } catch (RaiseException re) {
281
+ if (re.getException().getType() != underflowErrorClass) {
282
+ throw re;
283
+ } else {
284
+ throw ctx.runtime.newEOFError();
285
+ }
286
+ }
287
+ }
288
+ return ctx.runtime.getNil();
289
+ }
290
+
291
+ @JRubyMethod
292
+ public IRubyObject read_map_header(ThreadContext ctx) {
293
+ if (decoder != null) {
294
+ try {
295
+ return decoder.read_map_header();
296
+ } catch (RaiseException re) {
297
+ if (re.getException().getType() != underflowErrorClass) {
298
+ throw re;
299
+ } else {
300
+ throw ctx.runtime.newEOFError();
301
+ }
302
+ }
303
+ }
304
+ return ctx.runtime.getNil();
305
+ }
306
+
307
+ @JRubyMethod(name = "stream")
308
+ public IRubyObject getStream(ThreadContext ctx) {
309
+ if (stream == null) {
310
+ return ctx.runtime.getNil();
311
+ } else {
312
+ return stream;
313
+ }
314
+ }
315
+
316
+ @JRubyMethod(name = "stream=", required = 1)
317
+ public IRubyObject setStream(ThreadContext ctx, IRubyObject stream) {
318
+ RubyString str;
319
+ if (stream instanceof RubyIO) {
320
+ str = stream.callMethod(ctx, "read").asString();
321
+ } else if (stream.respondsTo("read")) {
322
+ str = stream.callMethod(ctx, "read").asString();
323
+ } else {
324
+ throw ctx.runtime.newTypeError(stream, "IO");
325
+ }
326
+ ByteList byteList = str.getByteList();
327
+ this.stream = stream;
328
+ this.decoder = null;
329
+ this.decoder = new Decoder(ctx.runtime, this, byteList.unsafeBytes(), byteList.begin(), byteList.length(), symbolizeKeys, freeze, allowUnknownExt);
330
+ return getStream(ctx);
331
+ }
332
+
333
+ public ExtensionRegistry.ExtensionEntry lookupExtensionByTypeId(int typeId) {
334
+ return registry.lookupExtensionByTypeId(typeId);
335
+ }
336
+ }