Skip to content

Commit

Permalink
Showing 88 changed files with 945 additions and 928 deletions.
56 changes: 53 additions & 3 deletions truffle/src/main/java/org/jruby/truffle/nodes/RubyNode.java
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@
*/
package org.jruby.truffle.nodes;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -24,12 +23,19 @@
import jnr.ffi.provider.MemoryManager;
import jnr.posix.POSIX;

import org.jcodings.Encoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.nodes.instrument.RubyWrapperNode;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.NotProvided;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.sockets.NativeSockets;
import org.jruby.util.ByteList;

import java.nio.ByteBuffer;

@TypeSystemReference(RubyTypes.class)
@ImportStatic(RubyGuards.class)
@@ -55,10 +61,10 @@ public RubyNode(RubyContext context, SourceSection sourceSection) {
public abstract Object execute(VirtualFrame frame);

public Object isDefined(VirtualFrame frame) {
return getContext().makeString("expression");
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), "expression");
}

// Execute without returing the result
// Execute without returning the result

public void executeVoid(VirtualFrame frame) {
execute(frame);
@@ -242,6 +248,50 @@ protected RubyBasicObject nil() {
return getContext().getCoreLibrary().getNilObject();
}

protected RubyString createEmptyString() {
return StringNodes.createEmptyString(getContext().getCoreLibrary().getStringClass());
}

protected RubyString createString(String string) {
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), string);
}

protected RubyString createString(String string, Encoding encoding) {
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), string, encoding);
}

protected RubyString createString(byte[] bytes) {
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), bytes);
}

protected RubyString createString(ByteBuffer bytes) {
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), bytes);
}

protected RubyString createString(ByteList bytes) {
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), bytes);
}

protected RubyArray createEmptyArray() {
return ArrayNodes.createEmptyArray(getContext().getCoreLibrary().getArrayClass());
}

protected RubyArray createArray(int[] store, int size) {
return ArrayNodes.createArray(getContext().getCoreLibrary().getArrayClass(), store, size);
}

protected RubyArray createArray(long[] store, int size) {
return ArrayNodes.createArray(getContext().getCoreLibrary().getArrayClass(), store, size);
}

protected RubyArray createArray(double[] store, int size) {
return ArrayNodes.createArray(getContext().getCoreLibrary().getArrayClass(), store, size);
}

protected RubyArray createArray(Object[] store, int size) {
return ArrayNodes.createArray(getContext().getCoreLibrary().getArrayClass(), store, size);
}

protected POSIX posix() {
return getContext().getPosix();
}
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ private Object lookupRestKeywordArgumentHash(VirtualFrame frame) {
final RubyHash hash = RubyArguments.getUserKeywordsHash(frame.getArguments(), minimum);

if (hash == null) {
return new RubyHash(getContext().getCoreLibrary().getHashClass(), null, null, null, 0, null);
return HashNodes.createEmptyHash(getContext().getCoreLibrary().getHashClass());
}

final List<KeyValue> entries = new ArrayList<>();
Original file line number Diff line number Diff line change
@@ -13,9 +13,9 @@
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyClass;
import org.jruby.truffle.runtime.core.RubyHash;
import org.jruby.truffle.runtime.array.ArrayUtils;
@@ -59,16 +59,16 @@ public Object execute(VirtualFrame frame) {

if (startIndex == 0) {
final Object[] arguments = RubyArguments.extractUserArguments(frame.getArguments());
return new RubyArray(arrayClass, arguments, length);
return ArrayNodes.createArray(arrayClass, arguments, length);
} else {
if (startIndex >= endIndex) {
noArgumentsLeftProfile.enter();
return new RubyArray(arrayClass);
return ArrayNodes.createEmptyArray(arrayClass);
} else {
subsetOfArgumentsProfile.enter();
final Object[] arguments = RubyArguments.extractUserArguments(frame.getArguments());
// TODO(CS): risk here of widening types too much - always going to be Object[] - does seem to be something that does happen
return new RubyArray(arrayClass, ArrayUtils.extractRange(arguments, startIndex, endIndex), length);
return ArrayNodes.createArray(arrayClass, ArrayUtils.extractRange(arguments, startIndex, endIndex), length);
}
}
}
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ public RubyArray cast(RubyArray array) {
public Object cast(Object nil) {
switch (nilBehavior) {
case EMPTY_ARRAY:
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();

case ARRAY_WITH_NIL:
return ArrayNodes.fromObject(getContext().getCoreLibrary().getArrayClass(), nil());
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.core.array.ArrayDupNode;
import org.jruby.truffle.nodes.core.array.ArrayDupNodeGen;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
@@ -64,7 +65,7 @@ public SplatCastNode(RubyContext context, SourceSection sourceSection, NilBehavi
public RubyArray splat(Object nil) {
switch (nilBehavior) {
case EMPTY_ARRAY:
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();

case ARRAY_WITH_NIL:
return ArrayNodes.fromObject(getContext().getCoreLibrary().getArrayClass(), nil());
@@ -94,7 +95,7 @@ public RubyArray splat(VirtualFrame frame, Object object) {
}

// MRI tries to call dynamic respond_to? here.
Object respondToResult = respondToToA.call(frame, object, "respond_to?", null, getContext().makeString(method), true);
Object respondToResult = respondToToA.call(frame, object, "respond_to?", null, createString(method), true);
if (respondToResult != DispatchNode.MISSING && respondToCast.executeBoolean(frame, respondToResult)) {
final Object array = toA.call(frame, object, method, null);

Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
package org.jruby.truffle.nodes.constants;

import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.literal.LiteralNode;
import org.jruby.truffle.runtime.LexicalScope;
import org.jruby.truffle.runtime.RubyConstant;
@@ -47,7 +48,7 @@ public Object isDefined(VirtualFrame frame) {

if (name.equals("Encoding")) {
// Work-around so I don't have to load the iconv library - runners/formatters/junit.rb.
return context.makeString("constant");
return createString("constant");
}

final Object receiverObject;
@@ -82,7 +83,7 @@ public Object isDefined(VirtualFrame frame) {
if (constant == null) {
return nil();
} else {
return context.makeString("constant");
return createString("constant");
}
}

Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyBinding;
@@ -41,8 +42,8 @@ public TraceNode(RubyContext context, SourceSection sourceSection) {
traceAssumption = context.getTraceManager().getTraceAssumption();
traceFunc = null;
callNode = null;
event = context.makeString("line");
file = context.makeString(sourceSection.getSource().getName());
event = createString("line");
file = createString(sourceSection.getSource().getName());
line = sourceSection.getStartLine();
}

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

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -478,9 +477,10 @@ public RubyString toS(RubyBasicObject value) {
final BigDecimal bigDecimal = getBigDecimalValue(value);
final boolean negative = bigDecimal.signum() == -1;

return getContext().makeString((negative ? "-" : "") + "0." +
String string = (negative ? "-" : "") + "0." +
(negative ? bigDecimal.unscaledValue().toString().substring(1) : bigDecimal.unscaledValue()) +
"E" + (bigDecimal.precision() - bigDecimal.scale()));
"E" + (bigDecimal.precision() - bigDecimal.scale());
return createString(string);
}

}
Original file line number Diff line number Diff line change
@@ -535,7 +535,7 @@ public RubyArray coerce(RubyBasicObject a, int b) {
// TODO (eregon, 16 Feb. 2015): This is NOT spec, but let's try to see if we can make it work.
// b is converted to a Bignum here in other implementations.
Object[] store = new Object[] { b, a };
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, store.length);
return createArray(store, store.length);
}

@Specialization
@@ -545,15 +545,15 @@ public RubyArray coerce(RubyBasicObject a, long b) {
// TODO (eregon, 16 Feb. 2015): This is NOT spec, but let's try to see if we can make it work.
// b is converted to a Bignum here in other implementations.
Object[] store = new Object[] { b, a };
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, store.length);
return createArray(store, store.length);
}

@Specialization(guards = "isRubyBignum(b)")
public RubyArray coerce(RubyBasicObject a, RubyBasicObject b) {
CompilerDirectives.transferToInterpreter();

Object[] store = new Object[] { b, a };
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, store.length);
return createArray(store, store.length);
}

}
@@ -662,7 +662,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString toS(RubyBasicObject value, NotProvided base) {
return getContext().makeString(getBigIntegerValue(value).toString());
return createString(getBigIntegerValue(value).toString());
}

@TruffleBoundary
@@ -673,7 +673,7 @@ public RubyString toS(RubyBasicObject value, int base) {
throw new RaiseException(getContext().getCoreLibrary().argumentErrorInvalidRadix(base, this));
}

return getContext().makeString(getBigIntegerValue(value).toString(base));
return createString(getBigIntegerValue(value).toString(base));
}

}
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ public LocalVariablesNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyArray localVariables(RubyBinding binding) {
final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());
final RubyArray array = createEmptyArray();

MaterializedFrame frame = binding.getFrame();

Original file line number Diff line number Diff line change
@@ -137,11 +137,11 @@ public Object transcodingMap(VirtualFrame frame) {
final TranscoderDB.Entry e = destinationEntry.value;

if (key == null) {
final Object upcased = upcaseNode.call(frame, getContext().makeString(new ByteList(e.getSource())), "upcase", null);
final Object upcased = upcaseNode.call(frame, createString(new ByteList(e.getSource())), "upcase", null);
key = toSymNode.call(frame, upcased, "to_sym", null);
}

final Object upcasedLookupTableKey = upcaseNode.call(frame, getContext().makeString(new ByteList(e.getDestination())), "upcase", null);
final Object upcasedLookupTableKey = upcaseNode.call(frame, createString(new ByteList(e.getDestination())), "upcase", null);
final Object lookupTableKey = toSymNode.call(frame, upcasedLookupTableKey, "to_sym", null);
final Object lookupTableValue = newTranscodingNode.call(frame, getContext().getCoreLibrary().getTranscodingClass(), "create", null, key, lookupTableKey);
lookupTableWriteNode.call(frame, value, "[]=", null, lookupTableKey, lookupTableValue);
Original file line number Diff line number Diff line change
@@ -278,7 +278,7 @@ public RubyArray list() {

final RubyEncoding[] encodings = RubyEncoding.cloneEncodingList();

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), encodings, encodings.length);
return createArray(encodings, encodings.length);
}
}

@@ -294,7 +294,7 @@ public LocaleCharacterMapNode(RubyContext context, SourceSection sourceSection)
public RubyString localeCharacterMap() {
CompilerDirectives.transferToInterpreter();
final ByteList name = new ByteList(getContext().getRuntime().getEncodingService().getLocaleEncoding().getName());
return getContext().makeString(name);
return createString(name);
}
}

