Skip to content

Commit

Permalink
Showing 9 changed files with 81 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -365,7 +365,7 @@ public RubyBasicObject copy(VirtualFrame frame, RubyBasicObject self) {

final RubyBasicObject newObject = self.getLogicalClass().allocate(this);

newObject.getObjectType().setInstanceVariables(newObject, self.getObjectType().getInstanceVariables(self));
RubyBasicObject.setInstanceVariables(newObject, RubyBasicObject.getInstanceVariables(self));

return newObject;
}
@@ -879,14 +879,14 @@ public InstanceVariableSetNode(RubyContext context, SourceSection sourceSection)
@TruffleBoundary
@Specialization
public Object instanceVariableSet(RubyBasicObject object, RubyString name, Object value) {
object.getObjectType().setInstanceVariable(object, RubyContext.checkInstanceVariableName(getContext(), name.toString(), this), value);
RubyBasicObject.setInstanceVariable(object, RubyContext.checkInstanceVariableName(getContext(), name.toString(), this), value);
return value;
}

@TruffleBoundary
@Specialization
public Object instanceVariableSet(RubyBasicObject object, RubySymbol name, Object value) {
object.getObjectType().setInstanceVariable(object, RubyContext.checkInstanceVariableName(getContext(), name.toString(), this), value);
RubyBasicObject.setInstanceVariable(object, RubyContext.checkInstanceVariableName(getContext(), name.toString(), this), value);
return value;
}

