Skip to content

Commit

Permalink
[Truffle] New new byte[] source section API.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Sep 8, 2014
1 parent 7b36ac6 commit 1e927fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
21 changes: 16 additions & 5 deletions core/src/main/java/org/jruby/truffle/TruffleBridgeImpl.java
Expand Up @@ -10,6 +10,7 @@
package org.jruby.truffle;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.source.BytesDecoder;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.frame.MaterializedFrame;
Expand All @@ -29,6 +30,8 @@
import org.jruby.truffle.translator.TranslatorDriver;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class TruffleBridgeImpl implements TruffleBridge {

Expand Down Expand Up @@ -96,10 +99,20 @@ public Object execute(TranslatorDriver.ParserContext parserContext, Object self,
final Source source;

if (inputFile.equals("-e")) {
// TODO(CS): what if a file is legitimately called -e?
source = Source.asPseudoFile(runtime.getInstanceConfig().getInlineScript().toString(), "-e");
// Assume UTF-8 for the moment
source = Source.fromBytes(runtime.getInstanceConfig().inlineScript(), "-e", new BytesDecoder.UTF8BytesDecoder());
} else {
source = Source.fromFileName(inputFile);
final byte[] bytes;

try {
bytes = Files.readAllBytes(Paths.get(inputFile));
} catch (IOException e) {
throw new RuntimeException(e);
}

// Assume UTF-8 for the moment

source = Source.fromBytes(bytes, inputFile, new BytesDecoder.UTF8BytesDecoder());
}

final RubyParserResult parseResult = truffleContext.getTranslator().parse(truffleContext, source, parserContext, parentFrame, null);
Expand All @@ -114,8 +127,6 @@ public Object execute(TranslatorDriver.ParserContext parserContext, Object self,
}

return NilPlaceholder.INSTANCE;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand Down
18 changes: 4 additions & 14 deletions core/src/main/java/org/jruby/truffle/runtime/RubyContext.java
Expand Up @@ -117,27 +117,17 @@ public void loadFile(String fileName, RubyNode currentNode) {
}

private void loadFileAbsolute(String fileName, RubyNode currentNode) {
// TODO(CS): we have trouble working out where unicode characters are

final byte[] utf8Bytes;
final byte[] bytes;

try {
utf8Bytes = Files.readAllBytes(Paths.get(fileName));
bytes = Files.readAllBytes(Paths.get(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}

final String utf8String = new String(utf8Bytes, StandardCharsets.UTF_8);

final byte[] asciiBytes = utf8String.getBytes(StandardCharsets.US_ASCII);

final String asciiString = new String(asciiBytes, StandardCharsets.US_ASCII);

if (!utf8String.equals(asciiString)) {
warnings.warn("%s converted to ASCII", fileName);
}
// Assume UTF-8 for the moment

final Source source = Source.fromText(asciiString, fileName);
final Source source = Source.fromBytes(bytes, fileName, new BytesDecoder.UTF8BytesDecoder());

coreLibrary.getLoadedFeatures().slowPush(makeString(fileName));
execute(this, source, TranslatorDriver.ParserContext.TOP_LEVEL, coreLibrary.getMainObject(), null, currentNode);
Expand Down

0 comments on commit 1e927fc

Please sign in to comment.