Skip to content

Commit 1e0e0b6

Browse files
committed
final uses of getSingletonClass replaced
1 parent ae03bb0 commit 1e0e0b6

23 files changed

+79
-85
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ public RubyClass makeMetaClass(RubyClass superClass) {
100100

101101
private RubyClass getSuperSingletonMetaClass() {
102102
if (attached instanceof RubyClass att) {
103-
RubyClass superClass = att.getSuperClass();
103+
RubyClass superClass = att.superClass();
104104
if (superClass != null) superClass = superClass.getRealClass();
105105
// #<Class:BasicObject>'s singleton class == Class.singleton_class
106-
if (superClass == null) return runtime.getClassClass().getSingletonClass();
107-
return superClass.getMetaClass().getSingletonClass();
106+
// Context should be safe here as we never make a metaclass from a metaclass before first TC is made.
107+
var context = getRuntime().getCurrentContext();
108+
if (superClass == null) return runtime.getClassClass().singletonClass(context);
109+
return superClass.getMetaClass().singletonClass(context);
108110
}
109111

110112
return getSuperClass().getRealClass().getMetaClass(); // NOTE: is this correct?

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
package org.jruby;
3030

3131
import org.jcodings.Encoding;
32+
import org.jruby.api.JRubyAPI;
3233
import org.jruby.ast.util.ArgsUtil;
3334
import org.jruby.ir.interpreter.Interpreter;
3435
import org.jruby.java.proxies.JavaProxy;
@@ -461,23 +462,21 @@ public static RubyClass getMetaClass(IRubyObject arg) {
461462
return ((RubyBasicObject) arg).metaClass;
462463
}
463464

464-
/** rb_singleton_class
465-
*
466-
* Note: this method is specialized for RubyFixnum, RubySymbol,
467-
* RubyNil and RubyBoolean
468-
*
469-
* Will either return the existing singleton class for this
470-
* object, or create a new one and return that.
471-
*/
472465
@Override
473466
public RubyClass getSingletonClass() {
474-
RubyClass klass = metaClass.toSingletonClass(this);
475-
476-
if (isFrozen()) klass.setFrozen(true);
477-
478-
return klass;
467+
return singletonClass(getCurrentContext());
479468
}
480469

470+
/**
471+
* Will either return the existing singleton class for this object, or create a new one and return that.
472+
* For a few types a singleton class is not possible so it will throw an error.
473+
*
474+
* @param context the current thread context
475+
* @return the singleton of this type
476+
*/
477+
478+
// MRI: rb_singleton_class
479+
@JRubyAPI
481480
public RubyClass singletonClass(ThreadContext context) {
482481
RubyClass klass = metaClass.toSingletonClass(this);
483482

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@ public BigInteger getBigIntegerValue() {
156156
return value;
157157
}
158158

159-
@Override
160-
public RubyClass getSingletonClass() {
161-
return singletonClass(getRuntime().getCurrentContext());
162-
}
163-
164159
@Override
165160
public RubyClass singletonClass(ThreadContext context) {
166161
throw typeError(context, "can't define singleton");

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ public boolean isImmediate() {
8888
return true;
8989
}
9090

91-
@Override
92-
public RubyClass getSingletonClass() {
93-
return metaClass;
94-
}
95-
9691
public RubyClass singletonClass(ThreadContext context) {
9792
return metaClass;
9893
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,6 @@ public boolean isImmediate() {
180180
return true;
181181
}
182182

183-
public RubyClass getSingletonClass() {
184-
return singletonClass(getRuntime().getCurrentContext());
185-
}
186-
187183
public RubyClass singletonClass(ThreadContext context) {
188184
throw typeError(context, "can't define singleton");
189185
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ private RubyFloat(RubyClass klass, double value) {
147147
this.flags |= FROZEN_F;
148148
}
149149

150-
@Override
151-
public RubyClass getSingletonClass() {
152-
return singletonClass(getRuntime().getCurrentContext());
153-
}
154-
155150
public RubyClass singletonClass(ThreadContext context) {
156151
throw typeError(context, "can't define singleton");
157152
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ public boolean isImmediate() {
9393
return true;
9494
}
9595

96-
@Override
97-
public RubyClass getSingletonClass() {
98-
return metaClass;
99-
}
100-
10196
public RubyClass singletonClass(ThreadContext context) {
10297
return metaClass;
10398
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4973,11 +4973,6 @@ private static IRubyObject getPatternQuoted(ThreadContext context, IRubyObject p
49734973
return pat; // String
49744974
}
49754975

4976-
@Override
4977-
public RubyClass getSingletonClass() {
4978-
return singletonClass(getRuntime().getCurrentContext());
4979-
}
4980-
49814976
public RubyClass singletonClass(ThreadContext context) {
49824977
if (isChilled()) {
49834978
mutateChilledString();

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,6 @@ public boolean isImmediate() {
305305
return true;
306306
}
307307

308-
@Override
309-
public RubyClass getSingletonClass() {
310-
return singletonClass(getRuntime().getCurrentContext());
311-
}
312-
313308
public RubyClass singletonClass(ThreadContext context) {
314309
throw typeError(context, "can't define singleton");
315310
}

core/src/main/java/org/jruby/ext/ffi/jffi/Function.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ public static RubyClass createFunctionClass(ThreadContext context, RubyModule FF
5050

5151
this.enums = enums;
5252
this.saveError = saveError;
53+
var singleton = singletonClass(getRuntime().getCurrentContext());
5354
// Wire up Function#call(*args) to use the super-fast native invokers
54-
getSingletonClass().addMethod("call", createDynamicMethod(getSingletonClass()));
55+
singleton.addMethod("call", createDynamicMethod(singleton));
5556
}
5657

5758
Function(Ruby runtime, RubyClass klass, MemoryIO address,
@@ -64,8 +65,9 @@ public static RubyClass createFunctionClass(ThreadContext context, RubyModule FF
6465
functionInfo.jffiReturnType, functionInfo.jffiParameterTypes, functionInfo.convention);
6566
this.enums = enums;
6667
this.saveError = true;
68+
var singleton = singletonClass(getRuntime().getCurrentContext());
6769
// Wire up Function#call(*args) to use the super-fast native invokers
68-
getSingletonClass().addMethod("call", createDynamicMethod(getSingletonClass()));
70+
singleton.addMethod("call", createDynamicMethod(singleton));
6971
}
7072

7173
@JRubyMethod(name = { "new" }, meta = true, required = 2, optional = 2, checkArity = false)

0 commit comments

Comments
 (0)