@@ -338,7 +338,7 @@ public Object encodingMap(VirtualFrame frame) {

final RubyEncoding[] encodings = RubyEncoding.cloneEncodingList();
for (int i = 0; i < encodings.length; i++) {
final Object upcased = upcaseNode.call(frame, getContext().makeString(encodings[i].getName()), "upcase", null);
final Object upcased = upcaseNode.call(frame, createString(encodings[i].getName()), "upcase", null);
final Object key = toSymNode.call(frame, upcased, "to_sym", null);
final Object value = newTupleNode.call(frame, getContext().getCoreLibrary().getTupleClass(), "create", null, nil(), i);

@@ -350,9 +350,9 @@ public Object encodingMap(VirtualFrame frame) {
final CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<EncodingDB.Entry> e =
((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<EncodingDB.Entry>)i.next());

final Object upcased = upcaseNode.call(frame, getContext().makeString(new ByteList(e.bytes, e.p, e.end - e.p)), "upcase", null);
final Object upcased = upcaseNode.call(frame, createString(new ByteList(e.bytes, e.p, e.end - e.p)), "upcase", null);
final Object key = toSymNode.call(frame, upcased, "to_sym", null);
final RubyString alias = getContext().makeString(new ByteList(e.bytes, e.p, e.end - e.p));
final RubyString alias = createString(new ByteList(e.bytes, e.p, e.end - e.p));
final int index = e.value.getIndex();


@@ -361,19 +361,19 @@ public Object encodingMap(VirtualFrame frame) {
}

final Encoding defaultInternalEncoding = getContext().getRuntime().getDefaultInternalEncoding();
final Object internalTuple = getContext().makeTuple(frame, newTupleNode, getContext().makeString("internal"), indexLookup(encodings, defaultInternalEncoding));
final Object internalTuple = getContext().makeTuple(frame, newTupleNode, createString("internal"), indexLookup(encodings, defaultInternalEncoding));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("INTERNAL"), internalTuple);

final Encoding defaultExternalEncoding = getContext().getRuntime().getDefaultExternalEncoding();
final Object externalTuple = getContext().makeTuple(frame, newTupleNode, getContext().makeString("external"), indexLookup(encodings, defaultExternalEncoding));
final Object externalTuple = getContext().makeTuple(frame, newTupleNode, createString("external"), indexLookup(encodings, defaultExternalEncoding));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("EXTERNAL"), externalTuple);

final Encoding localeEncoding = getContext().getRuntime().getEncodingService().getLocaleEncoding();
final Object localeTuple = getContext().makeTuple(frame, newTupleNode, getContext().makeString("locale"), indexLookup(encodings, localeEncoding));
final Object localeTuple = getContext().makeTuple(frame, newTupleNode, createString("locale"), indexLookup(encodings, localeEncoding));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("LOCALE"), localeTuple);

final Encoding filesystemEncoding = getContext().getRuntime().getEncodingService().getLocaleEncoding();
final Object filesystemTuple = getContext().makeTuple(frame, newTupleNode, getContext().makeString("filesystem"), indexLookup(encodings, filesystemEncoding));
final Object filesystemTuple = getContext().makeTuple(frame, newTupleNode, createString("filesystem"), indexLookup(encodings, filesystemEncoding));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("FILESYSTEM"), filesystemTuple);

return ret;
@@ -408,7 +408,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {
public RubyString toS(RubyEncoding encoding) {
final ByteList name = encoding.getName().dup();
name.setEncoding(ASCIIEncoding.INSTANCE);
return getContext().makeString(name);
return createString(name);
}
}

Original file line number Diff line number Diff line change
@@ -666,7 +666,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString toS(double value) {
return getContext().makeString(Double.toString(value));
return createString(Double.toString(value));
}

}
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import com.oracle.truffle.api.utilities.BranchProfile;

import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyArray;
@@ -108,13 +109,13 @@ private RubyArray divMod(long a, long b) {

if (integerDiv instanceof Long && ((long) integerDiv) >= Integer.MIN_VALUE && ((long) integerDiv) <= Integer.MAX_VALUE && mod >= Integer.MIN_VALUE && mod <= Integer.MAX_VALUE) {
useFixnumPairProfile.enter();
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new int[]{(int) (long) integerDiv, (int) mod}, 2);
return createArray(new int[]{(int) (long) integerDiv, (int) mod}, 2);
} else if (integerDiv instanceof Long) {
useObjectPairProfile.enter();
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{integerDiv, mod}, 2);
return createArray(new Object[]{integerDiv, mod}, 2);
} else {
useObjectPairProfile.enter();
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{
return createArray(new Object[]{
fixnumOrBignumQuotient.fixnumOrBignum((BigInteger) integerDiv),
mod}, 2);
}
@@ -140,7 +141,7 @@ private RubyArray divMod(double a, double b) {
mod += b;
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{
return createArray(new Object[]{
fixnumOrBignumQuotient.fixnumOrBignum(div),
mod}, 2);
}
@@ -160,7 +161,7 @@ private RubyArray divMod(BigInteger a, BigInteger b) {
bigIntegerResults[1] = b.add(bigIntegerResults[1]);
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{
return createArray(new Object[]{
fixnumOrBignumQuotient.fixnumOrBignum(bigIntegerResults[0]),
fixnumOrBignumRemainder.fixnumOrBignum(bigIntegerResults[1])}, 2);
}
Original file line number Diff line number Diff line change
@@ -14,14 +14,14 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.nodes.methods.UnsupportedOperationBehavior;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.NotProvided;
import org.jruby.truffle.runtime.control.NextException;
import org.jruby.truffle.runtime.control.RedoException;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyBignum;
import org.jruby.truffle.runtime.core.RubyProc;

import java.math.BigInteger;
@@ -143,7 +143,7 @@ public RubyArray times(VirtualFrame frame, int n, NotProvided block) {
array[i] = i;
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), array, n);
return createArray(array, n);
}

@Specialization
Original file line number Diff line number Diff line change
@@ -119,7 +119,7 @@ public RubyString backtick(RubyString command) {
}

// TODO (nirvdrum 10-Mar-15) This should be using the default external encoding, rather than hard-coded to UTF-8.
return context.makeString(resultBuilder.toString(), RubyEncoding.getEncoding("UTF-8").getEncoding());
return createString(resultBuilder.toString(), RubyEncoding.getEncoding("UTF-8").getEncoding());
}

}
@@ -330,7 +330,7 @@ public RubyArray callerLocations(int omit, int length) {
locations[n] = ThreadBacktraceLocationNodes.createRubyThreadBacktraceLocation(threadBacktraceLocationClass, activation);
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), locations, locations.length);
return createArray(locations, locations.length);
}
}

@@ -699,7 +699,7 @@ public String block() throws InterruptedException {
}
});

final RubyString rubyLine = getContext().makeString(line);
final RubyString rubyLine = createString(line);

// Set the local variable $_ in the caller

@@ -907,7 +907,7 @@ public RubyArray instanceVariables(RubyBasicObject self) {

Arrays.sort(instanceVariableNames);

final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());
final RubyArray array = createEmptyArray();

for (Object name : instanceVariableNames) {
if (name instanceof String) {
@@ -1007,7 +1007,7 @@ public LocalVariablesNode(RubyContext context, SourceSection sourceSection) {
public RubyArray localVariables() {
CompilerDirectives.transferToInterpreter();

final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());
final RubyArray array = createEmptyArray();

for (Object name : Truffle.getRuntime().getCallerFrame().getFrame(FrameInstance.FrameAccess.READ_ONLY, false).getFrameDescriptor().getIdentifiers()) {
if (name instanceof String) {
@@ -1540,7 +1540,7 @@ public RubyArray singletonMethods(VirtualFrame frame, Object self, boolean inclu
RubyClass metaClass = metaClassNode.executeMetaClass(frame, self);

if (!metaClass.isSingleton()) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
}

CompilerDirectives.transferToInterpreter();
@@ -1731,7 +1731,7 @@ private RuntimeException handleException(PackException exception) {
}

private RubyString finishFormat(ByteList format, PackResult result) {
final RubyString string = getContext().makeString(new ByteList(result.getOutput(), 0, result.getOutputLength()));
final RubyString string = createString(new ByteList(result.getOutput(), 0, result.getOutputLength()));

if (format.length() == 0) {
StringNodes.forceEncoding(string, USASCIIEncoding.INSTANCE);
@@ -1913,7 +1913,7 @@ public RubyString toS(VirtualFrame frame, Object self) {
Object id = objectIDNode.executeObjectID(frame, self);
String hexID = toHexStringNode.executeToHexString(frame, id);

return getContext().makeString("#<" + className + ":0x" + hexID + ">");
return createString("#<" + className + ":0x" + hexID + ">");
}

}
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ public Object execute(VirtualFrame frame) {
Collection<String> requiredLibraries = getContext().getRuntime().getInstanceConfig().getRequiredLibraries();

for (String requiredLibrary : requiredLibraries) {
requireNode.call(frame, self, "require", null, getContext().makeString(requiredLibrary));
requireNode.call(frame, self, "require", null, createString(requiredLibrary));
}

return nil();
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ public Object getIndex(RubyMatchData matchData, int index, int length) {
final Object[] values = matchData.getValues();
final int normalizedIndex = ArrayNodes.normalizeIndex(values.length, index);
final Object[] store = Arrays.copyOfRange(values, normalizedIndex, normalizedIndex + length);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, length);
return createArray(store, length);
}

@Specialization
@@ -118,7 +118,7 @@ public Object getIndex(VirtualFrame frame, RubyMatchData matchData, RubyRange.In
final int length = exclusiveEnd - normalizedIndex;

final Object[] store = Arrays.copyOfRange(values, normalizedIndex, normalizedIndex + length);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, length);
return createArray(store, length);
}

}
@@ -258,7 +258,7 @@ public RubyString toS(RubyMatchData matchData) {
CompilerDirectives.transferToInterpreter();

final ByteList bytes = matchData.getGlobal().getByteList().dup();
return getContext().makeString(bytes);
return createString(bytes);
}
}

Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.RubyMath;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.nodes.dispatch.MissingBehavior;
@@ -25,7 +26,6 @@
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyBignum;

@CoreClass(name = "Math")
public abstract class MathNodes {
@@ -360,7 +360,7 @@ public RubyArray frexp(double a) {
for (; mantissa >= 1.0; mantissa *= 0.5, exponent +=1) { }
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{sign * mantissa, exponent}, 2);
return createArray(new Object[]{sign * mantissa, exponent}, 2);
}

@Fallback
@@ -589,7 +589,7 @@ public RubyArray lgamma(double a) {

final RubyMath.NemesLogGamma l = new RubyMath.NemesLogGamma(a);

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{l.value, l.sign}, 2);
return createArray(new Object[]{l.value, l.sign}, 2);
}

@Fallback
Original file line number Diff line number Diff line change
@@ -234,7 +234,7 @@ public Object sourceLocation(RubyBasicObject method) {
if (sourceSection instanceof NullSourceSection) {
return nil();
} else {
RubyString file = getContext().makeString(sourceSection.getSource().getName());
RubyString file = createString(sourceSection.getSource().getName());
return ArrayNodes.fromObjects(getContext().getCoreLibrary().getArrayClass(),
file, sourceSection.getStartLine());
}
Original file line number Diff line number Diff line change
@@ -748,7 +748,7 @@ public ClassVariablesNode(RubyContext context, SourceSection sourceSection) {
public RubyArray getClassVariables(RubyModule module) {
CompilerDirectives.transferToInterpreter();

final RubyArray array = new RubyArray(module.getContext().getCoreLibrary().getArrayClass());
final RubyArray array = ArrayNodes.createEmptyArray(module.getContext().getCoreLibrary().getArrayClass());

for (String variable : ModuleOperations.getAllClassVariables(module).keySet()) {
ArrayNodes.slowPush(array, RubySymbol.newSymbol(module.getContext(), variable));
@@ -1258,7 +1258,7 @@ public Object name(RubyModule module) {
return nil();
}

return getContext().makeString(module.getName());
return createString(module.getName());
}
}

@@ -1765,7 +1765,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {
public RubyString toS(RubyModule module) {
CompilerDirectives.transferToInterpreter();

return getContext().makeString(module.getName());
return createString(module.getName());
}

}
@@ -1816,7 +1816,7 @@ public RubyString userHome(RubyString uname) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().argumentError("user " + uname.toString() + " does not exist", this));
}
return getContext().makeString(passwd.getHome());
return createString(passwd.getHome());
}

}
Original file line number Diff line number Diff line change
@@ -189,7 +189,7 @@ public Object sourceLocation(RubyProc proc) {
if (sourceSection instanceof NullSourceSection) {
return nil();
} else {
RubyString file = getContext().makeString(sourceSection.getSource().getName());
RubyString file = createString(sourceSection.getSource().getName());
return ArrayNodes.fromObjects(getContext().getCoreLibrary().getArrayClass(),
file, sourceSection.getStartLine());
}
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.truffle.nodes.core.array.ArrayBuilderNode;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.NotProvided;
import org.jruby.truffle.runtime.control.NextException;
@@ -62,7 +63,7 @@ public RubyArray collect(VirtualFrame frame, RubyRange.IntegerFixnumRange range,
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilder.finish(store, length), length);
return ArrayNodes.createGeneralArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilder.finish(store, length), length);
}

}
@@ -410,15 +411,15 @@ public RubyArray toA(RubyRange.IntegerFixnumRange range) {
final int length = range.getExclusiveEnd() - begin;

if (length < 0) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
final int[] values = new int[length];

for (int n = 0; n < length; n++) {
values[n] = begin + n;
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), values, length);
return createArray(values, length);
}
}

