|
1 |
| -/** |
2 |
| - * Copyright (c) Rich Hickey. All rights reserved. |
3 |
| - * The use and distribution terms for this software are covered by the |
4 |
| - * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
5 |
| - * which can be found in the file epl-v10.html at the root of this distribution. |
6 |
| - * By using this software in any fashion, you are agreeing to be bound by |
7 |
| - * the terms of this license. |
8 |
| - * You must not remove this notice, or any other, from this software. |
9 |
| - **/ |
10 |
| - |
11 |
| -/** |
12 |
| - * Author: David Miller |
13 |
| - **/ |
14 |
| - |
15 |
| - |
16 |
| -using System; |
17 |
| -using System.Collections.Generic; |
18 |
| -using System.Linq; |
19 |
| -#if CLR2 |
20 |
| -using Microsoft.Scripting.Ast; |
21 |
| -#else |
22 |
| -using System.Linq.Expressions; |
23 |
| -#endif |
24 |
| -using Microsoft.Scripting.Actions; |
25 |
| -using Microsoft.Scripting.Actions.Calls; |
26 |
| -using Microsoft.Scripting.Utils; |
27 |
| -using Microsoft.Scripting.Generation; |
28 |
| -using System.Dynamic; |
29 |
| -using Microsoft.Scripting.Runtime; |
30 |
| - |
31 |
| -namespace clojure.lang.Runtime.Binding |
32 |
| -{ |
33 |
| - using Ast = Expression; |
34 |
| - using AstUtils = Microsoft.Scripting.Ast.Utils; |
35 |
| - using System.Collections; |
36 |
| - |
37 |
| - |
38 |
| - public class ClojureBinder : DefaultBinder |
39 |
| - { |
40 |
| - #region Instance variables |
41 |
| - |
42 |
| - readonly ClojureContext _context; |
43 |
| - |
44 |
| - #endregion |
45 |
| - |
46 |
| - #region Properties |
47 |
| - |
48 |
| - public ClojureContext Context { get { return _context; } } |
49 |
| - |
50 |
| - public override bool PrivateBinding { get { return _context.DomainManager.Configuration.PrivateBinding; } } |
51 |
| - |
52 |
| - #endregion |
53 |
| - |
54 |
| - #region Constructors |
55 |
| - |
56 |
| - public ClojureBinder(ClojureContext context) |
57 |
| - { |
58 |
| - _context = context; |
59 |
| - } |
60 |
| - |
61 |
| - #endregion |
62 |
| - |
63 |
| - #region conversions |
64 |
| - |
65 |
| - public override bool CanConvertFrom(Type fromType, Type toType, bool toNotNullable, NarrowingLevel level) |
66 |
| - { |
67 |
| - return Converter.CanConvertFrom(fromType, toType, level); |
68 |
| - } |
69 |
| - |
70 |
| - public override Ast ConvertExpression(Ast expr, Type toType, ConversionResultKind kind, OverloadResolverFactory resolverFactory) |
71 |
| - { |
72 |
| - Type exprType = expr.Type; |
73 |
| - Type visType = CompilerHelpers.GetVisibleType(toType); |
74 |
| - |
75 |
| - if (typeof(IFn).IsAssignableFrom(exprType) && typeof(Delegate).IsAssignableFrom(visType)) |
76 |
| - return Ast.Call(typeof(Converter).GetMethod("ConvertToDelegate"), Ast.Convert(expr, typeof(IFn)), Expression.Constant(visType)); |
77 |
| - |
78 |
| - // Follow through on our promise to convert IEnumerable<Object> or IEnumerable to IEnumerable<T> for any T |
79 |
| - if (toType.IsGenericType && ! toType.IsAssignableFrom(expr.Type)) |
80 |
| - { |
81 |
| - // The following is inspired by IronPython.Runtime.Binding.Python.ConversionBinder.FallbackConvert |
82 |
| - Type genTo = toType.GetGenericTypeDefinition(); |
83 |
| - if ( genTo == typeof(IList<>)) |
84 |
| - { |
85 |
| - return MakeToGenericConversion(expr,toType,typeof(IList<object>),typeof(ListGenericWrapper<>)); |
86 |
| - } |
87 |
| - else if (genTo == typeof(IDictionary<,>)) |
88 |
| - { |
89 |
| - return MakeToGenericConversion(expr,toType,typeof(IDictionary<object,object>),typeof(DictionaryGenericWrapper<,>)); |
90 |
| - } |
91 |
| - else if (genTo == typeof(IEnumerable<>)) |
92 |
| - { |
93 |
| - return MakeToGenericConversion(expr, toType, typeof(IEnumerable),typeof(IEnumerableOfTWrapper<>)); |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - return base.ConvertExpression(expr, toType, kind, resolverFactory); |
98 |
| - } |
99 |
| - |
100 |
| - private static Ast MakeToGenericConversion(Ast expr, Type toType, Type fromType, Type wrapperType) |
101 |
| - { |
102 |
| - |
103 |
| - Type destType = wrapperType.MakeGenericType(toType.GetGenericArguments()); |
104 |
| - return Ast.New( |
105 |
| - destType.GetConstructor(new Type[] { fromType }), |
106 |
| - AstUtils.Convert(expr, fromType)); |
107 |
| - } |
108 |
| - |
109 |
| - #endregion |
110 |
| - } |
111 |
| -} |
| 1 | +/** |
| 2 | + * Copyright (c) Rich Hickey. All rights reserved. |
| 3 | + * The use and distribution terms for this software are covered by the |
| 4 | + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 5 | + * which can be found in the file epl-v10.html at the root of this distribution. |
| 6 | + * By using this software in any fashion, you are agreeing to be bound by |
| 7 | + * the terms of this license. |
| 8 | + * You must not remove this notice, or any other, from this software. |
| 9 | + **/ |
| 10 | + |
| 11 | +/** |
| 12 | + * Author: David Miller |
| 13 | + **/ |
| 14 | + |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +#if CLR2 |
| 20 | +using Microsoft.Scripting.Ast; |
| 21 | +#else |
| 22 | +using System.Linq.Expressions; |
| 23 | +#endif |
| 24 | +using Microsoft.Scripting.Actions; |
| 25 | +using Microsoft.Scripting.Actions.Calls; |
| 26 | +using Microsoft.Scripting.Utils; |
| 27 | +using Microsoft.Scripting.Generation; |
| 28 | +using System.Dynamic; |
| 29 | +using Microsoft.Scripting.Runtime; |
| 30 | + |
| 31 | +namespace clojure.lang.Runtime.Binding |
| 32 | +{ |
| 33 | + using Ast = Expression; |
| 34 | + using AstUtils = Microsoft.Scripting.Ast.Utils; |
| 35 | + using System.Collections; |
| 36 | + |
| 37 | + |
| 38 | + public class ClojureBinder : DefaultBinder |
| 39 | + { |
| 40 | + #region Instance variables |
| 41 | + |
| 42 | + readonly ClojureContext _context; |
| 43 | + |
| 44 | + #endregion |
| 45 | + |
| 46 | + #region Properties |
| 47 | + |
| 48 | + public ClojureContext Context { get { return _context; } } |
| 49 | + |
| 50 | + public override bool PrivateBinding { get { return _context.DomainManager.Configuration.PrivateBinding; } } |
| 51 | + |
| 52 | + #endregion |
| 53 | + |
| 54 | + #region Constructors |
| 55 | + |
| 56 | + public ClojureBinder(ClojureContext context) |
| 57 | + { |
| 58 | + _context = context; |
| 59 | + } |
| 60 | + |
| 61 | + #endregion |
| 62 | + |
| 63 | + #region conversions |
| 64 | + |
| 65 | + public override bool CanConvertFrom(Type fromType, Type toType, bool toNotNullable, NarrowingLevel level) |
| 66 | + { |
| 67 | + return Converter.CanConvertFrom(fromType, toType, level); |
| 68 | + } |
| 69 | + |
| 70 | + public override Ast ConvertExpression(Ast expr, Type toType, ConversionResultKind kind, OverloadResolverFactory resolverFactory) |
| 71 | + { |
| 72 | + Type exprType = expr.Type; |
| 73 | + Type visType = CompilerHelpers.GetVisibleType(toType); |
| 74 | + |
| 75 | + if (typeof(IFn).IsAssignableFrom(exprType) && typeof(Delegate).IsAssignableFrom(visType)) |
| 76 | + return Ast.Call(typeof(Converter).GetMethod("ConvertToDelegate"), Ast.Convert(expr, typeof(IFn)), Expression.Constant(visType)); |
| 77 | + |
| 78 | + // Follow through on our promise to convert IEnumerable<Object> or IEnumerable to IEnumerable<T> for any T |
| 79 | + if (toType.IsGenericType && ! toType.IsAssignableFrom(expr.Type)) |
| 80 | + { |
| 81 | + // The following is inspired by IronPython.Runtime.Binding.Python.ConversionBinder.FallbackConvert |
| 82 | + Type genTo = toType.GetGenericTypeDefinition(); |
| 83 | + if ( genTo == typeof(IList<>)) |
| 84 | + { |
| 85 | + return MakeToGenericConversion(expr,toType,typeof(IList<object>),typeof(ListGenericWrapper<>)); |
| 86 | + } |
| 87 | + else if (genTo == typeof(IDictionary<,>)) |
| 88 | + { |
| 89 | + return MakeToGenericConversion(expr,toType,typeof(IDictionary<object,object>),typeof(DictionaryGenericWrapper<,>)); |
| 90 | + } |
| 91 | + else if (genTo == typeof(IEnumerable<>)) |
| 92 | + { |
| 93 | + return MakeToGenericConversion(expr, toType, typeof(IEnumerable),typeof(IEnumerableOfTWrapper<>)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return base.ConvertExpression(expr, toType, kind, resolverFactory); |
| 98 | + } |
| 99 | + |
| 100 | + private static Ast MakeToGenericConversion(Ast expr, Type toType, Type fromType, Type wrapperType) |
| 101 | + { |
| 102 | + |
| 103 | + Type destType = wrapperType.MakeGenericType(toType.GetGenericArguments()); |
| 104 | + return Ast.New( |
| 105 | + destType.GetConstructor(new Type[] { fromType }), |
| 106 | + AstUtils.Convert(expr, fromType)); |
| 107 | + } |
| 108 | + |
| 109 | + #endregion |
| 110 | + } |
| 111 | +} |
0 commit comments