Skip to content

Commit

Permalink
[Truffle] Move RubyConstant up to the top level.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Sep 27, 2014
1 parent f67e618 commit 49b2db7
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
package org.jruby.truffle.nodes.constants;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.*;
import org.jruby.truffle.nodes.*;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.control.*;
import org.jruby.truffle.runtime.core.*;
Expand Down Expand Up @@ -59,7 +56,7 @@ public Object execute(RubyBasicObject receiver) {

final RubyContext context = receiver.getRubyClass().getContext();

RubyModule.RubyConstant constant;
RubyConstant constant;

constant = receiver.getLookupNode().lookupConstant(name);

Expand All @@ -77,11 +74,11 @@ public Object execute(RubyBasicObject receiver) {
throw new RaiseException(context.getCoreLibrary().nameErrorUninitializedConstant(name, this));
}

replace(new CachedReadConstantNode(receiver.getRubyClass(), constant.value, this));
replace(new CachedReadConstantNode(receiver.getRubyClass(), constant.getValue(), this));

assert RubyContext.shouldObjectBeVisible(constant.value);
assert RubyContext.shouldObjectBeVisible(constant.getValue());

return constant.value;
return constant.getValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private static void addMethod(RubyClass rubyObjectClass, MethodDetails methodDet
if (methodDetails.getClassAnnotation().name().equals("main")) {
module = context.getCoreLibrary().getMainObject().getSingletonClass(null);
} else {
module = (RubyModule) rubyObjectClass.lookupConstant(methodDetails.getClassAnnotation().name()).value;
module = (RubyModule) rubyObjectClass.lookupConstant(methodDetails.getClassAnnotation().name()).getValue();
}

assert module != null : methodDetails.getClassAnnotation().name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private static void exec(RubyContext context, String[] commandLine) {
final ProcessBuilder builder = new ProcessBuilder(commandLine);
builder.inheritIO();

final RubyHash env = (RubyHash) context.getCoreLibrary().getObjectClass().lookupConstant("ENV").value;
final RubyHash env = (RubyHash) context.getCoreLibrary().getObjectClass().lookupConstant("ENV").getValue();

// TODO(CS): cast
for (Map.Entry<Object, Object> entry : ((LinkedHashMap<Object, Object>) env.getStore()).entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import java.util.List;
import java.util.Map;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.*;
import org.jruby.truffle.nodes.*;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.core.RubyHash;
Expand All @@ -42,7 +40,7 @@ public Object execute(VirtualFrame frame) {

final RubyContext context = getContext();

final RubyHash env = (RubyHash) getContext().getCoreLibrary().getObjectClass().lookupConstant("ENV").value;
final RubyHash env = (RubyHash) getContext().getCoreLibrary().getObjectClass().lookupConstant("ENV").getValue();

final List<String> envp = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package org.jruby.truffle.nodes.objects;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
Expand Down Expand Up @@ -50,7 +49,7 @@ public Object execute(VirtualFrame frame) {

// Look for a current definition of the class, or create a new one

final RubyModule.RubyConstant constant = parentModuleObject.getConstants().get(name);
final RubyConstant constant = parentModuleObject.getConstants().get(name);

RubyClass definingClass;
RubyClass superClassObject = getRubySuperClass(frame, context);
Expand All @@ -67,12 +66,12 @@ public Object execute(VirtualFrame frame) {
parentModuleObject.setConstant(this, name, definingClass);
parentModuleObject.getSingletonClass(this).setConstant(this, name, definingClass);
} else {
if (constant.value instanceof RubyClass) {
definingClass = (RubyClass) constant.value;
if (constant.getValue() instanceof RubyClass) {
definingClass = (RubyClass) constant.getValue();
checkSuperClassCompatibility(context, superClassObject, definingClass);

} else {
throw new RaiseException(context.getCoreLibrary().typeErrorIsNotA(constant.value.toString(), "class", this));
throw new RaiseException(context.getCoreLibrary().typeErrorIsNotA(constant.getValue().toString(), "class", this));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
*/
package org.jruby.truffle.nodes.objects;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import org.jruby.truffle.nodes.*;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.backtrace.Backtrace;
import org.jruby.truffle.runtime.control.*;
import org.jruby.truffle.runtime.core.*;

Expand Down Expand Up @@ -49,7 +47,7 @@ public Object execute(VirtualFrame frame) {
throw new RaiseException(context.getCoreLibrary().typeErrorIsNotA(e.getResult().toString(), "module", this));
}

final RubyModule.RubyConstant constantValue = parentModuleObject.getConstants().get(name);
final RubyConstant constantValue = parentModuleObject.getConstants().get(name);

RubyModule definingModule;

Expand All @@ -58,8 +56,8 @@ public Object execute(VirtualFrame frame) {
parentModuleObject.setConstant(this, name, definingModule);
parentModuleObject.getSingletonClass(this).setConstant(this, name, definingModule);
} else {
if (constantValue.value == getContext().getCoreLibrary().getModuleClass() || (constantValue.value instanceof RubyModule && !(constantValue.value instanceof RubyClass))) {
definingModule = (RubyModule) constantValue.value;
if (constantValue.getValue() == getContext().getCoreLibrary().getModuleClass() || (constantValue.getValue() instanceof RubyModule && !(constantValue.getValue() instanceof RubyClass))) {
definingModule = (RubyModule) constantValue.getValue();
} else {
throw new RaiseException(context.getCoreLibrary().typeErrorIsNotA(name, "module", this));
}
Expand Down
34 changes: 34 additions & 0 deletions core/src/main/java/org/jruby/truffle/runtime/RubyConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime;

public class RubyConstant {

private final Object value;
private boolean isPrivate;

public RubyConstant(Object value, boolean isPrivate) {
this.value = value;
this.isPrivate = isPrivate;
}

public Object getValue() {
return value;
}

public boolean isPrivate() {
return isPrivate;
}

public void setPrivate(boolean isPrivate) {
this.isPrivate = isPrivate;
}

}
18 changes: 4 additions & 14 deletions core/src/main/java/org/jruby/truffle/runtime/core/RubyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.oracle.truffle.api.utilities.CyclicAssumption;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyConstant;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.lookup.LookupFork;
Expand Down Expand Up @@ -284,7 +285,7 @@ public void changeConstantVisibility(RubyNode currentNode, RubySymbol constant,
checkFrozen(currentNode);

if (rubyConstant != null) {
rubyConstant.isPrivate = isPrivate;
rubyConstant.setPrivate(isPrivate);
} else {
throw new RaiseException(context.getCoreLibrary().nameErrorUninitializedConstant(constant.toString(), currentNode));
}
Expand Down Expand Up @@ -349,7 +350,7 @@ public void appendFeatures(RubyNode currentNode, RubyModule other) {

for (Map.Entry<String, RubyConstant> constantEntry : getConstants().entrySet()) {
final String constantName = constantEntry.getKey();
final Object constantValue = constantEntry.getValue().value;
final Object constantValue = constantEntry.getValue().getValue();
other.setModuleConstant(currentNode, constantName, constantValue);
}

Expand Down Expand Up @@ -482,21 +483,10 @@ public Map<String, RubyMethod> getMethods() {
return methods;
}

public static class RubyConstant {
public final Object value;
public boolean isPrivate;

public RubyConstant(Object value, boolean isPrivate) {
this.value = value;
this.isPrivate = isPrivate;
}

}

@Override
public void visitObjectGraphChildren(ObjectSpaceManager.ObjectGraphVisitor visitor) {
for (RubyConstant constant : constants.values()) {
getRubyClass().getContext().getCoreLibrary().box(constant.value).visitObjectGraph(visitor);
getRubyClass().getContext().getCoreLibrary().box(constant.getValue()).visitObjectGraph(visitor);
}

for (RubyMethod method : methods.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import java.util.*;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.utilities.*;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.core.RubyModule;
import org.jruby.truffle.runtime.RubyConstant;
import org.jruby.truffle.runtime.methods.*;

/**
Expand All @@ -40,8 +39,8 @@ public boolean setClassVariableIfAlreadySet(RubyNode currentNode, String variabl
}

@Override
public RubyModule.RubyConstant lookupConstant(String constantName) {
final RubyModule.RubyConstant firstResult = first.lookupConstant(constantName);
public RubyConstant lookupConstant(String constantName) {
final RubyConstant firstResult = first.lookupConstant(constantName);

if (firstResult != null) {
return firstResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.oracle.truffle.api.*;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.core.RubyModule;
import org.jruby.truffle.runtime.RubyConstant;
import org.jruby.truffle.runtime.methods.*;

/**
Expand All @@ -24,7 +24,7 @@ public interface LookupNode {

boolean setClassVariableIfAlreadySet(RubyNode currentNode, String variableName, Object value);

RubyModule.RubyConstant lookupConstant(String constantName);
RubyConstant lookupConstant(String constantName);

Object lookupClassVariable(String variableName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import java.util.*;

import com.oracle.truffle.api.*;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.utilities.*;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.core.RubyModule;
import org.jruby.truffle.runtime.RubyConstant;
import org.jruby.truffle.runtime.methods.*;

/**
Expand All @@ -30,7 +29,7 @@ public boolean setClassVariableIfAlreadySet(RubyNode currentNode, String variabl
}

@Override
public RubyModule.RubyConstant lookupConstant(String constantName) {
public RubyConstant lookupConstant(String constantName) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import java.util.Arrays;

import com.oracle.truffle.api.source.Source;
import org.jruby.common.IRubyWarnings;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.control.*;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyModule;
import org.jruby.util.cli.Options;

/**
Expand All @@ -36,7 +34,7 @@ public FeatureManager(RubyContext context) {
}

public boolean require(String feature, RubyNode currentNode) throws IOException {
final RubyModule.RubyConstant dataConstantBefore = context.getCoreLibrary().getObjectClass().lookupConstant("DATA");
final RubyConstant dataConstantBefore = context.getCoreLibrary().getObjectClass().lookupConstant("DATA");

try {
// Some features are handled specially
Expand Down Expand Up @@ -97,7 +95,7 @@ public boolean require(String feature, RubyNode currentNode) throws IOException
if (dataConstantBefore == null) {
context.getCoreLibrary().getObjectClass().removeConstant(currentNode, "DATA");
} else {
context.getCoreLibrary().getObjectClass().setConstant(currentNode, "DATA", dataConstantBefore.value);
context.getCoreLibrary().getObjectClass().setConstant(currentNode, "DATA", dataConstantBefore.getValue());
}
}
}
Expand Down

0 comments on commit 49b2db7

Please sign in to comment.