Original file line number Diff line number Diff line change
@@ -150,7 +150,7 @@ public EscapeNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString escape(RubyString pattern) {
return getContext().makeString(org.jruby.RubyRegexp.quote19(new ByteList(pattern.getByteList()), true).toString());
return createString(org.jruby.RubyRegexp.quote19(new ByteList(pattern.getByteList()), true).toString());
}

}
@@ -179,7 +179,7 @@ public InspectNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public RubyString match(RubyRegexp regexp) {
return new RubyString(getContext().getCoreLibrary().getStringClass(), ((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp(getContext().getRuntime(), regexp.getSource(), regexp.getRegex().getOptions()).inspect19()).getByteList());
return createString(((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp(getContext().getRuntime(), regexp.getSource(), regexp.getRegex().getOptions()).inspect19()).getByteList());
}

}
@@ -258,7 +258,7 @@ public QuoteNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public RubyString quote(RubyString raw) {
boolean isAsciiOnly = raw.getByteList().getEncoding().isAsciiCompatible() && raw.scanForCodeRange() == CR_7BIT;
return getContext().makeString(org.jruby.RubyRegexp.quote19(raw.getByteList(), isAsciiOnly));
return createString(org.jruby.RubyRegexp.quote19(raw.getByteList(), isAsciiOnly));
}

@Specialization
@@ -291,7 +291,7 @@ public SourceNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public RubyString source(RubyRegexp regexp) {
return getContext().makeString(regexp.getSource().dup());
return createString(regexp.getSource().dup());
}

}
@@ -305,7 +305,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public RubyString to_s(RubyRegexp regexp) {
return new RubyString(getContext().getCoreLibrary().getStringClass(), ((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp(getContext().getRuntime(), regexp.getSource(), regexp.getRegex().getOptions()).to_s()).getByteList());
return createString(((org.jruby.RubyString) org.jruby.RubyRegexp.newRegexp(getContext().getRuntime(), regexp.getSource(), regexp.getRegex().getOptions()).to_s()).getByteList());
}

}
82 changes: 48 additions & 34 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java
Original file line number Diff line number Diff line change
@@ -67,23 +67,12 @@
import org.jruby.util.io.EncodingUtils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;

