-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathHostVariable.cs
More file actions
179 lines (144 loc) · 5.86 KB
/
HostVariable.cs
File metadata and controls
179 lines (144 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Dynamic;
using System.Reflection;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
internal interface IHostVariable
{
Type Type { get; }
object Value { get; }
object SetValue(IHostContext context, object value);
}
internal abstract class HostVariable : HostTarget
{
private static readonly string[] auxPropertyNames = { "out", "ref", "value" };
public override string[] GetAuxPropertyNames(IHostContext context, BindingFlags bindFlags)
{
return auxPropertyNames;
}
}
internal sealed class HostVariable<T> : HostVariable, IHostVariable
{
public HostVariable(T initValue)
{
if ((typeof(T) == typeof(Undefined)) || (typeof(T) == typeof(VoidResult)))
{
throw new NotSupportedException("Unsupported variable type");
}
if (typeof(IHostItem).IsAssignableFrom(typeof(T)) || typeof(HostTarget).IsAssignableFrom(typeof(T)))
{
throw new NotSupportedException("Unsupported variable type");
}
if ((initValue is IHostItem) || (initValue is HostTarget))
{
throw new NotSupportedException("Unsupported value type");
}
Value = initValue;
}
public T Value { get; set; }
// Be careful when renaming or deleting this property; it is accessed by name in the
// expression tree construction code in DelegateFactory.CreateComplexDelegate().
#region Object overrides
public override string ToString()
{
var objectName = Value.GetFriendlyName(typeof(T));
return MiscHelpers.FormatInvariant("HostVariable:{0}", objectName);
}
#endregion
#region HostTarget overrides
public override Type Type => typeof(T);
public override object Target => Value;
public override object InvokeTarget => Value;
public override object DynamicInvokeTarget => Value;
public override HostTargetFlags GetFlags(IHostContext context)
{
var flags = HostTargetFlags.AllowInstanceMembers | HostTargetFlags.AllowExtensionMethods;
if (context.Engine.ExposeHostObjectStaticMembers)
{
flags |= HostTargetFlags.AllowStaticMembers;
}
return flags;
}
public override bool TryInvokeAuxMember(IHostContext context, string name, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
{
const BindingFlags getPropertyFlags =
BindingFlags.GetField |
BindingFlags.GetProperty;
const BindingFlags setPropertyFlags =
BindingFlags.SetProperty |
BindingFlags.PutDispProperty |
BindingFlags.PutRefDispProperty;
if (string.Equals(name, "out", invokeFlags.GetMemberNameComparison()))
{
if ((invokeFlags & getPropertyFlags) != 0)
{
result = new OutArg<T>(this);
return true;
}
}
else if (string.Equals(name, "ref", invokeFlags.GetMemberNameComparison()))
{
if ((invokeFlags & getPropertyFlags) != 0)
{
result = new RefArg<T>(this);
return true;
}
}
else if (string.Equals(name, "value", invokeFlags.GetMemberNameComparison()))
{
if (invokeFlags.HasAllFlags(BindingFlags.InvokeMethod))
{
if (InvokeHelpers.TryInvokeObject(context, Value, invokeFlags, args, bindArgs, typeof(IDynamicMetaObjectProvider).IsAssignableFrom(typeof(T)), out result))
{
return true;
}
if (invokeFlags.HasAllFlags(BindingFlags.GetField) && (args.Length < 1))
{
result = context.Engine.PrepareResult(Value, ScriptMemberFlags.None, false);
return true;
}
result = null;
return false;
}
if ((invokeFlags & getPropertyFlags) != 0)
{
result = context.Engine.PrepareResult(Value, ScriptMemberFlags.None, false);
return true;
}
if ((invokeFlags & setPropertyFlags) != 0)
{
if (args.Length == 1)
{
result = context.Engine.PrepareResult(((IHostVariable)this).SetValue(context, args[0]), typeof(T), ScriptMemberFlags.None, false);
return true;
}
}
}
result = null;
return false;
}
public override Invocability GetInvocability(IHostContext context, BindingFlags bindFlags, bool ignoreDynamic)
{
return typeof(T).GetInvocability(context, bindFlags, ignoreDynamic);
}
#endregion
#region IHostVariable implementation
object IHostVariable.Value => Value;
object IHostVariable.SetValue(IHostContext context, object value)
{
if (!typeof(T).IsAssignableFromValue(context, ref value))
{
throw new InvalidOperationException("Assignment invalid due to type mismatch");
}
if ((value is IHostItem) || (value is HostTarget))
{
throw new NotSupportedException("Unsupported value type");
}
return Value = (T)value;
}
#endregion
}
}