Skip to content

Commit be2b46e

Browse files
eregonandrykonchin
authored andcommitted
Handle a source code String in ToCallTargetNode
1 parent c8bbe57 commit be2b46e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/main/java/org/truffleruby/core/cast/ToCallTargetNode.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,25 @@
99
*/
1010
package org.truffleruby.core.cast;
1111

12+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1213
import com.oracle.truffle.api.dsl.GenerateCached;
1314
import com.oracle.truffle.api.dsl.GenerateInline;
1415
import com.oracle.truffle.api.nodes.Node;
16+
import com.oracle.truffle.api.source.Source;
1517
import org.truffleruby.core.method.RubyMethod;
1618
import org.truffleruby.core.method.RubyUnboundMethod;
1719
import org.truffleruby.core.proc.RubyProc;
20+
import org.truffleruby.core.string.TStringWithEncoding;
1821
import org.truffleruby.language.RubyBaseNode;
1922

2023
import com.oracle.truffle.api.RootCallTarget;
2124
import com.oracle.truffle.api.dsl.Specialization;
25+
import org.truffleruby.language.RubyGuards;
26+
import org.truffleruby.language.library.RubyStringLibrary;
27+
import org.truffleruby.language.loader.ByteBasedCharSequence;
28+
import org.truffleruby.parser.ParserContext;
29+
import org.truffleruby.parser.RubySource;
30+
import org.truffleruby.parser.TranslatorEnvironment;
2231

2332
@GenerateInline
2433
@GenerateCached(false)
@@ -41,4 +50,21 @@ static RootCallTarget proc(RubyProc proc) {
4150
return proc.callTarget;
4251
}
4352

53+
@TruffleBoundary
54+
@Specialization
55+
static RootCallTarget string(Node node, Object string) {
56+
var code = new TStringWithEncoding(RubyGuards.asTruffleStringUncached(string),
57+
RubyStringLibrary.getUncached().getEncoding(string));
58+
Source source = Source.newBuilder("ruby", new ByteBasedCharSequence(code), "<parse_ast>").build();
59+
TranslatorEnvironment.resetTemporaryVariablesIndex();
60+
var parserContext = ParserContext.TOP_LEVEL;
61+
62+
return getContext(node).getCodeLoader().parse(
63+
new RubySource(source, source.getName()),
64+
parserContext,
65+
null,
66+
getContext(node).getRootLexicalScope(),
67+
node);
68+
}
69+
4470
}

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Object ast(Object code,
254254
@Cached RubyStringLibrary strings,
255255
@Cached TruffleString.FromJavaStringNode fromJavaStringNode) {
256256
var codeString = new TStringWithEncoding(RubyGuards.asTruffleStringUncached(code),
257-
RubyStringLibrary.create().getEncoding(code));
257+
RubyStringLibrary.getUncached().getEncoding(code));
258258
String name = "<parse_ast>";
259259
var source = Source.newBuilder("ruby", new ByteBasedCharSequence(codeString), name).build();
260260
var rubySource = new RubySource(source, name);
@@ -1345,7 +1345,7 @@ Object parseAndDump(Object sourceCode, Object focusedNodeClassName, int index, b
13451345
String nodeClassNameString = RubyGuards.getJavaString(focusedNodeClassName);
13461346

13471347
var code = new TStringWithEncoding(RubyGuards.asTruffleStringUncached(sourceCode),
1348-
RubyStringLibrary.create().getEncoding(sourceCode));
1348+
RubyStringLibrary.getUncached().getEncoding(sourceCode));
13491349

13501350
RubyRootNode rootNode = parse(code, mainScript);
13511351
String output = TruffleASTPrinter.dump(rootNode, nodeClassNameString, index);

0 commit comments

Comments
 (0)