@CoreClass(name = "String")
public abstract class StringNodes {

public static RubyString fromJavaString(RubyClass stringClass, String string) {
return new RubyString(stringClass, new ByteList(org.jruby.RubyEncoding.encodeUTF8(string), USASCIIEncoding.INSTANCE, false));
}

public static RubyString fromJavaString(RubyClass stringClass, String string, Encoding encoding) {
return new RubyString(stringClass, new ByteList(org.jruby.RubyEncoding.encodeUTF8(string), encoding, false));
}

public static RubyString fromByteList(RubyClass stringClass, ByteList bytes) {
return new RubyString(stringClass, bytes);
}

public static void set(RubyString string, ByteList bytes) {
string.bytes = bytes;
}
@@ -139,6 +128,30 @@ public static boolean singleByteOptimizable(RubyString string) {
return StringSupport.isSingleByteOptimizable(string, EncodingUtils.STR_ENC_GET(string));
}

public static RubyString createEmptyString(RubyClass stringClass) {
return createString(stringClass, new ByteList());
}

public static RubyString createString(RubyClass stringClass, String string) {
return createString(stringClass, string, USASCIIEncoding.INSTANCE);
}

public static RubyString createString(RubyClass stringClass, String string, Encoding encoding) {
return createString(stringClass, new ByteList(org.jruby.RubyEncoding.encodeUTF8(string), encoding, false));
}

public static RubyString createString(RubyClass stringClass, byte[] bytes) {
return createString(stringClass, new ByteList(bytes));
}

public static RubyString createString(RubyClass stringClass, ByteBuffer bytes) {
return createString(stringClass, new ByteList(bytes.array()));
}

public static RubyString createString(RubyClass stringClass, ByteList bytes) {
return new RubyString(stringClass, bytes);
}

@CoreMethod(names = "+", required = 1)
@NodeChildren({
@NodeChild(type = RubyNode.class, value = "string"),
@@ -159,8 +172,7 @@ public AddNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public RubyString add(RubyString string, RubyString other) {
final Encoding enc = checkEncoding(string, other, this);
final RubyString ret = getContext().makeString(getContext().getCoreLibrary().getStringClass(),
StringSupport.addByteLists(string.getByteList(), other.getByteList()));
final RubyString ret = createString(StringSupport.addByteLists(string.getByteList(), other.getByteList()));

if (taintResultNode == null) {
CompilerDirectives.transferToInterpreter();
@@ -201,7 +213,7 @@ public RubyString multiply(RubyString string, int times) {
}

outputBytes.setEncoding(inputBytes.getEncoding());
final RubyString ret = getContext().makeString(string.getLogicalClass(), outputBytes);
final RubyString ret = StringNodes.createString(string.getLogicalClass(), outputBytes);
ret.setCodeRange(string.getCodeRange());

return ret;
@@ -250,7 +262,7 @@ public boolean equal(VirtualFrame frame, RubyString a, Object b) {
respondToNode = insert(KernelNodesFactory.RespondToNodeFactory.create(getContext(), getSourceSection(), new RubyNode[] { null, null, null }));
}

if (respondToNode.doesRespondTo(frame, b, getContext().makeString("to_str"), false)) {
if (respondToNode.doesRespondTo(frame, b, StringNodes.createString(getContext().getCoreLibrary().getStringClass(), "to_str"), false)) {
if (objectEqualNode == null) {
CompilerDirectives.transferToInterpreter();
objectEqualNode = insert(DispatchHeadNodeFactory.createMethodCall(getContext()));
@@ -298,7 +310,7 @@ public Object compare(VirtualFrame frame, RubyString a, Object b) {
respondToToStrNode = insert(KernelNodesFactory.RespondToNodeFactory.create(getContext(), getSourceSection(), new RubyNode[] { null, null, null }));
}

if (respondToToStrNode.doesRespondTo(frame, b, getContext().makeString("to_str"), false)) {
if (respondToToStrNode.doesRespondTo(frame, b, StringNodes.createString(getContext().getCoreLibrary().getStringClass(), "to_str"), false)) {
if (toStrNode == null) {
CompilerDirectives.transferToInterpreter();
toStrNode = insert(ToStrNodeGen.create(getContext(), getSourceSection(), null));
@@ -322,7 +334,7 @@ public Object compare(VirtualFrame frame, RubyString a, Object b) {
respondToCmpNode = insert(KernelNodesFactory.RespondToNodeFactory.create(getContext(), getSourceSection(), new RubyNode[] { null, null, null }));
}

if (respondToCmpNode.doesRespondTo(frame, b, getContext().makeString("<=>"), false)) {
if (respondToCmpNode.doesRespondTo(frame, b, StringNodes.createString(getContext().getCoreLibrary().getStringClass(), "<=>"), false)) {
if (cmpNode == null) {
CompilerDirectives.transferToInterpreter();
cmpNode = insert(DispatchHeadNodeFactory.createMethodCall(getContext()));
@@ -526,7 +538,9 @@ private Object sliceRange(VirtualFrame frame, RubyString string, int begin, int
} else {

if (begin == stringLength) {
return getContext().makeString(string.getLogicalClass(), "", string.getByteList().getEncoding());
final ByteList byteList = new ByteList();
byteList.setEncoding(string.getByteList().getEncoding());
return StringNodes.createString(string.getLogicalClass(), byteList);
}

end = normalizeIndex(stringLength, end);
@@ -662,7 +676,7 @@ public BNode(RubyContext context, SourceSection sourceSection) {
public RubyString b(RubyString string) {
final ByteList bytes = string.getByteList().dup();
bytes.setEncoding(ASCIIEncoding.INSTANCE);
return getContext().makeString(bytes);
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), bytes);
}

}
@@ -684,7 +698,7 @@ public RubyArray bytes(RubyString string) {
store[n] = ((int) bytes[n]) & 0xFF;
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, bytes.length);
return createArray(store, bytes.length);
}

}
@@ -859,7 +873,7 @@ public Object crypt(RubyString string, RubyString salt) {

final Encoding ascii8bit = getContext().getRuntime().getEncodingService().getAscii8bitEncoding();
ByteList otherBL = salt.getByteList().dup();
final RubyString otherStr = getContext().makeString(otherBL);
final RubyString otherStr = StringNodes.createString(getContext().getCoreLibrary().getStringClass(), otherBL);

otherStr.modify();
StringSupport.associateEncoding(otherStr, ascii8bit);
@@ -887,7 +901,7 @@ public Object crypt(RubyString string, RubyString salt) {
throw new RaiseException(getContext().getCoreLibrary().errnoError(posix.errno(), this));
}

final RubyString result = getContext().makeString(new ByteList(cryptedString, 0, cryptedString.length - 1));
final RubyString result = StringNodes.createString(getContext().getCoreLibrary().getStringClass(), new ByteList(cryptedString, 0, cryptedString.length - 1));
StringSupport.associateEncoding(result, ascii8bit);

return result;
@@ -973,7 +987,7 @@ public DowncaseNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public RubyString downcase(RubyString string) {
final ByteList newByteList = StringNodesHelper.downcase(getContext().getRuntime(), string.getByteList());
return string.getContext().makeString(string.getLogicalClass(), newByteList);
return StringNodes.createString(string.getLogicalClass(), newByteList);
}
}

@@ -1093,7 +1107,7 @@ private Object substr(RubyString string, int beg, int len) {
taintResultNode = insert(new TaintResultNode(getContext(), getSourceSection()));
}

final RubyString ret = getContext().makeString(string.getLogicalClass(), substringBytes);
final RubyString ret = StringNodes.createString(string.getLogicalClass(), substringBytes);

return taintResultNode.maybeTaint(string, ret);
}
@@ -1211,7 +1225,7 @@ public InspectNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public RubyString inspect(RubyString string) {
final org.jruby.RubyString inspected = (org.jruby.RubyString) org.jruby.RubyString.inspect19(getContext().getRuntime(), string.getByteList());
return getContext().makeString(inspected.getByteList());
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), inspected.getByteList());
}
}

@@ -1616,7 +1630,7 @@ public RubyString strip(RubyString string) {
CompilerDirectives.transferToInterpreter();

// Hacky implementation to get something working
return getContext().makeString(string.toString().trim());
return StringNodes.createString(getContext().getCoreLibrary().getStringClass(), string.toString().trim());
}

}
@@ -1635,7 +1649,7 @@ public RubyString dumpAsciiCompatible(RubyString string) {

ByteList outputBytes = dumpCommon(string);

final RubyString result = getContext().makeString(string.getLogicalClass(), outputBytes);
final RubyString result = StringNodes.createString(string.getLogicalClass(), outputBytes);
result.getByteList().setEncoding(string.getByteList().getEncoding());
result.setCodeRange(StringSupport.CR_7BIT);

@@ -1658,7 +1672,7 @@ public RubyString dump(RubyString string) {
outputBytes.append((byte) '"');
outputBytes.append((byte) ')');

final RubyString result = getContext().makeString(string.getLogicalClass(), outputBytes);
final RubyString result = StringNodes.createString(string.getLogicalClass(), outputBytes);
result.getByteList().setEncoding(ASCIIEncoding.INSTANCE);
result.setCodeRange(StringSupport.CR_7BIT);

@@ -1742,7 +1756,7 @@ public RubyString scan(VirtualFrame frame, RubyString string, RubyRegexp regexp,
}

final Object[] captures = ((RubyMatchData) matchData).getCaptures();
yield(frame, block, new RubyArray(context.getCoreLibrary().getArrayClass(), captures, captures.length));
yield(frame, block, ArrayNodes.createArray(context.getCoreLibrary().getArrayClass(), captures, captures.length));

lastGoodMatchData = matchData;
end = StringSupport.positionEndForScan(string.getByteList(), matcher, encoding, p, range);
@@ -1914,9 +1928,9 @@ public SuccNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public RubyString succ(RubyString string) {
if (length(string) > 0) {
return getContext().makeString(string.getLogicalClass(), StringSupport.succCommon(getContext().getRuntime(), string.getByteList()));
return StringNodes.createString(string.getLogicalClass(), StringSupport.succCommon(getContext().getRuntime(), string.getByteList()));
} else {
return getContext().makeString(string.getLogicalClass(), "");
return StringNodes.createEmptyString(string.getLogicalClass());
}
}
}
@@ -2245,7 +2259,7 @@ public UpcaseNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public RubyString upcase(RubyString string) {
final ByteList byteListString = StringNodesHelper.upcase(getContext().getRuntime(), string.getByteList());
return string.getContext().makeString(string.getLogicalClass(), byteListString);
return StringNodes.createString(string.getLogicalClass(), byteListString);
}

}
@@ -2458,7 +2472,7 @@ public static class StringAllocator implements Allocator {

@Override
public RubyBasicObject allocate(RubyContext context, RubyClass rubyClass, Node currentNode) {
return new RubyString(rubyClass, new ByteList());
return createString(rubyClass, new ByteList());
}

}
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ public AllSymbolsNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyArray allSymbols() {
final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());
final RubyArray array = createEmptyArray();

for (RubySymbol s : getContext().getSymbolTable().allSymbols()) {
ArrayNodes.slowPush(array, s);
@@ -225,7 +225,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public RubyString toS(RubySymbol symbol) {
return getContext().makeString(symbol.getSymbolBytes().dup());
return createString(symbol.getSymbolBytes().dup());
}

}
@@ -240,7 +240,7 @@ public InspectNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString inspect(RubySymbol symbol) {
return getContext().makeString(symbol.getJRubySymbol().inspect(getContext().getRuntime().getCurrentContext()).asString().decodeString());
return createString(symbol.getJRubySymbol().inspect(getContext().getRuntime().getCurrentContext()).asString().decodeString());
}

}
Original file line number Diff line number Diff line change
@@ -9,26 +9,19 @@
*/
package org.jruby.truffle.nodes.core;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.*;
import com.oracle.truffle.api.source.NullSourceSection;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.truffle.nodes.objects.Allocator;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.backtrace.Activation;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyBignum;
import org.jruby.truffle.runtime.core.RubyClass;
import org.jruby.truffle.runtime.core.RubyString;

import java.math.BigInteger;
import java.util.EnumSet;
import java.util.concurrent.locks.ReentrantLock;

@CoreClass(name = "Thread::Backtrace::Location")
public class ThreadBacktraceLocationNodes {
@@ -68,12 +61,12 @@ public RubyString absolutePath(RubyBasicObject threadBacktraceLocation) {
final SourceSection sourceSection = activation.getCallNode().getEncapsulatingSourceSection();

if (sourceSection instanceof NullSourceSection) {
return getContext().makeString(sourceSection.getShortDescription());
return createString(sourceSection.getShortDescription());
}

// TODO CS 30-Apr-15: not absolute - not sure how to solve that

return getContext().makeString(sourceSection.getSource().getPath());
return createString(sourceSection.getSource().getPath());
}

}
@@ -112,13 +105,13 @@ public RubyString toS(RubyBasicObject threadBacktraceLocation) {
final SourceSection sourceSection = activation.getCallNode().getEncapsulatingSourceSection();

if (sourceSection instanceof NullSourceSection) {
return getContext().makeString(sourceSection.getShortDescription());
return createString(sourceSection.getShortDescription());
}

return getContext().makeString(String.format("%s:%d:in `%s'",
sourceSection.getSource().getShortName(),
sourceSection.getStartLine(),
sourceSection.getIdentifier()));
return createString(String.format("%s:%d:in `%s'",
sourceSection.getSource().getShortName(),
sourceSection.getStartLine(),
sourceSection.getIdentifier()));
}

}
Original file line number Diff line number Diff line change
@@ -179,7 +179,7 @@ public RubyBasicObject raise(VirtualFrame frame, RubyThread thread, RubyString m

@Specialization
public RubyBasicObject raise(VirtualFrame frame, RubyThread thread, RubyClass exceptionClass, NotProvided message) {
return raise(frame, thread, exceptionClass, getContext().makeString(""));
return raise(frame, thread, exceptionClass, createEmptyString());
}

@Specialization
@@ -224,7 +224,7 @@ public Object status(RubyThread self) {
}
}

return new RubyString(getContext().getCoreLibrary().getStringClass(), self.getStatus().bytes);
return createString(self.getStatus().bytes);
}

}
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ public String visitFrame(FrameInstance frameInstance) {

});

return getContext().makeString(source);
return createString(source);
}

}
@@ -175,7 +175,7 @@ public JavaClassOfNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public RubyString javaClassOf(Object value) {
return getContext().makeString(value.getClass().getSimpleName());
return createString(value.getClass().getSimpleName());
}

}
@@ -196,7 +196,7 @@ public RubyString dumpString(RubyString string) {
builder.append(String.format("\\x%02x", b));
}

return getContext().makeString(builder.toString());
return createString(builder.toString());
}

}
@@ -240,7 +240,7 @@ public GraalVersionNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString graalVersion() {
return getContext().makeString(System.getProperty("graal.version", "unknown"));
return createString(System.getProperty("graal.version", "unknown"));
}

}
@@ -279,8 +279,8 @@ public RubyHash coverageResult() {

for (Map.Entry<Source, Long[]> source : getContext().getCoverageTracker().getCounts().entrySet()) {
final Object[] store = lineCountsStore(source.getValue());
final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, store.length);
keyValues.add(new KeyValue(getContext().makeString(source.getKey().getPath()), array));
final RubyArray array = createArray(store, store.length);
keyValues.add(new KeyValue(createString(source.getKey().getPath()), array));
}

return HashOperations.verySlowFromEntries(getContext(), keyValues, false);
@@ -437,7 +437,7 @@ public HomeDirectoryNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString homeDirectory() {
return getContext().makeString(getContext().getRuntime().getJRubyHome());
return createString(getContext().getRuntime().getJRubyHome());
}

}
@@ -451,7 +451,7 @@ public HostOSNode(RubyContext context, SourceSection sourceSection) {

@Specialization
public RubyString hostOS() {
return getContext().makeString(RbConfigLibrary.getOSName());
return createString(RbConfigLibrary.getOSName());
}

}
Original file line number Diff line number Diff line change
@@ -225,7 +225,7 @@ public Object sourceLocation(RubyBasicObject unboundMethod) {
if (sourceSection instanceof NullSourceSection) {
return nil();
} else {
RubyString file = getContext().makeString(sourceSection.getSource().getName());
RubyString file = createString(sourceSection.getSource().getName());
return ArrayNodes.fromObjects(getContext().getCoreLibrary().getArrayClass(),
file, sourceSection.getStartLine());
}
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ private RubyArray executeSingle(VirtualFrame frame, Object store, int length) {
store = arrayBuilderNode.append(store, length, childObject);
length++;
}
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilderNode.finish(store, length), length);
return ArrayNodes.createGeneralArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilderNode.finish(store, length), length);
}

