Skip to content

Commit

Permalink
[GR-28100] Imported javascript File is read twice by Truffle virtual …
Browse files Browse the repository at this point in the history
…file system.

PullRequest: graal/8058
  • Loading branch information
tzezula committed Jan 27, 2021
2 parents 549cb8f + 57f4fa3 commit 39815fa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,15 @@ public void testDetectMimeType() {
@Test
public void testDetectEncoding() {
TruffleFile file = languageEnv.getPublicTruffleFile("/folder/filename.duplicate");
Charset encoding = TestAPIAccessor.languageAccess().detectEncoding(file, null);
assertFails(() -> TestAPIAccessor.languageAccess().detectEncoding(file, null), IllegalArgumentException.class);
String mimeType = "text/x-duplicate-mime";
Charset encoding = TestAPIAccessor.languageAccess().detectEncoding(file, mimeType);
assertNull(encoding);
Language1Detector detector1 = Language1Detector.getInstance();
Language2Detector detector2 = Language2Detector.getInstance();
String mimeType = "text/x-duplicate-mime";
detector1.reset();
detector2.reset().mimeType(mimeType);
encoding = TestAPIAccessor.languageAccess().detectEncoding(file, null);
encoding = TestAPIAccessor.languageAccess().detectEncoding(file, mimeType);
assertNull(encoding);
detector1.reset().mimeType(mimeType);
detector2.reset().mimeType(mimeType).encoding(UTF_16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Charset findEncoding(TruffleFile file) throws IOException {
return null;
}

@TruffleLanguage.Registration(id = "TestFooXML", name = "", byteMimeTypes = "text/foo+xml", fileTypeDetectors = {CommonMIMETypeTestDetector.class})
@TruffleLanguage.Registration(id = "TestFooXML", name = "", characterMimeTypes = "text/foo+xml", fileTypeDetectors = {CommonMIMETypeTestDetector.class})
public static class TestFooXMLLanguage extends ProxyLanguage {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -738,21 +738,23 @@ public void testEncodingTruffleFile() throws IOException {
TruffleFile file = languageEnv.getPublicTruffleFile(testFile.getPath());
Source src = Source.newBuilder("lang", file).encoding(StandardCharsets.UTF_16LE).build();
assertEquals(content, src.getCharacters().toString());
src = Source.newBuilder("TestTxt", file).encoding(StandardCharsets.UTF_16LE).build();
assertEquals(content, src.getCharacters().toString());

testFile = File.createTempFile("test", ".xml").getAbsoluteFile();
try (FileOutputStream out = new FileOutputStream(testFile)) {
out.write(xmlContent.getBytes(Charset.forName("windows-1250")));
}
file = languageEnv.getPublicTruffleFile(testFile.getPath());
src = Source.newBuilder("lang", file).build();
src = Source.newBuilder("TestFooXML", file).build();
assertEquals(xmlContent, src.getCharacters().toString());

testFile = File.createTempFile("test", ".txt").getAbsoluteFile();
try (FileOutputStream out = new FileOutputStream(testFile)) {
out.write(content.getBytes(StandardCharsets.UTF_8));
}
file = languageEnv.getPublicTruffleFile(testFile.getPath());
src = Source.newBuilder("lang", file).build();
src = Source.newBuilder("TestTxt", file).build();
assertEquals(content, src.getCharacters().toString());
}

Expand All @@ -769,19 +771,21 @@ public void testEncodingURL() throws IOException {
}
Source src = Source.newBuilder("lang", testFile.toURI().toURL()).encoding(StandardCharsets.UTF_16LE).build();
assertEquals(content, src.getCharacters().toString());
src = Source.newBuilder("TestTxt", testFile.toURI().toURL()).encoding(StandardCharsets.UTF_16LE).build();
assertEquals(content, src.getCharacters().toString());

testFile = File.createTempFile("test", ".xml").getAbsoluteFile();
try (FileOutputStream out = new FileOutputStream(testFile)) {
out.write(xmlContent.getBytes(Charset.forName("windows-1250")));
}
src = Source.newBuilder("lang", testFile.toURI().toURL()).build();
src = Source.newBuilder("TestFooXML", testFile.toURI().toURL()).build();
assertEquals(xmlContent, src.getCharacters().toString());

testFile = File.createTempFile("test", ".txt").getAbsoluteFile();
try (FileOutputStream out = new FileOutputStream(testFile)) {
out.write(content.getBytes(StandardCharsets.UTF_8));
}
src = Source.newBuilder("lang", testFile.toURI().toURL()).build();
src = Source.newBuilder("TestTxt", testFile.toURI().toURL()).build();
assertEquals(content, src.getCharacters().toString());
}

Expand Down Expand Up @@ -926,7 +930,7 @@ public static class TestJavaLanguage extends ProxyLanguage {
public static class TestJSLanguage extends ProxyLanguage {
}

@Registration(id = "TestTxt", name = "", byteMimeTypes = "text/plain", fileTypeDetectors = CommonMIMETypeTestDetector.class)
@Registration(id = "TestTxt", name = "", characterMimeTypes = "text/plain", fileTypeDetectors = CommonMIMETypeTestDetector.class)
public static class TestTxtLanguage extends ProxyLanguage {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,10 @@ public String detectMimeType(TruffleFile file, Set<String> validMimeTypes) {

@Override
public Charset detectEncoding(TruffleFile file, String mimeType) {
String useMimeType = mimeType == null ? file.detectMimeType() : mimeType;
return useMimeType == null ? null : file.detectEncoding(useMimeType);
if (mimeType == null) {
throw new IllegalArgumentException("MimeType must be non null.");
}
return file.detectEncoding(mimeType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,8 @@ static Source buildSource(String language, Object origin, String name, String pa
useTruffleFile = useTruffleFile.exists() ? useTruffleFile.getCanonicalFile() : useTruffleFile;
if (useContent == CONTENT_UNSET) {
if (isCharacterBased(useFileSystemContext, language, useMimeType)) {
useEncoding = useEncoding == null ? findEncoding(useTruffleFile, useMimeType) : useEncoding;
String fileMimeType = useMimeType == null ? SourceAccessor.detectMimeType(useTruffleFile, getValidMimeTypes(useFileSystemContext, language)) : useMimeType;
useEncoding = useEncoding == null ? findEncoding(useTruffleFile, fileMimeType) : useEncoding;
useContent = read(useTruffleFile, useEncoding);
} else {
useContent = ByteSequence.create(useTruffleFile.readAllBytes());
Expand Down Expand Up @@ -1282,7 +1283,7 @@ static <E extends Exception> RuntimeException silenceException(Class<E> type, Ex
}

private static Charset findEncoding(TruffleFile file, String mimeType) {
Charset encoding = SourceAccessor.detectEncoding(file, mimeType);
Charset encoding = mimeType == null ? null : SourceAccessor.detectEncoding(file, mimeType);
encoding = encoding == null ? StandardCharsets.UTF_8 : encoding;
return encoding;
}
Expand Down

0 comments on commit 39815fa

Please sign in to comment.