1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System;
6 using System.Collections.Generic;
7 
8 using Internal.TypeSystem;
9 
10 namespace Internal.IL.Stubs.StartupCode
11 {
12     /// <summary>
13     /// Startup code that does initialization, Main invocation
14     /// and shutdown of the runtime.
15     /// </summary>
16     public sealed partial class NativeLibraryStartupMethod : ILStubMethod
17     {
18         private TypeDesc _owningType;
19         private MethodSignature _signature;
20         private IList<MethodDesc> _libraryInitializers;
21 
NativeLibraryStartupMethod(TypeDesc owningType, IList<MethodDesc> libraryInitializers)22         public NativeLibraryStartupMethod(TypeDesc owningType, IList<MethodDesc> libraryInitializers)
23         {
24             _owningType = owningType;
25             _libraryInitializers = libraryInitializers;
26         }
27 
28         public override TypeSystemContext Context
29         {
30             get
31             {
32                 return _owningType.Context;
33             }
34         }
35 
36         public override TypeDesc OwningType
37         {
38             get
39             {
40                 return _owningType;
41             }
42         }
43 
44         public override string Name
45         {
46             get
47             {
48                 return "NativeLibraryStartup";
49             }
50         }
51 
EmitIL()52         public override MethodIL EmitIL()
53         {
54             ILEmitter emitter = new ILEmitter();
55             ILCodeStream codeStream = emitter.NewCodeStream();
56 
57             // Allow the class library to run explicitly ordered class constructors first thing in start-up.
58             if (_libraryInitializers != null)
59             {
60                 foreach (MethodDesc method in _libraryInitializers)
61                 {
62                     codeStream.Emit(ILOpcode.call, emitter.NewToken(method));
63                 }
64             }
65 
66             codeStream.Emit(ILOpcode.ret);
67             return emitter.Link(this);
68         }
69 
70         public override MethodSignature Signature
71         {
72             get
73             {
74                 if (_signature == null)
75                 {
76                     _signature = new MethodSignature(MethodSignatureFlags.Static, 0,
77                             Context.GetWellKnownType(WellKnownType.Void),
78                             new TypeDesc[0]);
79                 }
80 
81                 return _signature;
82             }
83         }
84 
85         public override bool IsNativeCallable
86         {
87             get
88             {
89                 return true;
90             }
91         }
92     }
93 }
94