Skip to content

Commit aa88483

Browse files
committed
fix and test multiplying a string by a foreign boolean
1 parent 66afc14 commit aa88483

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/JavaInteropTest.java

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
import java.util.Arrays;
5353
import java.util.List;
5454

55+
import com.oracle.graal.python.runtime.interop.InteropArray;
56+
import com.oracle.graal.python.test.PythonTests;
57+
import com.oracle.truffle.api.interop.ArityException;
58+
import com.oracle.truffle.api.interop.InteropLibrary;
59+
import com.oracle.truffle.api.interop.TruffleObject;
60+
import com.oracle.truffle.api.interop.UnknownIdentifierException;
61+
import com.oracle.truffle.api.library.ExportLibrary;
62+
import com.oracle.truffle.api.library.ExportMessage;
63+
5564
import org.graalvm.polyglot.Context;
5665
import org.graalvm.polyglot.Context.Builder;
5766
import org.graalvm.polyglot.Engine;
@@ -67,15 +76,6 @@
6776
import org.junit.runners.Parameterized.Parameter;
6877
import org.junit.runners.Parameterized.Parameters;
6978

70-
import com.oracle.graal.python.runtime.interop.InteropArray;
71-
import com.oracle.graal.python.test.PythonTests;
72-
import com.oracle.truffle.api.interop.ArityException;
73-
import com.oracle.truffle.api.interop.InteropLibrary;
74-
import com.oracle.truffle.api.interop.TruffleObject;
75-
import com.oracle.truffle.api.interop.UnknownIdentifierException;
76-
import com.oracle.truffle.api.library.ExportLibrary;
77-
import com.oracle.truffle.api.library.ExportMessage;
78-
7979
@RunWith(Enclosed.class)
8080
public class JavaInteropTest {
8181
public static class GeneralInterop extends PythonTests {
@@ -509,6 +509,54 @@ public void writableBindings() {
509509
assertTrue(javaObj.isNumber());
510510
assertEquals(javaObj.asInt(), 42);
511511
}
512+
513+
@ExportLibrary(InteropLibrary.class)
514+
static final class WrapString implements TruffleObject {
515+
private final String str;
516+
517+
WrapString(String str) {
518+
this.str = str;
519+
}
520+
521+
@ExportMessage
522+
@SuppressWarnings("static-method")
523+
boolean isString() {
524+
return true;
525+
}
526+
527+
@ExportMessage
528+
String asString() {
529+
return str;
530+
}
531+
}
532+
533+
@ExportLibrary(InteropLibrary.class)
534+
static final class WrapBoolean implements TruffleObject {
535+
private final boolean flag;
536+
537+
WrapBoolean(boolean flag) {
538+
this.flag = flag;
539+
}
540+
541+
@ExportMessage
542+
@SuppressWarnings("static-method")
543+
boolean isBoolean() {
544+
return true;
545+
}
546+
547+
@ExportMessage
548+
boolean asBoolean() {
549+
return flag;
550+
}
551+
}
552+
553+
@Test
554+
public void multiplyStrBool() {
555+
context.getBindings("python").putMember("javaBool", new WrapBoolean(true));
556+
context.getBindings("python").putMember("javaStr", new WrapString("test"));
557+
assertEquals(context.eval("python", "javaStr * javaBool").asString(), "test");
558+
assertEquals(context.eval("python", "javaBool * javaStr").asString(), "test");
559+
}
512560
}
513561

514562
@RunWith(Parameterized.class)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/DefaultPythonObjectExports.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ static Object asIndex(Object receiver,
8585
CompilerDirectives.transferToInterpreter();
8686
throw new IllegalStateException(e);
8787
}
88+
} else if (interopLib.isBoolean(receiver)) {
89+
try {
90+
return interopLib.asBoolean(receiver) ? 1 : 0;
91+
} catch (UnsupportedMessageException e) {
92+
CompilerDirectives.transferToInterpreter();
93+
throw new IllegalStateException(e);
94+
}
8895
} else {
8996
throw raise.raiseIntegerInterpretationError(receiver);
9097
}

0 commit comments

Comments
 (0)