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

AssemblyGetExecutingAssemblyMethodThunk.cs « Stubs « IL « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce1d69716a7b978089208b3c03569e18e6e55869 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Internal.TypeSystem;

namespace Internal.IL.Stubs
{
    /// <summary>
    /// Thunk that replaces calls to Assembly.GetExecutingAssembly in user code. The purpose of the thunk
    /// is to load something that will let us identify the current assembly and call a class library
    /// helper that will let us get the Assembly.
    /// </summary>
    internal partial class AssemblyGetExecutingAssemblyMethodThunk : ILStubMethod
    {
        public AssemblyGetExecutingAssemblyMethodThunk(TypeDesc owningType, IAssemblyDesc executingAssembly)
        {
            OwningType = owningType;
            ExecutingAssembly = executingAssembly;

            TypeSystemContext context = owningType.Context;

            Signature = new MethodSignature(MethodSignatureFlags.Static, 0,
                context.SystemModule.GetKnownType("System.Reflection", "Assembly"), TypeDesc.EmptyTypes);
        }

        public override TypeSystemContext Context
        {
            get
            {
                return OwningType.Context;
            }
        }

        public IAssemblyDesc ExecutingAssembly
        {
            get;
        }

        public override string Name
        {
            get
            {
                return $"GetExecutingAssembly_{ExecutingAssembly.GetName().Name}";
            }
        }

        public override string DiagnosticName
        {
            get
            {
                return $"GetExecutingAssembly_{ExecutingAssembly.GetName().Name}";
            }
        }

        public override TypeDesc OwningType
        {
            get;
        }

        public override MethodSignature Signature
        {
            get;
        }

        public override MethodIL EmitIL()
        {
            ILEmitter emit = new ILEmitter();
            ILCodeStream codeStream = emit.NewCodeStream();

            MethodDesc classlibHelper = Context.GetHelperEntryPoint("ReflectionHelpers", "GetExecutingAssembly");

            // Use the global module type as "a type from the assembly that has metadata"
            // Our reflection policy always makes sure this has metadata.
            MetadataType moduleType = ((ModuleDesc)ExecutingAssembly).GetGlobalModuleType();

            codeStream.Emit(ILOpcode.ldtoken, emit.NewToken(moduleType));
            codeStream.Emit(ILOpcode.call, emit.NewToken(classlibHelper));
            codeStream.Emit(ILOpcode.ret);

            return emit.Link(this);
        }
    }

    internal class AssemblyGetExecutingAssemblyMethodThunkCache
    {
        private TypeDesc _owningTypeForThunks;
        private Unifier _cache;

        public AssemblyGetExecutingAssemblyMethodThunkCache(TypeDesc owningTypeForThunks)
        {
            _owningTypeForThunks = owningTypeForThunks;
            _cache = new Unifier(this);
        }

        public MethodDesc GetHelper(IAssemblyDesc executingAssembly)
        {
            return _cache.GetOrCreateValue(executingAssembly);
        }

        private class Unifier : LockFreeReaderHashtable<IAssemblyDesc, AssemblyGetExecutingAssemblyMethodThunk>
        {
            private AssemblyGetExecutingAssemblyMethodThunkCache _parent;

            public Unifier(AssemblyGetExecutingAssemblyMethodThunkCache parent)
            {
                _parent = parent;
            }

            protected override int GetKeyHashCode(IAssemblyDesc key)
            {
                return key.GetHashCode();
            }
            protected override int GetValueHashCode(AssemblyGetExecutingAssemblyMethodThunk value)
            {
                return value.ExecutingAssembly.GetHashCode();
            }
            protected override bool CompareKeyToValue(IAssemblyDesc key, AssemblyGetExecutingAssemblyMethodThunk value)
            {
                return key == value.ExecutingAssembly;
            }
            protected override bool CompareValueToValue(AssemblyGetExecutingAssemblyMethodThunk value1, AssemblyGetExecutingAssemblyMethodThunk value2)
            {
                return value1.ExecutingAssembly == value2.ExecutingAssembly;
            }
            protected override AssemblyGetExecutingAssemblyMethodThunk CreateValueFromKey(IAssemblyDesc key)
            {
                return new AssemblyGetExecutingAssemblyMethodThunk(_parent._owningTypeForThunks, key);
            }
        }
    }
}