-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathHostObject.cs
More file actions
168 lines (124 loc) · 4.5 KB
/
HostObject.cs
File metadata and controls
168 lines (124 loc) · 4.5 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
internal sealed class HostObject : HostTarget
{
#region data
private readonly object target;
private readonly Type type;
private static readonly MethodInfo getNullWrapperGenericMethod = typeof(HostObject).GetMethod("GetNullWrapperGeneric", BindingFlags.NonPublic | BindingFlags.Static);
#endregion
#region constructors
private HostObject(object target, Type type, bool isCanonicalRef)
{
if (!isCanonicalRef)
{
target = CanonicalRefTable.GetCanonicalRef(target);
}
if (type is null)
{
type = target.GetType();
}
if (type.IsUnknownCOMObject())
{
if (target is IEnumVARIANT enumVariant)
{
target = new ScriptableEnumeratorOnEnumVariant(enumVariant);
type = typeof(IScriptableEnumerator);
}
}
this.target = target;
this.type = type;
}
#endregion
#region wrappers
public static HostObject Wrap(object target)
{
return Wrap(target, null);
}
public static HostObject Wrap(object target, Type type)
{
return Wrap(target, type, false);
}
public static HostObject Wrap(object target, Type type, bool isCanonicalRef)
{
return (target is not null) ? new HostObject(target, type, isCanonicalRef) : null;
}
public static object WrapResult(object result, Type type, bool wrapNull)
{
if ((result is IScriptMarshalWrapper) || (result is HostTarget))
{
return result;
}
if (result is null)
{
return wrapNull ? GetNullWrapper(type) : null;
}
if ((type == typeof(void)) || (type == typeof(object)) || type.IsNullable())
{
return result;
}
if ((type == result.GetType()) || (Type.GetTypeCode(type) != TypeCode.Object))
{
return result;
}
return Wrap(result, type);
}
#endregion
#region internal members
private static HostObject GetNullWrapper(Type type)
{
return (HostObject)getNullWrapperGenericMethod.MakeGenericMethod(type).Invoke(null, ArrayHelpers.GetEmptyArray<object>());
}
// ReSharper disable UnusedMember.Local
private static HostObject GetNullWrapperGeneric<T>()
{
return NullWrapper<T>.Value;
}
// ReSharper restore UnusedMember.Local
#endregion
#region Object overrides
public override string ToString()
{
if ((target is ScriptItem) && (typeof(ScriptItem).IsAssignableFrom(type)))
{
return "ScriptItem";
}
var objectName = target.GetFriendlyName(type);
return MiscHelpers.FormatInvariant("HostObject:{0}", objectName);
}
#endregion
#region HostTarget overrides
public override Type Type => type;
public override object Target => target;
public override object InvokeTarget => target;
public override object DynamicInvokeTarget => target;
public override HostTargetFlags GetFlags(IHostContext context)
{
var flags = HostTargetFlags.AllowInstanceMembers | HostTargetFlags.AllowExtensionMethods;
if (context.Engine.ExposeHostObjectStaticMembers)
{
flags |= HostTargetFlags.AllowStaticMembers;
}
return flags;
}
public override Invocability GetInvocability(IHostContext context, BindingFlags bindFlags, bool ignoreDynamic)
{
return type.GetInvocability(context, bindFlags, ignoreDynamic);
}
#endregion
#region Nested type: NullWrapper<T>
// ReSharper disable UnusedMember.Local
private static class NullWrapper<T>
{
public static readonly HostObject Value = new(null, typeof(T), true);
}
// ReSharper restore UnusedMember.Local
#endregion
}
}