Example #1
0
        // Perform system library startup tasks.  This is normally
        // called just before invoking the program's "main" function.
        public static void Startup()
        {
            Module    mainModule;
            Assembly  assembly;
            Type      type;
            FieldInfo field;

            // Bail out if we've already done the startup code.
            lock (typeof(Crt0))
            {
                if (startupDone)
                {
                    return;
                }
                startupDone = true;
            }

            // Find the module that contains the "main" function.
            mainModule = null;
            try
            {
                assembly = Assembly.GetCallingAssembly();
                type     = assembly.GetType("<Module>");
                if (type != null)
                {
                    mainModule = type.Module;
                }
            }
            catch (NotImplementedException)
            {
                // The runtime engine probably does not have support
                // for reflection, so there's nothing we can do.
            }

            // Find standard C library's global module.
            libcModule = null;
            try
            {
                assembly = Assembly.Load("libc");
                type     = assembly.GetType("libc");
                if (type != null)
                {
                    libcModule = type.Module;
                }
            }
            catch (OutOfMemoryException)
            {
                // Send out of memory conditions back up the stack.
                throw;
            }
            catch (Exception)
            {
                // We weren't able to load "libc" for some reason.
            }

            // Set the global "__environ" variable within "libc".
            if (libcModule != null)
            {
                field = libcModule.GetField("__environ");
                if (field != null)
                {
                    field.SetValue(null, (Object)environ);
                }
            }

            // Initialize the stdin, stdout, and stderr file descriptors.
                        #if CONFIG_SMALL_CONSOLE
            FileTable.SetFileDescriptor(0, Stream.Null);
            FileTable.SetFileDescriptor(1, new ConsoleStream());
            FileTable.SetFileDescriptor(2, new ConsoleStream());
                        #else
            FileTable.SetFileDescriptor
                (0, new FDStream(0, Console.OpenStandardInput()));
            FileTable.SetFileDescriptor
                (1, new FDStream(1, Console.OpenStandardOutput()));
            FileTable.SetFileDescriptor
                (2, new FDStream(2, Console.OpenStandardError()));
                        #endif

            // Invoke the application's ".init" function, if present.
            if (mainModule != null)
            {
                MethodInfo initMethod = mainModule.GetMethod(".init");
                if (initMethod != null)
                {
                    initMethod.Invoke(null, null);
                }
            }

            // Locate the application's ".fini" function.
            if (mainModule != null)
            {
                finiMethod = mainModule.GetMethod(".fini");
            }
            else
            {
                finiMethod = null;
            }
        }