2 * Placed into the Public Domain.
3 * written by Walter Bright
8 * Modified by Sean Kelly for use with the D Runtime Project
17 import core.stdc.stddef;
18 import core.stdc.stdlib;
19 import core.stdc.string;
24 extern (Windows) alias int function() FARPROC;
25 extern (Windows) FARPROC GetProcAddress(void*, in char*);
26 extern (Windows) void* LoadLibraryA(in char*);
27 extern (Windows) int FreeLibrary(void*);
28 extern (Windows) void* LocalFree(void*);
29 extern (Windows) wchar_t* GetCommandLineW();
30 extern (Windows) wchar_t** CommandLineToArgvW(wchar_t*, int*);
31 extern (Windows) export int WideCharToMultiByte(uint, uint, wchar_t*, int, char*, int, char*, int);
32 pragma(lib, "shell32.lib"); // needed for CommandLineToArgvW
35 extern (C) void _STI_monitor_staticctor();
36 extern (C) void _STD_monitor_staticdtor();
37 extern (C) void _STI_critical_init();
38 extern (C) void _STD_critical_term();
39 extern (C) void gc_init();
40 extern (C) void gc_term();
41 extern (C) void _minit();
42 extern (C) void _moduleCtor();
43 extern (C) void _moduleDtor();
44 extern (C) void thread_joinAll();
46 /***********************************
47 * These are a temporary means of providing a GC hook for DLL use. They may be
48 * replaced with some other similar functionality later.
53 void gc_setProxy(void* p);
56 alias void* function() gcGetFn;
57 alias void function(void*) gcSetFn;
58 alias void function() gcClrFn;
61 extern (C) void* rt_loadLibrary(in char[] name)
65 char[260] temp = void;
66 temp[0 .. name.length] = name[];
67 temp[name.length] = cast(char) 0;
68 void* ptr = LoadLibraryA(temp.ptr);
71 gcSetFn gcSet = cast(gcSetFn) GetProcAddress(ptr, "gc_setProxy");
79 throw new Exception("rt_loadLibrary not yet implemented on linux.");
83 extern (C) bool rt_unloadLibrary(void* ptr)
87 gcClrFn gcClr = cast(gcClrFn) GetProcAddress(ptr, "gc_clrProxy");
90 return FreeLibrary(ptr) != 0;
94 throw new Exception("rt_unloadLibrary not yet implemented on linux.");
98 /***********************************
99 * These functions must be defined for any D program linked
100 * against this library.
102 extern (C) void onAssertError(string file, size_t line);
103 extern (C) void onAssertErrorMsg(string file, size_t line, string msg);
104 extern (C) void onRangeError(string file, size_t line);
105 extern (C) void onHiddenFuncError(Object o);
106 extern (C) void onSwitchError(string file, size_t line);
107 extern (C) bool runModuleUnitTests();
109 // this function is called from the utf module
110 //extern (C) void onUnicodeError(string msg, size_t idx);
112 /***********************************
113 * These are internal callbacks for various language errors.
115 extern (C) void _d_assert(string file, uint line)
117 onAssertError(file, line);
120 extern (C) static void _d_assert_msg(string msg, string file, uint line)
122 onAssertErrorMsg(file, line, msg);
125 extern (C) void _d_array_bounds(string file, uint line)
127 onRangeError(file, line);
130 extern (C) void _d_switch_error(string file, uint line)
132 onSwitchError(file, line);
135 extern (C) void _d_hidden_func()
142 onHiddenFuncError(o);
145 bool _d_isHalting = false;
147 extern (C) bool rt_isHalting()
152 extern (C) bool rt_trapExceptions = true;
154 void _d_criticalInit()
158 _STI_monitor_staticctor();
159 _STI_critical_init();
163 alias void delegate(Throwable) ExceptionHandler;
165 extern (C) bool rt_init(ExceptionHandler dg = null)
191 void _d_criticalTerm()
195 _STD_critical_term();
196 _STD_monitor_staticdtor();
200 extern (C) bool rt_term(ExceptionHandler dg = null)
226 /***********************************
227 * The D main() function supplied by the user's program
229 int main(char[][] args);
231 /***********************************
232 * Substitutes for the C main() function.
233 * It's purpose is to wrap the call to the D main()
234 * function and catch any unhandled exceptions.
237 extern (C) int main(int argc, char **argv)
244 _STI_monitor_staticctor();
245 _STI_critical_init();
250 wchar_t* wcbuf = GetCommandLineW();
251 size_t wclen = wcslen(wcbuf);
253 wchar_t** wargs = CommandLineToArgvW(wcbuf, &wargc);
254 assert(wargc == argc);
257 size_t cargl = WideCharToMultiByte(65001, 0, wcbuf, wclen, null, 0, null, 0);
259 cargp = cast(char*) alloca(cargl);
260 args = ((cast(char[]*) alloca(wargc * (char[]).sizeof)))[0 .. wargc];
262 for (size_t i = 0, p = 0; i < wargc; i++)
264 int wlen = wcslen(wargs[i]);
265 int clen = WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, null, 0, null, 0);
266 args[i] = cargp[p .. p+clen];
267 p += clen; assert(p <= cargl);
268 WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, &args[i][0], clen, null, 0);
276 char[]* am = cast(char[]*) malloc(argc * (char[]).sizeof);
277 scope(exit) free(am);
279 for (size_t i = 0; i < argc; i++)
281 auto len = strlen(argv[i]);
282 am[i] = argv[i][0 .. len];
284 args = am[0 .. argc];
287 bool trapExceptions = rt_trapExceptions;
289 void tryExec(scope void delegate() dg)
304 // fprintf(stderr, "%.*s(%u): %.*s\n", e.file, e.line, e.msg);
305 console (e.classinfo.name)("@")(e.file)("(")(e.line)("): ")(e.msg)("\n");
309 // fprintf(stderr, "%.*s\n", e.toString());
310 console (e.toString)("\n");
314 console ("----------------\n");
322 result = EXIT_FAILURE;
326 // fprintf(stderr, "%.*s\n", o.toString());
327 console (o.toString)("\n");
328 result = EXIT_FAILURE;
337 // NOTE: The lifetime of a process is much like the lifetime of an object:
338 // it is initialized, then used, then destroyed. If initialization
339 // fails, the successive two steps are never reached. However, if
340 // initialization succeeds, then cleanup will occur even if the use
341 // step fails in some way. Here, the use phase consists of running
342 // the user's main function. If main terminates with an exception,
343 // the exception is handled and then cleanup begins. An exception
344 // thrown during cleanup, however, will abort the cleanup process.
358 if (runModuleUnitTests())
370 _STD_critical_term();
371 _STD_monitor_staticdtor();