pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/078f36d7fe76dcdf072e1101ff6d0b52f5be10a5

r-b48faa60c69660fa.css" /> Some obvious moves to using defineConstant vs setConstant for definin… · jruby/jruby@078f36d · GitHub
Skip to content

Commit 078f36d

Browse files
committed
Some obvious moves to using defineConstant vs setConstant for defining extensions/classes/modules
1 parent 4e925df commit 078f36d

15 files changed

Lines changed: 80 additions & 54 deletions

File tree

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4684,14 +4684,9 @@ public Invalidator getCheckpointInvalidator() {
46844684
* @param <C> the enum type, which must implement {@link Constant}.
46854685
* @deprecated Use {@link org.jruby.RubyModule#defineConstantsFrom(ThreadContext, Class)} instead.
46864686
*/
4687-
@Deprecated
4687+
@Deprecated(since = "10.0")
46884688
public <C extends Enum<C> & Constant> void loadConstantSet(RubyModule module, Class<C> enumClass) {
4689-
for (C constant : EnumSet.allOf(enumClass)) {
4690-
String name = constant.name();
4691-
if (constant.defined() && Character.isUpperCase(name.charAt(0))) {
4692-
module.setConstant(name, newFixnum(constant.intValue()));
4693-
}
4694-
}
4689+
module.defineConstantsFrom(getCurrentContext(), enumClass);
46954690
}
46964691

46974692
/**
@@ -4700,10 +4695,12 @@ public <C extends Enum<C> & Constant> void loadConstantSet(RubyModule module, Cl
47004695
* @param module the module in which we want to define the constants
47014696
* @param constantSetName the name of the constant set from which to get the constants
47024697
*/
4698+
@Deprecated(since = "10.0")
47034699
public void loadConstantSet(RubyModule module, String constantSetName) {
4700+
var context = getCurrentContext();
47044701
for (Constant c : ConstantSet.getConstantSet(constantSetName)) {
47054702
if (c.defined() && Character.isUpperCase(c.name().charAt(0))) {
4706-
module.setConstant(c.name(), newFixnum(c.intValue()));
4703+
module.defineConstant(context, c.name(), newFixnum(c.intValue()));
47074704
}
47084705
}
47094706
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public static RubyClass newClass(Ruby runtime, RubyClass superClass, String name
531531

532532
clazz.makeMetaClass(superClass.getMetaClass());
533533
if (setParent) clazz.setParent(parent);
534-
parent.setConstant(name, clazz);
534+
parent.defineConstant(context, name, clazz);
535535
superClass.invokeInherited(context, superClass, clazz);
536536
return clazz;
537537
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static RubyClass createIOBufferClass(ThreadContext context, RubyClass Obj
4141
defineMethods(context, RubyIOBuffer.class).
4242
defineConstants(context, RubyIOBuffer.class);
4343

44-
IO.setConstant("READABLE", asFixnum(context, OpenFile.READABLE));
45-
IO.setConstant("WRITABLE", asFixnum(context, OpenFile.WRITABLE));
44+
IO.defineConstant(context, "READABLE", asFixnum(context, OpenFile.READABLE));
45+
IO.defineConstant(context, "WRITABLE", asFixnum(context, OpenFile.WRITABLE));
4646

4747
return IOBuffer;
4848
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Arrays;
5050
import java.util.Collection;
5151
import java.util.Collections;
52+
import java.util.EnumSet;
5253
import java.util.HashMap;
5354
import java.util.HashSet;
5455
import java.util.IdentityHashMap;
@@ -1374,9 +1375,11 @@ private boolean defineAnnotatedConstant(ThreadContext context, Field field) {
13741375

13751376
String[] names = jrubyConstant.value();
13761377
if (names.length == 0) {
1377-
setConstant(field.getName(), realVal);
1378+
defineConstant(field.getName(), realVal);
13781379
} else {
1379-
for (String name : names) setConstant(name, realVal);
1380+
for (String name : names) {
1381+
defineConstant(name, realVal);
1382+
}
13801383
}
13811384

13821385
return true;
@@ -1609,7 +1612,13 @@ public <T extends RubyModule> T defineConstants(ThreadContext context, Class con
16091612
*/
16101613
@JRubyAPI
16111614
public <C extends Enum<C> &Constant, T extends RubyModule> T defineConstantsFrom(ThreadContext context, Class<C> enumClass) {
1612-
context.runtime.loadConstantSet(this, enumClass);
1615+
for (C constant : EnumSet.allOf(enumClass)) {
1616+
String name = constant.name();
1617+
if (constant.defined() && Character.isUpperCase(name.charAt(0))) {
1618+
defineConstant(context, name, asFixnum(context, constant.intValue()));
1619+
}
1620+
}
1621+
16131622
return (T) this;
16141623
}
16151624

@@ -4593,12 +4602,16 @@ public IRubyObject const_source_location(ThreadContext context, IRubyObject[] ar
45934602
return context.nil;
45944603
}
45954604

4605+
@Deprecated(since = "10.0")
4606+
public IRubyObject const_set(IRubyObject name, IRubyObject value) {
4607+
return const_set(getCurrentContext(), name, value);
4608+
}
4609+
45964610
/** rb_mod_const_set
45974611
*
45984612
*/
45994613
@JRubyMethod(name = "const_set")
4600-
public IRubyObject const_set(IRubyObject name, IRubyObject value) {
4601-
ThreadContext context = getRuntime().getCurrentContext();
4614+
public IRubyObject const_set(ThreadContext context, IRubyObject name, IRubyObject value) {
46024615
return setConstant(validateConstant(name), value, context.getFile(), context.getLine() + 1);
46034616
}
46044617

core/src/main/java/org/jruby/ext/etc/RubyEtc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ public static RubyModule createEtcModule(ThreadContext context) {
7777
if (!Platform.IS_WINDOWS) {
7878
for (Constant c : ConstantSet.getConstantSet("Sysconf")) {
7979
String name = c.name().substring(1); // leading "_"
80-
Etc.setConstant(name, asFixnum(context, c.intValue()));
80+
Etc.defineConstant(context, name, asFixnum(context, c.intValue()));
8181
}
8282
for (Constant c : ConstantSet.getConstantSet("Confstr")) {
8383
String name = c.name().substring(1); // leading "_"
84-
Etc.setConstant(name, asFixnum(context, c.intValue()));
84+
Etc.defineConstant(context, name, asFixnum(context, c.intValue()));
8585
}
8686
for (Constant c : ConstantSet.getConstantSet("Pathconf")) {
8787
String name = c.name().substring(1); // leading "_"
88-
Etc.setConstant(name, asFixnum(context, c.intValue()));
88+
Etc.defineConstant(context, name, asFixnum(context, c.intValue()));
8989
}
9090
}
9191

core/src/main/java/org/jruby/ext/fcntl/FcntlLibrary.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@
2828

2929
import java.io.IOException;
3030

31+
import jnr.constants.Constant;
32+
import jnr.constants.ConstantSet;
3133
import org.jruby.Ruby;
3234
import org.jruby.RubyFixnum;
3335
import org.jruby.RubyModule;
36+
import org.jruby.runtime.ThreadContext;
3437
import org.jruby.runtime.load.Library;
3538

39+
import static org.jruby.api.Convert.asFixnum;
3640
import static org.jruby.api.Define.defineModule;
3741

3842
/**
@@ -48,7 +52,21 @@ public void load(final Ruby runtime, boolean wrap) throws IOException {
4852
var Fcntl = defineModule(context, "Fcntl").
4953
defineConstant(context, "FD_CLOEXEC", RubyFixnum.newFixnum(runtime, FD_CLOEXEC));
5054

51-
runtime.loadConstantSet(Fcntl, "Fcntl");
52-
runtime.loadConstantSet(Fcntl, "OpenFlags");
55+
loadConstantSet(context, Fcntl, "Fcntl");
56+
loadConstantSet(context, Fcntl, "OpenFlags");
57+
}
58+
59+
/**
60+
* Define all constants from the named jnr-constants set which are defined on the current platform.
61+
*
62+
* @param module the module in which we want to define the constants
63+
* @param constantSetName the name of the constant set from which to get the constants
64+
*/
65+
private static void loadConstantSet(ThreadContext context, RubyModule module, String constantSetName) {
66+
for (Constant c : ConstantSet.getConstantSet(constantSetName)) {
67+
if (c.defined() && Character.isUpperCase(c.name().charAt(0))) {
68+
module.defineConstant(context, c.name(), asFixnum(context, c.intValue()));
69+
}
70+
}
5371
}
5472
}

core/src/main/java/org/jruby/ext/ffi/Factory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void init(Ruby runtime, RubyModule FFI) {
128128
FileDescriptorIO.createFileDescriptorIOClass(context, FFI, ioClass(context));
129129
}
130130

131-
FFI.setConstant("TypeDefs", newHash(context));
131+
FFI.defineConstant(context, "TypeDefs", newHash(context));
132132

133133
Platform.createPlatformModule(context, FFI);
134134
IOModule.createIOModule(context, FFI);

core/src/main/java/org/jruby/ext/ffi/Pointer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public boolean isKindOf(IRubyObject obj, RubyModule type) {
4444

4545
// Add Pointer::NULL as a constant
4646
Pointer nullPointer = new Pointer(context.runtime, _Pointer, new NullMemoryIO(context.runtime));
47-
_Pointer.setConstant("NULL", nullPointer);
47+
_Pointer.defineConstant(context, "NULL", nullPointer);
4848

4949
var NilClass = nilClass(context);
5050
NilClass.addMethod("to_ptr", new NilToPointerMethod(NilClass, nullPointer, "to_ptr"));

core/src/main/java/org/jruby/ext/ffi/Type.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ private static final void defineBuiltinType(ThreadContext context, RubyClass bui
8585
try {
8686
if (names.length > 0) {
8787
for (String n : names) {
88-
builtinClass.setConstant(n.toUpperCase(LOCALE),
88+
builtinClass.defineConstant(context, n.toUpperCase(LOCALE),
8989
new Builtin(context, builtinClass, nativeType, n.toLowerCase(LOCALE)));
9090
}
9191
} else {
92-
builtinClass.setConstant(nativeType.name(),
92+
builtinClass.defineConstant(context, nativeType.name(),
9393
new Builtin(context, builtinClass, nativeType, nativeType.name().toLowerCase(LOCALE)));
9494
}
9595
} catch (UnsupportedOperationException ex) {

core/src/main/java/org/jruby/ext/rbconfig/RbConfigLibrary.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,12 @@ public static String getSysConfDir(Ruby runtime) {
227227
public void load(Ruby runtime, boolean wrap) {
228228
ThreadContext context = runtime.getCurrentContext();
229229

230-
final RubyModule rbConfig = defineModule(context, "RbConfig");
231-
230+
RubyString destDir = newEmptyString(context);
232231
normalizedHome = getNormalizedHome(runtime);
233232

234-
// Ruby installed directory.
235-
rbConfig.setConstant("TOPDIR", newString(context, normalizedHome));
236-
RubyString destDir = newEmptyString(context);
237-
// DESTDIR on make install.
238-
rbConfig.setConstant("DESTDIR", destDir);
233+
final RubyModule rbConfig = defineModule(context, "RbConfig").
234+
defineConstant(context, "TOPDIR", newString(context, normalizedHome)). // Ruby installed directory.
235+
defineConstant(context, "DESTDIR", destDir); // DESTDIR on make install.
239236

240237
// The hash configurations stored.
241238
final RubyHash CONFIG = new RubyHash(runtime, 48);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy