Skip to content

Commit dabd447

Browse files
authored
Merge pull request jruby#8525 from enebo/warn_api
Warn api
2 parents e3392ea + 009d000 commit dabd447

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+689
-731
lines changed

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

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
package org.jruby;
4242

4343
import org.jcodings.specific.UTF8Encoding;
44-
import org.jruby.anno.FrameField;
4544
import org.jruby.anno.TypePopulator;
4645
import org.jruby.api.Create;
4746
import org.jruby.api.Define;
@@ -68,7 +67,6 @@
6867
import org.jruby.parser.ParserManager;
6968
import org.jruby.parser.StaticScope;
7069
import org.jruby.runtime.JavaSites;
71-
import org.jruby.runtime.MethodIndex;
7270
import org.jruby.runtime.TraceEventManager;
7371
import org.jruby.runtime.invokedynamic.InvokeDynamicSupport;
7472
import org.jruby.specialized.RubyObjectSpecializer;
@@ -96,7 +94,6 @@
9694
import org.jruby.ast.executable.RuntimeCache;
9795
import org.jruby.ast.executable.Script;
9896
import org.jruby.ast.executable.ScriptAndCode;
99-
import org.jruby.common.IRubyWarnings.ID;
10097
import org.jruby.common.RubyWarnings;
10198
import org.jruby.compiler.JITCompiler;
10299
import org.jruby.embed.Extension;
@@ -112,7 +109,6 @@
112109
import org.jruby.internal.runtime.ThreadService;
113110
import org.jruby.internal.runtime.ValueAccessor;
114111
import org.jruby.internal.runtime.methods.DynamicMethod;
115-
import org.jruby.internal.runtime.methods.JavaMethod;
116112
import org.jruby.ir.Compiler;
117113
import org.jruby.ir.IRManager;
118114
import org.jruby.ir.interpreter.Interpreter;
@@ -135,7 +131,6 @@
135131
import org.jruby.runtime.ObjectSpace;
136132
import org.jruby.runtime.RubyEvent;
137133
import org.jruby.runtime.ThreadContext;
138-
import org.jruby.runtime.Visibility;
139134
import org.jruby.runtime.builtin.IRubyObject;
140135
import org.jruby.runtime.encoding.EncodingService;
141136
import org.jruby.runtime.invokedynamic.MethodNames;
@@ -225,8 +220,9 @@
225220
import static org.jruby.api.Create.newEmptyString;
226221
import static org.jruby.api.Create.newFrozenString;
227222
import static org.jruby.api.Error.*;
228-
import static org.jruby.internal.runtime.GlobalVariable.Scope.GLOBAL;
223+
import static org.jruby.api.Warn.warn;
229224
import static org.jruby.parser.ParserType.*;
225+
import static org.jruby.runtime.ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR;
230226
import static org.jruby.util.RubyStringBuilder.str;
231227
import static org.jruby.util.RubyStringBuilder.ids;
232228
import static org.jruby.util.RubyStringBuilder.types;
@@ -346,11 +342,9 @@ private Ruby(RubyInstanceConfig config) {
346342
classClass.makeMetaClass(metaClass);
347343
refinementClass.makeMetaClass(metaClass);
348344

349-
RubyBasicObject.createBasicObjectClass(this, basicObjectClass);
350345
RubyObject.createObjectClass(objectClass);
351346
RubyModule.createModuleClass(moduleClass);
352347
RubyClass.createClassClass(this, classClass);
353-
RubyModule.createRefinementClass(refinementClass);
354348

355349
// set constants now that they're initialized
356350
basicObjectClass.setConstant("BasicObject", basicObjectClass);
@@ -363,18 +357,13 @@ private Ruby(RubyInstanceConfig config) {
363357
// specializer for RubyObject subclasses
364358
objectSpecializer = new RubyObjectSpecializer(this);
365359

366-
// Initialize Kernel and include into Object
367-
RubyModule kernel = kernelModule = RubyKernel.createKernelModule(this, objectClass, config);
368-
// In 1.9 and later, Kernel.gsub is defined only when '-p' or '-n' is given on the command line
369-
initKernelGsub(kernel);
370-
371-
// Object is ready, create top self
372-
topSelf = TopSelfFactory.createTopSelf(this,objectClass, false);
360+
kernelModule = defineModuleUnder("Kernel", objectClass); // Initialize Kernel and include into Object
361+
topSelf = new RubyObject(this, objectClass); // Object is ready, create top self
373362

374363
// Pre-create all the core classes potentially referenced during startup
375-
nilClass = RubyNil.createNilClass(this, objectClass);
376-
falseClass = RubyBoolean.createFalseClass(this, objectClass);
377-
trueClass = RubyBoolean.createTrueClass(this, objectClass);
364+
nilClass = defineClass("NilClass", objectClass, NOT_ALLOCATABLE_ALLOCATOR);
365+
falseClass = defineClass("FalseClass", objectClass, NOT_ALLOCATABLE_ALLOCATOR);
366+
trueClass = defineClass("TrueClass", objectClass, NOT_ALLOCATABLE_ALLOCATOR);
378367

379368
nilObject = new RubyNil(this, nilClass);
380369
nilPrefilledArray = new IRubyObject[NIL_PREFILLED_ARRAY_SIZE];
@@ -392,6 +381,16 @@ private Ruby(RubyInstanceConfig config) {
392381
// Get the main threadcontext (gets constructed for us)
393382
final ThreadContext context = getCurrentContext();
394383

384+
RubyModule.finishCreateModuleClass(context, moduleClass);
385+
RubyClass.finishCreateClassClass(context, classClass);
386+
RubyKernel.finishKernelModule(context, kernelModule, config);
387+
RubyNil.finishNilClass(context, nilClass);
388+
RubyBoolean.finishFalseClass(context, falseClass);
389+
RubyBoolean.finishTrueClass(context, trueClass);
390+
RubyModule.finishRefinementClass(context, refinementClass);
391+
RubyBasicObject.finishBasicObjectClass(context, basicObjectClass);
392+
TopSelfFactory.finishTopSelf(context, topSelf, objectClass, false);
393+
395394
// includeModule uses TC.
396395
objectClass.includeModule(kernelModule);
397396

@@ -433,7 +432,7 @@ private Ruby(RubyInstanceConfig config) {
433432
encodingService.defineEncodings();
434433
encodingService.defineAliases();
435434

436-
initDefaultEncodings();
435+
initDefaultEncodings(context);
437436

438437
complexClass = profile.allowClass("Complex") ? RubyComplex.createComplexClass(context, numericClass) : null;
439438
rationalClass = profile.allowClass("Rational") ? RubyRational.createRationalClass(context, numericClass) : null;
@@ -573,7 +572,7 @@ private void initProfiling(ThreadContext context) {
573572
// recache core methods, since they'll have profiling wrappers now
574573
kernelModule.invalidateCacheDescendants(); // to avoid already-cached methods
575574
RubyKernel.recacheBuiltinMethods(this, kernelModule);
576-
RubyBasicObject.recacheBuiltinMethods(this);
575+
RubyBasicObject.recacheBuiltinMethods(context, basicObjectClass);
577576
}
578577

579578
private void initBootLibraries(ThreadContext context) {
@@ -600,20 +599,7 @@ private void initBootLibraries(ThreadContext context) {
600599
}
601600

602601
private void initKernelGsub(RubyModule kernel) {
603-
if (this.config.getKernelGsubDefined()) {
604-
MethodIndex.addMethodReadFields("gsub", FrameField.LASTLINE, FrameField.BACKREF);
605-
kernel.addMethod("gsub", new JavaMethod(kernel, Visibility.PRIVATE, "gsub") {
606-
607-
@Override
608-
public IRubyObject call(ThreadContext context1, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
609-
return switch (args.length) {
610-
case 1 -> RubyKernel.gsub(context1, self, args[0], block);
611-
case 2 -> RubyKernel.gsub(context1, self, args[0], args[1], block);
612-
default -> throw argumentError(context1, String.format("wrong number of arguments %d for 1..2", args.length));
613-
};
614-
}
615-
});
616-
}
602+
617603
}
618604

619605
private ObjectSpacer initObjectSpacer(RubyInstanceConfig config) {
@@ -643,7 +629,7 @@ private JRubyClassLoader initJRubyClassLoader(RubyInstanceConfig config) {
643629
return jrubyClassLoader;
644630
}
645631

646-
private void initDefaultEncodings() {
632+
private void initDefaultEncodings(ThreadContext context) {
647633
// External should always have a value, but Encoding.external_encoding{,=} will lazily setup
648634
String encoding = this.config.getExternalEncoding();
649635
if (encoding != null && !encoding.isEmpty()) {
@@ -658,7 +644,7 @@ private void initDefaultEncodings() {
658644

659645
// Filesystem should always have a value
660646
if (Platform.IS_WINDOWS) {
661-
setDefaultFilesystemEncoding(encodingService.getWindowsFilesystemEncoding(this));
647+
setDefaultFilesystemEncoding(encodingService.getWindowsFilesystemEncoding(context));
662648
} else {
663649
setDefaultFilesystemEncoding(getDefaultExternalEncoding());
664650
}
@@ -1490,7 +1476,7 @@ public RubyClass defineClassUnder(String id, RubyClass superClass, ObjectAllocat
14901476
if (superClass == null) {
14911477
IRubyObject className = parentIsObject ? ids(this, id) :
14921478
parent.toRubyString(getCurrentContext()).append(newString("::")).append(ids(this, id));
1493-
warnings.warn(ID.NO_SUPER_CLASS, str(this, "no super class for '", className, "', Object assumed"));
1479+
warn(getCurrentContext(), str(this, "no super class for '", className, "', Object assumed"));
14941480

14951481
superClass = objectClass;
14961482
}
@@ -3114,8 +3100,9 @@ public void loadScript(Script script, boolean wrap) {
31143100
* @param wrap Whether to use a new "self" for toplevel
31153101
*/
31163102
public void loadExtension(String extName, BasicLibraryService extension, boolean wrap) {
3117-
IRubyObject self = wrap ? TopSelfFactory.createTopSelf(this, objectClass, true) : getTopSelf();
31183103
ThreadContext context = getCurrentContext();
3104+
var topSelf = new RubyObject(this, objectClass);
3105+
IRubyObject self = wrap ? TopSelfFactory.finishTopSelf(context, topSelf, objectClass, true) : getTopSelf();
31193106

31203107
try {
31213108
context.preExtensionLoad(self);
@@ -6008,7 +5995,7 @@ public RaiseException newFrozenError(String objectType, boolean runtimeError) {
60085995

60095996
@Deprecated
60105997
public synchronized void addEventHook(EventHook hook) {
6011-
traceEvents.addEventHook(hook);
5998+
traceEvents.addEventHook(getCurrentContext(), hook);
60125999
}
60136000

60146001
@Deprecated

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import static org.jruby.api.Create.newString;
7676
import static org.jruby.api.Define.defineClass;
7777
import static org.jruby.api.Error.argumentError;
78+
import static org.jruby.api.Warn.warn;
7879
import static org.jruby.runtime.ThreadContext.CALL_KEYWORD;
7980
import static org.jruby.runtime.ThreadContext.resetCallInfo;
8081
import static org.jruby.runtime.Visibility.PRIVATE;
@@ -556,15 +557,6 @@ public static IRubyObject each_codepoint(ThreadContext context, IRubyObject recv
556557
return recv;
557558
}
558559

559-
@JRubyMethod
560-
public static IRubyObject codepoints(ThreadContext context, IRubyObject recv, Block block) {
561-
context.runtime.getWarnings().warn("ARGF#codepoints is deprecated; use #each_codepoint instead");
562-
563-
if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.runtime, recv, "each_line");
564-
565-
return each_codepoint(context, recv, block);
566-
}
567-
568560
/** Invoke a block for each line.
569561
*
570562
*/
@@ -593,13 +585,6 @@ public static IRubyObject each_line(ThreadContext context, IRubyObject recv, IRu
593585
return recv;
594586
}
595587

596-
@Deprecated // TODO "warning: ARGF#lines is deprecated; use #each_line instead"
597-
@JRubyMethod(optional = 1, checkArity = false)
598-
public static IRubyObject lines(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
599-
if (!block.isGiven()) return RubyEnumerator.enumeratorize(context.runtime, recv, "each_line");
600-
return each_line(context, recv, args, block);
601-
}
602-
603588
@JRubyMethod(name = "each", optional = 1, checkArity = false)
604589
public static IRubyObject each(final ThreadContext context, IRubyObject recv, IRubyObject[] args, final Block block) {
605590
return block.isGiven() ? each_line(context, recv, args, block) : enumeratorize(context.runtime, recv, "each", args);

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

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
import static org.jruby.api.Create.newSmallHash;
104104
import static org.jruby.api.Define.defineClass;
105105
import static org.jruby.api.Error.*;
106+
import static org.jruby.api.Warn.warn;
107+
import static org.jruby.api.Warn.warnDeprecated;
108+
import static org.jruby.api.Warn.warning;
106109
import static org.jruby.runtime.Helpers.*;
107110
import static org.jruby.runtime.Visibility.PRIVATE;
108111
import static org.jruby.util.Inspector.*;
@@ -706,9 +709,8 @@ public IRubyObject initialize(ThreadContext context, Block block) {
706709
modifyCheck(context);
707710
unpack(context);
708711
realLength = 0;
709-
if (block.isGiven() && context.runtime.isVerbose()) {
710-
context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
711-
}
712+
if (block.isGiven()) warning(context, "given block not used");
713+
712714
return this;
713715
}
714716

@@ -751,9 +753,7 @@ protected IRubyObject initializeCommon(ThreadContext context, IRubyObject arg0,
751753
}
752754

753755
if (block.isGiven()) {
754-
if (arg1 != null) {
755-
context.runtime.getWarnings().warn(ID.BLOCK_BEATS_DEFAULT_VALUE, "block supersedes default value argument");
756-
}
756+
if (arg1 != null) warn(context, "block supersedes default value argument");
757757

758758
if (block.getSignature() == Signature.NO_ARGUMENTS) {
759759
IRubyObject nil = context.nil;
@@ -1070,9 +1070,7 @@ public IRubyObject fetch(ThreadContext context, IRubyObject arg0, Block block) {
10701070
*/
10711071
@JRubyMethod
10721072
public IRubyObject fetch(ThreadContext context, IRubyObject arg0, IRubyObject arg1, Block block) {
1073-
if (block.isGiven()) {
1074-
context.runtime.getWarnings().warn(ID.BLOCK_BEATS_DEFAULT_VALUE, "block supersedes default value argument");
1075-
}
1073+
if (block.isGiven()) warn(context, "block supersedes default value argument");
10761074

10771075
long index = numericToLong(context, arg0);
10781076

@@ -2346,13 +2344,13 @@ public IRubyObject join19(ThreadContext context) {
23462344

23472345
@JRubyMethod(name = "join")
23482346
public IRubyObject join(ThreadContext context) {
2349-
return join(context, getDefaultSeparator(context));
2347+
return join(context, context.nil);
23502348
}
23512349

23522350
private IRubyObject getDefaultSeparator(ThreadContext context) {
23532351
IRubyObject sep = globalVariables(context).get("$,");
23542352

2355-
if (!sep.isNil()) context.runtime.getWarnings().warnDeprecated("$, is set to non-nil value");
2353+
if (!sep.isNil()) warnDeprecated(context, "$, is set to non-nil value");
23562354

23572355
return sep;
23582356
}
@@ -2700,7 +2698,7 @@ public IRubyObject index(ThreadContext context, IRubyObject obj) {
27002698

27012699
@JRubyMethod(name = {"index", "find_index"})
27022700
public IRubyObject index(ThreadContext context, IRubyObject obj, Block unused) {
2703-
if (unused.isGiven()) context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
2701+
if (unused.isGiven()) warn(context, "given block not used");
27042702
return index(context, obj);
27052703
}
27062704

@@ -2805,7 +2803,7 @@ public IRubyObject rindex(ThreadContext context, IRubyObject obj) {
28052803

28062804
@JRubyMethod
28072805
public IRubyObject rindex(ThreadContext context, IRubyObject obj, Block unused) {
2808-
if (unused.isGiven()) context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
2806+
if (unused.isGiven()) warn(context, "given block not used");
28092807
return rindex(context, obj);
28102808
}
28112809

@@ -2826,33 +2824,6 @@ public IRubyObject rindex(ThreadContext context, Block block) {
28262824
return context.nil;
28272825
}
28282826

2829-
@Deprecated
2830-
public IRubyObject indexes(IRubyObject[] args) {
2831-
return indexes(getRuntime().getCurrentContext(), args);
2832-
}
2833-
2834-
/** rb_ary_indexes
2835-
*
2836-
*/
2837-
@JRubyMethod(name = {"indexes", "indices"}, required = 1, rest = true, checkArity = false)
2838-
public IRubyObject indexes(ThreadContext context, IRubyObject[] args) {
2839-
int argc = Arity.checkArgumentCount(context, args, 1, -1);
2840-
2841-
Ruby runtime = context.runtime;
2842-
runtime.getWarnings().warn(ID.DEPRECATED_METHOD, "Array#indexes is deprecated; use Array#values_at");
2843-
2844-
if (argc == 1) return newArray(runtime, args[0]);
2845-
2846-
RubyArray ary = newBlankArrayInternal(runtime, argc);
2847-
2848-
for (int i = 0; i < argc; i++) {
2849-
ary.storeInternal(context, i, aref(context, args[i]));
2850-
}
2851-
ary.realLength = argc;
2852-
2853-
return ary;
2854-
}
2855-
28562827
/**
28572828
* @return ""
28582829
* @deprecated Use {@link RubyArray#reverse_bang(ThreadContext)} instead.
@@ -3658,7 +3629,7 @@ public IRubyObject count(ThreadContext context, Block block) {
36583629

36593630
@JRubyMethod(name = "count")
36603631
public IRubyObject count(ThreadContext context, IRubyObject obj, Block block) {
3661-
if (block.isGiven()) context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
3632+
if (block.isGiven()) warn(context, "given block not used");
36623633

36633634
int n = 0;
36643635
for (int i = 0; i < realLength; i++) {
@@ -5011,10 +4982,7 @@ public IRubyObject all_pCommon(ThreadContext context, IRubyObject arg, Block blo
50114982
if (!self_each.isBuiltin(this)) return RubyEnumerable.all_pCommon(context, self_each, this, arg, block);
50124983
boolean patternGiven = arg != null;
50134984

5014-
if (block.isGiven() && patternGiven) {
5015-
context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
5016-
}
5017-
4985+
if (block.isGiven() && patternGiven) warn(context, "given block not used");
50184986
if (!block.isGiven() || patternGiven) return all_pBlockless(context, arg);
50194987

50204988
for (int i = 0; i < realLength; i++) {
@@ -5054,9 +5022,7 @@ public IRubyObject any_pCommon(ThreadContext context, IRubyObject arg, Block blo
50545022
if (!self_each.isBuiltin(this)) return RubyEnumerable.any_pCommon(context, self_each, this, arg, block);
50555023
boolean patternGiven = arg != null;
50565024

5057-
if (block.isGiven() && patternGiven) {
5058-
context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
5059-
}
5025+
if (block.isGiven() && patternGiven) warn(context, "given block not used");
50605026

50615027
if (!block.isGiven() || patternGiven) return any_pBlockless(context, arg);
50625028

@@ -5096,10 +5062,7 @@ public IRubyObject none_pCommon(ThreadContext context, IRubyObject arg, Block bl
50965062
if (!self_each.isBuiltin(this)) return RubyEnumerable.none_pCommon(context, self_each, this, arg, block);
50975063
boolean patternGiven = arg != null;
50985064

5099-
if (block.isGiven() && patternGiven) {
5100-
context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
5101-
}
5102-
5065+
if (block.isGiven() && patternGiven) warn(context, "given block not used");
51035066
if (!block.isGiven() || patternGiven) return none_pBlockless(context, arg);
51045067

51055068
for (int i = 0; i < realLength; i++) {
@@ -5138,10 +5101,7 @@ public IRubyObject one_pCommon(ThreadContext context, IRubyObject arg, Block blo
51385101
if (!self_each.isBuiltin(this)) return RubyEnumerable.one_pCommon(context, self_each, this, arg, block);
51395102
boolean patternGiven = arg != null;
51405103

5141-
if (block.isGiven() && patternGiven) {
5142-
context.runtime.getWarnings().warn(ID.BLOCK_UNUSED, "given block not used");
5143-
}
5144-
5104+
if (block.isGiven() && patternGiven) warn(context, "given block not used");
51455105
if (!block.isGiven() || patternGiven) return one_pBlockless(context, arg);
51465106

51475107
boolean found = false;

0 commit comments

Comments
 (0)