@ExplodeLoop
@@ -83,7 +83,7 @@ private RubyArray executeRubyArray(VirtualFrame frame, Object store, int length)
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilderNode.finish(store, length), length);
return ArrayNodes.createGeneralArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilderNode.finish(store, length), length);
}

@ExplodeLoop
Original file line number Diff line number Diff line change
@@ -35,17 +35,17 @@ public ArrayDropTailNode(RubyContext context, SourceSection sourceSection, int i
public RubyArray getHeadNull(RubyArray array) {
CompilerDirectives.transferToInterpreter();

return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
}

@Specialization(guards = "isIntegerFixnum(array)")
public RubyArray getHeadIntegerFixnum(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((int[]) ArrayNodes.getStore(array), 0, ArrayNodes.getSize(array) - index), ArrayNodes.getSize(array) - index);
return createArray(ArrayUtils.extractRange((int[]) ArrayNodes.getStore(array), 0, ArrayNodes.getSize(array) - index), ArrayNodes.getSize(array) - index);
}
}

@@ -54,10 +54,10 @@ public RubyArray geHeadLongFixnum(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
final int size = ArrayNodes.getSize(array) - index;
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((long[]) ArrayNodes.getStore(array), 0, size), size);
return createArray(ArrayUtils.extractRange((long[]) ArrayNodes.getStore(array), 0, size), size);
}
}

@@ -66,10 +66,10 @@ public RubyArray getHeadFloat(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
final int size = ArrayNodes.getSize(array) - index;
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((double[]) ArrayNodes.getStore(array), 0, size), size);
return createArray(ArrayUtils.extractRange((double[]) ArrayNodes.getStore(array), 0, size), size);
}
}

@@ -78,10 +78,10 @@ public RubyArray getHeadObject(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
final int size = ArrayNodes.getSize(array) - index;
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((Object[]) ArrayNodes.getStore(array), 0, size), size);
return createArray(ArrayUtils.extractRange((Object[]) ArrayNodes.getStore(array), 0, size), size);
}
}

Original file line number Diff line number Diff line change
@@ -36,27 +36,27 @@ public ArrayDupNode(RubyContext context, SourceSection sourceSection) {

@Specialization(guards = "isNull(from)")
public RubyArray dupNull(RubyArray from) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
return createEmptyArray();
}

