mscorlib(4.0.0.0) API with additions
RuntimeOps.cs
4 using System.Diagnostics;
5 using System.Dynamic;
6 using System.Dynamic.Utils;
9 
11 {
13  [EditorBrowsable(EditorBrowsableState.Never)]
14  [DebuggerStepThrough]
15  [global::__DynamicallyInvokable]
16  public static class RuntimeOps
17  {
18  private sealed class ExpressionQuoter : ExpressionVisitor
19  {
20  private readonly HoistedLocals _scope;
21 
22  private readonly object[] _locals;
23 
24  private readonly Stack<Set<ParameterExpression>> _shadowedVars = new Stack<Set<ParameterExpression>>();
25 
26  internal ExpressionQuoter(HoistedLocals scope, object[] locals)
27  {
28  _scope = scope;
29  _locals = locals;
30  }
31 
32  protected internal override Expression VisitLambda<T>(Expression<T> node)
33  {
34  _shadowedVars.Push(new Set<ParameterExpression>(node.Parameters));
35  Expression expression = Visit(node.Body);
36  _shadowedVars.Pop();
37  if (expression == node.Body)
38  {
39  return node;
40  }
41  return Expression.Lambda<T>(expression, node.Name, node.TailCall, node.Parameters);
42  }
43 
44  protected internal override Expression VisitBlock(BlockExpression node)
45  {
46  if (node.Variables.Count > 0)
47  {
48  _shadowedVars.Push(new Set<ParameterExpression>(node.Variables));
49  }
50  ReadOnlyCollection<Expression> readOnlyCollection = Visit(node.Expressions);
51  if (node.Variables.Count > 0)
52  {
53  _shadowedVars.Pop();
54  }
55  if (readOnlyCollection == node.Expressions)
56  {
57  return node;
58  }
59  return Expression.Block(node.Variables, readOnlyCollection);
60  }
61 
62  protected override CatchBlock VisitCatchBlock(CatchBlock node)
63  {
64  if (node.Variable != null)
65  {
66  _shadowedVars.Push(new Set<ParameterExpression>(new ParameterExpression[1]
67  {
68  node.Variable
69  }));
70  }
71  Expression expression = Visit(node.Body);
72  Expression expression2 = Visit(node.Filter);
73  if (node.Variable != null)
74  {
75  _shadowedVars.Pop();
76  }
77  if (expression == node.Body && expression2 == node.Filter)
78  {
79  return node;
80  }
81  return Expression.MakeCatchBlock(node.Test, node.Variable, expression, expression2);
82  }
83 
84  protected internal override Expression VisitRuntimeVariables(RuntimeVariablesExpression node)
85  {
86  int count = node.Variables.Count;
89  int[] array = new int[count];
90  for (int i = 0; i < count; i++)
91  {
92  IStrongBox box = GetBox(node.Variables[i]);
93  if (box == null)
94  {
95  array[i] = list2.Count;
96  list2.Add(node.Variables[i]);
97  }
98  else
99  {
100  array[i] = -1 - list.Count;
101  list.Add(box);
102  }
103  }
104  if (list.Count == 0)
105  {
106  return node;
107  }
108  ConstantExpression constantExpression = Expression.Constant(new RuntimeVariables(list.ToArray()), typeof(IRuntimeVariables));
109  if (list2.Count == 0)
110  {
111  return constantExpression;
112  }
113  return Expression.Call(typeof(RuntimeOps).GetMethod("MergeRuntimeVariables"), Expression.RuntimeVariables(new TrueReadOnlyCollection<ParameterExpression>(list2.ToArray())), constantExpression, Expression.Constant(array));
114  }
115 
116  protected internal override Expression VisitParameter(ParameterExpression node)
117  {
118  IStrongBox box = GetBox(node);
119  if (box == null)
120  {
121  return node;
122  }
123  return Expression.Field(Expression.Constant(box), "Value");
124  }
125 
126  private IStrongBox GetBox(ParameterExpression variable)
127  {
128  foreach (Set<ParameterExpression> shadowedVar in _shadowedVars)
129  {
130  if (shadowedVar.Contains(variable))
131  {
132  return null;
133  }
134  }
135  HoistedLocals hoistedLocals = _scope;
136  object[] array = _locals;
137  while (true)
138  {
139  if (hoistedLocals.Indexes.TryGetValue(variable, out int value))
140  {
141  return (IStrongBox)array[value];
142  }
143  hoistedLocals = hoistedLocals.Parent;
144  if (hoistedLocals == null)
145  {
146  break;
147  }
148  array = HoistedLocals.GetParent(array);
149  }
150  throw ContractUtils.Unreachable;
151  }
152  }
153 
154  private sealed class RuntimeVariables : IRuntimeVariables
155  {
156  private readonly IStrongBox[] _boxes;
157 
159  {
160  get
161  {
162  return _boxes.Length;
163  }
164  }
165 
166  object IRuntimeVariables.this[int index]
167  {
168  get
169  {
170  return _boxes[index].Value;
171  }
172  set
173  {
174  _boxes[index].Value = value;
175  }
176  }
177 
178  internal RuntimeVariables(IStrongBox[] boxes)
179  {
180  _boxes = boxes;
181  }
182  }
183 
184  private sealed class MergedRuntimeVariables : IRuntimeVariables
185  {
186  private readonly IRuntimeVariables _first;
187 
188  private readonly IRuntimeVariables _second;
189 
190  private readonly int[] _indexes;
191 
192  public int Count => _indexes.Length;
193 
194  public object this[int index]
195  {
196  get
197  {
198  index = _indexes[index];
199  if (index < 0)
200  {
201  return _second[-1 - index];
202  }
203  return _first[index];
204  }
205  set
206  {
207  index = _indexes[index];
208  if (index >= 0)
209  {
210  _first[index] = value;
211  }
212  else
213  {
214  _second[-1 - index] = value;
215  }
216  }
217  }
218 
219  internal MergedRuntimeVariables(IRuntimeVariables first, IRuntimeVariables second, int[] indexes)
220  {
221  _first = first;
222  _second = second;
223  _indexes = indexes;
224  }
225  }
226 
227  private sealed class EmptyRuntimeVariables : IRuntimeVariables
228  {
230  {
231  get
232  {
233  return 0;
234  }
235  }
236 
237  object IRuntimeVariables.this[int index]
238  {
239  get
240  {
241  throw new ArgumentOutOfRangeException("index");
242  }
243  set
244  {
245  throw new ArgumentOutOfRangeException("index");
246  }
247  }
248  }
249 
250  private sealed class RuntimeVariableList : IRuntimeVariables
251  {
252  private readonly object[] _data;
253 
254  private readonly long[] _indexes;
255 
256  public int Count => _indexes.Length;
257 
258  public object this[int index]
259  {
260  get
261  {
262  return GetStrongBox(index).Value;
263  }
264  set
265  {
266  GetStrongBox(index).Value = value;
267  }
268  }
269 
270  internal RuntimeVariableList(object[] data, long[] indexes)
271  {
272  _data = data;
273  _indexes = indexes;
274  }
275 
276  private IStrongBox GetStrongBox(int index)
277  {
278  long num = _indexes[index];
279  object[] array = _data;
280  for (int num2 = (int)(num >> 32); num2 > 0; num2--)
281  {
282  array = HoistedLocals.GetParent(array);
283  }
284  return (IStrongBox)array[(int)num];
285  }
286  }
287 
296  [Obsolete("do not use this method", true)]
297  [EditorBrowsable(EditorBrowsableState.Never)]
298  [global::__DynamicallyInvokable]
299  public static bool ExpandoTryGetValue(ExpandoObject expando, object indexClass, int index, string name, bool ignoreCase, out object value)
300  {
301  return expando.TryGetValue(indexClass, index, name, ignoreCase, out value);
302  }
303 
312  [Obsolete("do not use this method", true)]
313  [EditorBrowsable(EditorBrowsableState.Never)]
314  [global::__DynamicallyInvokable]
315  public static object ExpandoTrySetValue(ExpandoObject expando, object indexClass, int index, object value, string name, bool ignoreCase)
316  {
317  expando.TrySetValue(indexClass, index, value, name, ignoreCase, add: false);
318  return value;
319  }
320 
328  [Obsolete("do not use this method", true)]
329  [EditorBrowsable(EditorBrowsableState.Never)]
330  [global::__DynamicallyInvokable]
331  public static bool ExpandoTryDeleteValue(ExpandoObject expando, object indexClass, int index, string name, bool ignoreCase)
332  {
333  return expando.TryDeleteValue(indexClass, index, name, ignoreCase, ExpandoObject.Uninitialized);
334  }
335 
340  [Obsolete("do not use this method", true)]
341  [EditorBrowsable(EditorBrowsableState.Never)]
342  [global::__DynamicallyInvokable]
343  public static bool ExpandoCheckVersion(ExpandoObject expando, object version)
344  {
345  return expando.Class == version;
346  }
347 
352  [Obsolete("do not use this method", true)]
353  [EditorBrowsable(EditorBrowsableState.Never)]
354  [global::__DynamicallyInvokable]
355  public static void ExpandoPromoteClass(ExpandoObject expando, object oldClass, object newClass)
356  {
357  expando.PromoteClass(oldClass, newClass);
358  }
359 
365  [Obsolete("do not use this method", true)]
366  [EditorBrowsable(EditorBrowsableState.Never)]
367  [global::__DynamicallyInvokable]
368  public static Expression Quote(Expression expression, object hoistedLocals, object[] locals)
369  {
370  ExpressionQuoter expressionQuoter = new ExpressionQuoter((HoistedLocals)hoistedLocals, locals);
371  return expressionQuoter.Visit(expression);
372  }
373 
379  [Obsolete("do not use this method", true)]
380  [EditorBrowsable(EditorBrowsableState.Never)]
381  [global::__DynamicallyInvokable]
383  {
384  return new MergedRuntimeVariables(first, second, indexes);
385  }
386 
391  [Obsolete("do not use this method", true)]
392  [EditorBrowsable(EditorBrowsableState.Never)]
393  [global::__DynamicallyInvokable]
394  public static IRuntimeVariables CreateRuntimeVariables(object[] data, long[] indexes)
395  {
396  return new RuntimeVariableList(data, indexes);
397  }
398 
401  [Obsolete("do not use this method", true)]
402  [EditorBrowsable(EditorBrowsableState.Never)]
403  [global::__DynamicallyInvokable]
405  {
406  return new EmptyRuntimeVariables();
407  }
408  }
409 }
Represents an expression that has a constant value.
static bool ExpandoCheckVersion(ExpandoObject expando, object version)
Checks the version of the Expando object.
Definition: RuntimeOps.cs:343
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
A list of run-time variables. For more information, see T:System.Linq.Expressions....
Provides the base class for a generic read-only collection.
static IRuntimeVariables MergeRuntimeVariables(IRuntimeVariables first, IRuntimeVariables second, int[] indexes)
Combines two runtime variable lists and returns a new list.
Definition: RuntimeOps.cs:382
EditorBrowsableState
Specifies the browsable state of a property or method from within an editor.
Expression Filter
Gets the body of the T:System.Linq.Expressions.CatchBlock filter.
Definition: CatchBlock.cs:58
Represents a block that contains a sequence of expressions where variables can be defined.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
int Count
Gets the number of elements contained in the T:System.Collections.ObjectModel.ReadOnlyCollection`1 in...
static IRuntimeVariables CreateRuntimeVariables(object[] data, long[] indexes)
Creates an interface that can be used to modify closed over variables at runtime.
Definition: RuntimeOps.cs:394
Expression Body
Gets the body of the catch block.
Definition: CatchBlock.cs:46
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Definition: List.cs:510
static bool ExpandoTryDeleteValue(ExpandoObject expando, object indexClass, int index, string name, bool ignoreCase)
Deletes the value of an item in an expando object.
Definition: RuntimeOps.cs:331
Provides the base class from which the classes that represent expression tree nodes are derived....
Definition: Expression.cs:17
An expression that provides runtime read/write permission for variables.
static object ExpandoTrySetValue(ExpandoObject expando, object indexClass, int index, object value, string name, bool ignoreCase)
Sets the value of an item in an expando object.
Definition: RuntimeOps.cs:315
static void ExpandoPromoteClass(ExpandoObject expando, object oldClass, object newClass)
Promotes an Expando object from one class to a new class.
Definition: RuntimeOps.cs:355
ReadOnlyCollection< Expression > Expressions
Gets the expressions in this block.
ParameterExpression Variable
Gets a reference to the T:System.Exception object caught by this handler.
Definition: CatchBlock.cs:22
Type Test
Gets the type of T:System.Exception this handler catches.
Definition: CatchBlock.cs:34
Represents a named parameter expression.
static IRuntimeVariables CreateRuntimeVariables()
Creates an interface that can be used to modify closed over variables at runtime.
Definition: RuntimeOps.cs:404
ReadOnlyCollection< ParameterExpression > Variables
The variables or parameters to which to provide runtime access.
Represents a variable size last-in-first-out (LIFO) collection of instances of the same specified typ...
Definition: Stack.cs:14
static Expression Quote(Expression expression, object hoistedLocals, object[] locals)
Quotes the provided expression tree.
Definition: RuntimeOps.cs:368
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
Represents the values of run-time variables.
int Count
Gets a count of the run-time variables.
Represents a catch statement in a try block.
Definition: CatchBlock.cs:8
ReadOnlyCollection< ParameterExpression > Variables
Gets the variables defined in this block.
Defines a property for accessing the value that an object references.
Definition: IStrongBox.cs:5
Represents an object whose members can be dynamically added and removed at run time.
T [] ToArray()
Copies the elements of the T:System.Collections.Generic.List`1 to a new array.
Definition: List.cs:1531
Represents a visitor or rewriter for expression trees.
static bool ExpandoTryGetValue(ExpandoObject expando, object indexClass, int index, string name, bool ignoreCase, out object value)
Gets the value of an item in an expando object.
Definition: RuntimeOps.cs:299
Contains helper methods called from dynamically generated methods.
Definition: RuntimeOps.cs:16