Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

root/tags/releases/0.99.9/tango/core/Runtime.d

Revision 5246, 6.0 kB (checked in by kris, 3 years ago)

fixes #1802 :: Expose functionality in rt.compiler.util.console to user

should be used for ticket #1804 also

  • Property svn:eol-style set to native
Line 
1 /**
2  * The runtime module exposes information specific to the D runtime code.
3  *
4  * Copyright: Copyright (C) 2005-2006 Sean Kelly.  All rights reserved.
5  * License:   BSD style: $(LICENSE)
6  * Authors:   Sean Kelly
7  */
8 module tango.core.Runtime;
9
10 debug private extern(C) int printf(char*,...);
11
12 private
13 {
14     extern (C) bool rt_isHalting();
15
16     alias bool function() ModuleUnitTester;
17     alias bool function(Object) CollectHandler;
18     alias Exception.TraceInfo function( void* ptr = null ) TraceHandler;
19
20     extern (C) void rt_setCollectHandler( CollectHandler h );
21     extern (C) void rt_setTraceHandler( TraceHandler h );
22
23     alias void delegate( Exception ) ExceptionHandler;
24     extern (C) bool rt_init( ExceptionHandler dg = null );
25     extern (C) bool rt_term( ExceptionHandler dg = null );
26
27     extern(C) void consoleInteger (ulong i);
28     extern(C) void consoleString  (char[] str);
29 }
30
31
32 ////////////////////////////////////////////////////////////////////////////////
33 // Runtime
34 ////////////////////////////////////////////////////////////////////////////////
35
36
37 /**
38  * This struct encapsulates all functionality related to the underlying runtime
39  * module for the calling context.
40  */
41 struct Runtime
42 {
43     struct Console
44     {
45         alias stderr opCall;
46
47         Console stderr (char[] s)
48         {
49             consoleString (s);
50             return *this;
51         }
52
53         Console stderr (ulong i)
54         {
55             consoleInteger (i);
56             return *this;
57         }
58     }
59        
60
61     static Console console()
62     {
63         Console c;
64         return c;
65     }
66
67     /**
68      * Initializes the runtime.  This call is to be used in instances where the
69      * standard program initialization process is not executed.  This is most
70      * often in shared libraries or in libraries linked to a C program.
71      *
72      * Params:
73      *  dg = A delegate which will receive any exception thrown during the
74      *       initialization process or null if such exceptions should be
75      *       discarded.
76      *
77      * Returns:
78      *  true if initialization succeeds and false if initialization fails.
79      */
80     static bool initialize( void delegate( Exception ) dg = null )
81     {
82         return rt_init( dg );
83     }
84
85
86     /**
87      * Terminates the runtime.  This call is to be used in instances where the
88      * standard program termination process will not be not executed.  This is
89      * most often in shared libraries or in libraries linked to a C program.
90      *
91      * Params:
92      *  dg = A delegate which will receive any exception thrown during the
93      *       termination process or null if such exceptions should be
94      *       discarded.
95      *
96      * Returns:
97      *  true if termination succeeds and false if termination fails.
98      */
99     static bool terminate( void delegate( Exception ) dg = null )
100     {
101         return rt_term( dg );
102     }
103
104
105     /**
106      * Returns true if the runtime is halting.  Under normal circumstances,
107      * this will be set between the time that normal application code has
108      * exited and before module dtors are called.
109      *
110      * Returns:
111      *  true if the runtime is halting.
112      */
113     static bool isHalting()
114     {
115         return rt_isHalting();
116     }
117
118
119     /**
120      * Overrides the default trace mechanism with s user-supplied version.  A
121      * trace represents the context from which an exception was thrown, and the
122      * trace handler will be called when this occurs.  The pointer supplied to
123      * this routine indicates the base address from which tracing should occur.
124      * If the supplied pointer is null then the trace routine should determine
125      * an appropriate calling context from which to begin the trace.
126      *
127      * Params:
128      *  h = The new trace handler.  Set to null to use the default handler.
129      */
130     static void traceHandler( TraceHandler h )
131     {
132         rt_setTraceHandler( h );
133     }
134
135
136     /**
137      * Overrides the default collect hander with a user-supplied version.  This
138      * routine will be called for each resource object that is finalized in a
139      * non-deterministic manner--typically during a garbage collection cycle.
140      * If the supplied routine returns true then the object's dtor will called
141      * as normal, but if the routine returns false than the dtor will not be
142      * called.  The default behavior is for all object dtors to be called.
143      *
144      * Params:
145      *  h = The new collect handler.  Set to null to use the default handler.
146      */
147     static void collectHandler( CollectHandler h )
148     {
149         rt_setCollectHandler( h );
150     }
151
152
153     /**
154      * Overrides the default module unit tester with a user-supplied version.
155      * This routine will be called once on program initialization.  The return
156      * value of this routine indicates to the runtime whether the body of the
157      * program will be executed.
158      *
159      * Params:
160      *  h = The new unit tester.  Set to null to use the default unit tester.
161      */
162     static void moduleUnitTester( ModuleUnitTester h )
163     {
164         sm_moduleUnitTester = h;
165     }
166
167
168 private:
169     static ModuleUnitTester sm_moduleUnitTester = null;
170 }
171
172
173 ////////////////////////////////////////////////////////////////////////////////
174 // Overridable Callbacks
175 ////////////////////////////////////////////////////////////////////////////////
176
177 /**
178  * This routine is called by the runtime to run module unit tests on startup.
179  * The user-supplied unit tester will be called if one has been supplied,
180  * otherwise all unit tests will be run in sequence.
181  *
182  * Returns:
183  *  true if execution should continue after testing is complete and false if
184  *  not.  Default behavior is to return true.
185  */
186 extern (C) bool runModuleUnitTests()
187 {
188     if( Runtime.sm_moduleUnitTester is null )
189     {
190         debug printf("unittest start\n");
191         foreach( m; ModuleInfo )
192         {
193             if( m.unitTest ) {
194                 char[] name=m.name~"\n\0";
195                 debug printf(name.ptr);
196                 m.unitTest();
197             }
198         }
199         debug printf("unittest end\n");
200         return true;
201     }
202     return Runtime.sm_moduleUnitTester();
203 }
Note: See TracBrowser for help on using the browser.