Skip to content

Commit

Permalink
more TCification of constants and other shared things it is hitting
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed Dec 20, 2024
1 parent a81a581 commit 0d1730d
Show file tree
Hide file tree
Showing 37 changed files with 530 additions and 418 deletions.
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/IncludedModuleWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Set;

import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.builtin.Variable;
Expand Down Expand Up @@ -161,8 +162,8 @@ protected IRubyObject constantTableRemove(String name) {
}

@Override
protected IRubyObject getAutoloadConstant(String name, boolean forceLoad) {
return origin.getAutoloadConstant(name, forceLoad);
protected IRubyObject getAutoloadConstant(ThreadContext context, String name, boolean forceLoad) {
return origin.getAutoloadConstant(context, name, forceLoad);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/NativeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static RubyClass createClass(ThreadContext context, RubyClass baseClass,
RubyClass NativeException = defineClass(context, CLASS_NAME, baseClass, NativeException::new).
defineMethods(context, NativeException.class);

Object.deprecateConstant(context.runtime, CLASS_NAME);
Object.deprecateConstant(context, CLASS_NAME);

return NativeException;
}
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/PrependedModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.internal.runtime.methods.RefinedMarker;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

/**
Expand Down Expand Up @@ -192,8 +193,8 @@ protected IRubyObject constantTableRemove(String name) {
}

@Override
protected IRubyObject getAutoloadConstant(String name, boolean forceLoad) {
return origin.getAutoloadConstant(name, forceLoad);
protected IRubyObject getAutoloadConstant(ThreadContext context, String name, boolean forceLoad) {
return origin.getAutoloadConstant(context, name, forceLoad);
}

@Override
Expand Down
11 changes: 7 additions & 4 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ private Ruby(RubyInstanceConfig config) {
encodingClass = RubyEncoding.createEncodingClass(context, objectClass);
converterClass = RubyConverter.createConverterClass(context, objectClass, encodingClass);

encodingService.defineEncodings();
encodingService.defineAliases();
encodingService.defineEncodings(context);
encodingService.defineAliases(context);

initDefaultEncodings(context);

Expand Down Expand Up @@ -463,7 +463,7 @@ private Ruby(RubyInstanceConfig config) {
methodClass = profile.allowClass("Method") ? RubyMethod.createMethodClass(context, objectClass) : null;
if (profile.allowClass("MatchData")) {
matchDataClass = RubyMatchData.createMatchDataClass(context, objectClass);
defineGlobalConstant("MatchingData", matchDataClass);
objectClass.defineConstant(context, "MatchingData", matchDataClass);
} else {
matchDataClass = null;
}
Expand Down Expand Up @@ -1572,7 +1572,10 @@ public void setKCode(KCode kcode) {
*
* @param name the name
* @param value the value
* @deprecated Use {@link RubyModule#defineConstant(ThreadContext, String, IRubyObject)} with a reference
* to Object.
*/
@Deprecated(since = "10.0")
public void defineGlobalConstant(String name, IRubyObject value) {
objectClass.defineConstant(name, value);
}
Expand Down Expand Up @@ -5073,7 +5076,7 @@ public ParserManager getParserManager() {
public void defineDATA(IRubyObject io) {
IRubyObject verbose = getVerbose();
setVerbose(getNil());
defineGlobalConstant("DATA", io);
objectClass.defineConstant(getCurrentContext(), "DATA", io);
setVerbose(verbose);
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ public RubyArgsFile(Ruby runtime, RubyClass metaClass) {
}

public static void initArgsFile(ThreadContext context, RubyModule Enumerable, GlobalVariables globals) {
RubyClass ARGF = defineClass(context, "ARGFClass", objectClass(context), RubyArgsFile::new).
var Object = objectClass(context);
RubyClass ARGF = defineClass(context, "ARGFClass", Object, RubyArgsFile::new).
include(context, Enumerable).
defineMethods(context, RubyArgsFile.class);

IRubyObject argsFile = ARGF.newInstance(context, new IRubyObject[] { null }, null);

context.runtime.setArgsFile(argsFile);
globals.defineReadonly("$<", new ArgsFileAccessor(context.runtime), GlobalVariable.Scope.GLOBAL);
context.runtime.defineGlobalConstant("ARGF", argsFile);
Object.defineConstant(context, "ARGF", argsFile);
globals.defineReadonly("$FILENAME", new ValueAccessor(newString(context, "-")), GlobalVariable.Scope.GLOBAL);
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyComplex.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static RubyClass createComplexClass(ThreadContext context, RubyClass Nume
undefMethods(context, "<", "<=", ">", ">=", "between?", "clamp", "%", "div", "divmod", "floor", "ceil",
"modulo", "remainder", "round", "step", "truncate", "positive?", "negative?").
tap(c -> c.singletonClass(context).undefMethods(context, "allocate", "new")).
tap(c -> c.defineConstant("I", RubyComplex.convert(context, c, asFixnum(context, 0), asFixnum(context, 1))));
tap(c -> c.defineConstant(context, "I", RubyComplex.convert(context, c, asFixnum(context, 0), asFixnum(context, 1))));
}

private RubyComplex(Ruby runtime, RubyClass clazz, IRubyObject real, IRubyObject image) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyEnumerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static RubyClass defineEnumerator(ThreadContext context, RubyClass Object
defineMethods(context, RubyEnumerator.class);

Enumerator.defineClassUnder(context, "FeedValue", Object, NOT_ALLOCATABLE_ALLOCATOR).defineMethods(context, FeedValue.class);
Enumerator.setConstantVisibility(context.runtime, "FeedValue", true);
Enumerator.setConstantVisibility(context, "FeedValue", true);

return Enumerator;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyFatal.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected RubyFatal(Ruby runtime, RubyClass exceptionClass) {
static RubyClass define(ThreadContext context, RubyClass Exception, RubyClass Object) {
var Fatal = defineClass(context, "Fatal", Exception, RubyFatal::new);

Object.deleteConstant("Fatal"); // Remove the constant so it's not accessible (jruby/jruby#5648)
Object.deleteConstant(context, "Fatal"); // Remove the constant so it's not accessible (jruby/jruby#5648)

return Fatal;
}
Expand Down
43 changes: 23 additions & 20 deletions core/src/main/java/org/jruby/RubyGlobal.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ public static void createGlobals(Ruby runtime) {

public static RubyHash createGlobalsAndENV(ThreadContext context, GlobalVariables globals, RubyInstanceConfig instanceConfig) {
var runtime = context.runtime;
var Object = objectClass(context);
var topLevelBinding = runtime.newBinding();
runtime.setTopLevelBinding(topLevelBinding);
runtime.defineGlobalConstant("TOPLEVEL_BINDING", topLevelBinding);
Object.defineConstant(context, "TOPLEVEL_BINDING", topLevelBinding);

initARGV(runtime);

Expand All @@ -156,27 +157,27 @@ public static RubyHash createGlobalsAndENV(ThreadContext context, GlobalVariable
IRubyObject version = RubyString.newFString(runtime, Constants.RUBY_VERSION);
IRubyObject patchlevel = asFixnum(context, 0);

runtime.defineGlobalConstant("RUBY_VERSION", version);
runtime.defineGlobalConstant("RUBY_PATCHLEVEL", patchlevel);
runtime.defineGlobalConstant("RUBY_RELEASE_DATE", release);
runtime.defineGlobalConstant("RUBY_PLATFORM", platform);
Object.defineConstant(context, "RUBY_VERSION", version);
Object.defineConstant(context, "RUBY_PATCHLEVEL", patchlevel);
Object.defineConstant(context, "RUBY_RELEASE_DATE", release);
Object.defineConstant(context, "RUBY_PLATFORM", platform);

IRubyObject description = RubyString.newFString(runtime, OutputStrings.getVersionString());
runtime.defineGlobalConstant("RUBY_DESCRIPTION", description);
Object.defineConstant(context, "RUBY_DESCRIPTION", description);

IRubyObject copyright = RubyString.newFString(runtime, OutputStrings.getCopyrightString());
runtime.defineGlobalConstant("RUBY_COPYRIGHT", copyright);
Object.defineConstant(context, "RUBY_COPYRIGHT", copyright);

runtime.defineGlobalConstant("RELEASE_DATE", release);
runtime.defineGlobalConstant("PLATFORM", platform);
Object.defineConstant(context, "RELEASE_DATE", release);
Object.defineConstant(context, "PLATFORM", platform);

IRubyObject jrubyVersion = RubyString.newFString(runtime, Constants.VERSION);
IRubyObject jrubyRevision = RubyString.newFString(runtime, Constants.REVISION);
runtime.defineGlobalConstant("JRUBY_VERSION", jrubyVersion);
runtime.defineGlobalConstant("JRUBY_REVISION", jrubyRevision);
runtime.defineGlobalConstant("RUBY_REVISION", RubyString.newFString(runtime, Constants.REVISION));
runtime.defineGlobalConstant("RUBY_ENGINE", engine);
runtime.defineGlobalConstant("RUBY_ENGINE_VERSION", jrubyVersion);
Object.defineConstant(context, "JRUBY_VERSION", jrubyVersion);
Object.defineConstant(context, "JRUBY_REVISION", jrubyRevision);
Object.defineConstant(context, "RUBY_REVISION", RubyString.newFString(runtime, Constants.REVISION));
Object.defineConstant(context, "RUBY_ENGINE", engine);
Object.defineConstant(context, "RUBY_ENGINE_VERSION", jrubyVersion);

RubyInstanceConfig.Verbosity verbosity = instanceConfig.getVerbosity();
runtime.defineVariable(new WarningGlobalVariable(context, "$-W", verbosity), GLOBAL);
Expand Down Expand Up @@ -318,6 +319,7 @@ public IRubyObject setValue(IRubyObject newValue) {

public static void initSTDIO(Ruby runtime, GlobalVariables globals) {
var context = runtime.getCurrentContext();
var Object = objectClass(context);
RubyIO stdin, stdout, stderr;

// If we're the main for the process and native stdio is enabled, use default descriptors
Expand All @@ -341,15 +343,15 @@ public static void initSTDIO(Ruby runtime, GlobalVariables globals) {
}

var object = runtime.getObject();
if (object.getConstantFromNoConstMissing("STDIN") == null) {
if (object.getConstantFromNoConstMissing(context, "STDIN") == null) {
runtime.defineVariable(new InputGlobalVariable(runtime, "$stdin", stdin), GLOBAL);
runtime.defineVariable(new OutputGlobalVariable(runtime, "$stdout", stdout), GLOBAL);
globals.alias("$>", "$stdout");
runtime.defineVariable(new OutputGlobalVariable(runtime, "$stderr", stderr), GLOBAL);

runtime.defineGlobalConstant("STDIN", stdin);
runtime.defineGlobalConstant("STDOUT", stdout);
runtime.defineGlobalConstant("STDERR", stderr);
Object.defineConstant(context, "STDIN", stdin);
Object.defineConstant(context, "STDOUT", stdout);
Object.defineConstant(context, "STDERR", stderr);

runtime.setOriginalStderr(stderr);
} else {
Expand Down Expand Up @@ -425,20 +427,21 @@ private static int unwrapDripStream(Object stream) {
@SuppressWarnings("unchecked")
private static RubyHash defineGlobalEnvConstants(ThreadContext context) {
var runtime = context.runtime;
var Object = objectClass(context);
Map<RubyString, RubyString> environmentVariableMap = OSEnvironment.environmentVariableMap(runtime);
var instanceConfig = instanceConfig(context);
RubyHash env = new CaseInsensitiveStringOnlyRubyHash(
runtime, environmentVariableMap, context.nil,
instanceConfig.isNativeEnabled() && instanceConfig.isUpdateNativeENVEnabled()
);
env.singletonClass(context).defineMethods(context, CaseInsensitiveStringOnlyRubyHash.class);
runtime.defineGlobalConstant("ENV", env);
Object.defineConstant(context, "ENV", env);

// Define System.getProperties() in ENV_JAVA
Map<RubyString, RubyString> systemPropertiesMap = OSEnvironment.systemPropertiesMap(runtime);
RubyHash envJava = new ReadOnlySystemPropertiesHash(runtime, systemPropertiesMap, context.nil);
envJava.setFrozen(true);
runtime.defineGlobalConstant("ENV_JAVA", envJava);
Object.defineConstant(context, "ENV_JAVA", envJava);

return env;
}
Expand Down
Loading

0 comments on commit 0d1730d

Please sign in to comment.