Skip to content

Commit a1053e5

Browse files
committed
Never call RubyString#getJavaString() directly and use the library instead
1 parent 5ee4bc1 commit a1053e5

File tree

8 files changed

+33
-44
lines changed

8 files changed

+33
-44
lines changed

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,8 @@ protected Object debug(Object... objects) {
11501150

11511151
representation = RopeOperations.decodeRope(rope) + " (" + builder.toString() + ")";
11521152
} else if (RubyGuards.isRubyValue(object)) {
1153-
representation = object.toString() + " (" + callToS(object).getJavaString() + ")";
1153+
representation = object.toString() + " (" +
1154+
RubyStringLibrary.getUncached().getJavaString(callToS(object)) + ")";
11541155
} else {
11551156
representation = object.toString();
11561157
}
@@ -1160,13 +1161,13 @@ protected Object debug(Object... objects) {
11601161
return nil;
11611162
}
11621163

1163-
private RubyString callToS(Object object) {
1164+
private Object callToS(Object object) {
11641165
if (toSCall == null) {
11651166
CompilerDirectives.transferToInterpreterAndInvalidate();
11661167
toSCall = insert(DispatchNode.create());
11671168
}
11681169

1169-
return (RubyString) toSCall.call(object, "to_s");
1170+
return toSCall.call(object, "to_s");
11701171
}
11711172

11721173
}

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.truffle.api.nodes.Node;
4848
import com.oracle.truffle.api.object.DynamicObjectLibrary;
4949
import com.oracle.truffle.api.source.SourceSection;
50+
import org.truffleruby.language.library.RubyStringLibrary;
5051
import org.truffleruby.language.objects.LogicalClassNode;
5152

