Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LibraryInitializers.cs « Compiler « src « ILCompiler.Compiler « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e970fdd767ba127658dcbf9d5f4c0fa1868cfb2c (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;

using Internal.TypeSystem;

using Debug = System.Diagnostics.Debug;

namespace ILCompiler
{
    /// <summary>
    /// Encapsulates a list of class constructors that must be run in a prescribed order during start-up
    /// </summary>
    public sealed class LibraryInitializers
    {
        private const string LibraryInitializerContainerNamespaceName = "Internal.Runtime.CompilerHelpers";
        private const string LibraryInitializerContainerTypeName = "LibraryInitializer";
        private const string LibraryInitializerMethodName = "InitializeLibrary";

        private List<MethodDesc> _libraryInitializerMethods;

        private readonly TypeSystemContext _context;
        private IReadOnlyCollection<ModuleDesc> _librariesWithInitializers;

        public LibraryInitializers(TypeSystemContext context, IEnumerable<ModuleDesc> librariesWithInitalizers)
        {
            _context = context;
            _librariesWithInitializers = new List<ModuleDesc>(librariesWithInitalizers);
        }

        public IReadOnlyCollection<MethodDesc> LibraryInitializerMethods
        {
            get
            {
                if (_libraryInitializerMethods == null)
                    InitLibraryInitializers();

                return _libraryInitializerMethods;
            }
        }

        private void InitLibraryInitializers()
        {
            Debug.Assert(_libraryInitializerMethods == null);
            
            _libraryInitializerMethods = new List<MethodDesc>();

            foreach (var assembly in _librariesWithInitializers)
            {
                TypeDesc containingType = assembly.GetType(LibraryInitializerContainerNamespaceName, LibraryInitializerContainerTypeName, false);
                if (containingType == null)
                    continue;

                MethodDesc initializerMethod = containingType.GetMethod(LibraryInitializerMethodName, null);
                if (initializerMethod == null)
                    continue;

                _libraryInitializerMethods.Add(initializerMethod);
            }
        }
    }
}