Skip to content

Commit 0bfa19c

Browse files
committed
More TCification of getRuntime() consumers in RubyModule
1 parent 30b39b5 commit 0bfa19c

File tree

4 files changed

+88
-58
lines changed

4 files changed

+88
-58
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ protected RubyBasicObject dupSetup(ThreadContext context, RubyBasicObject dup) {
935935

936936
// MRI: rb_clone_setup
937937
protected RubyBasicObject cloneSetup(ThreadContext context, RubyBasicObject clone, IRubyObject freeze) {
938-
clone.setMetaClass(getSingletonClassCloneAndAttach(clone));
938+
clone.setMetaClass(getSingletonClassCloneAndAttach(context, clone));
939939

940940
initCopy(context, clone, this);
941941

@@ -976,39 +976,38 @@ private RubyBasicObject rbCloneInternal(ThreadContext context, IRubyObject freez
976976
return cloneSetup(context, clone, freeze);
977977
}
978978

979-
979+
@Deprecated(since = "10.0")
980980
protected RubyClass getSingletonClassClone() {
981981
return getSingletonClassCloneAndAttach(null);
982982
}
983983

984+
@Deprecated(since = "10.0")
985+
protected RubyClass getSingletonClassCloneAndAttach(RubyBasicObject attach) {
986+
return getSingletonClassCloneAndAttach(getCurrentContext(), attach);
987+
}
988+
984989
/** rb_singleton_class_clone
985990
*
986991
* Will make sure that if the current objects class is a
987992
* singleton, it will get cloned.
988993
*
989994
* @return either a real class, or a clone of the current singleton class
990995
*/
991-
protected RubyClass getSingletonClassCloneAndAttach(RubyBasicObject attach) {
996+
protected RubyClass getSingletonClassCloneAndAttach(ThreadContext context, RubyBasicObject attach) {
992997
RubyClass klass = getMetaClass();
993998

994-
if (!klass.isSingleton()) {
995-
return klass;
996-
}
999+
if (!klass.isSingleton()) return klass;
9971000

998-
MetaClass clone = new MetaClass(getRuntime(), klass.getSuperClass(), attach);
1001+
MetaClass clone = new MetaClass(context.runtime, klass.getSuperClass(), attach);
9991002
clone.flags = klass.flags;
10001003

1001-
if (this instanceof RubyClass) {
1002-
clone.setMetaClass(clone);
1003-
} else {
1004-
clone.setMetaClass(klass.getSingletonClassClone());
1005-
}
1004+
clone.setMetaClass(this instanceof RubyClass ? clone : klass.getSingletonClassCloneAndAttach(context, null));
10061005

10071006
if (klass.hasVariables()) clone.syncVariables(klass);
10081007

10091008
clone.syncConstants(klass);
10101009

1011-
klass.cloneMethods(clone);
1010+
klass.cloneMethods(context, clone);
10121011

10131012
((MetaClass) clone.getMetaClass()).setAttached(clone);
10141013

@@ -2333,7 +2332,7 @@ public final IRubyObject methods(ThreadContext context, IRubyObject[] args, bool
23332332
* in the receiver will be listed.
23342333
*/
23352334
public IRubyObject public_methods(ThreadContext context, IRubyObject[] args) {
2336-
return getMetaClass().instanceMethods(args, PUBLIC, true, false);
2335+
return getMetaClass().instanceMethods(context, args, PUBLIC, true, false);
23372336
}
23382337

23392338
/** rb_obj_protected_methods
@@ -2349,7 +2348,7 @@ public IRubyObject public_methods(ThreadContext context, IRubyObject[] args) {
23492348
* {@link RubyModule#protected_instance_methods} method.
23502349
*/
23512350
public IRubyObject protected_methods(ThreadContext context, IRubyObject[] args) {
2352-
return getMetaClass().instanceMethods(args, PROTECTED, true, false);
2351+
return getMetaClass().instanceMethods(context, args, PROTECTED, true, false);
23532352
}
23542353

23552354
/** rb_obj_private_methods
@@ -2365,7 +2364,7 @@ public IRubyObject protected_methods(ThreadContext context, IRubyObject[] args)
23652364
* {@link RubyModule#private_instance_methods} method.
23662365
*/
23672366
public IRubyObject private_methods(ThreadContext context, IRubyObject[] args) {
2368-
return getMetaClass().instanceMethods(args, PRIVATE, true, false);
2367+
return getMetaClass().instanceMethods(context, args, PRIVATE, true, false);
23692368
}
23702369

23712370
/** rb_obj_singleton_methods
@@ -2476,7 +2475,12 @@ public IRubyObject singleton_method(IRubyObject name) {
24762475
*/
24772476
public IRubyObject method(IRubyObject name) {
24782477
final RubySymbol symbol = TypeConverter.checkID(name);
2479-
return getMetaClass().newMethod(this, symbol.idString(), true, null, true);
2478+
return getMetaClass().newMethod(getRuntime().getCurrentContext(), this, symbol.idString(), null, true, null, true, true);
2479+
}
2480+
2481+
@Deprecated(since = "10.0")
2482+
public IRubyObject method(IRubyObject name, StaticScope refinedScope) {
2483+
return method(getCurrentContext(), name, refinedScope);
24802484
}
24812485

24822486
/**
@@ -2486,9 +2490,9 @@ public IRubyObject method(IRubyObject name) {
24862490
* @param refinedScope the static scope for the caller method
24872491
* @return
24882492
*/
2489-
public IRubyObject method(IRubyObject name, StaticScope refinedScope) {
2493+
public IRubyObject method(ThreadContext context, IRubyObject name, StaticScope refinedScope) {
24902494
final RubySymbol symbol = TypeConverter.checkID(name);
2491-
return getMetaClass().newMethod(this, symbol.idString(), refinedScope, true, null, true);
2495+
return getMetaClass().newMethod(context, this, symbol.idString(), refinedScope, true, null, true, true);
24922496
}
24932497

24942498
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ else if (argc > 1) {
689689

690690
@JRubyMethod(module = true)
691691
public static IRubyObject public_method(ThreadContext context, IRubyObject recv, IRubyObject symbol) {
692-
return recv.getMetaClass().newMethod(recv, symbol.asJavaString(), true, PUBLIC, true, false);
692+
return recv.getMetaClass().newMethod(context, recv, symbol.asJavaString(), null, true, PUBLIC, true, false);
693693
}
694694

695695
/** rb_f_putc
@@ -2328,7 +2328,7 @@ public static IRubyObject singleton_method(IRubyObject self, IRubyObject symbol)
23282328

23292329
@JRubyMethod(name = "method", required = 1, reads = SCOPE)
23302330
public static IRubyObject method(ThreadContext context, IRubyObject self, IRubyObject symbol) {
2331-
return ((RubyBasicObject)self).method(symbol, context.getCurrentStaticScope());
2331+
return ((RubyBasicObject)self).method(context, symbol, context.getCurrentStaticScope());
23322332
}
23332333

23342334
/**

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

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ public void prependModule(ThreadContext context, RubyModule module) {
12711271

12721272
if (this.isModule()) {
12731273
boolean doPrepend = true;
1274-
synchronized (getRuntime().getHierarchyLock()) {
1274+
synchronized (context.runtime.getHierarchyLock()) {
12751275
for (RubyClass includeClass : includingHierarchies) {
12761276
RubyClass checkClass = includeClass;
12771277
while (checkClass != null) {
@@ -1290,7 +1290,7 @@ public void prependModule(ThreadContext context, RubyModule module) {
12901290

12911291
invalidateCoreClasses(context);
12921292
invalidateCacheDescendants(context);
1293-
invalidateConstantCacheForModuleInclusion(module);
1293+
invalidateConstantCacheForModuleInclusion(context, module);
12941294
}
12951295
}
12961296

@@ -1328,7 +1328,7 @@ public synchronized void includeModule(ThreadContext context, IRubyObject arg) {
13281328

13291329
if (this.isModule()) {
13301330
boolean doInclude = true;
1331-
synchronized (getRuntime().getHierarchyLock()) {
1331+
synchronized (context.runtime.getHierarchyLock()) {
13321332
for (RubyClass includeClass : includingHierarchies) {
13331333
RubyClass checkClass = includeClass;
13341334
while (checkClass != null) {
@@ -1347,7 +1347,7 @@ public synchronized void includeModule(ThreadContext context, IRubyObject arg) {
13471347

13481348
invalidateCoreClasses(context);
13491349
invalidateCacheDescendants(context);
1350-
invalidateConstantCacheForModuleInclusion(module);
1350+
invalidateConstantCacheForModuleInclusion(context, module);
13511351
}
13521352

13531353
/**
@@ -2104,12 +2104,12 @@ private CacheEntry cacheHit(String name) {
21042104
return null;
21052105
}
21062106

2107-
private void invalidateConstantCacheForModuleInclusion(RubyModule module) {
2107+
private void invalidateConstantCacheForModuleInclusion(ThreadContext context, RubyModule module) {
21082108
Map<String, Invalidator> invalidators = null;
21092109
for (RubyModule mod : gatherModules(module)) {
21102110
for (String name : mod.getConstantMap().keySet()) {
21112111
if (invalidators == null) invalidators = new HashMap<>();
2112-
invalidators.put(name, getRuntime().getConstantInvalidator(name));
2112+
invalidators.put(name, context.runtime.getConstantInvalidator(name));
21132113
}
21142114
}
21152115
if (invalidators != null) {
@@ -2381,7 +2381,7 @@ protected void invalidateConstantCache(ThreadContext context, String constantNam
23812381
@Deprecated(since = "10.0")
23822382
protected void invalidateConstantCaches(Set<String> constantNames) {
23832383
if (constantNames.size() > 0) {
2384-
Ruby runtime = getRuntime();
2384+
Ruby runtime = getCurrentContext().runtime;
23852385

23862386
List<Invalidator> constantInvalidators = new ArrayList<>(constantNames.size());
23872387
for (String name : constantNames) {
@@ -2787,20 +2787,24 @@ public boolean isMethodBound(String name, boolean checkVisibility, boolean check
27872787
return checkRespondTo ? respondsToMethod(name, checkVisibility): isMethodBound(name, checkVisibility);
27882788
}
27892789

2790+
@Deprecated(since = "10.0")
27902791
public final IRubyObject newMethod(IRubyObject receiver, String methodName, boolean bound, Visibility visibility) {
2791-
return newMethod(receiver, methodName, bound, visibility, false, true);
2792+
return newMethod(getCurrentContext(), receiver, methodName, null, bound, visibility, false, true);
27922793
}
27932794

2795+
@Deprecated(since = "10.0")
27942796
public final IRubyObject newMethod(IRubyObject receiver, String methodName, StaticScope refinedScope, boolean bound, Visibility visibility) {
2795-
return newMethod(receiver, methodName, refinedScope, bound, visibility, false, true);
2797+
return newMethod(getCurrentContext(), receiver, methodName, refinedScope, bound, visibility, false, true);
27962798
}
27972799

2800+
@Deprecated(since = "10.0")
27982801
public final IRubyObject newMethod(IRubyObject receiver, final String methodName, boolean bound, Visibility visibility, boolean respondToMissing) {
2799-
return newMethod(receiver, methodName, bound, visibility, respondToMissing, true);
2802+
return newMethod(getCurrentContext(), receiver, methodName, null, bound, visibility, respondToMissing, true);
28002803
}
28012804

2805+
@Deprecated(since = "10.0")
28022806
public final IRubyObject newMethod(IRubyObject receiver, final String methodName, StaticScope scope, boolean bound, Visibility visibility, boolean respondToMissing) {
2803-
return newMethod(receiver, methodName, scope, bound, visibility, respondToMissing, true);
2807+
return newMethod(getCurrentContext(), receiver, methodName, scope, bound, visibility, respondToMissing, true);
28042808
}
28052809

28062810
public static class RespondToMissingMethod extends JavaMethod.JavaMethodNBlock {
@@ -2832,12 +2836,20 @@ public int hashCode() {
28322836

28332837
}
28342838

2835-
public IRubyObject newMethod(IRubyObject receiver, final String methodName, boolean bound, Visibility visibility, boolean respondToMissing, boolean priv) {
2836-
return newMethod(receiver, methodName, null, bound, visibility, respondToMissing, priv);
2839+
@Deprecated(since = "10.0")
2840+
public IRubyObject newMethod(IRubyObject receiver, final String methodName, boolean bound, Visibility visibility,
2841+
boolean respondToMissing, boolean priv) {
2842+
return newMethod(getCurrentContext(), receiver, methodName, null, bound, visibility, respondToMissing, priv);
28372843
}
28382844

2839-
public IRubyObject newMethod(IRubyObject receiver, final String methodName, StaticScope scope, boolean bound, Visibility visibility, boolean respondToMissing, boolean priv) {
2840-
var context = getRuntime().getCurrentContext();
2845+
@Deprecated(since = "10.0")
2846+
public IRubyObject newMethod(IRubyObject receiver, final String methodName, StaticScope scope, boolean bound,
2847+
Visibility visibility, boolean respondToMissing, boolean priv) {
2848+
return newMethod(getCurrentContext(), receiver, methodName, scope, bound, visibility, respondToMissing, priv);
2849+
}
2850+
2851+
protected IRubyObject newMethod(ThreadContext context, IRubyObject receiver, final String methodName, StaticScope scope,
2852+
boolean bound, Visibility visibility, boolean respondToMissing, boolean priv) {
28412853
CacheEntry entry = scope == null ? searchWithCache(methodName) : searchWithRefinements(methodName, scope);
28422854

28432855
if (entry.method.isUndefined() || visibility != null && entry.method.getVisibility() != visibility) {
@@ -2978,8 +2990,12 @@ public IRubyObject name(ThreadContext context) {
29782990
return getBaseName() == null ? context.nil : rubyName(context);
29792991
}
29802992

2993+
@Deprecated(since = "10.0")
29812994
protected final IRubyObject cloneMethods(RubyModule clone) {
2982-
Ruby runtime = getRuntime();
2995+
return cloneMethods(getCurrentContext(), clone);
2996+
}
2997+
2998+
protected final IRubyObject cloneMethods(ThreadContext context, RubyModule clone) {
29832999
RubyModule realType = this.getOrigin();
29843000
for (Map.Entry<String, DynamicMethod> entry : getMethods().entrySet()) {
29853001
DynamicMethod method = entry.getValue();
@@ -2991,7 +3007,7 @@ protected final IRubyObject cloneMethods(RubyModule clone) {
29913007
// TODO: Make DynamicMethod immutable
29923008
DynamicMethod clonedMethod = method.dup();
29933009
clonedMethod.setImplementationClass(clone);
2994-
clone.putMethod(runtime, entry.getKey(), clonedMethod);
3010+
clone.putMethod(context.runtime, entry.getKey(), clonedMethod);
29953011
}
29963012
}
29973013

@@ -3010,13 +3026,13 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject original)
30103026
RubyModule originalModule = (RubyModule)original;
30113027

30123028
if (!getMetaClass().isSingleton()) {
3013-
setMetaClass(originalModule.getSingletonClassCloneAndAttach(this));
3029+
setMetaClass(originalModule.getSingletonClassCloneAndAttach(context, this));
30143030
}
30153031
superClass(originalModule.superClass());
30163032
if (originalModule.hasVariables()) syncVariables(originalModule);
30173033
syncConstants(originalModule);
30183034

3019-
originalModule.cloneMethods(this);
3035+
originalModule.cloneMethods(context, this);
30203036

30213037
this.javaProxy = originalModule.javaProxy;
30223038

@@ -3492,18 +3508,28 @@ public IRubyObject attr_accessor(ThreadContext context, IRubyObject[] args) {
34923508
* @param not if true only find methods not matching supplied visibility
34933509
* @return a RubyArray of instance method names
34943510
*/
3511+
@Deprecated(since = "10.0")
34953512
private RubyArray instance_methods(IRubyObject[] args, Visibility visibility, boolean not) {
34963513
boolean includeSuper = args.length > 0 ? args[0].isTrue() : true;
3497-
return instanceMethods(visibility, includeSuper, true, not);
3514+
return instanceMethods(getCurrentContext(), visibility, includeSuper, true, not);
34983515
}
34993516

3517+
@Deprecated(since = "10.0")
35003518
public RubyArray instanceMethods(IRubyObject[] args, Visibility visibility, boolean obj, boolean not) {
3519+
return instanceMethods(getCurrentContext(), args, visibility, obj, not);
3520+
}
3521+
3522+
public RubyArray instanceMethods(ThreadContext context, IRubyObject[] args, Visibility visibility, boolean obj, boolean not) {
35013523
boolean includeSuper = args.length > 0 ? args[0].isTrue() : true;
3502-
return instanceMethods(visibility, includeSuper, obj, not);
3524+
return instanceMethods(context, visibility, includeSuper, obj, not);
35033525
}
35043526

3527+
@Deprecated(since = "10.0")
35053528
public RubyArray instanceMethods(Visibility visibility, boolean includeSuper, boolean obj, boolean not) {
3506-
var context = getRuntime().getCurrentContext();
3529+
return instanceMethods(getCurrentContext(), visibility, includeSuper, obj, not);
3530+
}
3531+
3532+
private RubyArray instanceMethods(ThreadContext context, Visibility visibility, boolean includeSuper, boolean obj, boolean not) {
35073533
var ary = newArray(context);
35083534

35093535
populateInstanceMethodNames(context, new HashSet<>(), ary, visibility, obj, not, includeSuper);
@@ -3570,7 +3596,7 @@ public RubyArray<?> instance_methods(IRubyObject[] args) {
35703596
public RubyArray instance_methods(ThreadContext context, IRubyObject[] args) {
35713597
Arity.checkArgumentCount(context, args, 0, 1);
35723598

3573-
return instanceMethods(args, PRIVATE, false, true);
3599+
return instanceMethods(context, args, PRIVATE, false, true);
35743600
}
35753601

35763602
@Deprecated
@@ -3587,17 +3613,18 @@ public RubyArray<?> public_instance_methods(IRubyObject[] args) {
35873613
public RubyArray public_instance_methods(ThreadContext context, IRubyObject[] args) {
35883614
Arity.checkArgumentCount(context, args, 0, 1);
35893615

3590-
return instanceMethods(args, PUBLIC, false, false);
3616+
return instanceMethods(context, args, PUBLIC, false, false);
35913617
}
35923618

35933619
@JRubyMethod(name = "instance_method", reads = SCOPE)
35943620
public IRubyObject instance_method(ThreadContext context, IRubyObject symbol) {
3595-
return newMethod(null, checkID(context, symbol).idString(), context.getCurrentStaticScope(), false, null);
3621+
return newMethod(context, null, checkID(context, symbol).idString(), context.getCurrentStaticScope(),
3622+
false, null, false, true);
35963623
}
35973624

35983625
@Deprecated(since = "10.0")
35993626
public IRubyObject instance_method(IRubyObject symbol) {
3600-
return newMethod(null, checkID(getCurrentContext(), symbol).idString(), false, null);
3627+
return instance_method(getCurrentContext(), symbol);
36013628
}
36023629

36033630
@Deprecated(since = "10.0")
@@ -3607,7 +3634,7 @@ public IRubyObject public_instance_method(IRubyObject symbol) {
36073634

36083635
@JRubyMethod(name = "public_instance_method")
36093636
public IRubyObject public_instance_method(ThreadContext context, IRubyObject symbol) {
3610-
return newMethod(null, checkID(context, symbol).idString(), false, PUBLIC);
3637+
return newMethod(context, null, checkID(context, symbol).idString(), null, false, PUBLIC, false, true);
36113638
}
36123639

36133640
@Deprecated
@@ -3622,7 +3649,7 @@ public RubyArray protected_instance_methods(IRubyObject[] args) {
36223649
public RubyArray protected_instance_methods(ThreadContext context, IRubyObject[] args) {
36233650
Arity.checkArgumentCount(context, args, 0, 1);
36243651

3625-
return instanceMethods(args, PROTECTED, false, false);
3652+
return instanceMethods(context, args, PROTECTED, false, false);
36263653
}
36273654

36283655
@Deprecated
@@ -3637,7 +3664,7 @@ public RubyArray private_instance_methods(IRubyObject[] args) {
36373664
public RubyArray private_instance_methods(ThreadContext context, IRubyObject[] args) {
36383665
Arity.checkArgumentCount(context, args, 0, 1);
36393666

3640-
return instanceMethods(args, PRIVATE, false, false);
3667+
return instanceMethods(context, args, PRIVATE, false, false);
36413668
}
36423669

36433670
@JRubyMethod(name = "undefined_instance_methods")
@@ -5294,7 +5321,7 @@ private IRubyObject getConstantSkipAutoload(ThreadContext context, String name,
52945321
IRubyObject constant = iterateConstantNoConstMissing(context, name, this, inherit, false);
52955322

52965323
if (constant == null && !isClass() && includeObject) {
5297-
constant = iterateConstantNoConstMissing(context, name, getRuntime().getObject(), inherit, false);
5324+
constant = iterateConstantNoConstMissing(context, name, objectClass(context), inherit, false);
52985325
}
52995326

53005327
return constant;
@@ -5962,10 +5989,9 @@ private String validateClassVariable(ThreadContext context, String name) {
59625989

59635990
@Deprecated(since = "10.0")
59645991
protected final String validateClassVariable(IRubyObject nameObj, String name) {
5965-
if (IdUtil.isValidClassVariableName(name)) {
5966-
return name;
5967-
}
5968-
throw getRuntime().newNameError("'%1$s' is not allowed as a class variable name", this, nameObj);
5992+
if (IdUtil.isValidClassVariableName(name)) return name;
5993+
5994+
throw getCurrentContext().runtime.newNameError("'%1$s' is not allowed as a class variable name", this, nameObj);
59695995
}
59705996

59715997
@Deprecated(since = "10.0")

0 commit comments

Comments
 (0)