Skip to content

Commit ae03bb0

Browse files
committed
More conversion to new singletonClass
1 parent 0e654fd commit ae03bb0

File tree

13 files changed

+69
-66
lines changed

13 files changed

+69
-66
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import static org.jruby.api.Error.typeError;
7676
import static org.jruby.api.Warn.warn;
7777
import static org.jruby.ir.runtime.IRRuntimeHelpers.dupIfKeywordRestAtCallsite;
78+
import static org.jruby.ir.runtime.IRRuntimeHelpers.getCurrentClassBase;
7879
import static org.jruby.runtime.Helpers.invokeChecked;
7980
import static org.jruby.runtime.ThreadContext.*;
8081
import static org.jruby.runtime.Visibility.*;
@@ -1882,12 +1883,15 @@ public IRubyObject specificEval(ThreadContext context, RubyModule mod, IRubyObje
18821883
return evalUnder(context, mod, evalStr, file, line, evalType);
18831884
}
18841885

1886+
@Deprecated(since = "10.0")
18851887
protected RubyModule getInstanceEvalClass() {
1886-
if (isImmediate()) {
1887-
// Ruby uses Qnil here, we use "dummy" because we need a class
1888-
return getRuntime().getDummy();
1889-
}
1890-
return getSingletonClass();
1888+
return getInstanceEvalClass(getCurrentContext());
1889+
}
1890+
1891+
protected RubyModule getInstanceEvalClass(ThreadContext context) {
1892+
return isImmediate() ?
1893+
context.runtime.getDummy() : // MRI uses Qnil here, we use "dummy" because we need a class
1894+
singletonClass(context);
18911895
}
18921896

