Skip to content

Commit a984ded

Browse files
committed
Simplify now that RubySource always has a TruffleString
1 parent a454ad8 commit a984ded

File tree

4 files changed

+4
-47
lines changed

4 files changed

+4
-47
lines changed

src/main/java/org/truffleruby/language/TruffleBootNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int main(int argc, long argv, String kind, String toExecute) {
132132
if (getContext().getOptions().SYNTAX_CHECK) {
133133
checkSyntax.call(coreLibrary().truffleBootModule, "check_syntax", source);
134134
} else {
135-
var tstringWithEncoding = source.hasTruffleString() ? source.getTStringWithEncoding() : null;
135+
var tstringWithEncoding = source.getTStringWithEncoding();
136136
var sourceTStringPair = Pair.create(source.getSource(), tstringWithEncoding);
137137
final RootCallTarget callTarget = getContext()
138138
.getCodeLoader()

src/main/java/org/truffleruby/parser/RubySource.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public final class RubySource {
3535
private final String sourcePath;
3636
private final TruffleString code;
3737
private byte[] bytes;
38+
// FIXME: not always the source encoding, e.g. when loading from a file or eval, need to check the magic encoding comment
3839
private final RubyEncoding encoding;
3940
private final boolean isEval;
4041
private final int lineOffset;
@@ -81,22 +82,15 @@ public String getSourcePath() {
8182
return sourcePath;
8283
}
8384

84-
public boolean hasTruffleString() {
85-
return code != null;
86-
}
87-
8885
public TruffleString getTruffleString() {
89-
assert hasTruffleString();
9086
return code;
9187
}
9288

9389
public TStringWithEncoding getTStringWithEncoding() {
94-
assert hasTruffleString();
9590
return new TStringWithEncoding(code, encoding);
9691
}
9792

9893
public byte[] getBytes() {
99-
assert hasTruffleString();
10094
if (bytes != null) {
10195
return bytes;
10296
} else {
@@ -105,7 +99,6 @@ public byte[] getBytes() {
10599
}
106100

107101
public RubyEncoding getEncoding() {
108-
assert hasTruffleString();
109102
return encoding;
110103
}
111104

src/main/java/org/truffleruby/parser/lexer/LexerSource.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public final class LexerSource {
5252
public ParserRopeOperations parserRopeOperations;
5353
private final Source source;
5454
private final String sourcePath;
55-
private final boolean fromTruffleString;
5655

5756
private TruffleString sourceBytes;
5857
private final int sourceByteLength;
@@ -64,18 +63,8 @@ public LexerSource(RubySource rubySource) {
6463
this.source = rubySource.getSource();
6564
this.sourcePath = rubySource.getSourcePath();
6665

67-
fromTruffleString = rubySource.hasTruffleString();
68-
69-
final RubyEncoding rubyEncoding;
70-
if (fromTruffleString) {
71-
rubyEncoding = rubySource.getEncoding();
72-
this.sourceBytes = rubySource.getTruffleString();
73-
} else {
74-
rubyEncoding = Encodings.UTF_8;
75-
// TODO CS 5-Sep-17 can we get the bytes directly rather than using getCharacters -> toString -> getBytes?
76-
var sourceString = source.getCharacters().toString();
77-
this.sourceBytes = TStringUtils.utf8TString(sourceString);
78-
}
66+
final RubyEncoding rubyEncoding = rubySource.getEncoding();
67+
this.sourceBytes = rubySource.getTruffleString();
7968
this.sourceByteLength = sourceBytes.byteLength(rubyEncoding.tencoding);
8069
this.encoding = rubyEncoding;
8170
parserRopeOperations = new ParserRopeOperations(this.encoding);
@@ -138,10 +127,6 @@ private int nextNewLine() {
138127
}
139128
}
140129

141-
public boolean isFromTruffleString() {
142-
return fromTruffleString;
143-
}
144-
145130
public int getLineOffset() {
146131
return lineOffset;
147132
}

src/main/java/org/truffleruby/parser/lexer/RubyLexer.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -460,27 +460,6 @@ protected void setEncoding(TruffleString name) {
460460
throw argumentError(context, nameString + " is not ASCII compatible");
461461
}
462462

463-
if (!src.isFromTruffleString() && !isUTF8Subset(newEncoding)) {
464-
/* The source we are lexing came in via a String (or Reader, or File) from the Polyglot API, so we only have
465-
* the String - we don't have any access to the original bytes, so we cannot re-interpret them in another
466-
* encoding without risking errors. */
467-
468-
final String description;
469-
470-
if (src.getSourcePath().equals("-e")) {
471-
description = "program from an -e argument";
472-
} else {
473-
description = "Polyglot API Source";
474-
}
475-
476-
throw argumentError(
477-
context,
478-
String.format(
479-
"%s cannot be used as an encoding for a %s as it is not UTF-8 or a subset of UTF-8",
480-
nameString,
481-
description));
482-
}
483-
484463
setEncoding(newEncoding);
485464
}
486465

0 commit comments

Comments
 (0)