Skip to content

Commit a85dd6d

Browse files
authored
Merge pull request jruby#8532 from enebo/define_class_dep
Deprecate defineOrGetClassUnder
2 parents cc71299 + 31c93bf commit a85dd6d

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,49 +2438,60 @@ private void checkAliasFrameAccesses(ThreadContext context, String id, String ne
24382438
}
24392439
}
24402440

2441-
/** this method should be used only by interpreter or compiler
2442-
*
2443-
*/
2441+
@Deprecated(since = "10.0")
24442442
public RubyClass defineOrGetClassUnder(String name, RubyClass superClazz) {
2445-
return defineOrGetClassUnder(name, superClazz, null);
2443+
return defineClassUnder(getCurrentContext(), name, superClazz, null, null, -1);
24462444
}
24472445

2446+
@Deprecated(since = "10.0")
24482447
public RubyClass defineOrGetClassUnder(String name, RubyClass superClazz, String file, int line) {
2449-
return defineOrGetClassUnder(name, superClazz, null, file, line);
2448+
return defineClassUnder(getCurrentContext(), name, superClazz, null, file, line);
24502449
}
24512450

2451+
@Deprecated(since = "10.0")
24522452
public RubyClass defineOrGetClassUnder(String name, RubyClass superClazz, ObjectAllocator allocator) {
2453-
return defineOrGetClassUnder(name, superClazz, allocator, null, -1);
2453+
return defineClassUnder(getCurrentContext(), name, superClazz, allocator, null, -1);
24542454
}
24552455

2456-
public RubyClass defineOrGetClassUnder(String name, RubyClass superClazz, ObjectAllocator allocator,
2457-
String file, int line) {
2456+
/**
2457+
* Internal API only used by our IR runtime helpers in setting up Ruby-defined classes or re-accessing them
2458+
* if they already exist. Look at
2459+
* {@link RubyModule#defineClassUnder(ThreadContext, String, RubyClass, ObjectAllocator)} for native
2460+
* extensions.
2461+
*
2462+
* @param name
2463+
* @param superClazz
2464+
* @param allocator
2465+
* @param file
2466+
* @param line
2467+
* @return
2468+
*/
2469+
public RubyClass defineClassUnder(ThreadContext context, String name, RubyClass superClazz,
2470+
ObjectAllocator allocator, String file, int line) {
24582471
// This method is intended only for defining new classes in Ruby code,
24592472
// so it uses the allocator of the specified superclass or default to
24602473
// the Object allocator. It should NOT be used to define classes that require a native allocator.
24612474

2462-
Ruby runtime = getRuntime();
2463-
var context = runtime.getCurrentContext();
24642475
IRubyObject classObj = getConstantAtSpecial(name);
24652476
RubyClass clazz;
24662477

24672478
if (classObj != null) {
2468-
if (!(classObj instanceof RubyClass)) throw typeError(context, name + " is not a class");
2469-
clazz = (RubyClass)classObj;
2479+
if (!(classObj instanceof RubyClass clazzy)) throw typeError(context, name + " is not a class");
2480+
clazz = clazzy;
24702481

24712482
if (superClazz != null) {
24722483
RubyClass tmp = clazz.getSuperClass();
24732484
while (tmp != null && tmp.isIncluded()) tmp = tmp.getSuperClass(); // need to skip IncludedModuleWrappers
24742485
if (tmp != null) tmp = tmp.getRealClass();
2475-
if (tmp != superClazz) throw typeError(context, "superclass mismatch for class " + ids(runtime, name));
2486+
if (tmp != superClazz) throw typeError(context, "superclass mismatch for class " + ids(context.runtime, name));
24762487
}
24772488
} else if ((clazz = searchProvidersForClass(name, superClazz)) != null) {
24782489
// reopen a java class
24792490
} else {
2480-
if (superClazz == null) superClazz = runtime.getObject();
2491+
if (superClazz == null) superClazz = objectClass(context);
24812492

24822493
if (allocator == null) {
2483-
if (isReifiable(runtime, superClazz)) {
2494+
if (isReifiable(objectClass(context), superClazz)) {
24842495
if (Options.REIFY_CLASSES.load()) {
24852496
allocator = REIFYING_OBJECT_ALLOCATOR;
24862497
} else if (Options.REIFY_VARIABLES.load()) {
@@ -2493,7 +2504,7 @@ public RubyClass defineOrGetClassUnder(String name, RubyClass superClazz, Object
24932504
}
24942505
}
24952506

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

24992510
return clazz;
@@ -2502,12 +2513,8 @@ public RubyClass defineOrGetClassUnder(String name, RubyClass superClazz, Object
25022513
/**
25032514
* Determine if a new child of the given class can have its variables reified.
25042515
*/
2505-
private boolean isReifiable(Ruby runtime, RubyClass superClass) {
2506-
if (superClass == runtime.getObject()) return true;
2507-
2508-
if (superClass.getAllocator() == IVAR_INSPECTING_OBJECT_ALLOCATOR) return true;
2509-
2510-
return false;
2516+
private boolean isReifiable(RubyClass Object, RubyClass superClass) {
2517+
return superClass == Object || superClass.getAllocator() == IVAR_INSPECTING_OBJECT_ALLOCATOR;
25112518
}
25122519

25132520
/** this method should be used only by interpreter or compiler

core/src/main/java/org/jruby/ext/zlib/RubyZlib.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public static RubyModule createZlibModule(ThreadContext context) {
100100
var GZipFileError = GzipFile.defineClassUnder(context, "Error", ZlibError, errorAllocator);
101101
var fileErrorAllocator = ZlibError.getAllocator();
102102
GZipFileError.addReadAttribute(context, "input");
103-
GzipFile.defineOrGetClassUnder("CRCError", GZipFileError, fileErrorAllocator);
104-
GzipFile.defineOrGetClassUnder("NoFooter", GZipFileError, fileErrorAllocator);
105-
GzipFile.defineOrGetClassUnder("LengthError", GZipFileError, fileErrorAllocator);
103+
GzipFile.defineClassUnder("CRCError", GZipFileError, fileErrorAllocator);
104+
GzipFile.defineClassUnder("NoFooter", GZipFileError, fileErrorAllocator);
105+
GzipFile.defineClassUnder("LengthError", GZipFileError, fileErrorAllocator);
106106

107107
Zlib.defineClassUnder(context, "GzipReader", GzipFile, JZlibRubyGzipReader::new).
108108
include(context, enumerableModule(context)).

core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.jruby.RubyString;
3030
import org.jruby.RubySymbol;
3131
import org.jruby.api.Create;
32-
import org.jruby.common.IRubyWarnings;
3332
import org.jruby.exceptions.RaiseException;
3433
import org.jruby.exceptions.Unrescuable;
3534
import org.jruby.ext.coverage.CoverageData;
@@ -1780,7 +1779,7 @@ public static RubyModule newRubyClassFromIR(ThreadContext context, String id, St
17801779
sc = (RubyClass) superClass;
17811780
}
17821781

1783-
RubyModule newRubyClass = ((RubyModule)container).defineOrGetClassUnder(id, sc, scope.getFile(), context.getLine() + 1);
1782+
RubyModule newRubyClass = ((RubyModule)container).defineClassUnder(context, id, sc, null, scope.getFile(), context.getLine() + 1);
17841783

17851784
scope.setModule(newRubyClass);
17861785

0 commit comments

Comments
 (0)