diff truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java @ 22284:33d49924a921

PolyglotEngine.Value.as(String.class) gives the language that produced the value a chance to do its conversion to appropriate textual representation.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 07 Oct 2015 13:32:25 +0200
parents 088b3121f5ae
children 7e2abecabcac
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Wed Oct 07 12:39:09 2015 +0200
+++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Wed Oct 07 13:32:25 2015 +0200
@@ -707,7 +707,10 @@
          * Obtains Java view of the object represented by this symbol. The method basically
          * delegates to
          * {@link JavaInterop#asJavaObject(java.lang.Class, com.oracle.truffle.api.interop.TruffleObject)}
-         * just handles primitive types as well.
+         * . The method handles primitive types (like {@link Number}, etc.) by casting and returning
+         * them. When a {@link String}.<code>class</code> is requested, the method let's the
+         * language that produced the value to do the
+         * {@link TruffleLanguage#toString(java.lang.Object, java.lang.Object) necessary formating}.
          *
          * @param <T> the type of the view one wants to obtain
          * @param representation the class of the view interface (it has to be an interface)
@@ -723,6 +726,10 @@
                     return representation.cast(eto.getDelegate());
                 }
             }
+            if (representation == String.class) {
+                final Class<? extends TruffleLanguage> clazz = language.getClass();
+                return representation.cast(SPI.toString(language, findEnv(clazz), obj));
+            }
             if (representation.isInstance(obj)) {
                 return representation.cast(obj);
             }
@@ -1043,5 +1050,10 @@
         protected void dispose(TruffleLanguage<?> impl, TruffleLanguage.Env env) {
             super.dispose(impl, env);
         }
+
+        @Override
+        protected String toString(TruffleLanguage language, TruffleLanguage.Env env, Object obj) {
+            return super.toString(language, env, obj);
+        }
     } // end of SPIAccessor
 }