18931897
/**
@@ -2543,25 +2547,25 @@ public RubyArray to_a(ThreadContext context) {
25432547
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
25442548
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
25452549
public IRubyObject instance_eval(ThreadContext context, Block block) {
2546-
return specificEval(context, getInstanceEvalClass(), block, EvalType.INSTANCE_EVAL);
2550+
return specificEval(context, getInstanceEvalClass(context), block, EvalType.INSTANCE_EVAL);
25472551
}
25482552
@JRubyMethod(name = "instance_eval",
25492553
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
25502554
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
25512555
public IRubyObject instance_eval(ThreadContext context, IRubyObject arg0, Block block) {
2552-
return specificEval(context, getInstanceEvalClass(), arg0, block, EvalType.INSTANCE_EVAL);
2556+
return specificEval(context, getInstanceEvalClass(context), arg0, block, EvalType.INSTANCE_EVAL);
25532557
}
25542558
@JRubyMethod(name = "instance_eval",
25552559
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
25562560
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
25572561
public IRubyObject instance_eval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Block block) {
2558-
return specificEval(context, getInstanceEvalClass(), arg0, arg1, block, EvalType.INSTANCE_EVAL);
2562+
return specificEval(context, getInstanceEvalClass(context), arg0, arg1, block, EvalType.INSTANCE_EVAL);
25592563
}
25602564
@JRubyMethod(name = "instance_eval",
25612565
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
25622566
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
25632567
public IRubyObject instance_eval(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
2564-
return specificEval(context, getInstanceEvalClass(), arg0, arg1, arg2, block, EvalType.INSTANCE_EVAL);
2568+
return specificEval(context, getInstanceEvalClass(context), arg0, arg1, arg2, block, EvalType.INSTANCE_EVAL);
25652569
}
25662570

25672571
// This is callable and will work but the rest = true is put so we can match the expected arity error message
@@ -2601,14 +2605,9 @@ public IRubyObject instance_eval(ThreadContext context, IRubyObject[] args, Bloc
26012605
reads = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE},
26022606
writes = {LASTLINE, BACKREF, VISIBILITY, BLOCK, SELF, METHODNAME, LINE, CLASS, FILENAME, SCOPE})
26032607
public IRubyObject instance_exec(ThreadContext context, IRubyObject[] args, Block block) {
2604-
if (!block.isGiven()) {
2605-
throw context.runtime.newLocalJumpErrorNoBlock();
2606-
}
2607-
2608-
RubyModule klazz = isImmediate() ? // MRI uses Qnil here, we use "dummy" because we need a class
2609-
context.runtime.getDummy() : singletonClass(context);
2608+
if (!block.isGiven()) throw context.runtime.newLocalJumpErrorNoBlock();
26102609

2611-
return yieldUnder(context, klazz, args, block, EvalType.INSTANCE_EVAL);
2610+
return yieldUnder(context, getInstanceEvalClass(context), args, block, EvalType.INSTANCE_EVAL);
26122611
}
26132612

26142613
/** rb_obj_extend

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name
536536
}
537537

538538
/**
539-
* @see #getSingletonClass()
539+
* @see #singletonClass(ThreadContext)
540540
*/
541541
RubyClass toSingletonClass(RubyBasicObject target) {
542542
// replaced after makeMetaClass with MetaClass's toSingletonClass
@@ -2217,14 +2217,15 @@ protected void extraClinitLookup(SkinnyMethodAdapter m) {
22172217
// now create proxy class
22182218
m.getstatic(javaPath, RUBY_FIELD, ci(Ruby.class));
22192219
m.getstatic(javaPath, RUBY_CLASS_FIELD, ci(RubyClass.class));
2220+
m.invokevirtual("org/jruby/Ruby", "getCurrentContext", "()Lorg/jruby/runtime/ThreadContext;");
22202221
m.ldc(org.objectweb.asm.Type.getType("L" + javaPath + ";"));
22212222
// if (simpleAlloc) // if simple, don't init, if complex, do init
22222223
// m.iconst_0(); // false (as int)
22232224
// else
22242225
m.iconst_1(); // true (as int)
22252226

22262227
m.invokestatic(p(JavaProxyClass.class), "setProxyClassReified",
2227-
sig(JavaProxyClass.class, Ruby.class, RubyClass.class, Class.class, boolean.class));
2228+
sig(JavaProxyClass.class, ThreadContext.class, RubyClass.class, Class.class, boolean.class));
22282229
m.dup();
22292230
m.putstatic(javaPath, RUBY_PROXY_CLASS_FIELD, ci(JavaProxyClass.class));
22302231

core/src/main/java/org/jruby/anno/AnnotationBinder.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public void processType(TypeElement cd) {
131131
out.println("import org.jruby.runtime.Arity;");
132132
out.println("import org.jruby.runtime.Visibility;");
133133
out.println("import org.jruby.runtime.MethodIndex;");
134+
out.println("import org.jruby.runtime.ThreadContext;");
134135
out.println("import java.util.Arrays;");
135136
out.println("import java.util.List;");
136137
out.println("import jakarta.annotation.Generated;");
@@ -161,8 +162,11 @@ public void processType(TypeElement cd) {
161162

162163
out.println(" JavaMethod javaMethod;");
163164
out.println(" DynamicMethod moduleMethod, aliasedMethod;");
164-
if (hasMeta || hasModule) out.println(" RubyClass singletonClass = cls.getSingletonClass();");
165-
out.println(" Ruby runtime = cls.getRuntime();");
165+
out.println(" ThreadContext context = cls.getRuntime().getCurrentContext();");
166+
out.println(" Ruby runtime = context.runtime;");
167+
if (hasMeta || hasModule) {
168+
out.println(" RubyClass singletonClass = cls.singletonClass(context);");
169+
}
166170
out.println(" boolean core = runtime.isBootingCore();");
167171

168172
Map<CharSequence, List<ExecutableElement>> annotatedMethods = new LinkedHashMap<>();
@@ -501,7 +505,7 @@ private void generateMethodAddCalls(ExecutableElement md, final boolean meta, fi
501505
} else {
502506
defineMethodOnClass("javaMethod", "cls", names, aliases, md);
503507
if (module) {
504-
out.println(" moduleMethod = populateModuleMethod(cls, javaMethod);");
508+
out.println(" moduleMethod = populateModuleMethod(cls, singletonClass, javaMethod);");
505509
defineMethodOnClass("moduleMethod", "singletonClass", names, aliases, md);
506510
}
507511
}

core/src/main/java/org/jruby/anno/TypePopulator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Optional;
3636

3737
import org.jruby.Ruby;
38+
import org.jruby.RubyClass;
3839
import org.jruby.RubyModule;
3940
import org.jruby.internal.runtime.methods.DynamicMethod;
4041
import org.jruby.internal.runtime.methods.JavaMethod;
@@ -66,10 +67,16 @@ public static void populateMethod(JavaMethod javaMethod, int arity, String simpl
6667
javaMethod.setNotImplemented(notImplemented);
6768
javaMethod.setNativeCall(nativeTarget, nativeName, nativeReturn, nativeArguments, isStatic, false);
6869
}
69-
70+
71+
// Still needed for older generated populators.
72+
@Deprecated(since = "10.0")
7073
public static DynamicMethod populateModuleMethod(RubyModule cls, DynamicMethod javaMethod) {
74+
return populateModuleMethod(cls, cls.singletonClass(cls.getRuntime().getCurrentContext()), javaMethod);
75+
}
76+
77+
public static DynamicMethod populateModuleMethod(RubyModule cls, RubyClass singletonClass, DynamicMethod javaMethod) {
7178
DynamicMethod moduleMethod = javaMethod.dup();
72-
moduleMethod.setImplementationClass(cls.getSingletonClass());
79+
moduleMethod.setImplementationClass(singletonClass);
7380
moduleMethod.setVisibility(Visibility.PUBLIC);
7481
return moduleMethod;
7582
}

core/src/main/java/org/jruby/ir/instructions/DefineMetaClassInstr.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ public static DefineMetaClassInstr decode(IRReaderDecoder d) {
6868

6969
@Override
7070
public Object interpret(ThreadContext context, StaticScope currScope, DynamicScope currDynScope, IRubyObject self, Object[] temp) {
71-
Ruby runtime = context.runtime;
72-
7371
IRubyObject obj = (IRubyObject) getObject().retrieve(context, self, currScope, currDynScope, temp);
7472

75-
return IRRuntimeHelpers.newInterpretedMetaClass(runtime, metaClassBody, obj);
73+
return IRRuntimeHelpers.newInterpretedMetaClass(context, metaClassBody, obj);
7674
}
7775

7876
@Override

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,8 +1709,8 @@ public static IRubyObject setInstanceVariable(IRubyObject self, IRubyObject valu
17091709
* Construct a new DynamicMethod to wrap the given IRModuleBody and singletonizable object. Used by interpreter.
17101710
*/
17111711
@Interp
1712-
public static DynamicMethod newInterpretedMetaClass(Ruby runtime, IRScope metaClassBody, IRubyObject obj) {
1713-
RubyClass singletonClass = newMetaClassFromIR(runtime, metaClassBody.getStaticScope(), obj, metaClassBody.maybeUsingRefinements());
1712+
public static DynamicMethod newInterpretedMetaClass(ThreadContext context, IRScope metaClassBody, IRubyObject obj) {
1713+
RubyClass singletonClass = newMetaClassFromIR(context, metaClassBody.getStaticScope(), obj, metaClassBody.maybeUsingRefinements());
17141714

17151715
return new InterpretedIRBodyMethod(metaClassBody, singletonClass);
17161716
}
@@ -1720,18 +1720,18 @@ public static DynamicMethod newInterpretedMetaClass(Ruby runtime, IRScope metaCl
17201720
*/
17211721
@JIT
17221722
public static DynamicMethod newCompiledMetaClass(ThreadContext context, MethodHandle handle, StaticScope scope, IRubyObject obj, int line, boolean refinements, boolean dynscopeEliminated) {
1723-
RubyClass singletonClass = newMetaClassFromIR(context.runtime, scope, obj, refinements);
1723+
RubyClass singletonClass = newMetaClassFromIR(context, scope, obj, refinements);
17241724

17251725
return new CompiledIRNoProtocolMethod(handle, scope, scope.getFile(), line,
17261726
singletonClass, !dynscopeEliminated);
17271727
}
17281728

1729-
private static RubyClass newMetaClassFromIR(Ruby runtime, StaticScope scope, IRubyObject obj, boolean refinements) {
1730-
RubyClass singletonClass = Helpers.getSingletonClass(runtime, obj);
1729+
private static RubyClass newMetaClassFromIR(ThreadContext context, StaticScope scope, IRubyObject obj, boolean refinements) {
1730+
RubyClass singletonClass = obj.singletonClass(context);
17311731

17321732
scope.setModule(singletonClass);
17331733

1734-
if (refinements) scope.captureParentRefinements(runtime.getCurrentContext());
1734+
if (refinements) scope.captureParentRefinements(context);
17351735

17361736
return singletonClass;
17371737
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static RubyClass createArrayJavaProxy(ThreadContext context, RubyClass Ja
4444
return defineClass(context, "ArrayJavaProxy", JavaProxy, NOT_ALLOCATABLE_ALLOCATOR).
4545
defineMethods(context, ArrayJavaProxy.class).
4646
include(context, Enumerable).
47-
tap(c -> c.singletonClass(context).addMethod("new", new ArrayNewMethod(c.getSingletonClass(), Visibility.PUBLIC)));
47+
tap(c -> c.singletonClass(context).addMethod("new", new ArrayNewMethod(c.singletonClass(context), Visibility.PUBLIC)));
4848
}
4949

5050
public static ArrayJavaProxy newArray(final Ruby runtime, final Class<?> elementType, final int... dimensions) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,17 @@ public void ensureThis(Object self) {
426426
if (getObject() == null) setObject(self);
427427
}
428428

429+
@Deprecated(since = "10.0")
429430
protected static void initialize(final RubyClass concreteJavaProxy) {
431+
initialize(concreteJavaProxy.getRuntime().getCurrentContext(), concreteJavaProxy);
432+
}
433+
434+
protected static void initialize(ThreadContext context, final RubyClass concreteJavaProxy) {
430435
concreteJavaProxy.addMethod("initialize", new InitializeMethod(concreteJavaProxy));
431436
// We define a custom "new" method to ensure that __jcreate! is getting called,
432437
// so that if the user doesn't call super in their subclasses, the object will
433438
// still get set up properly. See JRUBY-4704.
434-
RubyClass singleton = concreteJavaProxy.getSingletonClass();
439+
RubyClass singleton = concreteJavaProxy.singletonClass(context);
435440
singleton.addMethod("new", new NewMethod(singleton));
436441
}
437442

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import static org.jruby.api.Access.objectClass;
6161
import static org.jruby.api.Convert.asSymbol;
6262
import static org.jruby.api.Convert.castAsModule;
63+
import static org.jruby.api.Create.newArray;
6364
import static org.jruby.api.Define.defineModule;
6465
import static org.jruby.api.Error.argumentError;
6566
import static org.jruby.api.Error.typeError;
@@ -129,13 +130,12 @@ public static IRubyObject append_features(ThreadContext context, IRubyObject sel
129130
}
130131

131132
private static void appendFeaturesToClass(ThreadContext context, final IRubyObject self, final RubyClass clazz) {
132-
final Ruby runtime = context.runtime;
133-
checkAlreadyReified(clazz, runtime);
133+
checkAlreadyReified(context, clazz);
134134

135135
final IRubyObject javaClass = JavaProxy.getJavaClass((RubyModule) self);
136136
RubyArray javaInterfaces;
137137
if ( ! clazz.hasInstanceVariable("@java_interfaces") ) {
138-
javaInterfaces = RubyArray.newArray(runtime, javaClass);
138+
javaInterfaces = newArray(context, javaClass);
139139
clazz.setInstanceVariable("@java_interfaces", javaInterfaces);
140140

141141
initInterfaceImplMethods(context, clazz);
@@ -151,19 +151,19 @@ private static void appendFeaturesToClass(ThreadContext context, final IRubyObje
151151
}
152152
}
153153

154-
private static void checkAlreadyReified(final RubyClass clazz, Ruby runtime) throws RaiseException {
154+
private static void checkAlreadyReified(ThreadContext context, final RubyClass clazz) throws RaiseException {
155155
// not allowed for original (non-generated) Java classes
156156
// note: not allowing for any previously created class right now;
157157
// this restriction might be loosened later for generated classes
158158
if ( ( Java.NEW_STYLE_EXTENSION && clazz.reifiedClass() != null )
159159
||
160160
( clazz.hasInstanceVariable("@java_class")
161161
&& clazz.getInstanceVariable("@java_class").isTrue()
162-
&& !clazz.getSingletonClass().isMethodBound("java_proxy_class", false) )
162+
&& !clazz.singletonClass(context).isMethodBound("java_proxy_class", false) )
163163
||
164164
( clazz.hasInstanceVariable("@java_proxy_class")
165165
&& clazz.getInstanceVariable("@java_proxy_class").isTrue() ) ) {
166-
throw runtime.newArgumentError("can not add Java interface to existing Java class");
166+
throw argumentError(context, "can not add Java interface to existing Java class");
167167
}
168168
}
169169

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,6 @@ public void setVariable(int index, Object value) {
576576
super.setVariable(index, value);
577577
}
578578

579-
public RubyClass getSingletonClass() {
580-
return singletonClass(getRuntime().getCurrentContext());
581-
}
582-
583579
/** rb_singleton_class
584580
*
585581
* Note: this method is specialized for RubyFixnum, RubySymbol,

0 commit comments

Comments
 (0)