Skip to content

Commit 7f365ef

Browse files
authored
Merge pull request jruby#8543 from enebo/allocate
allocate() needs ThreadContext
2 parents 0113a90 + 9d5e3b2 commit 7f365ef

30 files changed

+105
-88
lines changed

core/src/main/java/org/jruby/MetaClass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ private static void logSingleton(Ruby runtime, RubyClass superClass, RubyBasicOb
8989
}
9090

9191
@Override
92-
public final IRubyObject allocate() {
93-
throw typeError(getRuntime().getCurrentContext(), "can't create instance of virtual class");
92+
public final IRubyObject allocate(ThreadContext context) {
93+
throw typeError(context, "can't create instance of virtual class");
9494
}
9595

9696
@Override

core/src/main/java/org/jruby/Ruby.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4133,7 +4133,7 @@ public RaiseException newNameError(String message, IRubyObject recv, IRubyObject
41334133
*/
41344134
public RaiseException newNameError(String message, IRubyObject recv, IRubyObject name, boolean privateCall) {
41354135
IRubyObject msg = new RubyNameError.RubyNameErrorMessage(this, message, recv, name);
4136-
RubyException err = RubyNameError.newNameError(getNameError(), msg, name, privateCall);
4136+
RubyException err = RubyNameError.newNameError(getCurrentContext(), getNameError(), msg, name, privateCall);
41374137

41384138
return err.toThrowable();
41394139
}

core/src/main/java/org/jruby/RubyArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ public static IRubyObject create(IRubyObject klass, IRubyObject[] args, Block bl
160160
@JRubyMethod(name = "[]", rest = true, meta = true)
161161
public static IRubyObject create(ThreadContext context, IRubyObject klass, IRubyObject[] args, Block block) {
162162
switch (args.length) {
163-
case 0: return ((RubyClass) klass).allocate();
163+
case 0: return ((RubyClass) klass).allocate(context);
164164
case 1: return new RubyArrayOneObject((RubyClass) klass, args[0]);
165165
case 2: return new RubyArrayTwoObject((RubyClass) klass, args[0], args[1]);
166166
}
167167

168-
RubyArray arr = (RubyArray) ((RubyClass) klass).allocate();
168+
RubyArray arr = (RubyArray) ((RubyClass) klass).allocate(context);
169169
arr.values = args.clone();
170170
arr.realLength = args.length;
171171
return arr;

core/src/main/java/org/jruby/RubyBasicObject.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,10 @@ private Object unwrap_java_object() {
842842

843843
@Override
844844
public IRubyObject dup() {
845-
if (isSpecialObject()) {
846-
return this;
847-
}
845+
if (isSpecialObject()) return this;
848846

849-
IRubyObject dup = metaClass.getRealClass().allocate();
850847
ThreadContext context = getRuntime().getCurrentContext();
848+
IRubyObject dup = metaClass.getRealClass().allocate(context);
851849

852850
initCopy(context, dup, this);
853851
sites(context).initialize_dup.call(context, dup, dup, this);
@@ -971,7 +969,7 @@ private RubyBasicObject rbCloneInternal(ThreadContext context, IRubyObject freez
971969
}
972970

973971
// We're cloning ourselves, so we know the result should be a RubyObject
974-
RubyBasicObject clone = (RubyBasicObject) metaClass.getRealClass().allocate();
972+
RubyBasicObject clone = (RubyBasicObject) metaClass.getRealClass().allocate(context);
975973

976974
return cloneSetup(context, clone, freeze);
977975
}

core/src/main/java/org/jruby/RubyClass.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,18 @@ private static RaiseException newTypeError(final Ruby runtime, final String msg,
285285
return withException(createTypeError(runtime.getCurrentContext(), msg.toString()), e);
286286
}
287287

288-
@JRubyMethod(name = "allocate")
288+
@Deprecated(since = "10.0")
289289
public IRubyObject allocate() {
290-
if (superClass == null) {
291-
if (this != runtime.getBasicObject()) {
292-
throw typeError(runtime.getCurrentContext(), "can't instantiate uninitialized class");
293-
}
294-
}
295-
IRubyObject obj = allocator.allocate(runtime, this);
296-
if (getMetaClass(obj).getRealClass() != getRealClass()) {
297-
throw typeError(runtime.getCurrentContext(), "wrong instance allocation");
290+
return allocate(getCurrentContext());
291+
}
292+
293+
@JRubyMethod(name = "allocate")
294+
public IRubyObject allocate(ThreadContext context) {
295+
if (superClass == null && this != basicObjectClass(context)) {
296+
throw typeError(context, "can't instantiate uninitialized class");
298297
}
298+
IRubyObject obj = allocator.allocate(context.runtime, this);
299+
if (getMetaClass(obj).getRealClass() != getRealClass()) throw typeError(context, "wrong instance allocation");
299300
return obj;
300301
}
301302

@@ -1006,41 +1007,41 @@ public IRubyObject invokeInherited(ThreadContext context, IRubyObject self, IRub
10061007
*/
10071008
@JRubyMethod(name = "new", keywords = true)
10081009
public IRubyObject newInstance(ThreadContext context, Block block) {
1009-
IRubyObject obj = allocate();
1010+
IRubyObject obj = allocate(context);
10101011
baseCallSites[CS_IDX_INITIALIZE].call(context, obj, obj, block);
10111012
return obj;
10121013
}
10131014

10141015
@JRubyMethod(name = "new", keywords = true)
10151016
public IRubyObject newInstance(ThreadContext context, IRubyObject arg0, Block block) {
1016-
IRubyObject obj = allocate();
1017+
IRubyObject obj = allocate(context);
10171018
baseCallSites[CS_IDX_INITIALIZE].call(context, obj, obj, arg0, block);
10181019
return obj;
10191020
}
10201021

10211022
public IRubyObject newInstance(ThreadContext context, IRubyObject arg0) {
1022-
IRubyObject obj = allocate();
1023+
IRubyObject obj = allocate(context);
10231024
baseCallSites[CS_IDX_INITIALIZE].call(context, obj, obj, arg0);
10241025
return obj;
10251026
}
10261027

10271028
@JRubyMethod(name = "new", keywords = true)
10281029
public IRubyObject newInstance(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Block block) {
1029-
IRubyObject obj = allocate();
1030+
IRubyObject obj = allocate(context);
10301031
baseCallSites[CS_IDX_INITIALIZE].call(context, obj, obj, arg0, arg1, block);
10311032
return obj;
10321033
}
10331034

10341035
@JRubyMethod(name = "new", keywords = true)
10351036
public IRubyObject newInstance(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
1036-
IRubyObject obj = allocate();
1037+
IRubyObject obj = allocate(context);
10371038
baseCallSites[CS_IDX_INITIALIZE].call(context, obj, obj, arg0, arg1, arg2, block);
10381039
return obj;
10391040
}
10401041

10411042
@JRubyMethod(name = "new", rest = true, keywords = true)
10421043
public IRubyObject newInstance(ThreadContext context, IRubyObject[] args, Block block) {
1043-
IRubyObject obj = allocate();
1044+
IRubyObject obj = allocate(context);
10441045
baseCallSites[CS_IDX_INITIALIZE].call(context, obj, obj, args, block);
10451046
return obj;
10461047
}
@@ -1500,7 +1501,7 @@ public void marshalTo(Object obj, RubyClass type, NewMarshal marshalStream, Thre
15001501

15011502
@Override
15021503
public Object unmarshalFrom(Ruby runtime, RubyClass type, UnmarshalStream input) throws IOException {
1503-
IRubyObject result = input.entry(type.allocate());
1504+
IRubyObject result = input.entry(type.allocate(runtime.getCurrentContext()));
15041505

15051506
input.ivar(null, result, null);
15061507

core/src/main/java/org/jruby/RubyEnumerable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ public static IRubyObject chunk(ThreadContext context, IRubyObject self, final B
20412041
return enumeratorizeWithSize(context, self, "chunk", RubyEnumerable::size);
20422042
}
20432043

2044-
IRubyObject enumerator = runtime.getEnumerator().allocate();
2044+
IRubyObject enumerator = runtime.getEnumerator().allocate(context);
20452045
enumerator.getInternalVariables().setInternalVariable("chunk_enumerable", self);
20462046
enumerator.getInternalVariables().setInternalVariable("chunk_categorize",
20472047
RubyProc.newProc(runtime, block, block.type == Block.Type.LAMBDA ? block.type : Block.Type.PROC));

core/src/main/java/org/jruby/RubyEnumerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public static IRubyObject __from(ThreadContext context, IRubyObject klass, IRuby
226226
methodArgs = NULL_ARRAY;
227227
}
228228

229-
RubyEnumerator instance = (RubyEnumerator) ((RubyClass) klass).allocate();
229+
RubyEnumerator instance = (RubyEnumerator) ((RubyClass) klass).allocate(context);
230230

231231
if (size == null) {
232232
sizeFn = RubyEnumerable::size;

core/src/main/java/org/jruby/RubyException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,13 @@ public void marshalTo(RubyException exc, RubyClass type,
194194

195195
@Override
196196
public RubyException unmarshalFrom(Ruby runtime, RubyClass type, UnmarshalStream input) throws IOException {
197-
RubyException exc = (RubyException) input.entry(type.allocate());
197+
var context = runtime.getCurrentContext();
198+
RubyException exc = (RubyException) input.entry(type.allocate(context));
198199

199200
input.ivar(null, exc, null);
200201

201202
exc.setMessage((IRubyObject) exc.removeInternalVariable("mesg"));
202-
exc.set_backtrace(runtime.getCurrentContext(), (IRubyObject) exc.removeInternalVariable("bt"));
203+
exc.set_backtrace(context, (IRubyObject) exc.removeInternalVariable("bt"));
203204

204205
return exc;
205206
}

core/src/main/java/org/jruby/RubyHash.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static IRubyObject create(ThreadContext context, IRubyObject recv, IRubyO
164164
final IRubyObject nil = context.nil;
165165
tmp = TypeConverter.convertToTypeWithCheck(args[0], Array, "to_ary");
166166
if (tmp != nil) {
167-
RubyHash hash = (RubyHash) ((RubyClass) recv).allocate();
167+
RubyHash hash = (RubyHash) ((RubyClass) recv).allocate(context);
168168
var arr = (RubyArray<?>) tmp;
169169
for (int i = 0, j = arr.getLength(); i<j; i++) {
170170
IRubyObject e = arr.entry(i);
@@ -190,7 +190,7 @@ public static IRubyObject create(ThreadContext context, IRubyObject recv, IRubyO
190190

191191
if ((args.length & 1) != 0) throw argumentError(context, "odd number of arguments for Hash");
192192

193-
RubyHash hash = (RubyHash) ((RubyClass) recv).allocate();
193+
RubyHash hash = (RubyHash) ((RubyClass) recv).allocate(context);
194194
for (int i=0; i < args.length; i+=2) hash.fastASetCheckString(context.runtime, args[i], args[i+1]);
195195

196196
return hash;

core/src/main/java/org/jruby/RubyIO.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public RubyIO(Ruby runtime, ShellLauncher.POpenProcess process, IOOptions ioOpti
214214
// MRI: prep_stdio
215215
public static RubyIO prepStdio(Ruby runtime, InputStream f, Channel c, int fmode, RubyClass klass, String path) {
216216
OpenFile fptr;
217-
RubyIO io = prepIO(runtime, c, fmode | OpenFile.PREP | EncodingUtils.DEFAULT_TEXTMODE, klass, path);
217+
RubyIO io = prepIO(runtime.getCurrentContext(), c, fmode | OpenFile.PREP | EncodingUtils.DEFAULT_TEXTMODE, klass, path);
218218

219219
fptr = io.getOpenFileChecked();
220220

@@ -236,7 +236,7 @@ public static RubyIO prepStdio(Ruby runtime, InputStream f, Channel c, int fmode
236236
// MRI: prep_stdio
237237
public static RubyIO prepStdio(Ruby runtime, OutputStream f, Channel c, int fmode, RubyClass klass, String path) {
238238
OpenFile fptr;
239-
RubyIO io = prepIO(runtime, c, fmode | OpenFile.PREP | EncodingUtils.DEFAULT_TEXTMODE, klass, path);
239+
RubyIO io = prepIO(runtime.getCurrentContext(), c, fmode | OpenFile.PREP | EncodingUtils.DEFAULT_TEXTMODE, klass, path);
240240

241241
fptr = io.getOpenFileChecked();
242242

@@ -280,9 +280,9 @@ private static void prepStdioEcflags(OpenFile fptr, int fmode) {
280280
}
281281

282282
// MRI: prep_io
283-
private static RubyIO prepIO(Ruby runtime, Channel fd, int fmode, RubyClass klass, String path) {
283+
private static RubyIO prepIO(ThreadContext context, Channel fd, int fmode, RubyClass klass, String path) {
284284
OpenFile fp;
285-
RubyIO io = (RubyIO)klass.allocate();
285+
RubyIO io = (RubyIO)klass.allocate(context);
286286

287287
fp = io.MakeOpenFile();
288288
fp.setChannel(fd);
@@ -4222,10 +4222,9 @@ static RubyIO ioOpen(ThreadContext context, IRubyObject recv, RubyString filenam
42224222

42234223
// MRI: rb_io_open_generic
42244224
private static RubyIO ioOpenGeneric(ThreadContext context, IRubyObject recv, IRubyObject filename, int oflags, int fmode, IOEncodable convconfig, int perm) {
4225-
IRubyObject cmd;
4226-
42274225
if ((filename instanceof RubyString name) && name.isEmpty()) throw context.runtime.newErrnoENOENTError();
42284226

4227+
IRubyObject cmd;
42294228
if ((recv == ioClass(context)) && (cmd = PopenExecutor.checkPipeCommand(context, filename)) != context.nil) {
42304229
warningDeprecated(context, "IO process creation with a leading '|' is deprecated and will be removed in Ruby 4.0; use IO.popen instead");
42314230
if (PopenExecutor.nativePopenAvailable(context.runtime)) {
@@ -4234,7 +4233,8 @@ private static RubyIO ioOpenGeneric(ThreadContext context, IRubyObject recv, IRu
42344233
throw argumentError(context, "pipe open is not supported without native subprocess logic");
42354234
}
42364235
}
4237-
return (RubyIO) ((RubyFile) fileClass(context).allocate()).fileOpenGeneric(context, filename, oflags, fmode, convconfig, perm);
4236+
return (RubyIO) ((RubyFile) fileClass(context).allocate(context)).
4237+
fileOpenGeneric(context, filename, oflags, fmode, convconfig, perm);
42384238
}
42394239

42404240
/**

0 commit comments

Comments
 (0)