5253
public class CoreExceptions {
@@ -97,11 +98,11 @@ public void showExceptionIfDebug(RubyClass rubyClass, Object message, Backtrace
9798
}
9899

99100
public String inspectReceiver(Object receiver) {
100-
RubyString rubyString = (RubyString) context.send(
101+
Object rubyString = context.send(
101102
context.getCoreLibrary().truffleExceptionOperationsModule,
102103
"receiver_string",
103104
receiver);
104-
return rubyString.getJavaString();
105+
return RubyStringLibrary.getUncached().getJavaString(rubyString);
105106
}
106107

107108
// ArgumentError
@@ -214,7 +215,7 @@ public RubyException argumentErrorWrongArgumentType(Object object, String expect
214215
public RubyException argumentErrorInvalidStringToInteger(Object object, Node currentNode) {
215216
assert object instanceof RubyString || object instanceof ImmutableRubyString;
216217
// TODO (nirvdrum 19-Apr-18): Guard against String#inspect being redefined to return something other than a String.
217-
final String formattedObject = ((RubyString) context.send(object, "inspect")).getJavaString();
218+
final String formattedObject = RubyStringLibrary.getUncached().getJavaString(context.send(object, "inspect"));
218219
return argumentError(StringUtils.format("invalid value for Integer(): %s", formattedObject), currentNode);
219220
}
220221

@@ -615,7 +616,7 @@ public RubyException typeError(String message, Node currentNode, Throwable javaT
615616
@TruffleBoundary
616617
public RubyException typeErrorUnsupportedTypeException(UnsupportedTypeException exception, Node currentNode) {
617618
RubyArray rubyArray = createArray(context, language, exception.getSuppliedValues());
618-
String formattedValues = ((RubyString) context.send(rubyArray, "inspect")).getJavaString();
619+
String formattedValues = RubyStringLibrary.getUncached().getJavaString(context.send(rubyArray, "inspect"));
619620
return typeError("unsupported type " + formattedValues, currentNode);
620621
}
621622

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
import org.truffleruby.core.support.TypeNodes;
6161
import org.truffleruby.core.symbol.RubySymbol;
6262
import org.truffleruby.core.symbol.SymbolTable;
63-
import org.truffleruby.language.ImmutableRubyString;
6463
import org.truffleruby.language.LexicalScope;
6564
import org.truffleruby.language.NotProvided;
6665
import org.truffleruby.language.RubyConstant;
@@ -593,16 +592,13 @@ protected Object isAutoloadSymbol(RubyModule module, RubySymbol name) {
593592
return isAutoload(module, name.getString());
594593
}
595594

596-
@Specialization
597-
protected Object isAutoloadString(RubyModule module, RubyString name) {
598-
return isAutoload(module, name.getJavaString());
599-
}
600-
601-
@Specialization
602-
protected Object isAutoloadString(RubyModule module, ImmutableRubyString name) {
603-
return isAutoload(module, name.getJavaString());
595+
@Specialization(guards = "strings.isRubyString(name)")
596+
protected Object isAutoloadString(RubyModule module, Object name,
597+
@CachedLibrary(limit = "2") RubyStringLibrary strings) {
598+
return isAutoload(module, strings.getJavaString(name));
604599
}
605600

601+
@TruffleBoundary
606602
private Object isAutoload(RubyModule module, String name) {
607603
final ConstantLookupResult constant = ModuleOperations.lookupConstant(getContext(), module, name);
608604

@@ -1953,7 +1949,7 @@ protected RubyString toS(RubyModule module) {
19531949
}
19541950
final Object inspectResult = callRbInspect
19551951
.call(coreLibrary().truffleTypeModule, "rb_inspect", attached);
1956-
attachedName = ((RubyString) inspectResult).getJavaString();
1952+
attachedName = RubyStringLibrary.getUncached().getJavaString(inspectResult);
19571953
}
19581954
moduleName = "#<Class:" + attachedName + ">";
19591955
} else if (fields.isRefinement()) {

src/main/java/org/truffleruby/interop/InteropNodes.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.truffleruby.core.string.StringOperations;
4545
import org.truffleruby.core.string.StringUtils;
4646
import org.truffleruby.core.symbol.RubySymbol;
47-
import org.truffleruby.language.ImmutableRubyString;
4847
import org.truffleruby.language.Nil;
4948
import org.truffleruby.language.NotProvided;
5049
import org.truffleruby.language.RubyGuards;
@@ -189,15 +188,10 @@ protected Object proxyForeignObject(Object delegate, Object logger) {
189188
public abstract static class MimeTypeSupportedNode extends CoreMethodArrayArgumentsNode {
190189

191190
@TruffleBoundary
192-
@Specialization
193-
protected boolean isMimeTypeSupported(RubyString mimeType) {
194-
return getContext().getEnv().isMimeTypeSupported(mimeType.getJavaString());
195-
}
196-
197-
@TruffleBoundary
198-
@Specialization
199-
protected boolean isMimeTypeSupportedImmutable(ImmutableRubyString mimeType) {
200-
return getContext().getEnv().isMimeTypeSupported(mimeType.getJavaString());
191+
@Specialization(guards = "strings.isRubyString(mimeType)")
192+
protected boolean isMimeTypeSupported(RubyString mimeType,
193+
@CachedLibrary(limit = "2") RubyStringLibrary strings) {
194+
return getContext().getEnv().isMimeTypeSupported(strings.getJavaString(mimeType));
201195
}
202196

203197
}
@@ -1857,24 +1851,18 @@ public abstract static class JavaTypeNode extends CoreMethodArrayArgumentsNode {
18571851

18581852
// TODO CS 17-Mar-18 we should cache this in the future
18591853

1860-
@TruffleBoundary
18611854
@Specialization
18621855
protected Object javaTypeSymbol(RubySymbol name) {
18631856
return javaType(name.getString());
18641857
}
18651858

1866-
@TruffleBoundary
1867-
@Specialization
1868-
protected Object javaTypeString(RubyString name) {
1869-
return javaType(name.getJavaString());
1859+
@Specialization(guards = "strings.isRubyString(name)")
1860+
protected Object javaTypeString(Object name,
1861+
@CachedLibrary(limit = "2") RubyStringLibrary strings) {
1862+
return javaType(strings.getJavaString(name));
18701863
}
18711864

18721865
@TruffleBoundary
1873-
@Specialization
1874-
protected Object javaTypeString(ImmutableRubyString name) {
1875-
return javaType(name.getJavaString());
1876-
}
1877-
18781866
private Object javaType(String name) {
18791867
final TruffleLanguage.Env env = getContext().getEnv();
18801868

src/main/java/org/truffleruby/language/backtrace/BacktraceFormatter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
import org.truffleruby.core.array.RubyArray;
2727
import org.truffleruby.core.exception.ExceptionOperations;
2828
import org.truffleruby.core.exception.RubyException;
29-
import org.truffleruby.core.string.RubyString;
3029
import org.truffleruby.core.string.StringOperations;
3130
import org.truffleruby.core.string.StringUtils;
3231
import org.truffleruby.language.RubyRootNode;
3332
import org.truffleruby.language.control.RaiseException;
33+
import org.truffleruby.language.library.RubyStringLibrary;
3434
import org.truffleruby.language.methods.TranslateExceptionNode;
3535
import org.truffleruby.parser.RubySource;
3636

@@ -122,7 +122,9 @@ public void printRubyExceptionOnEnvStderr(String info, RubyException rubyExcepti
122122
context.getCoreLibrary().truffleExceptionOperationsModule,
123123
"get_formatted_backtrace",
124124
rubyException);
125-
final String formatted = fullMessage != null ? ((RubyString) fullMessage).getJavaString() : "<no message>";
125+
final String formatted = fullMessage != null
126+
? RubyStringLibrary.getUncached().getJavaString(fullMessage)
127+
: "<no message>";
126128
if (formatted.endsWith("\n")) {
127129
printer.print(formatted);
128130
} else {

src/main/java/org/truffleruby/language/loader/FeatureLoader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.truffleruby.language.RubyConstant;
4040
import org.truffleruby.language.control.RaiseException;
4141
import org.truffleruby.language.dispatch.DispatchNode;
42+
import org.truffleruby.language.library.RubyStringLibrary;
4243
import org.truffleruby.platform.NativeConfiguration;
4344
import org.truffleruby.platform.Platform;
4445
import org.truffleruby.platform.TruffleNFIPlatform;
@@ -289,7 +290,7 @@ public String findFeatureImpl(String feature) {
289290
context.getCoreLibrary().truffleFeatureLoaderModule,
290291
"get_expanded_load_path");
291292
for (Object pathObject : ArrayOperations.toIterable(expandedLoadPath)) {
292-
final String loadPath = ((RubyString) pathObject).getJavaString();
293+
final String loadPath = RubyStringLibrary.getUncached().getJavaString(pathObject);
293294

294295
if (context.getOptions().LOG_FEATURE_LOCATION) {
295296
RubyLanguage.LOGGER.info(String.format("from load path %s...", loadPath));
@@ -310,7 +311,7 @@ public String findFeatureImpl(String feature) {
310311
"get_expanded_load_path");
311312
for (Object pathObject : ArrayOperations.toIterable(expandedLoadPath)) {
312313
// $LOAD_PATH entries are canonicalized since Ruby 2.4.4
313-
final String loadPath = ((RubyString) pathObject).getJavaString();
314+
final String loadPath = RubyStringLibrary.getUncached().getJavaString(pathObject);
314315

315316
if (context.getOptions().LOG_FEATURE_LOCATION) {
316317
RubyLanguage.LOGGER.info(String.format("from load path %s...", loadPath));

src/main/java/org/truffleruby/platform/TruffleNFIPlatform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.oracle.truffle.api.interop.UnsupportedMessageException;
2121
import com.oracle.truffle.api.interop.UnsupportedTypeException;
2222
import com.oracle.truffle.api.source.Source;
23-
import org.truffleruby.language.ImmutableRubyString;
23+
import org.truffleruby.language.library.RubyStringLibrary;
2424

2525
public class TruffleNFIPlatform {
2626

@@ -84,7 +84,7 @@ public Object resolveTypeRaw(NativeConfiguration nativeConfiguration, String typ
8484

8585
public String resolveType(NativeConfiguration nativeConfiguration, String type) {
8686
final Object typedef = resolveTypeRaw(nativeConfiguration, type);
87-
return toNFIType(((ImmutableRubyString) typedef).getJavaString());
87+
return toNFIType(RubyStringLibrary.getUncached().getJavaString(typedef));
8888
}
8989

9090
private String toNFIType(String type) {

src/main/java/org/truffleruby/stdlib/readline/ReadlineNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void complete(LineReader lineReader, ParsedLine commandLine, List<Candida
291291
.createString(context, language, StringOperations.encodeRope(buffer, UTF8Encoding.INSTANCE));
292292
RubyArray completions = (RubyArray) context.send(proc, "call", string);
293293
for (Object element : ArrayOperations.toIterable(completions)) {
294-
final String completion = ((RubyString) element).getJavaString();
294+
final String completion = RubyStringLibrary.getUncached().getJavaString(element);
295295
candidates.add(new Candidate(completion + after, completion, null, null, null, null, complete));
296296
}
297297
}

0 commit comments

Comments
 (0)