2 * Placed into the Public Domain.
3 * written by Walter Bright
8 * Modified by Sean Kelly for use with the D Runtime Project
23 extern (Windows) void* LocalFree(void*);
24 extern (Windows) wchar_t* GetCommandLineW();
25 extern (Windows) wchar_t** CommandLineToArgvW(wchar_t*, int*);
26 extern (Windows) export int WideCharToMultiByte(uint, uint, wchar_t*, int, char*, int, char*, int);
27 pragma(lib, "shell32.lib"); // needed for CommandLineToArgvW
30 extern (C) void _STI_monitor_staticctor();
31 extern (C) void _STD_monitor_staticdtor();
32 extern (C) void _STI_critical_init();
33 extern (C) void _STD_critical_term();
34 extern (C) void gc_init();
35 extern (C) void gc_term();
36 extern (C) void _minit();
37 extern (C) void _moduleCtor();
38 extern (C) void _moduleDtor();
39 extern (C) void thread_joinAll();
41 /***********************************
42 * These functions must be defined for any D program linked
43 * against this library.
45 extern (C) void onAssertError( string file, size_t line );
46 extern (C) void onAssertErrorMsg( string file, size_t line, string msg );
47 extern (C) void onArrayBoundsError( string file, size_t line );
48 extern (C) void onSwitchError( string file, size_t line );
49 extern (C) bool runModuleUnitTests();
51 // this function is called from the utf module
52 //extern (C) void onUnicodeError( string msg, size_t idx );
54 /***********************************
55 * These are internal callbacks for various language errors.
57 extern (C) void _d_assert( string file, uint line )
59 onAssertError( file, line );
62 extern (C) static void _d_assert_msg( string msg, string file, uint line )
64 onAssertErrorMsg( file, line, msg );
67 extern (C) void _d_array_bounds( string file, uint line )
69 onArrayBoundsError( file, line );
72 extern (C) void _d_switch_error( string file, uint line )
74 onSwitchError( file, line );
77 bool _d_isHalting = false;
79 extern (C) bool rt_isHalting()
84 extern (C) bool rt_trapExceptions = true;
86 void _d_criticalInit()
90 _STI_monitor_staticctor();
95 alias void delegate( Exception ) ExceptionHandler;
97 extern (C) bool rt_init( ExceptionHandler dg = null )
122 void _d_criticalTerm()
126 _STD_critical_term();
127 _STD_monitor_staticdtor();
131 extern (C) bool rt_term( ExceptionHandler dg = null )
157 /***********************************
158 * The D main() function supplied by the user's program
160 int main(char[][] args);
162 /***********************************
163 * Substitutes for the C main() function.
164 * It's purpose is to wrap the call to the D main()
165 * function and catch any unhandled exceptions.
168 extern (C) int main(int argc, char **argv)
175 _STI_monitor_staticctor();
176 _STI_critical_init();
181 wchar_t* wcbuf = GetCommandLineW();
182 size_t wclen = wcslen(wcbuf);
184 wchar_t** wargs = CommandLineToArgvW(wcbuf, &wargc);
185 assert(wargc == argc);
188 size_t cargl = WideCharToMultiByte(65001, 0, wcbuf, wclen, null, 0, null, 0);
190 cargp = cast(char*) alloca(cargl);
191 args = ((cast(char[]*) alloca(wargc * (char[]).sizeof)))[0 .. wargc];
193 for (size_t i = 0, p = 0; i < wargc; i++)
195 int wlen = wcslen( wargs[i] );
196 int clen = WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, null, 0, null, 0);
197 args[i] = cargp[p .. p+clen];
198 p += clen; assert(p <= cargl);
199 WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, &args[i][0], clen, null, 0);
207 char[]* am = cast(char[]*) malloc(argc * (char[]).sizeof);
208 scope(exit) free(am);
210 for (size_t i = 0; i < argc; i++)
212 auto len = strlen(argv[i]);
213 am[i] = argv[i][0 .. len];
215 args = am[0 .. argc];
218 bool trapExceptions = rt_trapExceptions;
220 void tryExec(void delegate() dg)
235 // fprintf(stderr, "%.*s(%u): %.*s\n", e.file, e.line, e.msg);
236 console (e.classinfo.name)("@")(e.file)("(")(e.line)("): ")(e.toString)("\n");
240 // fprintf(stderr, "%.*s\n", e.toString());
241 console (e.classinfo.name)(": ")(e.toString)("\n");
245 console ("----------------\n");
253 result = EXIT_FAILURE;
257 // fprintf(stderr, "%.*s\n", o.toString());
258 console (o.toString)("\n");
259 result = EXIT_FAILURE;
268 // NOTE: The lifetime of a process is much like the lifetime of an object:
269 // it is initialized, then used, then destroyed. If initialization
270 // fails, the successive two steps are never reached. However, if
271 // initialization succeeds, then cleanup will occur even if the use
272 // step fails in some way. Here, the use phase consists of running
273 // the user's main function. If main terminates with an exception,
274 // the exception is handled and then cleanup begins. An exception
275 // thrown during cleanup, however, will abort the cleanup process.
288 if (runModuleUnitTests())
300 _STD_critical_term();
301 _STD_monitor_staticdtor();