@Specialization(guards = "isIntegerFixnum(from)")
public RubyArray dupIntegerFixnum(RubyArray from) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((int[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
return createArray(Arrays.copyOf((int[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
}

@Specialization(guards = "isLongFixnum(from)")
public RubyArray dupLongFixnum(RubyArray from) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((long[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
return createArray(Arrays.copyOf((long[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
}

@Specialization(guards = "isFloat(from)")
public RubyArray dupFloat(RubyArray from) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((double[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
return createArray(Arrays.copyOf((double[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
}

@Specialization(guards = "isObject(from)")
public RubyArray dupObject(RubyArray from) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((Object[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
return createArray(Arrays.copyOf((Object[]) ArrayNodes.getStore(from), ArrayNodes.getSize(from)), ArrayNodes.getSize(from));
}

}
Original file line number Diff line number Diff line change
@@ -35,17 +35,17 @@ public ArrayGetTailNode(RubyContext context, SourceSection sourceSection, int in
public RubyArray getTailNull(RubyArray array) {
CompilerDirectives.transferToInterpreter();

return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
}

@Specialization(guards = "isIntegerFixnum(array)")
public RubyArray getTailIntegerFixnum(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((int[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
return createArray(ArrayUtils.extractRange((int[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
}
}

@@ -54,9 +54,9 @@ public RubyArray getTailLongFixnum(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((long[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
return createArray(ArrayUtils.extractRange((long[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
}
}

@@ -65,9 +65,9 @@ public RubyArray getTailFloat(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((double[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
return createArray(ArrayUtils.extractRange((double[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
}
}

@@ -76,9 +76,9 @@ public RubyArray getTailObject(RubyArray array) {
CompilerDirectives.transferToInterpreter();

if (index >= ArrayNodes.getSize(array)) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((Object[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
return createArray(ArrayUtils.extractRange((Object[]) ArrayNodes.getStore(array), index, ArrayNodes.getSize(array)), ArrayNodes.getSize(array) - index);
}
}

Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ public EmptyArrayLiteralNode(RubyContext context, SourceSection sourceSection, R

@Override
public RubyArray executeRubyArray(VirtualFrame frame) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
return createEmptyArray();
}

}
@@ -113,7 +113,7 @@ public RubyArray executeRubyArray(VirtualFrame frame) {
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), executedValues, values.length);
return createArray(executedValues, values.length);
}

private RubyArray makeGeneric(VirtualFrame frame,
@@ -148,7 +148,7 @@ public RubyArray executeRubyArray(VirtualFrame frame) {
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), executedValues, values.length);
return createArray(executedValues, values.length);
}

private RubyArray makeGeneric(VirtualFrame frame,
@@ -183,7 +183,7 @@ public RubyArray executeRubyArray(VirtualFrame frame) {
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), executedValues, values.length);
return createArray(executedValues, values.length);
}

private RubyArray makeGeneric(VirtualFrame frame,
@@ -214,7 +214,7 @@ public RubyArray executeRubyArray(VirtualFrame frame) {
executedValues[n] = values[n].execute(frame);
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), executedValues, values.length);
return createArray(executedValues, values.length);
}

}
212 changes: 109 additions & 103 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/array/ArrayNodes.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ public Object execute(VirtualFrame frame) {

final RubyArray originalArray = (RubyArray) arrayObject;

final RubyArray newArray = new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayNodes.slowToArray(originalArray), ArrayNodes.getSize(originalArray));
final RubyArray newArray = createArray(ArrayNodes.slowToArray(originalArray), ArrayNodes.getSize(originalArray));
ArrayNodes.slowPush(newArray, pushed.execute(frame));
return newArray;
}
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public RubyBasicObject readNegativeLength(RubyArray array, int index, int length
guards={"indexInBounds(array, index)", "lengthPositive(length)", "isNullArray(array)"}
)
public RubyArray readNull(RubyArray array, int index, int length) {
return new RubyArray(array.getLogicalClass(), null, 0);
return ArrayNodes.createEmptyArray(array.getLogicalClass());
}

// Reading within bounds on an array with actual storage
@@ -67,31 +67,31 @@ public RubyArray readNull(RubyArray array, int index, int length) {
guards={"indexInBounds(array, index)", "lengthPositive(length)", "endInBounds(array, index, length)", "isIntArray(array)"}
)
public RubyArray readIntInBounds(RubyArray array, int index, int length) {
return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((int[]) ArrayNodes.getStore(array), index, index + length), length);
}

@Specialization(
guards={"indexInBounds(array, index)", "lengthPositive(length)", "endInBounds(array, index, length)", "isLongArray(array)"}
)
public RubyArray readLongInBounds(RubyArray array, int index, int length) {
return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((long[]) ArrayNodes.getStore(array), index, index + length), length);
}

@Specialization(
guards={"indexInBounds(array, index)", "lengthPositive(length)", "endInBounds(array, index, length)", "isDoubleArray(array)"}
)
public RubyArray readDoubleInBounds(RubyArray array, int index, int length) {
return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((double[]) ArrayNodes.getStore(array), index, index + length), length);
}

@Specialization(
guards={"indexInBounds(array, index)", "lengthPositive(length)", "endInBounds(array, index, length)", "isObjectArray(array)"}
)
public RubyArray readObjectInBounds(RubyArray array, int index, int length) {
return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((Object[]) ArrayNodes.getStore(array), index, index + length), length);
}

@@ -103,7 +103,7 @@ public RubyArray readObjectInBounds(RubyArray array, int index, int length) {
public RubyArray readIntOutOfBounds(RubyArray array, int index, int length) {
final int clampedLength = Math.min(ArrayNodes.getSize(array), index + length) - index;

return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((int[]) ArrayNodes.getStore(array), index, index + clampedLength), clampedLength);
}

@@ -113,7 +113,7 @@ public RubyArray readIntOutOfBounds(RubyArray array, int index, int length) {
public RubyArray readLongOutOfBounds(RubyArray array, int index, int length) {
final int clampedLength = Math.min(ArrayNodes.getSize(array), index + length) - index;

return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((long[]) ArrayNodes.getStore(array), index, index + clampedLength), clampedLength);
}

@@ -123,7 +123,7 @@ public RubyArray readLongOutOfBounds(RubyArray array, int index, int length) {
public RubyArray readDoubleOutOfBounds(RubyArray array, int index, int length) {
final int clampedLength = Math.min(ArrayNodes.getSize(array), index + length) - index;

return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((double[]) ArrayNodes.getStore(array), index, index + clampedLength), clampedLength);
}

@@ -133,7 +133,7 @@ public RubyArray readDoubleOutOfBounds(RubyArray array, int index, int length) {
public RubyArray readObjectOutOfBounds(RubyArray array, int index, int length) {
final int clampedLength = Math.min(ArrayNodes.getSize(array), index + length) - index;

return new RubyArray(array.getLogicalClass(),
return ArrayNodes.createArray(array.getLogicalClass(),
Arrays.copyOfRange((Object[]) ArrayNodes.getStore(array), index, index + clampedLength), clampedLength);
}

Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public ArraySliceNode(RubyContext context, SourceSection sourceSection, int from
public RubyArray sliceNull(RubyArray array) {
CompilerDirectives.transferToInterpreter();

return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
}

@Specialization(guards = "isIntegerFixnum(array)")
@@ -48,9 +48,9 @@ public RubyArray sliceIntegerFixnum(RubyArray array) {
final int to = ArrayNodes.getSize(array) + this.to;

if (from >= to) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((int[]) ArrayNodes.getStore(array), from, to), to - from);
return createArray(ArrayUtils.extractRange((int[]) ArrayNodes.getStore(array), from, to), to - from);
}
}

@@ -60,9 +60,9 @@ public RubyArray sliceLongFixnum(RubyArray array) {
final int to = ArrayNodes.getSize(array) + this.to;

if (from >= to) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((long[]) ArrayNodes.getStore(array), from, to), to - from);
return createArray(ArrayUtils.extractRange((long[]) ArrayNodes.getStore(array), from, to), to - from);
}
}

@@ -72,9 +72,9 @@ public RubyArray sliceFloat(RubyArray array) {
final int to = ArrayNodes.getSize(array) + this.to;

if (from >= to) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((double[]) ArrayNodes.getStore(array), from, to), to - from);
return createArray(ArrayUtils.extractRange((double[]) ArrayNodes.getStore(array), from, to), to - from);
}
}

@@ -84,9 +84,9 @@ public RubyArray sliceObject(RubyArray array) {
final int to = ArrayNodes.getSize(array) + this.to;

if (from >= to) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass());
return createEmptyArray();
} else {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange((Object[]) ArrayNodes.getStore(array), from, to), to - from);
return createArray(ArrayUtils.extractRange((Object[]) ArrayNodes.getStore(array), from, to), to - from);
}
}

Original file line number Diff line number Diff line change
@@ -1087,13 +1087,13 @@ public InspectNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString inspect(int n) {
return getContext().makeString(Integer.toString(n));
return createString(Integer.toString(n));
}

@TruffleBoundary
@Specialization
public RubyString inspect(long n) {
return getContext().makeString(Long.toString(n));
return createString(Long.toString(n));
}

}
@@ -1141,13 +1141,13 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization
public RubyString toS(int n, NotProvided base) {
return getContext().makeString(Integer.toString(n));
return createString(Integer.toString(n));
}

@TruffleBoundary
@Specialization
public RubyString toS(long n, NotProvided base) {
return getContext().makeString(Long.toString(n));
return createString(Long.toString(n));
}

@TruffleBoundary
@@ -1158,7 +1158,7 @@ public RubyString toS(long n, int base) {
throw new RaiseException(getContext().getCoreLibrary().argumentErrorInvalidRadix(base, this));
}

return getContext().makeString(Long.toString(n, base));
return createString(Long.toString(n, base));
}

}
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ public EmptyHashLiteralNode(RubyContext context, SourceSection sourceSection) {
@ExplodeLoop
@Override
public RubyHash executeRubyHash(VirtualFrame frame) {
return new RubyHash(getContext().getCoreLibrary().getHashClass(), null, null, null, 0, null);
return HashNodes.createEmptyHash(getContext().getCoreLibrary().getHashClass());
}

}
@@ -147,7 +147,7 @@ public RubyHash executeRubyHash(VirtualFrame frame) {
size++;
}

return new RubyHash(getContext().getCoreLibrary().getHashClass(), null, null, store, size, null);
return HashNodes.createHash(getContext().getCoreLibrary().getHashClass(), null, null, store, size, null);
}

}
Original file line number Diff line number Diff line change
@@ -99,6 +99,18 @@ public static void setLastInSequence(RubyHash hash, Entry lastInSequence) {
hash.lastInSequence = lastInSequence;
}

public static RubyHash createEmptyHash(RubyClass hashClass) {
return createHash(hashClass, null, null, null, 0, null);
}

public static RubyHash createHash(RubyClass hashClass, Object[] store, int size) {
return createHash(hashClass, null, null, (Object) store, size, null);
}

public static RubyHash createHash(RubyClass hashClass, RubyProc defaultBlock, Object defaultValue, Object store, int size, Entry firstInSequence) {
return new RubyHash(hashClass, defaultBlock, defaultValue, store, size, firstInSequence);
}

@CoreMethod(names = "[]", constructor = true, argumentsAsArray = true)
@ImportStatic(HashGuards.class)
public abstract static class ConstructNode extends CoreMethodArrayArgumentsNode {
@@ -147,7 +159,7 @@ public Object construct(VirtualFrame frame, RubyClass hashClass, Object[] args)
}
}

return new RubyHash(hashClass, null, null, newStore, size, null);
return createHash(hashClass, newStore, size);
}

@Specialization
@@ -675,7 +687,7 @@ public RubyHash eachPackedArray(VirtualFrame frame, RubyHash hash, RubyProc bloc
}

if (n < size) {
yield(frame, block, new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{PackedArrayStrategy.getKey(store, n), PackedArrayStrategy.getValue(store, n)}, 2));
yield(frame, block, createArray(new Object[]{PackedArrayStrategy.getKey(store, n), PackedArrayStrategy.getValue(store, n)}, 2));
}
}
} finally {
@@ -692,7 +704,7 @@ public RubyHash eachBuckets(VirtualFrame frame, RubyHash hash, RubyProc block) {
assert HashOperations.verifyStore(hash);

for (KeyValue keyValue : verySlowToKeyValues(hash)) {
yield(frame, block, new RubyArray(getContext().getCoreLibrary().getArrayClass(), new Object[]{keyValue.getKey(), keyValue.getValue()}, 2));
yield(frame, block, createArray(new Object[]{keyValue.getKey(), keyValue.getValue()}, 2));
}

return hash;
@@ -853,7 +865,7 @@ public MapNode(RubyContext context, SourceSection sourceSection) {
public RubyArray mapNull(VirtualFrame frame, RubyHash hash, RubyProc block) {
assert HashOperations.verifyStore(hash);

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
return createEmptyArray();
}

@ExplodeLoop
@@ -886,7 +898,7 @@ public RubyArray mapPackedArray(VirtualFrame frame, RubyHash hash, RubyProc bloc
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), result, size);
return createArray(result, size);
}

@Specialization(guards = "isBucketsStorage(hash)")
@@ -895,7 +907,7 @@ public RubyArray mapBuckets(VirtualFrame frame, RubyHash hash, RubyProc block) {

assert HashOperations.verifyStore(hash);

final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
final RubyArray array = createEmptyArray();

for (KeyValue keyValue : HashOperations.verySlowToKeyValues(hash)) {
ArrayNodes.slowPush(array, yield(frame, block, keyValue.getKey(), keyValue.getValue()));
@@ -928,7 +940,7 @@ public MergeNode(RubyContext context, SourceSection sourceSection) {
public RubyHash mergePackedArrayNull(RubyHash hash, RubyHash other, NotProvided block) {
final Object[] store = (Object[]) getStore(hash);
final Object[] copy = PackedArrayStrategy.copyStore(store);
return new RubyHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), copy, getSize(hash), null);
return createHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), copy, getSize(hash), null);
}

@ExplodeLoop
@@ -973,14 +985,14 @@ public RubyHash mergePackedArrayPackedArray(VirtualFrame frame, RubyHash hash, R

if (mergeFromACount == 0) {
nothingFromFirstProfile.enter();
return new RubyHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), PackedArrayStrategy.copyStore(storeB), storeBSize, null);
return createHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), PackedArrayStrategy.copyStore(storeB), storeBSize, null);
}

considerNothingFromSecondProfile.enter();

if (conflictsCount == storeBSize) {
nothingFromSecondProfile.enter();
return new RubyHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), PackedArrayStrategy.copyStore(storeA), storeASize, null);
return createHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), PackedArrayStrategy.copyStore(storeA), storeASize, null);
}

considerResultIsSmallProfile.enter();
@@ -1012,7 +1024,7 @@ public RubyHash mergePackedArrayPackedArray(VirtualFrame frame, RubyHash hash, R
index++;
}

return new RubyHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), merged, mergedSize, null);
return createHash(hash.getLogicalClass(), getDefaultBlock(hash), getDefaultValue(hash), merged, mergedSize, null);
}

CompilerDirectives.transferToInterpreter();
@@ -1025,7 +1037,7 @@ public RubyHash mergePackedArrayPackedArray(VirtualFrame frame, RubyHash hash, R
public RubyHash mergeBucketsBuckets(RubyHash hash, RubyHash other, NotProvided block) {
CompilerDirectives.transferToInterpreter();

final RubyHash merged = new RubyHash(hash.getLogicalClass(), null, null, new Entry[BucketsStrategy.capacityGreaterThan(getSize(hash) + getSize(other))], 0, null);
final RubyHash merged = createHash(hash.getLogicalClass(), null, null, new Entry[BucketsStrategy.capacityGreaterThan(getSize(hash) + getSize(other))], 0, null);

int size = 0;

@@ -1051,7 +1063,7 @@ public RubyHash mergeBucketsBuckets(RubyHash hash, RubyHash other, NotProvided b
public RubyHash merge(VirtualFrame frame, RubyHash hash, RubyHash other, RubyProc block) {
CompilerDirectives.transferToInterpreter();

final RubyHash merged = new RubyHash(hash.getLogicalClass(), null, null, new Entry[BucketsStrategy.capacityGreaterThan(getSize(hash) + getSize(other))], 0, null);
final RubyHash merged = createHash(hash.getLogicalClass(), null, null, new Entry[BucketsStrategy.capacityGreaterThan(getSize(hash) + getSize(other))], 0, null);

int size = 0;

@@ -1392,7 +1404,7 @@ public static class HashAllocator implements Allocator {

@Override
public RubyBasicObject allocate(RubyContext context, RubyClass rubyClass, Node currentNode) {
return new RubyHash(rubyClass, null, null, null, 0, null);
return createEmptyHash(rubyClass);
}

}
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;

public class DefinedWrapperNode extends RubyNode {
@@ -33,7 +34,7 @@ public Object execute(VirtualFrame frame) {

@Override
public Object isDefined(VirtualFrame frame) {
return getContext().makeString(definition);
return createString(definition);
}

}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import org.jruby.truffle.nodes.cast.BooleanCastNodeGen;
import org.jruby.truffle.nodes.cast.ProcOrNullNode;
import org.jruby.truffle.nodes.cast.ProcOrNullNodeGen;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.nodes.core.hash.HashLiteralNode;
import org.jruby.truffle.nodes.literal.LiteralNode;
@@ -384,7 +385,7 @@ public Object isDefined(VirtualFrame frame) {
final Object self = RubyArguments.getSelf(frame.getArguments());

if (method == null) {
final Object r = respondToMissing.call(frame, receiverObject, "respond_to_missing?", null, context.makeString(methodName));
final Object r = respondToMissing.call(frame, receiverObject, "respond_to_missing?", null, createString(methodName));

if (r != DispatchNode.MISSING && !respondToMissingCast.executeBoolean(frame, r)) {
return nil();
@@ -395,7 +396,7 @@ public Object isDefined(VirtualFrame frame) {
return nil();
}

return context.makeString("method");
return createString("method");
}

public String getName() {
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@
*/
package org.jruby.truffle.nodes.ext;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.*;
@@ -19,6 +18,7 @@
import org.jruby.truffle.nodes.core.CoreClass;
import org.jruby.truffle.nodes.core.CoreMethod;
import org.jruby.truffle.nodes.core.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyString;
@@ -206,7 +206,7 @@ public RubyString digest(RubyBasicObject digestObject) {
throw new RuntimeException(e);
}

return getContext().makeString(clonedDigest.digest());
return createString(clonedDigest.digest());
}

}
@@ -237,7 +237,7 @@ public BubbleBabbleNode(RubyContext context, SourceSection sourceSection) {
@Specialization
public RubyString bubblebabble(RubyString message) {
final ByteList byteList = message.getByteList();
return getContext().makeString(BubbleBabble.bubblebabble(byteList.unsafeBytes(), byteList.begin(), byteList.length()));
return createString(BubbleBabble.bubblebabble(byteList.unsafeBytes(), byteList.begin(), byteList.length()));
}

}
13 changes: 3 additions & 10 deletions truffle/src/main/java/org/jruby/truffle/nodes/ext/ZlibNodes.java
Original file line number Diff line number Diff line change
@@ -10,28 +10,21 @@
package org.jruby.truffle.nodes.ext;

import com.jcraft.jzlib.JZlib;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.*;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.ext.digest.BubbleBabble;
import org.jruby.truffle.nodes.core.CoreClass;
import org.jruby.truffle.nodes.core.CoreMethod;
import org.jruby.truffle.nodes.core.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.NotProvided;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.util.ByteList;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.EnumSet;
import java.util.zip.CRC32;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
@@ -112,7 +105,7 @@ public RubyString deflate(RubyString message, int level) {

deflater.end();

return getContext().makeString(outputBytes);
return createString(outputBytes);
}

}
@@ -150,7 +143,7 @@ public RubyString inflate(RubyString message) {

inflater.end();

return getContext().makeString(outputBytes);
return createString(outputBytes);
}

}
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.ThreadLocalObjectNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.nodes.objects.ReadInstanceVariableNode;
@@ -34,7 +35,7 @@ public ReadLastBacktraceNode(RubyContext context, SourceSection sourceSection) {

@Override
public Object isDefined(VirtualFrame frame) {
return getContext().makeString("global-variable");
return createString("global-variable");
}

@Override
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyMatchData;

@@ -73,7 +74,7 @@ public Object execute(VirtualFrame frame) {
@Override
public Object isDefined(VirtualFrame frame) {
if (execute(frame) != nil()) {
return getContext().makeString("global-variable");
return createString("global-variable");
} else {
return nil();
}
Original file line number Diff line number Diff line change
@@ -9,12 +9,12 @@
*/
package org.jruby.truffle.nodes.interop;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyString;

@@ -36,6 +36,6 @@ public Object doObject(VirtualFrame frame, Object index) {

@TruffleBoundary
private RubyString toString(String index) {
return getContext().makeString(index);
return createString(index);
}
}
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.util.ByteList;
@@ -30,7 +31,7 @@ public StringLiteralNode(RubyContext context, SourceSection sourceSection, ByteL

@Override
public RubyString execute(VirtualFrame frame) {
final RubyString string = new RubyString(getContext().getCoreLibrary().getStringClass(), bytes.dup());
final RubyString string = createString(bytes.dup());
string.setCodeRange(codeRange);

return string;
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.translator.ReadNode;
@@ -56,12 +57,12 @@ public Object isDefined(VirtualFrame frame) {
if (Translator.FRAME_LOCAL_GLOBAL_VARIABLES.contains(readFrameSlotNode.getFrameSlot().getIdentifier())) {
if (ALWAYS_DEFINED_GLOBALS.contains(readFrameSlotNode.getFrameSlot().getIdentifier())
|| readFrameSlotNode.executeRead(RubyArguments.getDeclarationFrame(frame, frameDepth)) != nil()) {
return getContext().makeString("global-variable");
return createString("global-variable");
} else {
return nil();
}
} else {
return getContext().makeString("local-variable");
return createString("local-variable");
}
}

Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.translator.ReadNode;
import org.jruby.truffle.translator.Translator;
@@ -46,12 +47,12 @@ public Object isDefined(VirtualFrame frame) {
if (Translator.FRAME_LOCAL_GLOBAL_VARIABLES.contains(readFrameSlotNode.getFrameSlot().getIdentifier())) {
if (Translator.ALWAYS_DEFINED_GLOBALS.contains(readFrameSlotNode.getFrameSlot().getIdentifier())
|| readFrameSlotNode.executeRead(frame) != nil()) {
return getContext().makeString("global-variable");
return createString("global-variable");
} else {
return nil();
}
} else {
return getContext().makeString("local-variable");
return createString("local-variable");
}
}

Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.translator.WriteNode;
@@ -46,7 +47,7 @@ public RubyNode makeReadNode() {

@Override
public Object isDefined(VirtualFrame frame) {
return getContext().makeString("assignment");
return createString("assignment");
}

}
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.translator.WriteNode;

@@ -39,7 +40,7 @@ public RubyNode makeReadNode() {

@Override
public Object isDefined(VirtualFrame frame) {
return getContext().makeString("assignment");
return createString("assignment");
}

}
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.LexicalScope;
import org.jruby.truffle.runtime.ModuleOperations;
import org.jruby.truffle.runtime.RubyContext;
@@ -55,7 +56,7 @@ public Object isDefined(VirtualFrame frame) {
if (value == null) {
return nil();
} else {
return getContext().makeString("class variable");
return createString("class variable");
}
}

Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.objectstorage.ReadHeadObjectFieldNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyBasicObject;
@@ -99,12 +100,12 @@ public Object isDefined(VirtualFrame frame) {
final RubyBasicObject receiverValue = (RubyBasicObject) receiver.execute(frame);

if (readNode.getName().equals("$~") || readNode.getName().equals("$!")) {
return getContext().makeString("global-variable");
return createString("global-variable");
} else if (readNode.isSet(receiverValue)) {
if (readNode.execute(receiverValue) == nil()) {
return nil();
} else {
return getContext().makeString("global-variable");
return createString("global-variable");
}
} else {
return nil();
@@ -122,7 +123,7 @@ public Object isDefined(VirtualFrame frame) {
final Property storageLocation = layout.getProperty(readNode.getName());

if (storageLocation != null) {
return context.makeString("instance-variable");
return createString("instance-variable");
} else {
return nil();
}
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ValueProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;

@@ -31,7 +32,7 @@ public Object execute(VirtualFrame frame) {

@Override
public Object isDefined(VirtualFrame frame) {
return getContext().makeString("self");
return createString("self");
}

}
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.LexicalScope;
import org.jruby.truffle.runtime.ModuleOperations;
import org.jruby.truffle.runtime.RubyContext;
@@ -46,7 +47,7 @@ public Object execute(VirtualFrame frame) {

@Override
public Object isDefined(VirtualFrame frame) {
return getContext().makeString("assignment");
return createString("assignment");
}

}
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.objectstorage.WriteHeadObjectFieldNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
@@ -117,7 +118,7 @@ public RubyNode makeReadNode() {

@Override
public Object isDefined(VirtualFrame frame) {
return getContext().makeString("assignment");
return createString("assignment");
}

}
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
*/
package org.jruby.truffle.nodes.rubinius;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.UnexpectedResultException;
@@ -46,6 +45,7 @@

import jnr.constants.platform.Errno;

import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.objectstorage.ReadHeadObjectFieldNode;
import org.jruby.truffle.nodes.objectstorage.WriteHeadObjectFieldNode;
import org.jruby.truffle.runtime.RubyContext;
@@ -147,14 +147,14 @@ public Object read(RubyBasicObject dir) {
writePositionNode.execute(dir, position + 1);

if (position == -2) {
return getContext().makeString(".");
return createString(".");
} else if (position == -1) {
return getContext().makeString("..");
return createString("..");
} else {
final String[] contents = (String[]) readContentsNode.execute(dir);

if (position < contents.length) {
return getContext().makeString(contents[position]);
return createString(contents[position]);
} else {
return nil();
}
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import org.jcodings.Ptr;
import org.jcodings.transcode.EConv;
import org.jcodings.transcode.EConvResult;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.runtime.RubyContext;
@@ -181,7 +182,7 @@ private RubyString putback(RubyEncodingConverter encodingConverter, int n) {
bytes.setEncoding(ec.sourceEncoding);
}

return getContext().makeString(bytes);
return createString(bytes);
}
}

@@ -213,9 +214,9 @@ public Object encodingConverterLastError(VirtualFrame frame, RubyEncodingConvert
Object ret = newLookupTableNode.call(frame, getContext().getCoreLibrary().getLookupTableClass(), "new", null);

lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("result"), eConvResultToSymbol(lastError.getResult()));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("source_encoding_name"), getContext().makeString(new ByteList(lastError.getSource())));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("destination_encoding_name"), getContext().makeString(new ByteList(lastError.getDestination())));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("error_bytes"), getContext().makeString(new ByteList(lastError.getErrorBytes())));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("source_encoding_name"), createString(new ByteList(lastError.getSource())));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("destination_encoding_name"), createString(new ByteList(lastError.getDestination())));
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("error_bytes"), createString(new ByteList(lastError.getErrorBytes())));

if (lastError.getReadAgainLength() != 0) {
lookupTableWriteNode.call(frame, ret, "[]=", null, getContext().getSymbol("read_again_bytes"), lastError.getReadAgainLength());
@@ -256,19 +257,19 @@ public Object encodingConverterLastError(RubyEncodingConverter encodingConverter
final Object[] ret = { getContext().getSymbol(ec.lastError.getResult().symbolicName()), nil(), nil(), nil(), nil() };

if (ec.lastError.getSource() != null) {
ret[1] = getContext().makeString(new ByteList(ec.lastError.getSource()));
ret[1] = createString(new ByteList(ec.lastError.getSource()));
}

if (ec.lastError.getDestination() != null) {
ret[2] = getContext().makeString(new ByteList(ec.lastError.getDestination()));
ret[2] = createString(new ByteList(ec.lastError.getDestination()));
}

if (ec.lastError.getErrorBytes() != null) {
ret[3] = getContext().makeString(new ByteList(ec.lastError.getErrorBytes(), ec.lastError.getErrorBytesP(), ec.lastError.getErrorBytesLength()));
ret[4] = getContext().makeString(new ByteList(ec.lastError.getErrorBytes(), ec.lastError.getErrorBytesP() + ec.lastError.getErrorBytesLength(), ec.lastError.getReadAgainLength()));
ret[3] = createString(new ByteList(ec.lastError.getErrorBytes(), ec.lastError.getErrorBytesP(), ec.lastError.getErrorBytesLength()));
ret[4] = createString(new ByteList(ec.lastError.getErrorBytes(), ec.lastError.getErrorBytesP() + ec.lastError.getErrorBytesLength(), ec.lastError.getReadAgainLength()));
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), ret, ret.length);
return createArray(ret, ret.length);
}

}
Original file line number Diff line number Diff line change
@@ -9,21 +9,20 @@
*/
package org.jruby.truffle.nodes.rubinius;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ConditionProfile;

import org.jruby.truffle.nodes.core.BignumNodes;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.nodes.dispatch.DoesRespondDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.MissingBehavior;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyBignum;

import java.math.BigInteger;

@@ -46,12 +45,12 @@ public FixnumCoercePrimitiveNode(RubyContext context, SourceSection sourceSectio

@Specialization
public RubyArray coerce(int a, int b) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new int[]{b, a}, 2);
return createArray(new int[]{b, a}, 2);
}

@Specialization
public RubyArray coerce(long a, int b) {
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), new long[]{b, a}, 2);
return createArray(new long[]{b, a}, 2);
}

@Specialization(guards = "!isInteger(b)")
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@
import com.oracle.truffle.api.utilities.BranchProfile;

import org.jruby.truffle.nodes.core.FixnumOrBignumNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyArray;
@@ -62,8 +64,8 @@ public RubyArray dToA(double value) {

final int sign = value < 0 ? 1 : 0;

return new RubyArray(getContext().getCoreLibrary().getArrayClass(),
new Object[]{getContext().makeString(string), decimal, sign, string.length()}, 4);
return ArrayNodes.createArray(getContext().getCoreLibrary().getArrayClass(),
new Object[]{createString(string), decimal, sign, string.length()}, 4);
}

}
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@
import jnr.ffi.byref.IntByReference;

import org.jruby.RubyEncoding;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
@@ -554,7 +555,7 @@ public RubyString sysread(VirtualFrame frame, RubyBasicObject file, int length)
toRead -= readIteration;
}

return getContext().makeString(buffer);
return createString(buffer);
}

}
@@ -641,7 +642,7 @@ private RubyArray getSetObjects(Object[] objects, int[] fds, FDSet set) {
}
}

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), setObjects, setFdsCount);
return createArray(setObjects, setFdsCount);
}

}
Original file line number Diff line number Diff line change
@@ -18,12 +18,12 @@

