summaryrefslogtreecommitdiff
path: root/src/library/scala/runtime/InterpreterSupport.java
blob: 9272583ca17d8b0ba5b36119249a373bd423a574 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002, LAMP/EPFL                  **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$

package scala.runtime;

/** This class provides support methods for the interpreter. */
public class InterpreterSupport {

    //########################################################################
    // Public classes

    /** This interface provides method to show definitions. */
    public static interface DefinitionPrinter {

        /** This method is invoked for each non-value definition. */
        public void showDefinition(String signature);

        /** This method is invoked for each value definition. */
        public void showValueDefinition(String signature, Object value);

    }

    /** This class describes an evaluation result. */
    public static class EvaluationResult {

        /** The value of the result */
        public final Object value;

        /** The type of the result */
        public final String type;

        /** Creates a new instance */
        public EvaluationResult(Object value, String type) {
            this.value = value;
            this.type = type;
        }
    }

    //########################################################################
    // Private Variables

    private static final ThreadLocal printer = new ThreadLocal();
    private static final ThreadLocal result  = new ThreadLocal();

    //########################################################################
    // Public Functions

    /** Sets the definition printer of the current thread. */
    public static void setDefinitionPrinter(DefinitionPrinter object) {
        printer.set(object);
    }

    /** Returns the definition printer of the current thread. */
    public static DefinitionPrinter getDefinitionPrinter() {
        return (DefinitionPrinter)printer.get();
    }

    /**
     * This function is invoked for each non-value definition. It
     * forwards the call to the current thread's definition printer.
     *
     * @meta method (java.lang.String, scala.Any) scala.Unit;
     */
    public static void showDefinition(String signature) {
        DefinitionPrinter printer = getDefinitionPrinter();
        if (printer != null) printer.showDefinition(signature);
    }

    /**
     * This method is invoked for each value definition.  It forwards
     * the call to the current thread's definition printer.
     *
     * @meta method (java.lang.String, scala.Any) scala.Unit;
     */
    public static void showValueDefinition(String signature, Object value) {
        DefinitionPrinter printer = getDefinitionPrinter();
        if (printer != null) printer.showValueDefinition(signature, value);
    }

    /**
     * Sets the evaluation result of the current thread.
     *
     * @meta method (scala.Any, java.lang.String) scala.Unit;
     */
    public static void setEvaluationResult(Object value, String type) {
        result.set(new EvaluationResult(value, type));
    }

    /**
     * Returns and resets the evaluation result of the current
     * thread. A null value indicates that the last evaluation had no
     * result (only definitions).
     */
    public static EvaluationResult getAndResetEvaluationResult() {
        Object object = result.get();
        result.set(null);
        return (EvaluationResult)object;
    }

    //########################################################################
}