diff --git a/src/org/jruby/RubyModule.java b/src/org/jruby/RubyModule.java
index 567862b..b83cd5c 100644
--- a/src/org/jruby/RubyModule.java
+++ b/src/org/jruby/RubyModule.java
@@ -2785,33 +2785,53 @@ public class RubyModule extends RubyObject {
return getConstantNoConstMissing(name, true);
}
+ public boolean getConstantDefined(String name) {
+ assert IdUtil.isConstant(name);
+
+ IRubyObject constant = iterateConstantNoConstMissing(name, this, true, false);
+
+ if (constant == null && !isClass()) {
+ constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), true, false);
+ }
+
+ return constant != null;
+ }
+
public IRubyObject getConstantNoConstMissing(String name, boolean inherit) {
assert IdUtil.isConstant(name);
- IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit);
+ IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit, true);
if (constant == null && !isClass()) {
- constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit);
+ constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit, true);
}
return constant;
}
- private IRubyObject iterateConstantNoConstMissing(String name, RubyModule init, boolean inherit) {
+ private IRubyObject iterateConstantNoConstMissing(String name, RubyModule init, boolean inherit, boolean autoload) {
for (RubyModule p = init; p != null; p = p.getSuperClass()) {
IRubyObject value = p.getConstantInner(name);
- if (value != null) return value == UNDEF ? null : value;
+ if (value != null) {
+ if (autoload) return value == UNDEF ? null : value;
+ }
if (!inherit) break;
}
return null;
}
protected IRubyObject getConstantInner(String name) {
+ return getConstantInner(name, true);
+ }
+
+ protected IRubyObject getConstantInner(String name, boolean autoload) {
IRubyObject value = constantTableFetch(name);
- for (; value == UNDEF; value = constantTableFetch(name)) {
- if (resolveUndefConstant(getRuntime(), name) == null) return UNDEF;
+ if (autoload) {
+ for (; value == UNDEF; value = constantTableFetch(name)) {
+ if (resolveUndefConstant(getRuntime(), name) == null) return UNDEF;
+ }
}
return value;
diff --git a/src/org/jruby/parser/StaticScope.java b/src/org/jruby/parser/StaticScope.java
index 62d6600..61a33bd 100644
--- a/src/org/jruby/parser/StaticScope.java
+++ b/src/org/jruby/parser/StaticScope.java
@@ -171,41 +171,48 @@ public abstract class StaticScope implements Serializable {
/* Note: Only used by compiler until it can use getConstant again or use some other refactoring */
public IRubyObject getConstantWithConstMissing(Ruby runtime, String internedName, RubyModule object) {
- IRubyObject result = getConstantInner(runtime, internedName, object);
+ IRubyObject result = getConstantInner(runtime, internedName, object, true);
// If we could not find the constant from cref..then try getting from inheritence hierarchy
return result == null ? cref.fastGetConstant(internedName) : result;
}
public IRubyObject getConstant(Ruby runtime, String internedName, RubyModule object) {
- IRubyObject result = getConstantInner(runtime, internedName, object);
+ IRubyObject result = getConstantInner(runtime, internedName, object, true);
// If we could not find the constant from cref..then try getting from inheritence hierarchy
return result == null ? cref.getConstantNoConstMissing(internedName) : result;
}
+
+ public boolean getConstantDefined(Ruby runtime, String internedName, RubyModule object) {
+ IRubyObject result = getConstantInner(runtime, internedName, object, false);
+
+ // If we could not find the constant from cref..then try getting from inheritence hierarchy
+ return result != null || cref.getConstantDefined(internedName);
+ }
- private IRubyObject getConstantInner(Ruby runtime, String internedName, RubyModule object) {
+ private IRubyObject getConstantInner(Ruby runtime, String internedName, RubyModule object, boolean autoload) {
IRubyObject result = cref.fastFetchConstant(internedName);
if (result != null) {
- if (result == RubyObject.UNDEF) return getUndefConstant(runtime, internedName, object);
+ if (autoload && result == RubyObject.UNDEF) return getUndefConstant(runtime, internedName, object);
return result;
}
- return previousCRefScope == null ? null : previousCRefScope.getConstantInnerNoObject(runtime, internedName, object);
+ return previousCRefScope == null ? null : previousCRefScope.getConstantInnerNoObject(runtime, internedName, object, autoload);
}
- private IRubyObject getConstantInnerNoObject(Ruby runtime, String internedName, RubyModule object) {
+ private IRubyObject getConstantInnerNoObject(Ruby runtime, String internedName, RubyModule object, boolean autoload) {
if (previousCRefScope == null) return null;
- return getConstantInner(runtime, internedName, object);
+ return getConstantInner(runtime, internedName, object, autoload);
}
/* Try and unload the autoload specified from internedName */
private IRubyObject getUndefConstant(Ruby runtime, String internedName, RubyModule object) {
if (cref.resolveUndefConstant(runtime, internedName) == null) return null;
- return getConstantInner(runtime, internedName, object);
+ return getConstantInner(runtime, internedName, object, true);
}
/**
diff --git a/src/org/jruby/runtime/ThreadContext.java b/src/org/jruby/runtime/ThreadContext.java
index f99ee4d..7a78f30 100644
--- a/src/org/jruby/runtime/ThreadContext.java
+++ b/src/org/jruby/runtime/ThreadContext.java
@@ -618,9 +618,7 @@ public final class ThreadContext {
}
public boolean getConstantDefined(String internedName) {
- IRubyObject value = getConstant(internedName);
-
- return value != null;
+ return getCurrentScope().getStaticScope().getConstantDefined(runtime, internedName, runtime.getObject());
}
/**
Fairly simple fix:
diff --git a/src/org/jruby/RubyModule.java b/src/org/jruby/RubyModule.java index 567862b..b83cd5c 100644 --- a/src/org/jruby/RubyModule.java +++ b/src/org/jruby/RubyModule.java @@ -2785,33 +2785,53 @@ public class RubyModule extends RubyObject { return getConstantNoConstMissing(name, true); } + public boolean getConstantDefined(String name) { + assert IdUtil.isConstant(name); + + IRubyObject constant = iterateConstantNoConstMissing(name, this, true, false); + + if (constant == null && !isClass()) { + constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), true, false); + } + + return constant != null; + } + public IRubyObject getConstantNoConstMissing(String name, boolean inherit) { assert IdUtil.isConstant(name); - IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit); + IRubyObject constant = iterateConstantNoConstMissing(name, this, inherit, true); if (constant == null && !isClass()) { - constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit); + constant = iterateConstantNoConstMissing(name, getRuntime().getObject(), inherit, true); } return constant; } - private IRubyObject iterateConstantNoConstMissing(String name, RubyModule init, boolean inherit) { + private IRubyObject iterateConstantNoConstMissing(String name, RubyModule init, boolean inherit, boolean autoload) { for (RubyModule p = init; p != null; p = p.getSuperClass()) { IRubyObject value = p.getConstantInner(name); - if (value != null) return value == UNDEF ? null : value; + if (value != null) { + if (autoload) return value == UNDEF ? null : value; + } if (!inherit) break; } return null; } protected IRubyObject getConstantInner(String name) { + return getConstantInner(name, true); + } + + protected IRubyObject getConstantInner(String name, boolean autoload) { IRubyObject value = constantTableFetch(name); - for (; value == UNDEF; value = constantTableFetch(name)) { - if (resolveUndefConstant(getRuntime(), name) == null) return UNDEF; + if (autoload) { + for (; value == UNDEF; value = constantTableFetch(name)) { + if (resolveUndefConstant(getRuntime(), name) == null) return UNDEF; + } } return value; diff --git a/src/org/jruby/parser/StaticScope.java b/src/org/jruby/parser/StaticScope.java index 62d6600..61a33bd 100644 --- a/src/org/jruby/parser/StaticScope.java +++ b/src/org/jruby/parser/StaticScope.java @@ -171,41 +171,48 @@ public abstract class StaticScope implements Serializable { /* Note: Only used by compiler until it can use getConstant again or use some other refactoring */ public IRubyObject getConstantWithConstMissing(Ruby runtime, String internedName, RubyModule object) { - IRubyObject result = getConstantInner(runtime, internedName, object); + IRubyObject result = getConstantInner(runtime, internedName, object, true); // If we could not find the constant from cref..then try getting from inheritence hierarchy return result == null ? cref.fastGetConstant(internedName) : result; } public IRubyObject getConstant(Ruby runtime, String internedName, RubyModule object) { - IRubyObject result = getConstantInner(runtime, internedName, object); + IRubyObject result = getConstantInner(runtime, internedName, object, true); // If we could not find the constant from cref..then try getting from inheritence hierarchy return result == null ? cref.getConstantNoConstMissing(internedName) : result; } + + public boolean getConstantDefined(Ruby runtime, String internedName, RubyModule object) { + IRubyObject result = getConstantInner(runtime, internedName, object, false); + + // If we could not find the constant from cref..then try getting from inheritence hierarchy + return result != null || cref.getConstantDefined(internedName); + } - private IRubyObject getConstantInner(Ruby runtime, String internedName, RubyModule object) { + private IRubyObject getConstantInner(Ruby runtime, String internedName, RubyModule object, boolean autoload) { IRubyObject result = cref.fastFetchConstant(internedName); if (result != null) { - if (result == RubyObject.UNDEF) return getUndefConstant(runtime, internedName, object); + if (autoload && result == RubyObject.UNDEF) return getUndefConstant(runtime, internedName, object); return result; } - return previousCRefScope == null ? null : previousCRefScope.getConstantInnerNoObject(runtime, internedName, object); + return previousCRefScope == null ? null : previousCRefScope.getConstantInnerNoObject(runtime, internedName, object, autoload); } - private IRubyObject getConstantInnerNoObject(Ruby runtime, String internedName, RubyModule object) { + private IRubyObject getConstantInnerNoObject(Ruby runtime, String internedName, RubyModule object, boolean autoload) { if (previousCRefScope == null) return null; - return getConstantInner(runtime, internedName, object); + return getConstantInner(runtime, internedName, object, autoload); } /* Try and unload the autoload specified from internedName */ private IRubyObject getUndefConstant(Ruby runtime, String internedName, RubyModule object) { if (cref.resolveUndefConstant(runtime, internedName) == null) return null; - return getConstantInner(runtime, internedName, object); + return getConstantInner(runtime, internedName, object, true); } /** diff --git a/src/org/jruby/runtime/ThreadContext.java b/src/org/jruby/runtime/ThreadContext.java index f99ee4d..7a78f30 100644 --- a/src/org/jruby/runtime/ThreadContext.java +++ b/src/org/jruby/runtime/ThreadContext.java @@ -618,9 +618,7 @@ public final class ThreadContext { } public boolean getConstantDefined(String internedName) { - IRubyObject value = getConstant(internedName); - - return value != null; + return getCurrentScope().getStaticScope().getConstantDefined(runtime, internedName, runtime.getObject()); } /**Can't apply it right now because I'm seeing a test/test_load.rb failure on master with or without it...