Skip to content

Commit 4e5c02f

Browse files
author
Nicolas Laurent
committed
inline String#to_sym
1 parent dc6b619 commit 4e5c02f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/main/java/org/truffleruby/core/inlined/CoreMethods.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public class CoreMethods {
8787
public final InternalMethod STRING_BYTESIZE;
8888
public final InternalMethod MODULE_CASE_EQUAL;
8989
public final InternalMethod STRING_EQUAL;
90+
public final InternalMethod STRING_TO_SYM;
9091
public final InternalMethod SYMBOL_TO_PROC;
9192
public final InternalMethod ARRAY_AT;
9293
public final InternalMethod ARRAY_INDEX_GET;
@@ -153,6 +154,7 @@ public CoreMethods(RubyContext context) {
153154
KERNEL_KIND_OF = getMethod(kernelModule, "kind_of?");
154155
MODULE_CASE_EQUAL = getMethod(moduleClass, "===");
155156
STRING_EQUAL = getMethod(stringClass, "==");
157+
STRING_TO_SYM = getMethod(stringClass, "to_sym");
156158
SYMBOL_TO_PROC = getMethod(symbolClass, "to_proc");
157159
ARRAY_AT = getMethod(arrayClass, "at");
158160
ARRAY_INDEX_GET = getMethod(arrayClass, "[]");
@@ -211,6 +213,8 @@ public RubyNode createCallNode(RubyCallNodeParameters callParameters, Translator
211213
return InlinedIsNilNodeGen.create(context, callParameters, self);
212214
case "bytesize":
213215
return InlinedByteSizeNodeGen.create(context, callParameters, self);
216+
case "to_sym":
217+
return InlinedToSymNodeGen.create(context, callParameters, self);
214218
default:
215219
}
216220
} else if (n == 2) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.core.inlined;
11+
12+
import org.truffleruby.RubyContext;
13+
import org.truffleruby.core.string.RubyString;
14+
import org.truffleruby.core.string.StringNodes;
15+
import org.truffleruby.core.symbol.RubySymbol;
16+
import org.truffleruby.language.dispatch.RubyCallNodeParameters;
17+
18+
import com.oracle.truffle.api.dsl.Cached;
19+
import com.oracle.truffle.api.dsl.Specialization;
20+
import com.oracle.truffle.api.frame.VirtualFrame;
21+
import org.truffleruby.language.methods.LookupMethodOnSelfNode;
22+
23+
public abstract class InlinedToSymNode extends UnaryInlinedOperationNode {
24+
25+
protected static final String METHOD = "to_sym";
26+
27+
public InlinedToSymNode(RubyContext context, RubyCallNodeParameters callNodeParameters) {
28+
super(context, callNodeParameters);
29+
}
30+
31+
@Specialization(
32+
guards = "lookupNode.lookupProtected(frame, self, METHOD) == coreMethods().STRING_TO_SYM",
33+
assumptions = "assumptions",
34+
limit = "1")
35+
protected RubySymbol toSym(VirtualFrame frame, RubyString self,
36+
@Cached LookupMethodOnSelfNode lookupNode,
37+
@Cached StringNodes.ToSymNode toSym) {
38+
return toSym.execute(self);
39+
}
40+
41+
@Specialization
42+
protected Object fallback(VirtualFrame frame, Object self) {
43+
return rewriteAndCall(frame, self);
44+
}
45+
}

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,12 @@ public abstract static class ToSymNode extends CoreMethodArrayArgumentsNode {
24932493

24942494
@Child RopeNodes.CodeRangeNode codeRangeNode = RopeNodes.CodeRangeNode.create();
24952495

2496+
public static ToSymNode create() {
2497+
return StringNodesFactory.ToSymNodeFactory.create(null);
2498+
}
2499+
2500+
public abstract RubySymbol execute(RubyString string);
2501+
24962502
@Specialization(
24972503
guards = { "!isBrokenCodeRange(string, codeRangeNode)", "equalNode.execute(string.rope,cachedRope)" },
24982504
limit = "getDefaultCacheLimit()")

0 commit comments

Comments
 (0)