Skip to content

Commit ab15259

Browse files
committed
More deprecations with newModule
1 parent 9444211 commit ab15259

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,6 @@ private RubyClass foundExistingClass(ThreadContext context, String id, RubyClass
14891489
* @return The new module
14901490
*/
14911491
@Deprecated(since = "10.0")
1492-
@Extension
14931492
public RubyModule defineModule(String name) {
14941493
return defineModuleUnder(getCurrentContext(), name, objectClass);
14951494
}
@@ -1501,7 +1500,7 @@ public RubyModule defineModule(String name) {
15011500
* @return The new module
15021501
*/
15031502
public RubyModule defineModuleBootstrap(String name) {
1504-
return RubyModule.newModule(this, name, objectClass, false);
1503+
return RubyModule.newModuleBootstrap(this, name, objectClass);
15051504
}
15061505

15071506
@Deprecated(since = "10.0")
@@ -1527,7 +1526,7 @@ public RubyModule defineModuleUnder(ThreadContext context, String name, RubyModu
15271526

15281527
return moduleObj != null ?
15291528
foundExistingModule(context, parent, moduleObj, parentIsObject) :
1530-
RubyModule.newModule(this, name, parent, !parentIsObject);
1529+
RubyModule.newModule(context, name, parent, !parentIsObject, null, -1);
15311530
}
15321531

15331532
private RubyModule foundExistingModule(ThreadContext context, RubyModule parent, IRubyObject moduleObj,
@@ -3048,7 +3047,7 @@ public void loadScope(IRScope scope, boolean wrap) {
30483047

30493048
if (wrap) {
30503049
// toss an anonymous module into the search path
3051-
scope.getStaticScope().setModule(RubyModule.newModule(this));
3050+
scope.getStaticScope().setModule(new RubyModule(this));
30523051
}
30533052

30543053
runInterpreter(getCurrentContext(), scope, self);
@@ -3081,7 +3080,7 @@ public StaticScope setupWrappedToplevel(IRubyObject self, StaticScope top) {
30813080
RubyModule wrapper = loadService.getWrapperSelf();
30823081

30833082
if (wrapper == null || wrapper.isNil()) {
3084-
wrapper = RubyModule.newModule(this);
3083+
wrapper = new RubyModule(this);
30853084
}
30863085

30873086
// toss an anonymous module into the search path

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
import static org.jruby.api.Access.objectClass;
152152
import static org.jruby.api.Convert.*;
153153
import static org.jruby.api.Create.*;
154+
import static org.jruby.api.Define.defineModule;
154155
import static org.jruby.api.Error.*;
155156
import static org.jruby.api.Warn.warn;
156157
import static org.jruby.api.Warn.warnDeprecated;
@@ -436,7 +437,7 @@ protected RubyModule(Ruby runtime, RubyClass metaClass) {
436437
/** standard path for Module construction
437438
*
438439
*/
439-
protected RubyModule(Ruby runtime) {
440+
public RubyModule(Ruby runtime) {
440441
this(runtime, runtime.getModule());
441442
}
442443

@@ -447,27 +448,43 @@ public boolean needsImplementer() {
447448
/** rb_module_new
448449
*
449450
*/
451+
@Deprecated(since = "10.0")
450452
public static RubyModule newModule(Ruby runtime) {
451453
return new RubyModule(runtime);
452454
}
453455

456+
@Deprecated(since = "10.0")
457+
public static RubyModule newModule(Ruby runtime, String name, RubyModule parent, boolean setParent, String file, int line) {
458+
return newModule(runtime.getCurrentContext(), name, parent, setParent, file, line);
459+
}
460+
454461
/** rb_module_new/rb_define_module_id/rb_name_class/rb_set_class_path
455462
*
463+
* This is used by IR to define a new module.
456464
*/
457-
public static RubyModule newModule(Ruby runtime, String name, RubyModule parent, boolean setParent, String file, int line) {
458-
RubyModule module = newModule(runtime).baseName(name);
465+
public static RubyModule newModule(ThreadContext context, String name, RubyModule parent, boolean setParent, String file, int line) {
466+
RubyModule module = defineModule(context).baseName(name);
459467
if (setParent) module.setParent(parent);
460-
parent.setConstant(name, module, file, line);
468+
if (file != null) {
469+
parent.setConstant(name, module, file, line);
470+
} else {
471+
parent.setConstant(name, module);
472+
}
461473
return module;
462474
}
463475

476+
@Deprecated(since = "10.0")
464477
public static RubyModule newModule(Ruby runtime, String name, RubyModule parent, boolean setParent) {
465-
RubyModule module = newModule(runtime).baseName(name);
478+
RubyModule module = new RubyModule(runtime).baseName(name);
466479
if (setParent) module.setParent(parent);
467480
parent.setConstant(name, module);
468481
return module;
469482
}
470483

484+
public static RubyModule newModuleBootstrap(Ruby runtime, String name, RubyModule parent) {
485+
return (RubyModule) parent.setConstant(name, new RubyModule(runtime).baseName(name));
486+
}
487+
471488
// synchronized method per JRUBY-1173 (unsafe Double-Checked Locking)
472489
// FIXME: synchronization is still wrong in CP code
473490
public final synchronized void addClassProvider(ClassProvider provider) {
@@ -2537,7 +2554,7 @@ public RubyModule defineOrGetModuleUnder(ThreadContext context, String name, Str
25372554
} else if ((module = searchProvidersForModule(name)) != null) {
25382555
// reopen a java module
25392556
} else {
2540-
module = RubyModule.newModule(context.runtime, name, this, true, file, line);
2557+
module = RubyModule.newModule(context, name, this, true, file, line);
25412558
}
25422559
return module;
25432560
}

core/src/main/java/org/jruby/api/Define.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ public static RubyClass defineClass(ThreadContext context, String name, RubyClas
3838
* @param name The name of the new module
3939
* @return The new module or existing one if it has already been defined.
4040
*/
41-
@Extension
4241
public static RubyModule defineModule(ThreadContext context, String name) {
4342
return context.runtime.defineModuleUnder(context, name, objectClass(context));
4443
}
4544

46-
45+
/**
46+
* There are times when an anonymous module is needed. It is up to the caller
47+
* to set it up as a constant or include methods or whatever else is needed.
48+
*
49+
* @param context the current thread context
50+
* @return an instance of a RubyModule
51+
*/
52+
public static RubyModule defineModule(ThreadContext context) {
53+
return new RubyModule(context.runtime);
54+
}
4755
}

core/src/main/java/org/jruby/parser/StaticScope.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.jruby.runtime.scope.ManyVarsDynamicScope;
6767

6868
import static org.jruby.api.Convert.asSymbol;
69+
import static org.jruby.api.Define.defineModule;
6970

7071
/**
7172
* StaticScope represents lexical scoping of variables and module/class constants.
@@ -704,7 +705,7 @@ public RubyModule getOverlayModuleForRead() {
704705
public RubyModule getOverlayModuleForWrite(ThreadContext context) {
705706
RubyModule omod = overlayModule;
706707
if (omod == null) {
707-
overlayModule = omod = RubyModule.newModule(context.runtime);
708+
overlayModule = omod = defineModule(context);
708709
}
709710
return omod;
710711
}

core/src/main/java/org/jruby/runtime/Helpers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import static org.jruby.api.Convert.asBoolean;
9393
import static org.jruby.api.Convert.asSymbol;
9494
import static org.jruby.api.Create.*;
95+
import static org.jruby.api.Define.defineModule;
9596
import static org.jruby.api.Error.argumentError;
9697
import static org.jruby.api.Error.typeError;
9798
import static org.jruby.api.Warn.warn;
@@ -1863,7 +1864,7 @@ public static StaticScope preLoad(ThreadContext context, String[] varNames) {
18631864
}
18641865

18651866
public static void preLoadCommon(ThreadContext context, StaticScope staticScope, boolean wrap) {
1866-
RubyModule objectClass = wrap ? RubyModule.newModule(context.runtime) : objectClass(context);
1867+
RubyModule objectClass = wrap ? defineModule(context) : objectClass(context);
18671868

18681869
staticScope.setModule(objectClass);
18691870

0 commit comments

Comments
 (0)