-
-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathRubyMarshal.java
More file actions
222 lines (189 loc) · 9.19 KB
/
RubyMarshal.java
File metadata and controls
222 lines (189 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2007 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2003 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.runtime.*;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.MarshalDumper;
import org.jruby.runtime.marshal.MarshalLoader;
import org.jruby.util.ByteList;
import org.jruby.util.IOInputStream;
import org.jruby.util.IOOutputStream;
import org.jruby.util.io.RubyInputStream;
import org.jruby.util.io.RubyOutputStream;
import org.jruby.util.io.TransparentByteArrayOutputStream;
import static org.jruby.api.Convert.asFixnum;
import static org.jruby.api.Convert.toInt;
import static org.jruby.api.Create.newString;
import static org.jruby.api.Define.defineModule;
import static org.jruby.api.Error.argumentError;
import static org.jruby.api.Error.typeError;
/**
* Marshal module
*
* @author Anders
*/
@JRubyModule(name="Marshal")
public class RubyMarshal {
public static RubyModule createMarshalModule(ThreadContext context) {
return defineModule(context, "Marshal").
defineMethods(context, RubyMarshal.class).
defineConstant(context, "MAJOR_VERSION", asFixnum(context, Constants.MARSHAL_MAJOR)).
defineConstant(context, "MINOR_VERSION", asFixnum(context, Constants.MARSHAL_MINOR));
}
@JRubyMethod(module = true, visibility = Visibility.PRIVATE)
public static IRubyObject dump(ThreadContext context, IRubyObject recv, IRubyObject object) {
return dumpCommon(context, object, null, -1);
}
@JRubyMethod(module = true, visibility = Visibility.PRIVATE)
public static IRubyObject dump(ThreadContext context, IRubyObject recv, IRubyObject object, IRubyObject ioOrLimit) {
IRubyObject io = null;
int depthLimit = -1;
if (ioOrLimit instanceof RubyIO || sites(context).respond_to_write.respondsTo(context, ioOrLimit, ioOrLimit)) {
io = ioOrLimit;
} else if (ioOrLimit instanceof RubyFixnum fixnum) {
depthLimit = fixnum.asInt(context);
} else {
throw typeError(context, "Instance of IO needed");
}
return dumpCommon(context, object, io, depthLimit);
}
@JRubyMethod(module = true, visibility = Visibility.PRIVATE)
public static IRubyObject dump(ThreadContext context, IRubyObject recv, IRubyObject object, IRubyObject io, IRubyObject limit) {
if (!(io instanceof RubyIO || sites(context).respond_to_write.respondsTo(context, io, io))) {
throw typeError(context, "Instance of IO needed");
}
int depthLimit = toInt(context, limit);
return dumpCommon(context, object, io, depthLimit);
}
private static IRubyObject dumpCommon(ThreadContext context, IRubyObject objectToDump, IRubyObject io, int depthLimit) {
OutputStream outputStream;
TransparentByteArrayOutputStream stringOutput = null;
if (io != null) {
if (io instanceof RubyIO rubyIO) {
outputStream = rubyIO.getOutStream();
} else {
outputStream = outputStream(context, io);
}
} else {
outputStream = stringOutput = new TransparentByteArrayOutputStream();
}
dumpToStream(context, objectToDump, outputStream, depthLimit);
return io != null ? io :
newString(context, new ByteList(stringOutput.getRawBytes(), 0, stringOutput.size(), false));
}
@JRubyMethod(name = {"load", "restore"}, required = 1, optional = 2, checkArity = false, module = true, visibility = Visibility.PRIVATE)
public static IRubyObject load(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
int argc = Arity.checkArgumentCount(context, args, 1, 3);
IRubyObject in = args[0];
boolean freeze = false;
IRubyObject proc = null;
if (argc > 1) {
RubyHash kwargs = ArgsUtil.extractKeywords(args[argc - 1]);
if (kwargs != null) {
IRubyObject freezeOpt = ArgsUtil.getFreezeOpt(context, kwargs);
freeze = freezeOpt != null && freezeOpt.isTrue();
if (argc > 2) proc = args[1];
} else {
proc = args[1];
}
}
final IRubyObject str = in.checkStringType();
InputStream rawInput;
if (str instanceof RubyString string) {
if (string.size() == 0) {
throw argumentError(context, "marshal data too short");
}
ByteList bytes = string.getByteList();
rawInput = new ByteArrayInputStream(bytes.getUnsafeBytes(), bytes.begin(), bytes.length());
} else if (sites(context).respond_to_getc.respondsTo(context, in, in) &&
sites(context).respond_to_read.respondsTo(context, in, in)) {
rawInput = inputStream(context, in);
} else {
throw typeError(context, "instance of IO needed");
}
MarshalLoader loader = new MarshalLoader(context, freeze, proc);
RubyInputStream rubyIn = new RubyInputStream(context.runtime, rawInput);
loader.start(context, rubyIn);
return loader.unmarshalObject(context, rubyIn);
}
private static InputStream inputStream(ThreadContext context, IRubyObject in) {
setBinmodeIfPossible(context, in);
return new IOInputStream(in, false); // respond_to?(:read) already checked
}
private static OutputStream outputStream(ThreadContext context, IRubyObject out) {
setBinmodeIfPossible(context, out);
return new IOOutputStream(out, true, false); // respond_to?(:write) already checked
}
private static void dumpToStream(ThreadContext context, IRubyObject object, OutputStream rawOutput, int depthLimit) {
MarshalDumper output = new MarshalDumper(depthLimit);
RubyOutputStream out = new RubyOutputStream(context.runtime, rawOutput);
output.start(out);
output.dumpObject(context, out, object);
}
private static void setBinmodeIfPossible(ThreadContext context, IRubyObject io) {
if (sites(context).respond_to_binmode.respondsTo(context, io, io)) {
sites(context).binmode.call(context, io, io);
}
}
/**
* Convenience method for objects that are undumpable. Always throws (a TypeError).
*/
public static IRubyObject undumpable(ThreadContext context, RubyObject self) {
throw typeError(context, "can't dump ", self, "");
}
@Deprecated(since = "10.0.0.0")
public static IRubyObject dump(IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
return dump(((RubyBasicObject) recv).getCurrentContext(), recv, args, unusedBlock);
}
@Deprecated(since = "10.0.0.0")
public static IRubyObject dump(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
int argc = Arity.checkArgumentCount(context, args, 1, 3);
IRubyObject objectToDump = args[0];
int depthLimit = -1;
return switch (argc) {
case 1 -> dump(context, recv, args[0]);
case 2 -> dump(context, recv, args[0], args[1]);
case 3 -> dump(context, recv, args[0], args[1], args[2]);
default -> dumpCommon(context, objectToDump, null, depthLimit);
};
}
private static JavaSites.MarshalSites sites(ThreadContext context) {
return context.sites.Marshal;
}
}