@@ -903,7 +903,7 @@ public InstanceVariablesNode(RubyContext context, SourceSection sourceSection) {
public RubyArray instanceVariables(RubyBasicObject self) {
CompilerDirectives.transferToInterpreter();

final Object[] instanceVariableNames = self.getObjectType().getFieldNames(self);
final Object[] instanceVariableNames = RubyBasicObject.getFieldNames(self);

Arrays.sort(instanceVariableNames);

Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ private Object handleException(VirtualFrame frame, RaiseException exception) {
CompilerDirectives.transferToInterpreter();

final RubyBasicObject threadLocals = getContext().getThreadManager().getCurrentThread().getThreadLocals();
threadLocals.getObjectType().setInstanceVariable(threadLocals, "$!", exception.getRubyException());
RubyBasicObject.setInstanceVariable(threadLocals, "$!", exception.getRubyException());

for (RescueNode rescue : rescueParts) {
if (rescue.canHandle(frame, exception.getRubyException())) {
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ public void execute(RubyBasicObject object, Object value) {
newProperty = currentProperty;
newProperty.setSafe(object.getDynamicObject(), value, null);
} else {
object.getObjectType().setInstanceVariable(object, name, value);
RubyBasicObject.setInstanceVariable(object, name, value);
newShape = object.getDynamicObject().getShape();
newProperty = newShape.getProperty(name);

Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ public RegexpSetLastMatchPrimitiveNode(RubyContext context, SourceSection source

@Specialization
public Object setLastMatch(RubyClass regexpClass, Object matchData) {
getContext().getThreadManager().getCurrentThread().getThreadLocals().getObjectType().setInstanceVariable(
RubyBasicObject.setInstanceVariable(
getContext().getThreadManager().getCurrentThread().getThreadLocals(), "$~", matchData);

return matchData;
Original file line number Diff line number Diff line change
@@ -469,7 +469,7 @@ public IRubyObject toJRuby(RubyEncoding encoding) {
public org.jruby.RubyString toJRuby(RubyString string) {
final org.jruby.RubyString jrubyString = runtime.newString(string.getByteList().dup());

final Object tainted = string.getObjectType().getInstanceVariable(string, RubyBasicObject.TAINTED_IDENTIFIER);
final Object tainted = RubyBasicObject.getInstanceVariable(string, RubyBasicObject.TAINTED_IDENTIFIER);

if (tainted instanceof Boolean && (boolean) tainted) {
jrubyString.setTaint(true);
@@ -530,7 +530,7 @@ public RubyString toTruffle(org.jruby.RubyString jrubyString) {
final RubyString truffleString = new RubyString(getCoreLibrary().getStringClass(), jrubyString.getByteList().dup());

if (jrubyString.isTaint()) {
truffleString.getObjectType().setInstanceVariable(truffleString, RubyBasicObject.TAINTED_IDENTIFIER, true);
RubyBasicObject.setInstanceVariable(truffleString, RubyBasicObject.TAINTED_IDENTIFIER, true);
}

return truffleString;
@@ -651,7 +651,7 @@ public NativeSockets getNativeSockets() {

@Override
public Object execute(final org.jruby.ast.RootNode rootNode) {
coreLibrary.getGlobalVariablesObject().getObjectType().setInstanceVariable(
RubyBasicObject.setInstanceVariable(
coreLibrary.getGlobalVariablesObject(), "$0",
toTruffle(runtime.getGlobalVariables().get("$0")));

Original file line number Diff line number Diff line change
@@ -456,25 +456,25 @@ private void addCoreMethods() {
private void initializeGlobalVariables() {
RubyBasicObject globals = globalVariablesObject;

globals.getObjectType().setInstanceVariable(globals, "$LOAD_PATH", new RubyArray(arrayClass));
globals.getObjectType().setInstanceVariable(globals, "$LOADED_FEATURES", new RubyArray(arrayClass));
globals.getObjectType().setInstanceVariable(globals, "$:", globals.getInstanceVariable("$LOAD_PATH"));
globals.getObjectType().setInstanceVariable(globals, "$\"", globals.getInstanceVariable("$LOADED_FEATURES"));
globals.getObjectType().setInstanceVariable(globals, "$,", nilObject);
globals.getObjectType().setInstanceVariable(globals, "$0", context.toTruffle(context.getRuntime().getGlobalVariables().get("$0")));
RubyBasicObject.setInstanceVariable(globals, "$LOAD_PATH", new RubyArray(arrayClass));
RubyBasicObject.setInstanceVariable(globals, "$LOADED_FEATURES", new RubyArray(arrayClass));
RubyBasicObject.setInstanceVariable(globals, "$:", globals.getInstanceVariable("$LOAD_PATH"));
RubyBasicObject.setInstanceVariable(globals, "$\"", globals.getInstanceVariable("$LOADED_FEATURES"));
RubyBasicObject.setInstanceVariable(globals, "$,", nilObject);
RubyBasicObject.setInstanceVariable(globals, "$0", context.toTruffle(context.getRuntime().getGlobalVariables().get("$0")));

globals.getObjectType().setInstanceVariable(globals, "$DEBUG", context.getRuntime().isDebug());
RubyBasicObject.setInstanceVariable(globals, "$DEBUG", context.getRuntime().isDebug());

Object value = context.getRuntime().warningsEnabled() ? context.getRuntime().isVerbose() : nilObject;
globals.getObjectType().setInstanceVariable(globals, "$VERBOSE", value);
RubyBasicObject.setInstanceVariable(globals, "$VERBOSE", value);

final RubyString defaultRecordSeparator = RubyString.fromJavaString(stringClass, CLI_RECORD_SEPARATOR);
node.freezeNode.executeFreeze(defaultRecordSeparator);

// TODO (nirvdrum 05-Feb-15) We need to support the $-0 alias as well.
globals.getObjectType().setInstanceVariable(globals, "$/", defaultRecordSeparator);
RubyBasicObject.setInstanceVariable(globals, "$/", defaultRecordSeparator);

globals.getObjectType().setInstanceVariable(globals, "$SAFE", 0);
RubyBasicObject.setInstanceVariable(globals, "$SAFE", 0);
}

private void initializeConstants() {
@@ -909,7 +909,7 @@ public RubyException typeErrorWrongArgumentType(Object object, String expectedTy
public RubyException nameError(String message, String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
RubyException nameError = new RubyException(nameErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
nameError.getObjectType().setInstanceVariable(nameError, "@name", context.getSymbolTable().getSymbol(name));
RubyBasicObject.setInstanceVariable(nameError, "@name", context.getSymbolTable().getSymbol(name));
return nameError;
}

@@ -971,7 +971,7 @@ public RubyException nameErrorLocalVariableNotDefined(String name, RubyBinding b
public RubyException noMethodError(String message, String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
RubyException noMethodError = new RubyException(context.getCoreLibrary().getNoMethodErrorClass(), context.makeString(message), RubyCallStack.getBacktrace(currentNode));
noMethodError.getObjectType().setInstanceVariable(noMethodError, "@name", context.getSymbolTable().getSymbol(name));
RubyBasicObject.setInstanceVariable(noMethodError, "@name", context.getSymbolTable().getSymbol(name));
return noMethodError;
}

Original file line number Diff line number Diff line change
@@ -18,13 +18,15 @@
import com.oracle.truffle.api.object.*;

import org.jruby.truffle.nodes.objects.Allocator;
import org.jruby.truffle.runtime.DebugOperations;
import org.jruby.truffle.runtime.ModuleOperations;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.object.RubyObjectType;
import org.jruby.truffle.runtime.subsystems.ObjectSpaceManager;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Represents the Ruby {@code BasicObject} class - the root of the Ruby class hierarchy.
*/
@@ -98,7 +100,7 @@ public long verySlowGetObjectID() {
}

public Object getInstanceVariable(String name) {
final Object value = getObjectType().getInstanceVariable(this, name);
final Object value = getInstanceVariable(this, name);

if (value == null) {
return getContext().getCoreLibrary().getNilObject();
@@ -108,7 +110,7 @@ public Object getInstanceVariable(String name) {
}

public boolean isFieldDefined(String name) {
return getObjectType().isFieldDefined(this, name);
return isFieldDefined(this, name);
}

@Override
@@ -120,7 +122,7 @@ public final void visitObjectGraph(ObjectSpaceManager.ObjectGraphVisitor visitor
if (visitor.visit(this)) {
getMetaClass().visitObjectGraph(visitor);

for (Object instanceVariable : getObjectType().getInstanceVariables(this).values()) {
for (Object instanceVariable : getInstanceVariables(this).values()) {
if (instanceVariable instanceof RubyBasicObject) {
((RubyBasicObject) instanceVariable).visitObjectGraph(visitor);
}
@@ -174,4 +176,55 @@ public String toString() {
return String.format("RubyBasicObject@%x<logicalClass=%s>", System.identityHashCode(this), logicalClass.getName());
}

@CompilerDirectives.TruffleBoundary
public static void setInstanceVariable(RubyBasicObject receiver, Object name, Object value) {
Shape shape = receiver.getDynamicObject().getShape();
Property property = shape.getProperty(name);
if (property != null) {
property.setGeneric(receiver.getDynamicObject(), value, null);
} else {
receiver.getDynamicObject().define(name, value, 0);
}
}

@CompilerDirectives.TruffleBoundary
public static void setInstanceVariables(RubyBasicObject receiver, Map<Object, Object> instanceVariables) {
for (Map.Entry<Object, Object> entry : instanceVariables.entrySet()) {
setInstanceVariable(receiver, entry.getKey(), entry.getValue());
}
}

@CompilerDirectives.TruffleBoundary
public static Object getInstanceVariable(RubyBasicObject receiver, Object name) {
Shape shape = receiver.getDynamicObject().getShape();
Property property = shape.getProperty(name);
if (property != null) {
return property.get(receiver.getDynamicObject(), false);
} else {
return receiver.getContext().getCoreLibrary().getNilObject();
}
}

@CompilerDirectives.TruffleBoundary
public static Map<Object, Object> getInstanceVariables(RubyBasicObject receiver) {
Shape shape = receiver.getDynamicObject().getShape();
Map<Object, Object> vars = new LinkedHashMap<>();
List<Property> properties = shape.getPropertyList();
for (Property property : properties) {
vars.put((String) property.getKey(), property.get(receiver.getDynamicObject(), false));
}
return vars;
}

@CompilerDirectives.TruffleBoundary
public static Object[] getFieldNames(RubyBasicObject receiver) {
List<Object> keys = receiver.getDynamicObject().getShape().getKeyList();
return keys.toArray(new Object[keys.size()]);
}

@CompilerDirectives.TruffleBoundary
public static boolean isFieldDefined(RubyBasicObject receiver, String name) {
return receiver.getDynamicObject().getShape().hasProperty(name);
}

}
Original file line number Diff line number Diff line change
@@ -237,7 +237,7 @@ private void setFrame(Frame frame, String name, Object value) {
public void setThread(String name, Object value) {
assert value != null;

getContext().getThreadManager().getCurrentThread().getThreadLocals().getObjectType().setInstanceVariable(getContext().getThreadManager().getCurrentThread().getThreadLocals(), name, value);
setInstanceVariable(getContext().getThreadManager().getCurrentThread().getThreadLocals(), name, value);
}

@CompilerDirectives.TruffleBoundary
Original file line number Diff line number Diff line change
@@ -9,69 +9,8 @@
*/
package org.jruby.truffle.runtime.object;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.ObjectType;
import com.oracle.truffle.api.object.Property;
import com.oracle.truffle.api.object.Shape;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyBasicObject;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RubyObjectType extends ObjectType {

@CompilerDirectives.TruffleBoundary
public void setInstanceVariable(RubyBasicObject receiver, Object name, Object value) {
Shape shape = receiver.getDynamicObject().getShape();
Property property = shape.getProperty(name);
if (property != null) {
property.setGeneric(receiver.getDynamicObject(), value, null);
} else {
receiver.getDynamicObject().define(name, value, 0);
}
}

@CompilerDirectives.TruffleBoundary
public void setInstanceVariables(RubyBasicObject receiver, Map<Object, Object> instanceVariables) {
for (Map.Entry<Object, Object> entry : instanceVariables.entrySet()) {
setInstanceVariable(receiver, entry.getKey(), entry.getValue());
}
}

@CompilerDirectives.TruffleBoundary
public Object getInstanceVariable(RubyBasicObject receiver, Object name) {
Shape shape = receiver.getDynamicObject().getShape();
Property property = shape.getProperty(name);
if (property != null) {
return property.get(receiver.getDynamicObject(), false);
} else {
return receiver.getContext().getCoreLibrary().getNilObject();
}
}

@CompilerDirectives.TruffleBoundary
public Map<Object, Object> getInstanceVariables(RubyBasicObject receiver) {
Shape shape = receiver.getDynamicObject().getShape();
Map<Object, Object> vars = new LinkedHashMap<>();
List<Property> properties = shape.getPropertyList();
for (Property property : properties) {
vars.put((String) property.getKey(), property.get(receiver.getDynamicObject(), false));
}
return vars;
}

@CompilerDirectives.TruffleBoundary
public Object[] getFieldNames(RubyBasicObject receiver) {
List<Object> keys = receiver.getDynamicObject().getShape().getKeyList();
return keys.toArray(new Object[keys.size()]);
}

@CompilerDirectives.TruffleBoundary
public boolean isFieldDefined(RubyBasicObject receiver, String name) {
return receiver.getDynamicObject().getShape().hasProperty(name);
}

}

0 comments on commit 1bb107f

Please sign in to comment.