Skip to content

Commit 25e460a

Browse files
committed
Rewriting SREModuleBuiltins
1 parent 065036f commit 25e460a

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SREModuleBuiltins.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5959
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
6060
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
61+
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
6162
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6263
import com.oracle.truffle.api.CompilerAsserts;
6364
import com.oracle.truffle.api.CompilerDirectives;
@@ -68,6 +69,7 @@
6869
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6970
import com.oracle.truffle.api.dsl.NodeFactory;
7071
import com.oracle.truffle.api.dsl.Specialization;
72+
import com.oracle.truffle.api.dsl.TypeSystemReference;
7173
import com.oracle.truffle.api.interop.ArityException;
7274
import com.oracle.truffle.api.interop.ForeignAccess;
7375
import com.oracle.truffle.api.interop.Message;
@@ -253,26 +255,21 @@ Object run(Object o) {
253255

254256
}
255257

256-
@Builtin(name = "tregex_call_safe", minNumOfArguments = 1, takesVariableArguments = true)
258+
@Builtin(name = "tregex_call_safe", fixedNumOfArguments = 3)
259+
@TypeSystemReference(PythonArithmeticTypes.class)
257260
@GenerateNodeFactory
258261
abstract static class TRegexCallSafe extends PythonBuiltinNode {
259-
@Specialization(guards = "isForeignObject(callable)")
260-
Object call(TruffleObject callable, Object[] arguments,
262+
263+
private Node invokeNode;
264+
265+
private Object doIt(TruffleObject callable, String arg1, Object arg2,
261266
@Cached("create()") BranchProfile runtimeError,
262-
@Cached("create()") BranchProfile typeError,
263-
@Cached("createExecute()") Node invokeNode) {
267+
@Cached("create()") BranchProfile typeError) {
268+
if (invokeNode == null) {
269+
invokeNode = Message.createExecute(0).createNode();
270+
}
264271
try {
265-
// TODO This is a hack. The right solution would be to fix it
266-
// in
267-
// com.oracle.truffle.regex.RegexEngine.RegexEngineMessageResolution.RegexEngineExecuteNode
268-
// where is only check whether the argument is instance of String.
269-
// PString should be there unboxed.
270-
for (int i = 0; i < arguments.length; i++) {
271-
if (arguments[i] instanceof PString) {
272-
arguments[i] = ((PString) arguments[i]).getValue();
273-
}
274-
}
275-
return ForeignAccess.sendExecute(invokeNode, callable, arguments);
272+
return ForeignAccess.sendExecute(invokeNode, callable, new Object[]{arg1, arg2});
276273
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
277274
typeError.enter();
278275
throw raise(TypeError, "%s", e);
@@ -282,10 +279,19 @@ Object call(TruffleObject callable, Object[] arguments,
282279
}
283280
}
284281

285-
protected static Node createExecute() {
286-
return Message.createExecute(0).createNode();
282+
@Specialization(guards = "isForeignObject(callable)")
283+
Object call(TruffleObject callable, String arg1, String arg2,
284+
@Cached("create()") BranchProfile runtimeError,
285+
@Cached("create()") BranchProfile typeError) {
286+
return doIt(callable, arg1, arg2, runtimeError, typeError);
287+
}
288+
289+
@Specialization(guards = "isForeignObject(callable)")
290+
Object call(TruffleObject callable, String arg1, int arg2,
291+
@Cached("create()") BranchProfile runtimeError,
292+
@Cached("create()") BranchProfile typeError) {
293+
return doIt(callable, arg1, arg2, runtimeError, typeError);
287294
}
288295

289296
}
290-
291297
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/LazyString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ private static int lengthIntl(CharSequence cs) {
6767

6868
@TruffleBoundary
6969
public static CharSequence create(CharSequence left, CharSequence right) {
70-
assert PGuards.isString(left);
71-
assert PGuards.isString(right);
70+
assert PGuards.isString(left) || left instanceof LazyString;
71+
assert PGuards.isString(right) || right instanceof LazyString;
7272
if (UseLazyStrings) {
7373
if (left.length() == 0) {
7474
return right;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,12 +1107,16 @@ String rstrip(String self, PNone chars) {
11071107

11081108
@Builtin(name = SpecialMethodNames.__LEN__, fixedNumOfArguments = 1)
11091109
@GenerateNodeFactory
1110-
@TypeSystemReference(PythonArithmeticTypes.class)
11111110
public abstract static class LenNode extends PythonUnaryBuiltinNode {
11121111
@Specialization
11131112
public int len(String self) {
11141113
return self.length();
11151114
}
1115+
1116+
@Specialization
1117+
public int len(PString self) {
1118+
return self.len();
1119+
}
11161120
}
11171121

11181122
@Builtin(name = "index", minNumOfArguments = 2, maxNumOfArguments = 4)

0 commit comments

Comments
 (0)