1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
24 #endregion
25 
26 #if !(NET35 || NET20)
27 using System.Threading;
28 using System;
29 using System.Collections.Generic;
30 using System.Linq;
31 using System.Reflection;
32 using System.Text;
33 using Newtonsoft.Json.Serialization;
34 
35 namespace Newtonsoft.Json.Utilities
36 {
37     internal class FSharpFunction
38     {
39         private readonly object _instance;
40         private readonly MethodCall<object, object> _invoker;
41 
FSharpFunction(object instance, MethodCall<object, object> invoker)42         public FSharpFunction(object instance, MethodCall<object, object> invoker)
43         {
44             _instance = instance;
45             _invoker = invoker;
46         }
47 
Invoke(params object[] args)48         public object Invoke(params object[] args)
49         {
50             object o = _invoker(_instance, args);
51 
52             return o;
53         }
54     }
55 
56     internal static class FSharpUtils
57     {
58         private static readonly object Lock = new object();
59 
60         private static bool _initialized;
61         private static MethodInfo _ofSeq;
62         private static Type _mapType;
63 
64         public static Assembly FSharpCoreAssembly { get; private set; }
65         public static MethodCall<object, object> IsUnion { get; private set; }
66         public static MethodCall<object, object> GetUnionCases { get; private set; }
67         public static MethodCall<object, object> PreComputeUnionTagReader { get; private set; }
68         public static MethodCall<object, object> PreComputeUnionReader { get; private set; }
69         public static MethodCall<object, object> PreComputeUnionConstructor { get; private set; }
70         public static Func<object, object> GetUnionCaseInfoDeclaringType { get; private set; }
71         public static Func<object, object> GetUnionCaseInfoName { get; private set; }
72         public static Func<object, object> GetUnionCaseInfoTag { get; private set; }
73         public static MethodCall<object, object> GetUnionCaseInfoFields { get; private set; }
74 
75         public const string FSharpSetTypeName = "FSharpSet`1";
76         public const string FSharpListTypeName = "FSharpList`1";
77         public const string FSharpMapTypeName = "FSharpMap`2";
78 
EnsureInitialized(Assembly fsharpCoreAssembly)79         public static void EnsureInitialized(Assembly fsharpCoreAssembly)
80         {
81             if (!_initialized)
82             {
83                 lock (Lock)
84                 {
85                     if (!_initialized)
86                     {
87                         FSharpCoreAssembly = fsharpCoreAssembly;
88 
89                         Type fsharpType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Reflection.FSharpType");
90 
91                         MethodInfo isUnionMethodInfo = GetMethodWithNonPublicFallback(fsharpType, "IsUnion", BindingFlags.Public | BindingFlags.Static);
92                         IsUnion = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(isUnionMethodInfo);
93 
94                         MethodInfo getUnionCasesMethodInfo = GetMethodWithNonPublicFallback(fsharpType, "GetUnionCases", BindingFlags.Public | BindingFlags.Static);
95                         GetUnionCases = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(getUnionCasesMethodInfo);
96 
97                         Type fsharpValue = fsharpCoreAssembly.GetType("Microsoft.FSharp.Reflection.FSharpValue");
98 
99                         PreComputeUnionTagReader = CreateFSharpFuncCall(fsharpValue, "PreComputeUnionTagReader");
100                         PreComputeUnionReader = CreateFSharpFuncCall(fsharpValue, "PreComputeUnionReader");
101                         PreComputeUnionConstructor = CreateFSharpFuncCall(fsharpValue, "PreComputeUnionConstructor");
102 
103                         Type unionCaseInfo = fsharpCoreAssembly.GetType("Microsoft.FSharp.Reflection.UnionCaseInfo");
104 
105                         GetUnionCaseInfoName = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(unionCaseInfo.GetProperty("Name"));
106                         GetUnionCaseInfoTag = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(unionCaseInfo.GetProperty("Tag"));
107                         GetUnionCaseInfoDeclaringType = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(unionCaseInfo.GetProperty("DeclaringType"));
108                         GetUnionCaseInfoFields = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(unionCaseInfo.GetMethod("GetFields"));
109 
110                         Type listModule = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.ListModule");
111                         _ofSeq = listModule.GetMethod("OfSeq");
112 
113                         _mapType = fsharpCoreAssembly.GetType("Microsoft.FSharp.Collections.FSharpMap`2");
114 
115 #if !(DOTNET || PORTABLE)
116                         Thread.MemoryBarrier();
117 #endif
118                         _initialized = true;
119                     }
120                 }
121             }
122         }
123 
GetMethodWithNonPublicFallback(Type type, string methodName, BindingFlags bindingFlags)124         private static MethodInfo GetMethodWithNonPublicFallback(Type type, string methodName, BindingFlags bindingFlags)
125         {
126             MethodInfo methodInfo = type.GetMethod(methodName, bindingFlags);
127 
128             // if no matching method then attempt to find with nonpublic flag
129             // this is required because in WinApps some methods are private but always using NonPublic breaks medium trust
130             // https://github.com/JamesNK/Newtonsoft.Json/pull/649
131             // https://github.com/JamesNK/Newtonsoft.Json/issues/821
132             if (methodInfo == null && (bindingFlags & BindingFlags.NonPublic) != BindingFlags.NonPublic)
133             {
134                 methodInfo = type.GetMethod(methodName, bindingFlags | BindingFlags.NonPublic);
135             }
136 
137             return methodInfo;
138         }
139 
CreateFSharpFuncCall(Type type, string methodName)140         private static MethodCall<object, object> CreateFSharpFuncCall(Type type, string methodName)
141         {
142             MethodInfo innerMethodInfo = GetMethodWithNonPublicFallback(type, methodName, BindingFlags.Public | BindingFlags.Static);
143             MethodInfo invokeFunc = innerMethodInfo.ReturnType.GetMethod("Invoke", BindingFlags.Public | BindingFlags.Instance);
144 
145             MethodCall<object, object> call = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(innerMethodInfo);
146             MethodCall<object, object> invoke = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(invokeFunc);
147 
148             MethodCall<object, object> createFunction = (target, args) =>
149             {
150                 object result = call(target, args);
151 
152                 FSharpFunction f = new FSharpFunction(result, invoke);
153                 return f;
154             };
155 
156             return createFunction;
157         }
158 
CreateSeq(Type t)159         public static ObjectConstructor<object> CreateSeq(Type t)
160         {
161             MethodInfo seqType = _ofSeq.MakeGenericMethod(t);
162 
163             return JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(seqType);
164         }
165 
CreateMap(Type keyType, Type valueType)166         public static ObjectConstructor<object> CreateMap(Type keyType, Type valueType)
167         {
168             MethodInfo creatorDefinition = typeof(FSharpUtils).GetMethod("BuildMapCreator");
169 
170             MethodInfo creatorGeneric = creatorDefinition.MakeGenericMethod(keyType, valueType);
171 
172             return (ObjectConstructor<object>)creatorGeneric.Invoke(null, null);
173         }
174 
BuildMapCreator()175         public static ObjectConstructor<object> BuildMapCreator<TKey, TValue>()
176         {
177             Type genericMapType = _mapType.MakeGenericType(typeof(TKey), typeof(TValue));
178             ConstructorInfo ctor = genericMapType.GetConstructor(new[] { typeof(IEnumerable<Tuple<TKey, TValue>>) });
179             ObjectConstructor<object> ctorDelegate = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(ctor);
180 
181             ObjectConstructor<object> creator = args =>
182             {
183                 // convert dictionary KeyValuePairs to Tuples
184                 IEnumerable<KeyValuePair<TKey, TValue>> values = (IEnumerable<KeyValuePair<TKey, TValue>>)args[0];
185                 IEnumerable<Tuple<TKey, TValue>> tupleValues = values.Select(kv => new Tuple<TKey, TValue>(kv.Key, kv.Value));
186 
187                 return ctorDelegate(tupleValues);
188             };
189 
190             return creator;
191         }
192     }
193 }
194 
195 #endif