Skip to content

Commit 9444211

Browse files
committed
Some deprecations and consolidation of newClass
1 parent 4324ccd commit 9444211

File tree

8 files changed

+76
-47
lines changed

8 files changed

+76
-47
lines changed

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ private Ruby(RubyInstanceConfig config) {
362362

363363
// nil, true, and false all are set in TC so they need to be created above (both class and instances).
364364
// their methods are added afterward since no dispatch happens until after first TC is defined.
365-
nilClass = defineClassBootstrap("NilClass");
366-
falseClass = defineClassBootstrap("FalseClass");
367-
trueClass = defineClassBootstrap("TrueClass");
365+
nilClass = RubyClass.newClassBootstrap(this, objectClass, "NilClass");
366+
falseClass = RubyClass.newClassBootstrap(this, objectClass, "FalseClass");
367+
trueClass = RubyClass.newClassBootstrap(this, objectClass, "TrueClass");
368368

369369
nilObject = new RubyNil(this, nilClass);
370370
nilPrefilledArray = new IRubyObject[NIL_PREFILLED_ARRAY_SIZE];
@@ -1458,17 +1458,15 @@ public RubyClass defineClassUnder(ThreadContext context, String id, RubyClass su
14581458

14591459
if (superClass == null) superClass = determineSuperClass(context, id, parent, parentIsObject);
14601460

1461-
return RubyClass.newClass(this, superClass, id, allocator, parent, !parentIsObject, callSites);
1461+
return RubyClass.newClass(context, superClass, id, allocator, parent, !parentIsObject, callSites);
14621462
}
14631463

14641464
private RubyClass determineSuperClass(ThreadContext context, String id, RubyModule parent, boolean parentIsObject) {
1465-
RubyClass superClass;
14661465
IRubyObject className = parentIsObject ? ids(this, id) :
14671466
parent.toRubyString(context).append(newString("::")).append(ids(this, id));
14681467
warn(context, str(this, "no super class for '", className, "', Object assumed"));
14691468

1470-
superClass = objectClass;
1471-
return superClass;
1469+
return objectClass;
14721470
}
14731471

14741472
private RubyClass foundExistingClass(ThreadContext context, String id, RubyClass superClass, ObjectAllocator allocator, IRubyObject obj) {
@@ -1496,16 +1494,6 @@ public RubyModule defineModule(String name) {
14961494
return defineModuleUnder(getCurrentContext(), name, objectClass);
14971495
}
14981496

1499-
/**
1500-
* This is only for defining nil,true,false. The reason we have this is so all other define methods
1501-
* can count on ThreadContext being available. These are defined before that point.
1502-
* @param name The name for the new class
1503-
* @return The new class
1504-
*/
1505-
public RubyClass defineClassBootstrap(String name) {
1506-
return RubyClass.newClass(this, objectClass, name, NOT_ALLOCATABLE_ALLOCATOR, objectClass, false, null);
1507-
}
1508-
15091497
/**
15101498
* This is only for defining kernel. The reason we have this is so all other define methods
15111499
* can count on ThreadContext being available. These are defined before that point.

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

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.jruby.api.Convert.asSymbol;
3939
import static org.jruby.api.Error.*;
4040
import static org.jruby.api.Warn.warn;
41+
import static org.jruby.runtime.ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR;
4142
import static org.jruby.runtime.Visibility.PRIVATE;
4243
import static org.jruby.runtime.Visibility.PUBLIC;
4344
import static org.jruby.util.CodegenUtils.ci;
@@ -135,7 +136,7 @@ public static void finishCreateClassClass(ThreadContext context, RubyClass Class
135136

136137
public static final ObjectAllocator CLASS_ALLOCATOR = (runtime, klass) -> {
137138
RubyClass clazz = new RubyClass(runtime);
138-
clazz.allocator = ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR; // Class.allocate object is not allocatable before it is initialized
139+
clazz.allocator = NOT_ALLOCATABLE_ALLOCATOR; // Class.allocate object is not allocatable before it is initialized
139140
return clazz;
140141
};
141142

@@ -491,20 +492,26 @@ protected RubyClass(Ruby runtime, RubyClass superClazz, CallSite[] extraCallSite
491492
* and with Object as its immediate superclass.
492493
* Corresponds to rb_class_new in MRI.
493494
*/
495+
@Deprecated(since = "10.0")
494496
public static RubyClass newClass(Ruby runtime, RubyClass superClass) {
495497
if (superClass == runtime.getClassClass()) throw typeError(runtime.getCurrentContext(), "can't make subclass of Class");
496498
if (superClass.isSingleton()) throw typeError(runtime.getCurrentContext(), "can't make subclass of virtual class");
497499
return new RubyClass(runtime, superClass);
498500
}
499501

502+
@Deprecated(since = "10.0")
503+
public static RubyClass newClass(Ruby runtime, RubyClass superClass, CallSite[] extraCallSites) {
504+
return newClass(runtime.getCurrentContext(), superClass, extraCallSites);
505+
}
506+
500507
/**
501508
* A variation on newClass that allow passing in an array of supplementary
502509
* call sites to improve dynamic invocation.
503510
*/
504-
public static RubyClass newClass(Ruby runtime, RubyClass superClass, CallSite[] extraCallSites) {
505-
if (superClass == runtime.getClassClass()) throw typeError(runtime.getCurrentContext(), "can't make subclass of Class");
506-
if (superClass.isSingleton()) throw typeError(runtime.getCurrentContext(), "can't make subclass of virtual class");
507-
return new RubyClass(runtime, superClass, extraCallSites);
511+
public static RubyClass newClass(ThreadContext context, RubyClass superClass, CallSite[] extraCallSites) {
512+
if (superClass == classClass(context)) throw typeError(context, "can't make subclass of Class");
513+
if (superClass.isSingleton()) throw typeError(context, "can't make subclass of virtual class");
514+
return new RubyClass(context.runtime, superClass, extraCallSites);
508515
}
509516

510517
/**
@@ -515,42 +522,76 @@ public static RubyClass newClass(Ruby runtime, RubyClass superClass, CallSite[]
515522
* Corresponds to rb_class_new/rb_define_class_id/rb_name_class/rb_set_class_path
516523
* in MRI.
517524
*/
525+
@Deprecated(since = "10.0")
518526
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent) {
519-
RubyClass clazz = newClass(runtime, superClass).
527+
var context = runtime.getCurrentContext();
528+
RubyClass clazz = newClass(context, superClass, null).
520529
allocator(allocator).
521530
baseName(name);
522531

523532
clazz.makeMetaClass(superClass.getMetaClass());
524533
if (setParent) clazz.setParent(parent);
525534
parent.setConstant(name, clazz);
526-
clazz.inherit(superClass);
535+
superClass.invokeInherited(context, superClass, clazz);
527536
return clazz;
528537
}
529538

539+
@Deprecated(since = "10.0")
530540
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator,
531541
RubyModule parent, boolean setParent, String file, int line) {
532-
RubyClass clazz = newClass(runtime, superClass).
542+
return newClass(runtime.getCurrentContext(), superClass, name, allocator, parent, setParent, file, line);
543+
}
544+
545+
public static RubyClass newClass(ThreadContext context, RubyClass superClass, String name, ObjectAllocator allocator,
546+
RubyModule parent, boolean setParent, String file, int line) {
547+
assert superClass != null;
548+
RubyClass clazz = newClass(context, superClass, null).
533549
allocator(allocator).
534550
baseName(name);
535551
clazz.makeMetaClass(superClass.getMetaClass());
536552
if (setParent) clazz.setParent(parent);
537553
parent.setConstant(name, clazz, file, line);
538-
clazz.inherit(superClass);
554+
superClass.invokeInherited(context, superClass, clazz);
539555
return clazz;
540556
}
541557

558+
@Deprecated(since = "10.0")
559+
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent, CallSite[] extraCallSites) {
560+
return newClass(runtime.getCurrentContext(), superClass, name, allocator, parent, setParent, extraCallSites);
561+
}
562+
542563
/**
543564
* A variation on newClass that allows passing in an array of supplementary
544565
* call sites to improve dynamic invocation performance.
545566
*/
546-
public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name, ObjectAllocator allocator, RubyModule parent, boolean setParent, CallSite[] extraCallSites) {
547-
RubyClass clazz = newClass(runtime, superClass, extraCallSites).
567+
public static RubyClass newClass(ThreadContext context, RubyClass superClass, String name,
568+
ObjectAllocator allocator, RubyModule parent, boolean setParent,
569+
CallSite[] extraCallSites) {
570+
RubyClass clazz = newClass(context, superClass, extraCallSites).
548571
allocator(allocator).
549572
baseName(name);
550573
clazz.makeMetaClass(superClass.getMetaClass());
551574
if (setParent) clazz.setParent(parent);
552575
parent.setConstant(name, clazz, BUILTIN_CONSTANT, -1);
553-
clazz.inherit(superClass);
576+
superClass.invokeInherited(context, superClass, clazz);
577+
return clazz;
578+
}
579+
580+
/**
581+
* This is an internal API for bootstrapping a few classes before ThreadContext is available. The API
582+
* is intentionally limited/obtuse so no one is tempted to try and use it.
583+
*
584+
* @param runtime the runtime
585+
* @param Object reference to Object which is superclass and parent for new type
586+
* @param name the name of the new class
587+
* @return the new class.
588+
*/
589+
public static RubyClass newClassBootstrap(Ruby runtime, RubyClass Object, String name) {
590+
RubyClass clazz = new RubyClass(runtime, Object).
591+
allocator(NOT_ALLOCATABLE_ALLOCATOR).
592+
baseName(name);
593+
clazz.makeMetaClass(Object.getMetaClass());
594+
Object.setConstant(name, clazz, BUILTIN_CONSTANT, -1);
554595
return clazz;
555596
}
556597

@@ -1029,7 +1070,7 @@ private RubyClass initializeCommon(ThreadContext context, RubyClass superClazz,
10291070

10301071
marshal = superClazz.marshal;
10311072

1032-
inherit(superClazz);
1073+
superClazz.invokeInherited(runtime.getCurrentContext(), superClazz, this);
10331074
super.initialize(context, block);
10341075

10351076
return this;
@@ -1347,12 +1388,11 @@ public IRubyObject inherited(ThreadContext context, IRubyObject arg) {
13471388
/** rb_class_inherited (reversed semantics!)
13481389
*
13491390
*/
1391+
@Deprecated(since = "10.0")
13501392
public void inherit(RubyClass superClazz) {
13511393
if (superClazz == null) superClazz = runtime.getObject();
13521394

1353-
if (runtime.getNil() != null) {
1354-
superClazz.invokeInherited(runtime.getCurrentContext(), superClazz, this);
1355-
}
1395+
superClazz.invokeInherited(runtime.getCurrentContext(), superClazz, this);
13561396
}
13571397

13581398
/** Return the real super class of this class.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,12 +2459,12 @@ public RubyClass defineOrGetClassUnder(String name, RubyClass superClazz, Object
24592459
* {@link RubyModule#defineClassUnder(ThreadContext, String, RubyClass, ObjectAllocator)} for native
24602460
* extensions.
24612461
*
2462-
* @param name
2463-
* @param superClazz
2464-
* @param allocator
2465-
* @param file
2466-
* @param line
2467-
* @return
2462+
* @param name to be defined
2463+
* @param superClazz the super class of this new class
2464+
* @param allocator how to allocate it
2465+
* @param file location where it was defined from
2466+
* @param line location where it was defined from
2467+
* @return the new class.
24682468
*/
24692469
public RubyClass defineClassUnder(ThreadContext context, String name, RubyClass superClazz,
24702470
ObjectAllocator allocator, String file, int line) {
@@ -2504,7 +2504,7 @@ public RubyClass defineClassUnder(ThreadContext context, String name, RubyClass
25042504
}
25052505
}
25062506

2507-
clazz = RubyClass.newClass(context.runtime, superClazz, name, allocator, this, true, file, line);
2507+
clazz = RubyClass.newClass(context, superClazz, name, allocator, this, true, file, line);
25082508
}
25092509

25102510
return clazz;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public static RubyBignum randomSeed(Ruby runtime) {
200200

201201
@SuppressWarnings("deprecation")
202202
public static RubyClass createRandomClass(ThreadContext context, RubyClass Object) {
203-
RubyClass RandomBase = RubyClass.newClass(context.runtime, Object).
203+
RubyClass RandomBase = RubyClass.newClass(context, Object, null).
204204
allocator(ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR).
205205
baseName("Base").
206206
defineMethods(context, RubyRandomBase.class);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,10 @@ public static RubyClass newInstance(ThreadContext context, IRubyObject recv, IRu
281281
RubyClass superClass = (RubyClass)recv;
282282

283283
if (name == null || nilName) {
284-
newStruct = RubyClass.newClass(context.runtime, superClass).allocator(RubyStruct::new);
284+
newStruct = RubyClass.newClass(context, superClass, null).
285+
allocator(RubyStruct::new);
285286
newStruct.makeMetaClass(superClass.metaClass);
286-
newStruct.inherit(superClass);
287+
superClass.invokeInherited(context, superClass, newStruct);
287288
} else {
288289
IRubyObject type = superClass.getConstantAt(name);
289290
if (type != null) {

core/src/main/java/org/jruby/java/proxies/JavaInterfaceTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public static IRubyObject impl(ThreadContext context, IRubyObject self, IRubyObj
381381
// RubySymbol implements a Java compareTo thus will always work
382382
}
383383

384-
RubyClass implClass = RubyClass.newClass(context.runtime, objectClass(context)); // ImplClass = Class.new
384+
RubyClass implClass = RubyClass.newClass(context, objectClass(context), null); // ImplClass = Class.new
385385
implClass.include(context, self); // ImplClass.include Interface
386386

387387
final BlockInterfaceImpl ifaceImpl = new BlockInterfaceImpl(implClass, implBlock, methodNames);

core/src/main/java/org/jruby/javasupport/Java.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ static RubyModule createProxyClassForClass(final Ruby runtime, final Class<?> cl
480480
// other java proxy classes added under their superclass' java proxy
481481
superClass = (RubyClass) getProxyClass(runtime, clazz.getSuperclass());
482482
}
483-
proxy = RubyClass.newClass(runtime, superClass);
483+
proxy = RubyClass.newClass(context, superClass, null);
484484
}
485485

486486
// ensure proxy is visible down-thread
@@ -570,7 +570,7 @@ private static RubyClass createProxyClass(ThreadContext context, final RubyClass
570570
}
571571
proxyClass.defineMethods(context, JavaProxy.ClassMethods.class);
572572

573-
if ( invokeInherited ) proxyClass.inherit(superClass);
573+
if (invokeInherited) superClass.invokeInherited(context, superClass, proxyClass);
574574

575575
Initializer.setupProxyClass(context, javaClass, proxyClass);
576576

core/src/main/java/org/jruby/javasupport/JavaPackage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class JavaPackage extends RubyModule {
6363

6464
static RubyClass createJavaPackageClass(ThreadContext context, final RubyModule Java, RubyClass Module, RubyModule Kernel) {
6565
RubyClass superClass = new BlankSlateWrapper(context.runtime, Module, Kernel);
66-
RubyClass JavaPackage = RubyClass.newClass(context.runtime, superClass);
66+
RubyClass JavaPackage = RubyClass.newClass(context, superClass, null);
6767
JavaPackage.setMetaClass(Module);
6868
JavaPackage.allocator(ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR).
6969
baseName("JavaPackage").

0 commit comments

Comments
 (0)