|
26 | 26 | package com.oracle.graal.python;
|
27 | 27 |
|
28 | 28 | import java.io.IOException;
|
29 |
| -import java.math.BigInteger; |
30 | 29 | import java.util.ArrayList;
|
31 | 30 | import java.util.concurrent.ConcurrentHashMap;
|
32 | 31 | import java.util.concurrent.Semaphore;
|
33 | 32 | import java.util.function.Supplier;
|
34 | 33 | import java.util.logging.Level;
|
35 | 34 |
|
36 | 35 | import com.oracle.graal.python.builtins.Python3Core;
|
37 |
| -import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
38 | 36 | import com.oracle.graal.python.builtins.objects.PEllipsis;
|
39 | 37 | import com.oracle.graal.python.builtins.objects.PNone;
|
40 | 38 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
|
74 | 72 | import com.oracle.truffle.api.frame.VirtualFrame;
|
75 | 73 | import com.oracle.truffle.api.instrumentation.ProvidedTags;
|
76 | 74 | 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; |
77 | 81 | import com.oracle.truffle.api.nodes.ExecutableNode;
|
78 | 82 | import com.oracle.truffle.api.nodes.ExplodeLoop;
|
79 | 83 | import com.oracle.truffle.api.nodes.ExplodeLoop.LoopExplosionKind;
|
@@ -319,38 +323,64 @@ protected static ExpressionNode parseInline(Source code, PythonContext context,
|
319 | 323 |
|
320 | 324 | @Override
|
321 | 325 | 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)); |
329 | 352 | } else {
|
330 |
| - return context.getCore().getFalse(); |
| 353 | + return new ForeignLanguageView(value); |
331 | 354 | }
|
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; |
354 | 384 | }
|
355 | 385 | }
|
356 | 386 |
|
|
0 commit comments