Skip to content

Commit 5e59a45

Browse files
committed
Read core library files as bytes in ResourceLoader
* Reading as char[] is useless and extra work.
1 parent eefc08e commit 5e59a45

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ public void loadRubyCoreLibraryAndPostBoot() {
783783

784784
public RubySource loadCoreFileSource(String path) throws IOException {
785785
if (path.startsWith(RubyLanguage.RESOURCE_SCHEME)) {
786-
return new RubySource(ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL), path);
786+
return ResourceLoader.loadResource(path, language.options.CORE_AS_INTERNAL);
787787
} else {
788788
final FileLoader fileLoader = new FileLoader(context, language);
789789
return fileLoader.loadFile(path);

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
import java.io.FileNotFoundException;
1313
import java.io.IOException;
1414
import java.io.InputStream;
15-
import java.io.InputStreamReader;
16-
import java.nio.charset.StandardCharsets;
1715
import java.util.Locale;
1816

19-
import org.truffleruby.RubyContext;
2017
import org.truffleruby.RubyLanguage;
18+
import org.truffleruby.core.encoding.Encodings;
19+
import org.truffleruby.parser.MagicCommentParser;
20+
import org.truffleruby.parser.RubySource;
2121
import org.truffleruby.shared.TruffleRuby;
2222

2323
import com.oracle.truffle.api.source.Source;
@@ -27,29 +27,35 @@
2727
*/
2828
public abstract class ResourceLoader {
2929

30-
public static Source loadResource(String path, boolean internal) throws IOException {
30+
public static RubySource loadResource(String path, boolean internal) throws IOException {
3131
assert path.startsWith(RubyLanguage.RESOURCE_SCHEME);
3232

3333
if (!path.toLowerCase(Locale.ENGLISH).endsWith(".rb")) {
3434
throw new FileNotFoundException(path);
3535
}
3636

37-
final Class<?> relativeClass = RubyContext.class;
3837
final String resourcePath = path.substring(RubyLanguage.RESOURCE_SCHEME.length());
39-
final InputStream stream = relativeClass.getResourceAsStream(resourcePath);
4038

41-
if (stream == null) {
42-
throw new FileNotFoundException(resourcePath);
39+
final byte[] sourceBytes;
40+
try (InputStream stream = ResourceLoader.class.getResourceAsStream(resourcePath)) {
41+
if (stream == null) {
42+
throw new FileNotFoundException(resourcePath);
43+
}
44+
45+
sourceBytes = stream.readAllBytes();
4346
}
4447

45-
final Source source;
4648

47-
// We guarantee that we only put UTF-8 source files into resources
48-
try (final InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
49-
source = Source.newBuilder(TruffleRuby.LANGUAGE_ID, reader, path).internal(internal).build();
50-
}
49+
var sourceTString = MagicCommentParser.createSourceTStringBasedOnMagicEncodingComment(sourceBytes,
50+
Encodings.UTF_8);
51+
52+
Source source = Source
53+
.newBuilder(TruffleRuby.LANGUAGE_ID, new ByteBasedCharSequence(sourceTString), path)
54+
.mimeType(RubyLanguage.getMimeType(false))
55+
.internal(internal)
56+
.build();
5157

52-
return source;
58+
return new RubySource(source, path, sourceTString);
5359
}
5460

5561
}

0 commit comments

Comments
 (0)