forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostVariable.cs
More file actions
181 lines (146 loc) · 5.71 KB
/
HostVariable.cs
File metadata and controls
181 lines (146 loc) · 5.71 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
180
181
// 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; set; }
}
internal abstract class HostVariableBase : HostTarget
{
private static readonly string[] auxPropertyNames = { "out", "ref", "value" };
public override string[] GetAuxPropertyNames(IHostInvokeContext context, BindingFlags bindFlags)
{
return auxPropertyNames;
}
}
internal sealed class HostVariable<T> : HostVariableBase, IHostVariable
{
public HostVariable(T initValue)
{
if ((typeof(T) == typeof(Undefined)) || (typeof(T) == typeof(VoidResult)))
{
throw new NotSupportedException("Unsupported variable type");
}
if (typeof(HostItem).IsAssignableFrom(typeof(T)) || typeof(HostTarget).IsAssignableFrom(typeof(T)))
{
throw new NotSupportedException("Unsupported variable type");
}
if ((initValue is HostItem) || (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(IHostInvokeContext context)
{
var flags = HostTargetFlags.AllowInstanceMembers | HostTargetFlags.AllowExtensionMethods;
if (context.Engine.ExposeHostObjectStaticMembers)
{
flags |= HostTargetFlags.AllowStaticMembers;
}
return flags;
}
public override bool TryInvokeAuxMember(IHostInvokeContext 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 (name == "out")
{
if ((invokeFlags & getPropertyFlags) != 0)
{
result = new OutArg<T>(this);
return true;
}
}
else if (name == "ref")
{
if ((invokeFlags & getPropertyFlags) != 0)
{
result = new RefArg<T>(this);
return true;
}
}
else if (name == "value")
{
if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
{
if (InvokeHelpers.TryInvokeObject(context, Value, invokeFlags, args, bindArgs, typeof(IDynamicMetaObjectProvider).IsAssignableFrom(typeof(T)), out result))
{
return true;
}
if (invokeFlags.HasFlag(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).Value = args[0], typeof(T), ScriptMemberFlags.None, false);
return true;
}
}
}
result = null;
return false;
}
public override Invocability GetInvocability(BindingFlags bindFlags, Type accessContext, ScriptAccess defaultAccess, bool ignoreDynamic)
{
return typeof(T).GetInvocability(bindFlags, accessContext, defaultAccess, ignoreDynamic);
}
#endregion
#region IHostVariable implementation
object IHostVariable.Value
{
get => Value;
set
{
if (!typeof(T).IsAssignableFrom(ref value))
{
throw new InvalidOperationException("Assignment invalid due to type mismatch");
}
if ((value is HostItem) || (value is HostTarget))
{
throw new NotSupportedException("Unsupported value type");
}
Value = (T)value;
}
}
#endregion
}
}