import jnr.ffi.Pointer;

import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.nodes.objects.Allocator;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyClass;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.rubinius.RubiniusConfiguration;
import org.jruby.truffle.runtime.rubinius.RubiniusTypes;
import org.jruby.util.ByteList;
import org.jruby.util.unsafe.UnsafeHolder;
@@ -177,7 +177,7 @@ public PointerReadStringPrimitiveNode(RubyContext context, SourceSection sourceS
public RubyString readString(RubyBasicObject pointer, int length) {
final byte[] bytes = new byte[length];
getPointer(pointer).get(0, bytes, 0, length);
return getContext().makeString(bytes);
return createString(bytes);
}

}
@@ -282,7 +282,7 @@ public long getAtOffsetLong(RubyBasicObject pointer, int offset, int type) {

@Specialization(guards = "type == TYPE_STRING")
public RubyString getAtOffsetString(RubyBasicObject pointer, int offset, int type) {
return getContext().makeString(getPointer(pointer).getString(offset));
return createString(getPointer(pointer).getString(offset));
}

@Specialization(guards = "type == TYPE_PTR")
@@ -324,7 +324,7 @@ public PointerReadStringToNullPrimitiveNode(RubyContext context, SourceSection s

@Specialization
public RubyString readStringToNull(RubyBasicObject pointer) {
return getContext().makeString(MemoryIO.getInstance().getZeroTerminatedByteArray(getPointer(pointer).address()));
return createString(MemoryIO.getInstance().getZeroTerminatedByteArray(getPointer(pointer).address()));
}

}
Original file line number Diff line number Diff line change
@@ -19,11 +19,11 @@
import org.jruby.truffle.nodes.core.CoreClass;
import org.jruby.truffle.nodes.core.CoreMethod;
import org.jruby.truffle.nodes.core.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.nodes.core.StringNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.rubinius.RubiniusConfiguration;

import java.nio.charset.StandardCharsets;

@@ -242,16 +242,12 @@ public ReadlinkNode(RubyContext context, SourceSection sourceSection) {
public int readlink(RubyString path, RubyBasicObject pointer, int bufsize) {
final String pathString = RubyEncoding.decodeUTF8(path.getByteList().getUnsafeBytes(), path.getByteList().getBegin(), path.getByteList().getRealSize());

final byte[] buffer = new byte[bufsize];

final int result = posix().readlink(pathString, buffer, bufsize);
final int result = posix().readlink(pathString, PointerPrimitiveNodes.getPointer(pointer), bufsize);
if (result == -1) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().errnoError(posix().errno(), this));
}

PointerPrimitiveNodes.getPointer(pointer).put(0, buffer, 0, buffer.length);

return result;
}

