Skip to content
Browse files

Fix defined?(@foo) when @foo has been assigned nil.

  • Loading branch information...
1 parent a914eb9 commit 2998175707b5efa6eb04a647b2ae6498b9f26d18 @headius headius committed with Prathamesh Sonpatki Sep 17, 2012
View
13 src/org/jruby/ast/InstVarNode.java
@@ -103,6 +103,13 @@ public IRubyObject interpret(Ruby runtime, ThreadContext context, IRubyObject se
}
private IRubyObject getVariable(Ruby runtime, IRubyObject self, boolean warn) {
+ IRubyObject value = getValue(runtime, self);
+ if (value != null) return value;
+ if (warn && runtime.isVerbose()) warnAboutUninitializedIvar(runtime);
+ return runtime.getNil();
+ }
+
+ private IRubyObject getValue(Ruby runtime, IRubyObject self) {
RubyClass cls = self.getMetaClass().getRealClass();
VariableAccessor localAccessor = accessor;
IRubyObject value;
@@ -114,9 +121,7 @@ private IRubyObject getVariable(Ruby runtime, IRubyObject self, boolean warn) {
} else {
value = (IRubyObject)localAccessor.get(self);
}
- if (value != null) return value;
- if (warn && runtime.isVerbose()) warnAboutUninitializedIvar(runtime);
- return runtime.getNil();
+ return value;
}
private void warnAboutUninitializedIvar(Ruby runtime) {
@@ -126,6 +131,6 @@ private void warnAboutUninitializedIvar(Ruby runtime) {
@Override
public RubyString definition(Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
- return getVariable(runtime, self, false).isNil() ? null : runtime.getDefinedMessage(DefinedMessage.INSTANCE_VARIABLE);
+ return getValue(runtime, self) == null ? null : runtime.getDefinedMessage(DefinedMessage.INSTANCE_VARIABLE);
}
}
View
23 src/org/jruby/ast/executable/RuntimeCache.java
@@ -175,28 +175,27 @@ public final BigInteger getBigInteger(int index, String pattern) {
}
public final IRubyObject getVariable(ThreadContext context, int index, String name, IRubyObject object) {
- return getVariable(context, index, name, object, true);
+ IRubyObject value = getValue(context, index, name, object);
+ if (value != null) return value;
+
+ Ruby runtime = context.runtime;
+ if (runtime.isVerbose()) {
+ warnAboutUninitializedIvar(runtime, name);
+ }
+ return runtime.getNil();
}
public final IRubyObject getVariableDefined(ThreadContext context, int index, String name, IRubyObject object) {
- return getVariable(context, index, name, object, false);
+ return getValue(context, index, name, object);
}
- private final IRubyObject getVariable(ThreadContext context, int index, String name, IRubyObject object, boolean warn) {
+ private final IRubyObject getValue(ThreadContext context, int index, String name, IRubyObject object) {
VariableAccessor variableAccessor = variableReaders[index];
RubyClass cls = object.getMetaClass().getRealClass();
if (variableAccessor.getClassId() != cls.hashCode()) {
variableReaders[index] = variableAccessor = cls.getVariableAccessorForRead(name);
}
- IRubyObject value = (IRubyObject) variableAccessor.get(object);
- if (value != null) {
- return value;
- }
- Ruby runtime = context.runtime;
- if (warn && runtime.isVerbose()) {
- warnAboutUninitializedIvar(runtime, name);
- }
- return runtime.getNil();
+ return (IRubyObject)variableAccessor.get(object);
}
private void warnAboutUninitializedIvar(Ruby runtime, String name) {
View
3 src/org/jruby/compiler/impl/BaseBodyCompiler.java
@@ -1976,10 +1976,9 @@ public void isConstantDefined(String name, BranchCallback trueBranch, BranchCall
public void isInstanceVariableDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch) {
script.getCacheCompiler().cachedGetVariableDefined(this, name);
- method.invokeinterface(p(IRubyObject.class), "isNil", sig(boolean.class));
Label trueLabel = new Label();
Label exitLabel = new Label();
- method.iffalse(trueLabel);
+ method.ifnonnull(trueLabel);
falseBranch.branch(this);
method.go_to(exitLabel);
method.label(trueLabel);

0 comments on commit 2998175

Please sign in to comment.
Something went wrong with that request. Please try again.