Repository navigation
Expand file tree
/
Copy pathPythonSiteCache.cs
More file actions
138 lines (127 loc) · 5.86 KB
/
Copy pathPythonSiteCache.cs
File metadata and controls
138 lines (127 loc) · 5.86 KB
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
132
133
134
135
136
137
138
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.Scripting;
using Microsoft.Scripting.Actions;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime.Operations;
namespace IronPython.Runtime.Types {
/// <summary>
/// Cached CallSites. User types are cached on the PythonType and System types are cached on the
/// PythonContext to avoid cross-runtime contamination due to the binder on the site.
/// </summary>
internal class PythonSiteCache {
private Dictionary<string, CallSite<Func<CallSite, object, CodeContext, object>>> _tryGetMemSite;
private Dictionary<string, CallSite<Func<CallSite, object, CodeContext, object>>> _tryGetMemSiteShowCls;
private CallSite<Func<CallSite, CodeContext, object, object>> _dirSite;
private CallSite<Func<CallSite, CodeContext, object, string, object>> _getAttributeSite;
private CallSite<Func<CallSite, CodeContext, object, object, string, object, object>> _setAttrSite;
private CallSite<Func<CallSite, CodeContext, object, object>> _lenSite;
internal CallSite<Func<CallSite, object, CodeContext, object>> GetTryGetMemberSite(CodeContext context, string name) {
CallSite<Func<CallSite, object, CodeContext, object>> site;
if (PythonOps.IsClsVisible(context)) {
if (_tryGetMemSiteShowCls == null) {
Interlocked.CompareExchange(
ref _tryGetMemSiteShowCls,
new Dictionary<string, CallSite<Func<CallSite, object, CodeContext, object>>>(StringComparer.Ordinal),
null
);
}
lock (_tryGetMemSiteShowCls) {
if (!_tryGetMemSiteShowCls.TryGetValue(name, out site)) {
_tryGetMemSiteShowCls[name] = site = CallSite<Func<CallSite, object, CodeContext, object>>.Create(
PythonContext.GetContext(context).GetMember(
name,
true
)
);
}
}
} else {
if (_tryGetMemSite == null) {
Interlocked.CompareExchange(
ref _tryGetMemSite,
new Dictionary<string, CallSite<Func<CallSite, object, CodeContext, object>>>(StringComparer.Ordinal),
null
);
}
lock (_tryGetMemSite) {
if (!_tryGetMemSite.TryGetValue(name, out site)) {
_tryGetMemSite[name] = site = CallSite<Func<CallSite, object, CodeContext, object>>.Create(
PythonContext.GetContext(context).GetMember(
name,
true
)
);
}
}
}
return site;
}
internal CallSite<Func<CallSite, CodeContext, object, object>> GetDirSite(CodeContext context) {
if (_dirSite == null) {
Interlocked.CompareExchange(
ref _dirSite,
CallSite<Func<CallSite, CodeContext, object, object>>.Create(
PythonContext.GetContext(context).InvokeNone
),
null);
}
return _dirSite;
}
internal CallSite<Func<CallSite, CodeContext, object, string, object>> GetGetAttributeSite(CodeContext context) {
if (_getAttributeSite == null) {
Interlocked.CompareExchange(
ref _getAttributeSite,
CallSite<Func<CallSite, CodeContext, object, string, object>>.Create(
PythonContext.GetContext(context).InvokeOne
),
null
);
}
return _getAttributeSite;
}
internal CallSite<Func<CallSite, CodeContext, object, object, string, object, object>> GetSetAttrSite(CodeContext context) {
if (_setAttrSite == null) {
Interlocked.CompareExchange(
ref _setAttrSite,
CallSite<Func<CallSite, CodeContext, object, object, string, object, object>>.Create(
PythonContext.GetContext(context).Invoke(
new CallSignature(4)
)
),
null
);
}
return _setAttrSite;
}
internal CallSite<Func<CallSite, CodeContext, object, object>> GetLenSite(CodeContext context) {
if (_lenSite == null) {
Interlocked.CompareExchange(
ref _lenSite,
CallSite<Func<CallSite, CodeContext, object, object>>.Create(
PythonContext.GetContext(context).InvokeNone
),
null
);
}
return _lenSite;
}
}
}