@@ -682,7 +678,7 @@ public GetAddrInfoNode(RubyContext context, SourceSection sourceSection) {

@Specialization(guards = "isNil(hostName)")
public int getaddrinfo(RubyBasicObject hostName, RubyString serviceName, RubyBasicObject hintsPointer, RubyBasicObject resultsPointer) {
return getaddrinfo(getContext().makeString("0.0.0.0"), serviceName, hintsPointer, resultsPointer);
return getaddrinfo(createString("0.0.0.0"), serviceName, hintsPointer, resultsPointer);
}

@Specialization
Original file line number Diff line number Diff line change
@@ -14,9 +14,9 @@
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.ConditionProfile;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.array.ArrayNodes;
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;

public class RubiniusSingleBlockArgNode extends RubyNode {
private final ConditionProfile emptyArgsProfile = ConditionProfile.createBinaryProfile();
@@ -50,7 +50,7 @@ public Object execute(VirtualFrame frame) {
} else {
Object[] extractedArguments = RubyArguments.extractUserArguments(frame.getArguments());

return new RubyArray(getContext().getCoreLibrary().getArrayClass(), extractedArguments, userArgumentCount);
return createArray(extractedArguments, userArgumentCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -182,7 +182,7 @@ private RubyString makeString(RubyString source, int index, int length) {
final ByteList bytes = new ByteList(source.getByteList(), index, length);
bytes.setEncoding(source.getByteList().getEncoding());

final RubyString ret = getContext().makeString(source.getLogicalClass(), bytes);
final RubyString ret = StringNodes.createString(source.getLogicalClass(), bytes);
taintResultNode.maybeTaint(source, ret);

return ret;
@@ -238,7 +238,7 @@ public Object stringByteSubstring(RubyString string, int index, int length) {
}

final byte[] copiedBytes = Arrays.copyOfRange(bytes.getUnsafeBytes(), normalizedIndex, rangeEnd);
final RubyString result = getContext().makeString(string.getLogicalClass(), new ByteList(copiedBytes, string.getByteList().getEncoding()));
final RubyString result = StringNodes.createString(string.getLogicalClass(), new ByteList(copiedBytes, string.getByteList().getEncoding()));

return taintResultNode.maybeTaint(string, result);
}
@@ -476,7 +476,7 @@ public Object stringFindCharacterSingleByte(RubyString string, int offset) {
return nil();
}

final RubyString ret = getContext().makeString(string.getLogicalClass(), new ByteList(string.getByteList().unsafeBytes(), offset, 1));
final RubyString ret = StringNodes.createString(string.getLogicalClass(), new ByteList(string.getByteList().unsafeBytes(), offset, 1));

return propagate(string, ret);
}
@@ -499,9 +499,9 @@ public Object stringFindCharacter(RubyString string, int offset) {

final RubyString ret;
if (StringSupport.MBCLEN_CHARFOUND_P(clen)) {
ret = getContext().makeString(string.getLogicalClass(), new ByteList(string.getByteList().unsafeBytes(), offset, clen));
ret = StringNodes.createString(string.getLogicalClass(), new ByteList(string.getByteList().unsafeBytes(), offset, clen));
} else {
ret = getContext().makeString(string.getLogicalClass(), new ByteList(string.getByteList().unsafeBytes(), offset, 1));
ret = StringNodes.createString(string.getLogicalClass(), new ByteList(string.getByteList().unsafeBytes(), offset, 1));
}

return propagate(string, ret);
@@ -532,7 +532,7 @@ public StringFromCodepointPrimitiveNode(RubyContext context, SourceSection sourc

@Specialization(guards = "isSimple(code, encoding)")
public RubyString stringFromCodepointSimple(int code, RubyEncoding encoding) {
return new RubyString(
return StringNodes.createString(
getContext().getCoreLibrary().getStringClass(),
new ByteList(new byte[]{(byte) code}, encoding.getEncoding()));
}
@@ -561,7 +561,7 @@ public RubyString stringFromCodepoint(int code, RubyEncoding encoding) {
throw new RaiseException(getContext().getCoreLibrary().rangeError(code, encoding, this));
}

return new RubyString(
return StringNodes.createString(
getContext().getCoreLibrary().getStringClass(),
new ByteList(bytes, encoding.getEncoding()));
}
@@ -1050,14 +1050,14 @@ public StringPatternPrimitiveNode(RubyContext context, SourceSection sourceSecti

@Specialization(guards = "value == 0")
public RubyString stringPatternZero(RubyClass stringClass, int size, int value) {
return new RubyString(stringClass, new ByteList(new byte[size]));
return StringNodes.createString(stringClass, new ByteList(new byte[size]));
}

@Specialization(guards = "value != 0")
public RubyString stringPattern(RubyClass stringClass, int size, int value) {
final byte[] bytes = new byte[size];
Arrays.fill(bytes, (byte) value);
return new RubyString(stringClass, new ByteList(bytes));
return StringNodes.createString(stringClass, new ByteList(bytes));
}

@Specialization
@@ -1071,7 +1071,7 @@ public RubyString stringPattern(RubyClass stringClass, int size, RubyString stri
}
}

return new RubyString(stringClass, new ByteList(bytes));
return StringNodes.createString(stringClass, new ByteList(bytes));
}

}
@@ -1242,7 +1242,7 @@ private RubyString makeSubstring(RubyString string, int beg, int len) {
taintResultNode = insert(new TaintResultNode(getContext(), getSourceSection()));
}

final RubyString ret = getContext().makeString(string.getLogicalClass(), new ByteList(string.getByteList(), beg, len));
final RubyString ret = StringNodes.createString(string.getLogicalClass(), new ByteList(string.getByteList(), beg, len));
ret.getByteList().setEncoding(string.getByteList().getEncoding());
taintResultNode.maybeTaint(string, ret);

@@ -1261,7 +1261,7 @@ public StringFromByteArrayPrimitiveNode(RubyContext context, SourceSection sourc
@Specialization
public RubyString stringFromByteArray(RubiniusByteArray bytes, int start, int count) {
// Data is copied here - can we do something COW?
return getContext().makeString(Arrays.copyOfRange(bytes.getBytes().unsafeBytes(), bytes.getBytes().begin() + start, bytes.getBytes().begin() + start + count));
return createString(Arrays.copyOfRange(bytes.getBytes().unsafeBytes(), bytes.getBytes().begin() + start, bytes.getBytes().begin() + start + count));
}

}
Loading

0 comments on commit 3e37d1b

Please sign in to comment.