Skip to content

Commit

Permalink
Showing 35 changed files with 280 additions and 261 deletions.
Original file line number Diff line number Diff line change
@@ -11,22 +11,23 @@
package org.jruby.truffle.nodes;

import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.ByteList;

public abstract class StringCachingGuards {

public static ByteList privatizeByteList(DynamicObject string) {
if (RubyGuards.isRubyString(string)) {
return Layouts.STRING.getByteList(string).dup();
return StringOperations.getByteList(string).dup();
} else {
return null;
}
}

public static boolean byteListsEqual(DynamicObject string, ByteList byteList) {
if (RubyGuards.isRubyString(string)) {
return Layouts.STRING.getByteList(string).equal(byteList);
return StringOperations.getByteList(string).equal(byteList);
} else {
return false;
}
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;

@NodeChild(value="child", type=RubyNode.class)
@@ -36,7 +37,7 @@ protected DynamicObject toSymbolSymbol(DynamicObject symbol) {

@Specialization(guards = "isRubyString(string)")
protected DynamicObject toSymbolString(DynamicObject string) {
return getSymbol(Layouts.STRING.getByteList(string));
return getSymbol(StringOperations.getByteList(string));
}

@Specialization
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@
import org.jruby.truffle.runtime.NotProvided;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;

@CoreClass(name = "BasicObject")
@@ -145,7 +146,7 @@ public InstanceEvalNode(RubyContext context, SourceSection sourceSection) {
@CompilerDirectives.TruffleBoundary
@Specialization(guards = "isRubyString(string)")
public Object instanceEval(Object receiver, DynamicObject string, NotProvided block) {
return getContext().instanceEval(Layouts.STRING.getByteList(string), receiver, "(eval)", this);
return getContext().instanceEval(StringOperations.getByteList(string), receiver, "(eval)", this);
}

@Specialization(guards = "isRubyProc(block)")
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ public Object isCompatibleEncodingEncoding(DynamicObject first, DynamicObject se
@TruffleBoundary
@Specialization(guards = {"isRubyString(first)", "isRubyRegexp(second)"})
public Object isCompatibleStringRegexp(DynamicObject first, DynamicObject second) {
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(Layouts.STRING.getByteList(first).getEncoding(), Layouts.REGEXP.getRegex(second).getEncoding());
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(StringOperations.getByteList(first).getEncoding(), Layouts.REGEXP.getRegex(second).getEncoding());

if (compatibleEncoding != null) {
return getEncoding(compatibleEncoding);
@@ -151,7 +151,7 @@ public Object isCompatibleStringRegexp(DynamicObject first, DynamicObject second
@TruffleBoundary
@Specialization(guards = {"isRubyRegexp(first)", "isRubyString(second)"})
public Object isCompatibleRegexpString(DynamicObject first, DynamicObject second) {
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(Layouts.REGEXP.getRegex(first).getEncoding(), Layouts.STRING.getByteList(second).getEncoding());
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(Layouts.REGEXP.getRegex(first).getEncoding(), StringOperations.getByteList(second).getEncoding());

if (compatibleEncoding != null) {
return getEncoding(compatibleEncoding);
@@ -223,7 +223,7 @@ public Object isCompatibleSymbolSymbol(DynamicObject first, DynamicObject second
@TruffleBoundary
@Specialization(guards = {"isRubyString(first)", "isRubyEncoding(second)"})
public Object isCompatibleStringEncoding(DynamicObject first, DynamicObject second) {
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(Layouts.STRING.getByteList(first).getEncoding(), EncodingOperations.getEncoding(second));
final Encoding compatibleEncoding = org.jruby.RubyEncoding.areCompatible(StringOperations.getByteList(first).getEncoding(), EncodingOperations.getEncoding(second));

if (compatibleEncoding != null) {
return getEncoding(compatibleEncoding);
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.StringOperations;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.translator.BodyTranslator;
import org.jruby.util.RegexpOptions;
@@ -42,7 +43,7 @@ public Object execute(VirtualFrame frame) {

for (int n = 0; n < children.length; n++) {
final Object child = children[n].execute(frame);
strings[n] = org.jruby.RubyString.newString(getContext().getRuntime(), Layouts.STRING.getByteList((DynamicObject) toS.call(frame, child, "to_s", null)));
strings[n] = org.jruby.RubyString.newString(getContext().getRuntime(), StringOperations.getByteList((DynamicObject) toS.call(frame, child, "to_s", null)));
}

final org.jruby.RubyString preprocessed = org.jruby.RubyRegexp.preprocessDRegexp(getContext().getRuntime(), strings, options);
Original file line number Diff line number Diff line change
@@ -661,7 +661,7 @@ public Object evalBadBinding(DynamicObject source, DynamicObject badBinding, Not

@TruffleBoundary
private Object doEval(DynamicObject source, DynamicObject binding, String filename, boolean ownScopeForAssignments) {
final Object result = getContext().eval(ParserContext.EVAL, Layouts.STRING.getByteList(source), binding, ownScopeForAssignments, filename, this);
final Object result = getContext().eval(ParserContext.EVAL, StringOperations.getByteList(source), binding, ownScopeForAssignments, filename, this);
assert result != null;
return result;
}
@@ -1963,7 +1963,7 @@ public DynamicObject formatUncached(
throw handleException(e);
}

return finishFormat(Layouts.STRING.getByteList(format), result);
return finishFormat(StringOperations.getByteList(format), result);
}

private RuntimeException handleException(PackException exception) {
@@ -2022,7 +2022,7 @@ protected CallTarget compileFormat(DynamicObject format) {
assert RubyGuards.isRubyString(format);

try {
return new FormatParser(getContext()).parse(Layouts.STRING.getByteList(format));
return new FormatParser(getContext()).parse(StringOperations.getByteList(format));
} catch (FormatException e) {
CompilerDirectives.transferToInterpreter();
throw new RaiseException(getContext().getCoreLibrary().argumentError(e.getMessage(), this));
Original file line number Diff line number Diff line change
@@ -183,7 +183,7 @@ public static void updateCharOffset(DynamicObject matchData) {
assert RubyGuards.isRubyMatchData(matchData);
if (Layouts.MATCH_DATA.getCharOffsetUpdated(matchData)) return;

ByteList value = Layouts.STRING.getByteList(Layouts.MATCH_DATA.getSource(matchData));
ByteList value = StringOperations.getByteList(Layouts.MATCH_DATA.getSource(matchData));
Encoding enc = value.getEncoding();

if (Layouts.MATCH_DATA.getRegion(matchData) == null) {
@@ -247,7 +247,7 @@ public Object getIndexSymbol(DynamicObject matchData, DynamicObject index, NotPr
@Specialization(guards = "isRubyString(index)")
public Object getIndexString(DynamicObject matchData, DynamicObject index, NotProvided length) {
try {
ByteList value = Layouts.STRING.getByteList(index);
ByteList value = StringOperations.getByteList(index);
final int i = Layouts.REGEXP.getRegex(Layouts.MATCH_DATA.getRegexp(matchData)).nameToBackrefNumber(value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(), Layouts.MATCH_DATA.getRegion(matchData));

return getIndex(matchData, i, NotProvided.INSTANCE);
@@ -455,7 +455,7 @@ public ToSNode(RubyContext context, SourceSection sourceSection) {
@CompilerDirectives.TruffleBoundary
@Specialization
public DynamicObject toS(DynamicObject matchData) {
final ByteList bytes = Layouts.STRING.getByteList(Layouts.MATCH_DATA.getGlobal(matchData)).dup();
final ByteList bytes = StringOperations.getByteList(Layouts.MATCH_DATA.getGlobal(matchData)).dup();
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), bytes, StringSupport.CR_UNKNOWN, null);
}
}
Original file line number Diff line number Diff line change
@@ -649,7 +649,7 @@ private Object classEvalSource(DynamicObject module, DynamicObject code, String

final MaterializedFrame callerFrame = RubyCallStack.getCallerFrame(getContext())
.getFrame(FrameInstance.FrameAccess.MATERIALIZE, false).materialize();
Encoding encoding = Layouts.STRING.getByteList(code).getEncoding();
Encoding encoding = StringOperations.getByteList(code).getEncoding();

CompilerDirectives.transferToInterpreter();
Source source = Source.fromText(code.toString(), file);
26 changes: 13 additions & 13 deletions truffle/src/main/java/org/jruby/truffle/nodes/core/RegexpNodes.java
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ public abstract class RegexpNodes {
public static DynamicObject makeString(DynamicObject source, int start, int length) {
assert RubyGuards.isRubyString(source);

final ByteList bytes = new ByteList(Layouts.STRING.getByteList(source), start, length);
final ByteList bytes = new ByteList(StringOperations.getByteList(source), start, length);
final DynamicObject ret = Layouts.STRING.createString(Layouts.CLASS.getInstanceFactory(Layouts.BASIC_OBJECT.getLogicalClass(source)), bytes, StringSupport.CR_UNKNOWN, null);

Layouts.STRING.setCodeRange(ret, Layouts.STRING.getCodeRange(source));
@@ -89,7 +89,7 @@ public static Object matchCommon(DynamicObject regexp, DynamicObject source, boo
assert RubyGuards.isRubyRegexp(regexp);
assert RubyGuards.isRubyString(source);

final ByteList sourceByteList = Layouts.STRING.getByteList(source);
final ByteList sourceByteList = StringOperations.getByteList(source);

final ByteList bl = Layouts.REGEXP.getSource(regexp);
final Encoding enc = checkEncoding(regexp, StringOperations.getCodeRangeable(source), true);
@@ -107,7 +107,7 @@ public static Object matchCommon(DynamicObject regexp, DynamicObject source, boo
assert RubyGuards.isRubyRegexp(regexp);
assert RubyGuards.isRubyString(source);

final ByteList bytes = Layouts.STRING.getByteList(source);
final ByteList bytes = StringOperations.getByteList(source);
final RubyContext context = Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(regexp)).getContext();

final Frame frame = RubyCallStack.getCallerFrame(Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(regexp)).getContext()).getFrame(FrameInstance.FrameAccess.READ_WRITE, false);
@@ -220,14 +220,14 @@ public static DynamicObject gsub(DynamicObject regexp, DynamicObject string, Str

final RubyContext context = Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(regexp)).getContext();

final byte[] stringBytes = Layouts.STRING.getByteList(string).bytes();
final byte[] stringBytes = StringOperations.getByteList(string).bytes();

final Encoding encoding = Layouts.STRING.getByteList(string).getEncoding();
final Encoding encoding = StringOperations.getByteList(string).getEncoding();
final Matcher matcher = Layouts.REGEXP.getRegex(regexp).matcher(stringBytes);

int p = Layouts.STRING.getByteList(string).getBegin();
int p = StringOperations.getByteList(string).getBegin();
int end = 0;
int range = p + Layouts.STRING.getByteList(string).getRealSize();
int range = p + StringOperations.getByteList(string).getRealSize();
int lastMatchEnd = 0;

// We only ever care about the entire matched string, not each of the matched parts, so we can hard-code the index.
@@ -253,7 +253,7 @@ public static DynamicObject gsub(DynamicObject regexp, DynamicObject string, Str
builder.append(StandardCharsets.UTF_8.decode(ByteBuffer.wrap(replacement.getBytes(StandardCharsets.UTF_8))));

lastMatchEnd = regionEnd;
end = StringSupport.positionEndForScan(Layouts.STRING.getByteList(string), matcher, encoding, p, range);
end = StringSupport.positionEndForScan(StringOperations.getByteList(string), matcher, encoding, p, range);
}

return Layouts.STRING.createString(Layouts.CLASS.getInstanceFactory(context.getCoreLibrary().getStringClass()), StringOperations.encodeByteList(builder.toString(), UTF8Encoding.INSTANCE), StringSupport.CR_UNKNOWN, null);
@@ -266,12 +266,12 @@ public static DynamicObject[] split(DynamicObject regexp, final DynamicObject st

final RubyContext context = Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(regexp)).getContext();

final ByteList bytes = Layouts.STRING.getByteList(string);
final ByteList bytes = StringOperations.getByteList(string);
final byte[] byteArray = bytes.bytes();
final int begin = bytes.getBegin();
final int len = bytes.getRealSize();
final int range = begin + len;
final Encoding encoding = Layouts.STRING.getByteList(string).getEncoding();
final Encoding encoding = StringOperations.getByteList(string).getEncoding();
final Matcher matcher = Layouts.REGEXP.getRegex(regexp).matcher(byteArray);

final ArrayList<DynamicObject> strings = new ArrayList<>();
@@ -521,7 +521,7 @@ public EscapeNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization(guards = "isRubyString(pattern)")
public DynamicObject escape(DynamicObject pattern) {
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.encodeByteList(org.jruby.RubyRegexp.quote19(new ByteList(Layouts.STRING.getByteList(pattern)), true).toString(), UTF8Encoding.INSTANCE), StringSupport.CR_UNKNOWN, null);
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), StringOperations.encodeByteList(org.jruby.RubyRegexp.quote19(new ByteList(StringOperations.getByteList(pattern)), true).toString(), UTF8Encoding.INSTANCE), StringSupport.CR_UNKNOWN, null);
}

}
@@ -570,8 +570,8 @@ public QuoteNode(RubyContext context, SourceSection sourceSection) {
@TruffleBoundary
@Specialization(guards = "isRubyString(raw)")
public DynamicObject quoteString(DynamicObject raw) {
boolean isAsciiOnly = Layouts.STRING.getByteList(raw).getEncoding().isAsciiCompatible() && StringOperations.scanForCodeRange(raw) == CR_7BIT;
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), org.jruby.RubyRegexp.quote19(Layouts.STRING.getByteList(raw), isAsciiOnly), StringSupport.CR_UNKNOWN, null);
boolean isAsciiOnly = StringOperations.getByteList(raw).getEncoding().isAsciiCompatible() && StringOperations.scanForCodeRange(raw) == CR_7BIT;
return Layouts.STRING.createString(getContext().getCoreLibrary().getStringFactory(), org.jruby.RubyRegexp.quote19(StringOperations.getByteList(raw), isAsciiOnly), StringSupport.CR_UNKNOWN, null);
}

@Specialization(guards = "isRubySymbol(raw)")
Original file line number Diff line number Diff line change
@@ -22,12 +22,12 @@ public class StringGuards {

public static boolean isSingleByteOptimizable(DynamicObject string) {
assert RubyGuards.isRubyString(string);
return StringSupport.isSingleByteOptimizable(StringOperations.getCodeRangeable(string), Layouts.STRING.getByteList(string).getEncoding());
return StringSupport.isSingleByteOptimizable(StringOperations.getCodeRangeable(string), StringOperations.getByteList(string).getEncoding());
}

public static boolean isAsciiCompatible(DynamicObject string) {
assert RubyGuards.isRubyString(string);
return Layouts.STRING.getByteList(string).getEncoding().isAsciiCompatible();
return StringOperations.getByteList(string).getEncoding().isAsciiCompatible();
}

public static boolean isSingleByteOptimizableOrAsciiOnly(DynamicObject string) {
@@ -38,7 +38,7 @@ public static boolean isSingleByteOptimizableOrAsciiOnly(DynamicObject string) {

public static boolean isSingleByte(DynamicObject string) {
assert RubyGuards.isRubyString(string);
return Layouts.STRING.getByteList(string).getEncoding().isSingleByte();
return StringOperations.getByteList(string).getEncoding().isSingleByte();
}

public static boolean isValidOr7BitEncoding(DynamicObject string) {
@@ -48,11 +48,11 @@ public static boolean isValidOr7BitEncoding(DynamicObject string) {

public static boolean isFixedWidthEncoding(DynamicObject string) {
assert RubyGuards.isRubyString(string);
return Layouts.STRING.getByteList(string).getEncoding().isFixedWidth();
return StringOperations.getByteList(string).getEncoding().isFixedWidth();
}

public static boolean isValidUtf8(DynamicObject string) {
assert RubyGuards.isRubyString(string);
return StringOperations.isCodeRangeValid(string) && Layouts.STRING.getByteList(string).getEncoding() instanceof UTF8Encoding;
return StringOperations.isCodeRangeValid(string) && StringOperations.getByteList(string).getEncoding() instanceof UTF8Encoding;
}
}
Loading

0 comments on commit b6552a7

Please sign in to comment.