Skip to content

Commit eb10d38

Browse files
committed
use a ForeignLanguageView
1 parent 3693972 commit eb10d38

File tree

1 file changed

+62
-32
lines changed

1 file changed

+62
-32
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
package com.oracle.graal.python;
2727

2828
import java.io.IOException;
29-
import java.math.BigInteger;
3029
import java.util.ArrayList;
3130
import java.util.concurrent.ConcurrentHashMap;
3231
import java.util.concurrent.Semaphore;
3332
import java.util.function.Supplier;
3433
import java.util.logging.Level;
3534

3635
import com.oracle.graal.python.builtins.Python3Core;
37-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3836
import com.oracle.graal.python.builtins.objects.PEllipsis;
3937
import com.oracle.graal.python.builtins.objects.PNone;
4038
import com.oracle.graal.python.builtins.objects.PNotImplemented;
@@ -74,6 +72,12 @@
7472
import com.oracle.truffle.api.frame.VirtualFrame;
7573
import com.oracle.truffle.api.instrumentation.ProvidedTags;
7674
import com.oracle.truffle.api.instrumentation.StandardTags;
75+
import com.oracle.truffle.api.interop.InteropLibrary;
76+
import com.oracle.truffle.api.interop.TruffleObject;
77+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
78+
import com.oracle.truffle.api.library.CachedLibrary;
79+
import com.oracle.truffle.api.library.ExportLibrary;
80+
import com.oracle.truffle.api.library.ExportMessage;
7781
import com.oracle.truffle.api.nodes.ExecutableNode;
7882
import com.oracle.truffle.api.nodes.ExplodeLoop;
7983
import com.oracle.truffle.api.nodes.ExplodeLoop.LoopExplosionKind;
@@ -319,38 +323,64 @@ protected static ExpressionNode parseInline(Source code, PythonContext context,
319323

320324
@Override
321325
protected Object getLanguageView(PythonContext context, Object value) {
322-
if (value instanceof PythonAbstractObject) {
323-
return value;
324-
}
325-
PythonObjectFactory uncached = PythonObjectFactory.getUncached();
326-
if (value instanceof Boolean) {
327-
if ((boolean) value) {
328-
return context.getCore().getTrue();
326+
assert !(value instanceof PythonAbstractObject);
327+
PythonObjectFactory factory = PythonObjectFactory.getUncached();
328+
InteropLibrary interopLib = InteropLibrary.getFactory().getUncached(value);
329+
try {
330+
if (interopLib.isBoolean(value)) {
331+
if (interopLib.asBoolean(value)) {
332+
return context.getCore().getTrue();
333+
} else {
334+
return context.getCore().getFalse();
335+
}
336+
} else if (interopLib.isString(value)) {
337+
return factory.createString(interopLib.asString(value));
338+
} else if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long) {
339+
// TODO: (tfel) once the interop protocol allows us to
340+
// distinguish fixed point from floating point reliably, we can
341+
// remove this branch
342+
return factory.createInt(interopLib.asLong(value));
343+
} else if (value instanceof Float || value instanceof Double) {
344+
// TODO: (tfel) once the interop protocol allows us to
345+
// distinguish fixed point from floating point reliably, we can
346+
// remove this branch
347+
return factory.createFloat(interopLib.asDouble(value));
348+
} else if (interopLib.fitsInLong(value)) {
349+
return factory.createInt(interopLib.asLong(value));
350+
} else if (interopLib.fitsInDouble(value)) {
351+
return factory.createFloat(interopLib.asDouble(value));
329352
} else {
330-
return context.getCore().getFalse();
353+
return new ForeignLanguageView(value);
331354
}
332-
} else if (value instanceof Byte) {
333-
return uncached.createInt(PythonBuiltinClassType.PInt, (byte) value);
334-
} else if (value instanceof Character) {
335-
return uncached.createString(Character.toString((char) value));
336-
} else if (value instanceof Short) {
337-
return uncached.createInt(PythonBuiltinClassType.PInt, (short) value);
338-
} else if (value instanceof Integer) {
339-
return uncached.createInt(PythonBuiltinClassType.PInt, (int) value);
340-
} else if (value instanceof Long) {
341-
return uncached.createInt(PythonBuiltinClassType.PInt, (long) value);
342-
} else if (value instanceof BigInteger) {
343-
return uncached.createInt(PythonBuiltinClassType.PInt, (BigInteger) value);
344-
} else if (value instanceof CharSequence) {
345-
return uncached.createString(((CharSequence) value).toString());
346-
} else if (value instanceof Float) {
347-
return uncached.createFloat((float) value);
348-
} else if (value instanceof Double) {
349-
return uncached.createFloat((double) value);
350-
} else if (value instanceof Object[]) {
351-
return uncached.createTuple((Object[]) value);
352-
} else {
353-
return super.getLanguageView(context, value);
355+
} catch (UnsupportedMessageException e) {
356+
throw new IllegalStateException(e);
357+
}
358+
}
359+
360+
@ExportLibrary(value = InteropLibrary.class, delegateTo = "delegate")
361+
static class ForeignLanguageView implements TruffleObject {
362+
final Object delegate;
363+
364+
ForeignLanguageView(Object delegate) {
365+
this.delegate = delegate;
366+
}
367+
368+
@ExportMessage
369+
@TruffleBoundary
370+
String toDisplayString(boolean allowSideEffects,
371+
@CachedLibrary("this.delegate") InteropLibrary lib) {
372+
return "<foreign '" + lib.toDisplayString(delegate, allowSideEffects) + "'>";
373+
}
374+
375+
@ExportMessage
376+
@SuppressWarnings("static-method")
377+
boolean hasLanguage() {
378+
return true;
379+
}
380+
381+
@ExportMessage
382+
Class<? extends TruffleLanguage<?>> getLanguage() {
383+
return PythonLanguage.class;
354384
}
355385
}
356386

0 commit comments

Comments
 (0)