Skip to content

Commit 729d28a

Browse files
committed
add isTrue message to PythonObjectLibrary
1 parent 0aa0575 commit 729d28a

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ static LazyPythonClass getLazyPythonClass(@SuppressWarnings("unused") Boolean va
7676
static long hash(Boolean value) {
7777
return value ? 1 : 0;
7878
}
79+
80+
@ExportMessage
81+
static boolean isTrue(Boolean value) {
82+
return value;
83+
}
7984
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@ static long hashDoubleWithDecimals(Double self) {
7474
return self.hashCode();
7575
}
7676
}
77+
78+
@ExportMessage
79+
static boolean isTrue(Double value) {
80+
return value != 0.0;
81+
}
7782
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ static LazyPythonClass getLazyPythonClass(@SuppressWarnings("unused") Integer va
7676
static long hash(Integer value) {
7777
return value;
7878
}
79+
80+
@ExportMessage
81+
static boolean isTrue(Integer value) {
82+
return value != 0;
83+
}
7984
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,9 @@ static LazyPythonClass getLazyPythonClass(@SuppressWarnings("unused") Long value
9393
static long hash(Long value) {
9494
return value;
9595
}
96+
97+
@ExportMessage
98+
static boolean isTrue(Long value) {
99+
return value != 0;
100+
}
96101
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4848
import com.oracle.truffle.api.dsl.Cached;
4949
import com.oracle.truffle.api.dsl.Cached.Shared;
50+
import com.oracle.truffle.api.dsl.Specialization;
5051
import com.oracle.truffle.api.interop.InteropLibrary;
5152
import com.oracle.truffle.api.interop.UnsupportedMessageException;
5253
import com.oracle.truffle.api.library.CachedLibrary;
@@ -138,4 +139,61 @@ static int length(Object receiver,
138139
throw raise.raiseHasNoLength(receiver);
139140
}
140141
}
142+
143+
144+
@ExportMessage
145+
static class IsTrue {
146+
@Specialization(guards = "lib.isBoolean(receiver)")
147+
static boolean bool(Object receiver,
148+
@CachedLibrary("receiver") InteropLibrary lib) {
149+
try {
150+
return lib.asBoolean(receiver);
151+
} catch (UnsupportedMessageException e) {
152+
CompilerDirectives.transferToInterpreter();
153+
throw new IllegalStateException(e);
154+
}
155+
}
156+
157+
@Specialization(guards = "lib.fitsInLong(receiver)")
158+
static boolean integer(Object receiver,
159+
@CachedLibrary("receiver") InteropLibrary lib) {
160+
try {
161+
return lib.asLong(receiver) != 0;
162+
} catch (UnsupportedMessageException e) {
163+
CompilerDirectives.transferToInterpreter();
164+
throw new IllegalStateException(e);
165+
}
166+
}
167+
168+
@Specialization(guards = "lib.fitsInDouble(receiver)")
169+
static boolean floatingPoint(Object receiver,
170+
@CachedLibrary("receiver") InteropLibrary lib) {
171+
try {
172+
return lib.asDouble(receiver) != 0.0;
173+
} catch (UnsupportedMessageException e) {
174+
CompilerDirectives.transferToInterpreter();
175+
throw new IllegalStateException(e);
176+
}
177+
}
178+
179+
@Specialization(guards = "lib.hasArrayElements(receiver)")
180+
static boolean array(Object receiver,
181+
@CachedLibrary("receiver") InteropLibrary lib) {
182+
try {
183+
return lib.getArraySize(receiver) > 0;
184+
} catch (UnsupportedMessageException e) {
185+
CompilerDirectives.transferToInterpreter();
186+
throw new IllegalStateException(e);
187+
}
188+
}
189+
190+
@Specialization(guards = {
191+
"!lib.isBoolean(receiver)", "!lib.fitsInLong(receiver)",
192+
"!lib.fitsInDouble(receiver)", "!lib.hasArrayElements(receiver)"
193+
})
194+
static boolean generic(Object receiver,
195+
@CachedLibrary("receiver") InteropLibrary lib) {
196+
return !lib.isNull(receiver);
197+
}
198+
}
141199
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,26 @@ public boolean isSequence(Object receiver) {
321321
return false;
322322
}
323323

324+
/**
325+
* Checks wether this object should be interpreted as {@code
326+
* true}-ish. Mimics the coercion behaviour of {@code PyObject_IsTrue}, and
327+
* thus uses both {@code slot_nb_bool} coercion and {@link #length}/{@link
328+
* #lengthWithState}.
329+
*/
330+
public boolean isTrueWithState(Object receiver, ThreadState state) {
331+
if (state == null) {
332+
return true;
333+
}
334+
return isTrue(receiver);
335+
}
336+
337+
/**
338+
* @see #isTrueWithState
339+
*/
340+
public boolean isTrue(Object receiver) {
341+
return isTrueWithState(receiver, null);
342+
}
343+
324344
/**
325345
* Implements the logic from {@code PyObject_Size} (to which {@code
326346
* PySequence_Length} is an alias). The logic which is to try a) {@code

0 commit comments

Comments
 (0)