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,128 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import java.util.Arrays;
5
+ import java.nio.ByteBuffer;
6
+
7
+ import org.jruby.Ruby;
8
+ import org.jruby.RubyClass;
9
+ import org.jruby.RubyObject;
10
+ import org.jruby.RubyFixnum;
11
+ import org.jruby.RubyString;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+ import org.jruby.runtime.ObjectAllocator;
14
+ import org.jruby.runtime.ThreadContext;
15
+ import org.jruby.anno.JRubyClass;
16
+ import org.jruby.anno.JRubyMethod;
17
+ import org.jruby.util.ByteList;
18
+
19
+ import static org.jruby.runtime.Visibility.PRIVATE;
20
+
21
+ import org.jcodings.Encoding;
22
+
23
+ import static org.msgpack.jruby.Types.*;
24
+
25
+
26
+ @JRubyClass(name="MessagePack::ExtensionValue")
27
+ public class ExtensionValue extends RubyObject {
28
+ private static final long serialVersionUID = 8451274621449322492L;
29
+ private transient final Encoding binaryEncoding;
30
+
31
+ private RubyFixnum type;
32
+ private RubyString payload;
33
+
34
+ public ExtensionValue(Ruby runtime, RubyClass type) {
35
+ super(runtime, type);
36
+ this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
37
+ }
38
+
39
+ public static class ExtensionValueAllocator implements ObjectAllocator {
40
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
41
+ return new ExtensionValue(runtime, klass);
42
+ }
43
+ }
44
+
45
+ public static ExtensionValue newExtensionValue(Ruby runtime, int type, byte[] payload) {
46
+ ExtensionValue v = new ExtensionValue(runtime, runtime.getModule("MessagePack").getClass("ExtensionValue"));
47
+ ByteList byteList = new ByteList(payload, runtime.getEncodingService().getAscii8bitEncoding());
48
+ v.initialize(runtime.getCurrentContext(), runtime.newFixnum(type), runtime.newString(byteList));
49
+ return v;
50
+ }
51
+
52
+ @JRubyMethod(name = "initialize", required = 2, visibility = PRIVATE)
53
+ public IRubyObject initialize(ThreadContext ctx, IRubyObject type, IRubyObject payload) {
54
+ this.type = (RubyFixnum) type;
55
+ this.payload = (RubyString) payload;
56
+ return this;
57
+ }
58
+
59
+ @JRubyMethod(name = {"to_s", "inspect"})
60
+ @Override
61
+ public IRubyObject to_s() {
62
+ IRubyObject payloadStr = payload.callMethod(getRuntime().getCurrentContext(), "inspect");
63
+ return getRuntime().newString(String.format("#<MessagePack::ExtensionValue @type=%d, @payload=%s>", type.getLongValue(), payloadStr));
64
+ }
65
+
66
+ @JRubyMethod(name = "hash")
67
+ @Override
68
+ public RubyFixnum hash() {
69
+ long hash = payload.hashCode() ^ (type.getLongValue() << 56);
70
+ return RubyFixnum.newFixnum(getRuntime(), hash);
71
+ }
72
+
73
+ @JRubyMethod(name = "eql?")
74
+ public IRubyObject eql_p(ThreadContext ctx, IRubyObject o) {
75
+ Ruby runtime = ctx.runtime;
76
+ if (this == o) {
77
+ return runtime.getTrue();
78
+ }
79
+ if (o instanceof ExtensionValue) {
80
+ ExtensionValue other = (ExtensionValue) o;
81
+ if (!this.type.eql_p(other.type).isTrue()) {
82
+ return runtime.getFalse();
83
+ } else {
84
+ return this.payload.str_eql_p(ctx, other.payload);
85
+ }
86
+ }
87
+ return runtime.getFalse();
88
+ }
89
+
90
+ @JRubyMethod(name = "==")
91
+ public IRubyObject op_equal(ThreadContext ctx, IRubyObject o) {
92
+ Ruby runtime = ctx.runtime;
93
+ if (this == o) {
94
+ return runtime.getTrue();
95
+ }
96
+ if (o instanceof ExtensionValue) {
97
+ ExtensionValue other = (ExtensionValue) o;
98
+ if (!this.type.op_equal(ctx, other.type).isTrue()) {
99
+ return runtime.getFalse();
100
+ } else {
101
+ return this.payload.op_equal(ctx, other.payload);
102
+ }
103
+ }
104
+ return runtime.getFalse();
105
+ }
106
+
107
+ @JRubyMethod(name = "type")
108
+ public IRubyObject get_type() {
109
+ return type;
110
+ }
111
+
112
+ @JRubyMethod
113
+ public IRubyObject payload() {
114
+ return payload;
115
+ }
116
+
117
+ @JRubyMethod(name = "type=", required = 1)
118
+ public IRubyObject set_type(final IRubyObject tpe) {
119
+ type = (RubyFixnum)tpe;
120
+ return tpe;
121
+ }
122
+
123
+ @JRubyMethod(name = "payload=", required = 1)
124
+ public IRubyObject set_payload(final IRubyObject pld) {
125
+ payload = (RubyString)pld;
126
+ return pld;
127
+ }
128
+ }
@@ -0,0 +1,130 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import org.jruby.Ruby;
5
+ import org.jruby.RubyModule;
6
+ import org.jruby.RubyClass;
7
+ import org.jruby.RubyObject;
8
+ import org.jruby.RubyArray;
9
+ import org.jruby.RubyHash;
10
+ import org.jruby.RubyIO;
11
+ import org.jruby.RubyInteger;
12
+ import org.jruby.RubyFixnum;
13
+ import org.jruby.RubyString;
14
+ import org.jruby.RubySymbol;
15
+ import org.jruby.RubyProc;
16
+ import org.jruby.RubyMethod;
17
+ import org.jruby.runtime.builtin.IRubyObject;
18
+ import org.jruby.anno.JRubyClass;
19
+ import org.jruby.anno.JRubyMethod;
20
+ import org.jruby.runtime.ThreadContext;
21
+ import org.jruby.runtime.ObjectAllocator;
22
+ import org.jruby.util.ByteList;
23
+
24
+ import static org.jruby.runtime.Visibility.PRIVATE;
25
+
26
+ @JRubyClass(name="MessagePack::Factory")
27
+ public class Factory extends RubyObject {
28
+ private static final long serialVersionUID = 8441284623445322492L;
29
+ private transient final Ruby runtime;
30
+ private transient ExtensionRegistry extensionRegistry;
31
+ private boolean hasSymbolExtType;
32
+ private boolean hasBigIntExtType;
33
+
34
+ public Factory(Ruby runtime, RubyClass type) {
35
+ super(runtime, type);
36
+ this.runtime = runtime;
37
+ this.extensionRegistry = new ExtensionRegistry();
38
+ this.hasSymbolExtType = false;
39
+ this.hasBigIntExtType = false;
40
+ }
41
+
42
+ static class FactoryAllocator implements ObjectAllocator {
43
+ public IRubyObject allocate(Ruby runtime, RubyClass type) {
44
+ return new Factory(runtime, type);
45
+ }
46
+ }
47
+
48
+ public ExtensionRegistry extensionRegistry() {
49
+ return extensionRegistry.dup();
50
+ }
51
+
52
+ @JRubyMethod(name = "initialize")
53
+ public IRubyObject initialize(ThreadContext ctx) {
54
+ return this;
55
+ }
56
+
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)
66
+ public Packer packer(ThreadContext ctx, IRubyObject[] args) {
67
+ return Packer.newPacker(ctx, extensionRegistry(), hasSymbolExtType, hasBigIntExtType, args);
68
+ }
69
+
70
+ @JRubyMethod(name = "unpacker", optional = 2)
71
+ public Unpacker unpacker(ThreadContext ctx, IRubyObject[] args) {
72
+ return Unpacker.newUnpacker(ctx, extensionRegistry(), args);
73
+ }
74
+
75
+ @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
76
+ public IRubyObject registeredTypesInternal(ThreadContext ctx) {
77
+ return RubyArray.newArray(ctx.runtime, new IRubyObject[] {
78
+ extensionRegistry.toInternalPackerRegistry(ctx),
79
+ extensionRegistry.toInternalUnpackerRegistry(ctx)
80
+ });
81
+ }
82
+
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");
86
+
87
+ Ruby runtime = ctx.runtime;
88
+ RubyHash options = (RubyHash) opts;
89
+
90
+ IRubyObject packerProc = options.fastARef(runtime.newSymbol("packer"));
91
+ IRubyObject unpackerProc = options.fastARef(runtime.newSymbol("unpacker"));
92
+
93
+ long typeId = ((RubyFixnum) type).getLongValue();
94
+ if (typeId < -128 || typeId > 127) {
95
+ throw runtime.newRangeError(String.format("integer %d too big to convert to `signed char'", typeId));
96
+ }
97
+
98
+ if (!(mod instanceof RubyModule)) {
99
+ throw runtime.newArgumentError(String.format("expected Module/Class but found %s.", mod.getType().getName()));
100
+ }
101
+ RubyModule extModule = (RubyModule) mod;
102
+
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;
108
+ }
109
+ }
110
+
111
+ extensionRegistry.put(extModule, (int) typeId, recursive, packerProc, unpackerProc);
112
+
113
+ if (extModule == runtime.getSymbol() && !packerProc.isNil()) {
114
+ hasSymbolExtType = true;
115
+ }
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
+
128
+ return runtime.getNil();
129
+ }
130
+ }
@@ -0,0 +1,45 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import org.jruby.Ruby;
5
+ import org.jruby.RubyModule;
6
+ import org.jruby.RubyClass;
7
+ import org.jruby.RubyString;
8
+ import org.jruby.RubyNil;
9
+ import org.jruby.RubyBoolean;
10
+ import org.jruby.RubyHash;
11
+ import org.jruby.runtime.load.Library;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+ import org.jruby.runtime.ThreadContext;
14
+ import org.jruby.runtime.Block;
15
+ import org.jruby.runtime.Visibility;
16
+ import org.jruby.anno.JRubyModule;
17
+ import org.jruby.anno.JRubyMethod;
18
+ import org.jruby.internal.runtime.methods.CallConfiguration;
19
+ import org.jruby.internal.runtime.methods.DynamicMethod;
20
+
21
+
22
+ public class MessagePackLibrary implements Library {
23
+ public void load(Ruby runtime, boolean wrap) {
24
+ RubyModule msgpackModule = runtime.defineModule("MessagePack");
25
+ RubyClass standardErrorClass = runtime.getStandardError();
26
+ RubyClass unpackErrorClass = msgpackModule.defineClassUnder("UnpackError", standardErrorClass, standardErrorClass.getAllocator());
27
+ RubyClass underflowErrorClass = msgpackModule.defineClassUnder("UnderflowError", unpackErrorClass, unpackErrorClass.getAllocator());
28
+ RubyClass malformedFormatErrorClass = msgpackModule.defineClassUnder("MalformedFormatError", unpackErrorClass, unpackErrorClass.getAllocator());
29
+ RubyClass stackErrorClass = msgpackModule.defineClassUnder("StackError", unpackErrorClass, unpackErrorClass.getAllocator());
30
+ RubyModule typeErrorModule = msgpackModule.defineModuleUnder("TypeError");
31
+ RubyClass unexpectedTypeErrorClass = msgpackModule.defineClassUnder("UnexpectedTypeError", unpackErrorClass, unpackErrorClass.getAllocator());
32
+ unexpectedTypeErrorClass.includeModule(typeErrorModule);
33
+ RubyClass unknownExtTypeErrorClass = msgpackModule.defineClassUnder("UnknownExtTypeError", unpackErrorClass, unpackErrorClass.getAllocator());
34
+ RubyClass extensionValueClass = msgpackModule.defineClassUnder("ExtensionValue", runtime.getObject(), new ExtensionValue.ExtensionValueAllocator());
35
+ extensionValueClass.defineAnnotatedMethods(ExtensionValue.class);
36
+ RubyClass packerClass = msgpackModule.defineClassUnder("Packer", runtime.getObject(), new Packer.PackerAllocator());
37
+ packerClass.defineAnnotatedMethods(Packer.class);
38
+ RubyClass unpackerClass = msgpackModule.defineClassUnder("Unpacker", runtime.getObject(), new Unpacker.UnpackerAllocator());
39
+ unpackerClass.defineAnnotatedMethods(Unpacker.class);
40
+ RubyClass bufferClass = msgpackModule.defineClassUnder("Buffer", runtime.getObject(), new Buffer.BufferAllocator());
41
+ bufferClass.defineAnnotatedMethods(Buffer.class);
42
+ RubyClass factoryClass = msgpackModule.defineClassUnder("Factory", runtime.getObject(), new Factory.FactoryAllocator());
43
+ factoryClass.defineAnnotatedMethods(Factory.class);
44
+ }
45
+ }
@@ -0,0 +1,266 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import org.jruby.Ruby;
5
+ import org.jruby.RubyModule;
6
+ import org.jruby.RubyClass;
7
+ import org.jruby.RubyObject;
8
+ import org.jruby.RubyArray;
9
+ import org.jruby.RubyHash;
10
+ import org.jruby.RubyIO;
11
+ import org.jruby.RubyNumeric;
12
+ import org.jruby.RubyInteger;
13
+ import org.jruby.RubyFixnum;
14
+ import org.jruby.runtime.Block;
15
+ import org.jruby.runtime.builtin.IRubyObject;
16
+ import org.jruby.anno.JRubyClass;
17
+ import org.jruby.anno.JRubyMethod;
18
+ import org.jruby.runtime.ThreadContext;
19
+ import org.jruby.runtime.ObjectAllocator;
20
+ import org.jruby.util.ByteList;
21
+ import org.jruby.util.TypeConverter;
22
+ import org.msgpack.jruby.ExtensionValue;
23
+
24
+ import org.jcodings.Encoding;
25
+
26
+ import static org.jruby.runtime.Visibility.PRIVATE;
27
+
28
+ @JRubyClass(name="MessagePack::Packer")
29
+ public class Packer extends RubyObject {
30
+ private static final long serialVersionUID = 8451274621499362492L;
31
+ public transient ExtensionRegistry registry;
32
+ private Buffer buffer;
33
+ private transient Encoder encoder;
34
+ private boolean hasSymbolExtType;
35
+ private boolean hasBigintExtType;
36
+ private transient Encoding binaryEncoding;
37
+
38
+ public Packer(Ruby runtime, RubyClass type, ExtensionRegistry registry, boolean hasSymbolExtType, boolean hasBigintExtType) {
39
+ super(runtime, type);
40
+ this.registry = registry;
41
+ this.hasSymbolExtType = hasSymbolExtType;
42
+ this.hasBigintExtType = hasBigintExtType;
43
+ }
44
+
45
+ static class PackerAllocator implements ObjectAllocator {
46
+ public IRubyObject allocate(Ruby runtime, RubyClass type) {
47
+ return new Packer(runtime, type, null, false, false);
48
+ }
49
+ }
50
+
51
+ @JRubyMethod(name = "initialize", optional = 2)
52
+ public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
53
+ boolean compatibilityMode = false;
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
+ }
67
+ }
68
+ if (registry == null) {
69
+ // registry is null when allocate -> initialize
70
+ // registry is already initialized (and somthing might be registered) when newPacker from Factory
71
+ this.registry = new ExtensionRegistry();
72
+ }
73
+ this.encoder = new Encoder(runtime, this, compatibilityMode, registry, hasSymbolExtType, hasBigintExtType);
74
+ this.buffer = new Buffer(runtime, runtime.getModule("MessagePack").getClass("Buffer"));
75
+ this.buffer.initialize(ctx, args);
76
+ this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
77
+ return this;
78
+ }
79
+
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);
82
+ packer.initialize(ctx, args);
83
+ return packer;
84
+ }
85
+
86
+ @JRubyMethod(name = "compatibility_mode?")
87
+ public IRubyObject isCompatibilityMode(ThreadContext ctx) {
88
+ return encoder.isCompatibilityMode() ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
89
+ }
90
+
91
+ @JRubyMethod(name = "registered_types_internal", visibility = PRIVATE)
92
+ public IRubyObject registeredTypesInternal(ThreadContext ctx) {
93
+ return registry.toInternalPackerRegistry(ctx);
94
+ }
95
+
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;
101
+
102
+ long typeId = ((RubyFixnum) type).getLongValue();
103
+ if (typeId < -128 || typeId > 127) {
104
+ throw runtime.newRangeError(String.format("integer %d too big to convert to `signed char'", typeId));
105
+ }
106
+
107
+ if (!(mod instanceof RubyModule)) {
108
+ throw runtime.newArgumentError(String.format("expected Module/Class but found %s.", mod.getType().getName()));
109
+ }
110
+ RubyModule extModule = (RubyModule) mod;
111
+
112
+ registry.put(extModule, (int) typeId, false, proc, null);
113
+
114
+ if (extModule == runtime.getSymbol() && !proc.isNil()) {
115
+ encoder.hasSymbolExtType = true;
116
+ }
117
+
118
+ return runtime.getNil();
119
+ }
120
+
121
+ @JRubyMethod(name = "write", alias = { "pack" })
122
+ public IRubyObject write(ThreadContext ctx, IRubyObject obj) {
123
+ buffer.write(ctx, encoder.encode(obj, this));
124
+ return this;
125
+ }
126
+
127
+ @JRubyMethod(name = "write_float")
128
+ public IRubyObject writeFloat(ThreadContext ctx, IRubyObject obj) {
129
+ checkType(ctx, obj, org.jruby.RubyFloat.class);
130
+ return write(ctx, obj);
131
+ }
132
+
133
+ @JRubyMethod(name = "write_array")
134
+ public IRubyObject writeArray(ThreadContext ctx, IRubyObject obj) {
135
+ checkType(ctx, obj, org.jruby.RubyArray.class);
136
+ return write(ctx, obj);
137
+ }
138
+
139
+ @JRubyMethod(name = "write_string")
140
+ public IRubyObject writeString(ThreadContext ctx, IRubyObject obj) {
141
+ checkType(ctx, obj, org.jruby.RubyString.class);
142
+ return write(ctx, obj);
143
+ }
144
+
145
+ @JRubyMethod(name = "write_bin")
146
+ public IRubyObject writeBin(ThreadContext ctx, IRubyObject obj) {
147
+ checkType(ctx, obj, org.jruby.RubyString.class);
148
+ obj = ((org.jruby.RubyString) obj).encode(ctx, ctx.runtime.getEncodingService().getEncoding(binaryEncoding));
149
+ return write(ctx, obj);
150
+ }
151
+
152
+ @JRubyMethod(name = "write_hash")
153
+ public IRubyObject writeHash(ThreadContext ctx, IRubyObject obj) {
154
+ checkType(ctx, obj, org.jruby.RubyHash.class);
155
+ return write(ctx, obj);
156
+ }
157
+
158
+ @JRubyMethod(name = "write_symbol")
159
+ public IRubyObject writeSymbol(ThreadContext ctx, IRubyObject obj) {
160
+ checkType(ctx, obj, org.jruby.RubySymbol.class);
161
+ return write(ctx, obj);
162
+ }
163
+
164
+ @JRubyMethod(name = "write_int")
165
+ public IRubyObject writeInt(ThreadContext ctx, IRubyObject obj) {
166
+ if (!(obj instanceof RubyFixnum)) {
167
+ checkType(ctx, obj, org.jruby.RubyBignum.class);
168
+ }
169
+ return write(ctx, obj);
170
+ }
171
+
172
+ @JRubyMethod(name = "write_extension")
173
+ public IRubyObject writeExtension(ThreadContext ctx, IRubyObject obj) {
174
+ if (!(obj instanceof ExtensionValue)) {
175
+ throw ctx.runtime.newTypeError("Expected extension");
176
+ }
177
+ return write(ctx, obj);
178
+ }
179
+
180
+ @JRubyMethod(name = "write_true")
181
+ public IRubyObject writeTrue(ThreadContext ctx) {
182
+ return write(ctx, ctx.runtime.getTrue());
183
+ }
184
+
185
+ @JRubyMethod(name = "write_false")
186
+ public IRubyObject writeFalse(ThreadContext ctx) {
187
+ return write(ctx, ctx.runtime.getFalse());
188
+ }
189
+
190
+ @JRubyMethod(name = "write_nil")
191
+ public IRubyObject writeNil(ThreadContext ctx) {
192
+ write(ctx, null);
193
+ return this;
194
+ }
195
+
196
+ @JRubyMethod(name = "write_float32")
197
+ public IRubyObject writeFloat32(ThreadContext ctx, IRubyObject numeric) {
198
+ Ruby runtime = ctx.runtime;
199
+ if (!(numeric instanceof RubyNumeric)) {
200
+ throw runtime.newArgumentError("Expected numeric");
201
+ }
202
+ buffer.write(ctx, encoder.encodeFloat32((RubyNumeric) numeric));
203
+ return this;
204
+ }
205
+
206
+ @JRubyMethod(name = "write_array_header")
207
+ public IRubyObject writeArrayHeader(ThreadContext ctx, IRubyObject size) {
208
+ int s = (int) size.convertToInteger().getLongValue();
209
+ buffer.write(ctx, encoder.encodeArrayHeader(s));
210
+ return this;
211
+ }
212
+
213
+ @JRubyMethod(name = "write_map_header")
214
+ public IRubyObject writeMapHeader(ThreadContext ctx, IRubyObject size) {
215
+ int s = (int) size.convertToInteger().getLongValue();
216
+ buffer.write(ctx, encoder.encodeMapHeader(s));
217
+ return this;
218
+ }
219
+
220
+ @JRubyMethod(name = "write_bin_header")
221
+ public IRubyObject writeBinHeader(ThreadContext ctx, IRubyObject size) {
222
+ int s = (int) size.convertToInteger().getLongValue();
223
+ buffer.write(ctx, encoder.encodeBinHeader(s));
224
+ return this;
225
+ }
226
+
227
+ @JRubyMethod(name = "full_pack")
228
+ public IRubyObject fullPack(ThreadContext ctx) {
229
+ if (buffer.hasIo()) {
230
+ return null;
231
+ }
232
+ return toS(ctx);
233
+ }
234
+
235
+ @JRubyMethod(name = "to_s", alias = { "to_str" })
236
+ public IRubyObject toS(ThreadContext ctx) {
237
+ return buffer.toS(ctx);
238
+ }
239
+
240
+ @JRubyMethod(name = "buffer")
241
+ public IRubyObject buffer(ThreadContext ctx) {
242
+ return buffer;
243
+ }
244
+
245
+ @JRubyMethod(name = "flush")
246
+ public IRubyObject flush(ThreadContext ctx) {
247
+ return buffer.flush(ctx);
248
+ }
249
+
250
+ @JRubyMethod(name = "size")
251
+ public IRubyObject size(ThreadContext ctx) {
252
+ return buffer.size(ctx);
253
+ }
254
+
255
+ @JRubyMethod(name = "clear", alias = { "reset" })
256
+ public IRubyObject clear(ThreadContext ctx) {
257
+ return buffer.clear(ctx);
258
+ }
259
+
260
+ private void checkType(ThreadContext ctx, IRubyObject obj, Class<? extends IRubyObject> expectedType) {
261
+ if (!expectedType.isInstance(obj)) {
262
+ String expectedName = expectedType.getName().substring("org.jruby.Ruby".length());
263
+ throw ctx.runtime.newTypeError(String.format("wrong argument type %s (expected %s)", obj.getMetaClass().toString(), expectedName));
264
+ }
265
+ }
266
+ }
@@ -0,0 +1,37 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ public interface Types {
5
+ public static final byte FIXSTR = (byte) 0xa0; // This is actually not header byte, but prefix bit mask
6
+ public static final byte NIL = (byte) 0xc0;
7
+ public static final byte FALSE = (byte) 0xc2;
8
+ public static final byte TRUE = (byte) 0xc3;
9
+ public static final byte BIN8 = (byte) 0xc4;
10
+ public static final byte BIN16 = (byte) 0xc5;
11
+ public static final byte BIN32 = (byte) 0xc6;
12
+ public static final byte VAREXT8 = (byte) 0xc7;
13
+ public static final byte VAREXT16 = (byte) 0xc8;
14
+ public static final byte VAREXT32 = (byte) 0xc9;
15
+ public static final byte FLOAT32 = (byte) 0xca;
16
+ public static final byte FLOAT64 = (byte) 0xcb;
17
+ public static final byte UINT8 = (byte) 0xcc;
18
+ public static final byte UINT16 = (byte) 0xcd;
19
+ public static final byte UINT32 = (byte) 0xce;
20
+ public static final byte UINT64 = (byte) 0xcf;
21
+ public static final byte INT8 = (byte) 0xd0;
22
+ public static final byte INT16 = (byte) 0xd1;
23
+ public static final byte INT32 = (byte) 0xd2;
24
+ public static final byte INT64 = (byte) 0xd3;
25
+ public static final byte FIXEXT1 = (byte) 0xd4;
26
+ public static final byte FIXEXT2 = (byte) 0xd5;
27
+ public static final byte FIXEXT4 = (byte) 0xd6;
28
+ public static final byte FIXEXT8 = (byte) 0xd7;
29
+ public static final byte FIXEXT16 = (byte) 0xd8;
30
+ public static final byte STR8 = (byte) 0xd9;
31
+ public static final byte STR16 = (byte) 0xda;
32
+ public static final byte STR32 = (byte) 0xdb;
33
+ public static final byte ARY16 = (byte) 0xdc;
34
+ public static final byte ARY32 = (byte) 0xdd;
35
+ public static final byte MAP16 = (byte) 0xde;
36
+ public static final byte MAP32 = (byte) 0xdf;
37
+ }