Browse files

Simplify constant and ivar defined? logic in the compiler.

  • Loading branch information...
1 parent 2998175 commit 970afc1e4c8cb394d82439a9ec0ff391024b71c6 @headius headius committed with Prathamesh Sonpatki Sep 17, 2012
View
12 src/org/jruby/ast/executable/AbstractScript.java
@@ -310,6 +310,18 @@ public IRubyObject run(ThreadContext context, IRubyObject self, IRubyObject[] ar
public final IRubyObject getConstant8(ThreadContext context, String name) {return runtimeCache.getConstant(context, name, 8);}
public final IRubyObject getConstant9(ThreadContext context, String name) {return runtimeCache.getConstant(context, name, 9);}
+ public final IRubyObject getConstantDefined(ThreadContext context, String name, int i) {return runtimeCache.getConstantDefined(context, name, i);}
+ public final IRubyObject getConstantDefined0(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 0);}
+ public final IRubyObject getConstantDefined1(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 1);}
+ public final IRubyObject getConstantDefined2(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 2);}
+ public final IRubyObject getConstantDefined3(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 3);}
+ public final IRubyObject getConstantDefined4(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 4);}
+ public final IRubyObject getConstantDefined5(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 5);}
+ public final IRubyObject getConstantDefined6(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 6);}
+ public final IRubyObject getConstantDefined7(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 7);}
+ public final IRubyObject getConstantDefined8(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 8);}
+ public final IRubyObject getConstantDefined9(ThreadContext context, String name) {return runtimeCache.getConstantDefined(context, name, 9);}
+
public static final int NUMBERED_CONSTANTFROM_COUNT = 10;
public final IRubyObject getConstantFrom(RubyModule target, ThreadContext context, String name, int i) {return runtimeCache.getConstantFrom(target, context, name, i);}
View
8 src/org/jruby/ast/executable/RuntimeCache.java
@@ -26,6 +26,7 @@
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.CacheEntry;
import org.jruby.util.ByteList;
+import org.jruby.util.DefinedMessage;
import org.jruby.util.RegexpOptions;
public class RuntimeCache {
@@ -186,7 +187,7 @@ public final IRubyObject getVariable(ThreadContext context, int index, String na
}
public final IRubyObject getVariableDefined(ThreadContext context, int index, String name, IRubyObject object) {
- return getValue(context, index, name, object);
+ return getValue(context, index, name, object) == null ? null : context.runtime.getDefinedMessage(DefinedMessage.INSTANCE_VARIABLE);
}
private final IRubyObject getValue(ThreadContext context, int index, String name, IRubyObject object) {
@@ -399,6 +400,11 @@ public final IRubyObject getConstant(ThreadContext context, String name, int ind
return value != null ? value : context.getCurrentScope().getStaticScope().getModule().callMethod(context, "const_missing", context.runtime.fastNewSymbol(name));
}
+ public final IRubyObject getConstantDefined(ThreadContext context, String name, int index) {
+ IRubyObject value = getValue(context, name, index);
+ return value == null ? null : context.runtime.getDefinedMessage(DefinedMessage.CONSTANT);
+ }
+
public IRubyObject getValue(ThreadContext context, String name, int index) {
IRubyObject value = constants[index]; // Store to temp so it does null out on us mid-stream
return isCached(context, value, index) ? value : reCache(context, name, index);
View
28 src/org/jruby/compiler/ASTCompiler.java
@@ -1549,34 +1549,10 @@ public void branch(BodyCompiler context) {
});
break;
case INSTVARNODE:
- context.isInstanceVariableDefined(((InstVarNode) node).getName(),
- new BranchCallback() {
-
- public void branch(BodyCompiler context) {
- context.pushDefinedMessage(DefinedMessage.INSTANCE_VARIABLE);
- }
- },
- new BranchCallback() {
-
- public void branch(BodyCompiler context) {
- context.pushNull();
- }
- });
+ context.isInstanceVariableDefined(((InstVarNode) node).getName());
break;
case CONSTNODE:
- context.isConstantDefined(((ConstNode) node).getName(),
- new BranchCallback() {
-
- public void branch(BodyCompiler context) {
- context.pushDefinedMessage(DefinedMessage.CONSTANT);
- }
- },
- new BranchCallback() {
-
- public void branch(BodyCompiler context) {
- context.pushNull();
- }
- });
+ context.isConstantDefined(((ConstNode) node).getName());
break;
case FCALLNODE:
context.loadSelf();
View
4 src/org/jruby/compiler/BodyCompiler.java
@@ -571,8 +571,8 @@ public void defineNewMethod(String name, int methodArity, StaticScope scope,
public void isMethodBound(String name, BranchCallback trueBranch, BranchCallback falseBranch);
public void hasBlock(BranchCallback trueBranch, BranchCallback falseBranch);
public void isGlobalDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
- public void isConstantDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
- public void isInstanceVariableDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
+ public void isConstantDefined(String name);
+ public void isInstanceVariableDefined(String name);
public void isClassVarDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch);
public Object getNewEnding();
public void ifNull(Object gotoToken);
View
2 src/org/jruby/compiler/CacheCompiler.java
@@ -56,6 +56,8 @@
public void cacheConstant(BaseBodyCompiler method, String constantName);
+ public void cacheConstantDefined(BaseBodyCompiler method, String constantName);
+
public void cacheConstantFrom(BaseBodyCompiler method, String constantName);
public int cacheStaticScope(BaseBodyCompiler method, StaticScope scope);
View
32 src/org/jruby/compiler/impl/BaseBodyCompiler.java
@@ -1448,12 +1448,8 @@ public void pollThreadEvents() {
}
public void nullToNil() {
- Label notNull = new Label();
- method.dup();
- method.ifnonnull(notNull);
- method.pop();
- loadNil();
- method.label(notNull);
+ loadThreadContext();
+ invokeUtilityMethod("nullToNil", sig(IRubyObject.class, IRubyObject.class, ThreadContext.class));
}
public void isInstanceOf(Class clazz, BranchCallback trueBranch, BranchCallback falseBranch) {
@@ -1960,30 +1956,12 @@ public void isGlobalDefined(String name, BranchCallback trueBranch, BranchCallba
method.label(exitLabel);
}
- public void isConstantDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch) {
- loadThreadContext();
- method.ldc(name);
- invokeThreadContext("getConstantDefined", sig(boolean.class, params(String.class)));
- Label falseLabel = new Label();
- Label exitLabel = new Label();
- method.ifeq(falseLabel); // EQ == 0 (i.e. false)
- trueBranch.branch(this);
- method.go_to(exitLabel);
- method.label(falseLabel);
- falseBranch.branch(this);
- method.label(exitLabel);
+ public void isConstantDefined(String name) {
+ script.getCacheCompiler().cacheConstantDefined(this, name);
}
- public void isInstanceVariableDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch) {
+ public void isInstanceVariableDefined(String name) {
script.getCacheCompiler().cachedGetVariableDefined(this, name);
- Label trueLabel = new Label();
- Label exitLabel = new Label();
- method.ifnonnull(trueLabel);
- falseBranch.branch(this);
- method.go_to(exitLabel);
- method.label(trueLabel);
- trueBranch.branch(this);
- method.label(exitLabel);
}
public void isClassVarDefined(String name, BranchCallback trueBranch, BranchCallback falseBranch) {
View
14 src/org/jruby/compiler/impl/InheritedCacheCompiler.java
@@ -315,6 +315,20 @@ public void cacheConstant(BaseBodyCompiler method, String constantName) {
inheritedConstantCount++;
}
+ public void cacheConstantDefined(BaseBodyCompiler method, String constantName) {
+ method.loadThis();
+ method.loadThreadContext();
+ method.method.ldc(constantName);
+ if (inheritedConstantCount < AbstractScript.NUMBERED_CONSTANT_COUNT) {
+ method.method.invokevirtual(scriptCompiler.getClassname(), "getConstantDefined" + inheritedConstantCount, sig(IRubyObject.class, ThreadContext.class, String.class));
+ } else {
+ method.method.pushInt(inheritedConstantCount);
+ method.method.invokevirtual(scriptCompiler.getClassname(), "getConstantDefined", sig(IRubyObject.class, ThreadContext.class, String.class, int.class));
+ }
+
+ inheritedConstantCount++;
+ }
+
public void cacheConstantFrom(BaseBodyCompiler method, String constantName) {
// module is on top of stack
method.loadThis();
View
4 src/org/jruby/javasupport/util/RuntimeHelpers.java
@@ -684,6 +684,10 @@ public static IRubyObject getConstant(ThreadContext context, String internedName
return context.getCurrentScope().getStaticScope().getConstantWithConstMissing(runtime, internedName, runtime.getObject());
}
+
+ public static IRubyObject nullToNil(IRubyObject value, ThreadContext context) {
+ return value != null ? value : context.nil;
+ }
public static IRubyObject nullToNil(IRubyObject value, Ruby runtime) {
return value != null ? value : runtime.getNil();

0 comments on commit 970afc1

Please sign in to comment.