Runner.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // Runner.cs
  3. //
  4. // (C) 2008 Mainsoft, Inc. (http://www.mainsoft.com)
  5. // (C) 2008 db4objects, Inc. (http://www.db4o.com)
  6. // (C) 2010 Novell, Inc. (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Linq.Expressions;
  28. using System.Reflection;
  29. namespace System.Linq.jvm {
  30. sealed class Runner {
  31. sealed class VoidTypeMarker {}
  32. static readonly Type VoidMarker = typeof (VoidTypeMarker);
  33. static readonly MethodInfo [] delegates = new MethodInfo [5];
  34. const BindingFlags method_flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
  35. readonly LambdaExpression lambda;
  36. readonly ExpressionInterpreter interpreter;
  37. static Runner ()
  38. {
  39. foreach (var method in typeof (Runner).GetMethods (method_flags).Where (m => m.Name == "GetDelegate"))
  40. delegates [method.GetGenericArguments ().Length - 1] = method;
  41. }
  42. public Runner (LambdaExpression lambda)
  43. {
  44. this.lambda = lambda;
  45. }
  46. public Runner (LambdaExpression lambda, ExpressionInterpreter interpreter)
  47. {
  48. this.lambda = lambda;
  49. this.interpreter = interpreter;
  50. }
  51. public Delegate CreateDelegate ()
  52. {
  53. var types = GetGenericSignature ();
  54. var creator = delegates [types.Length - 1].MakeGenericMethod (types);
  55. return (Delegate) creator.Invoke (this, new object [0]);
  56. }
  57. Type [] GetGenericSignature ()
  58. {
  59. var count = lambda.Parameters.Count;
  60. var types = new Type [count + 1];
  61. var return_type = lambda.GetReturnType ();
  62. if (return_type == typeof (void))
  63. return_type = VoidMarker;
  64. types [count] = return_type;
  65. for (int i = 0; i < count; i++) {
  66. types [i] = lambda.Parameters [i].Type;
  67. }
  68. return types;
  69. }
  70. object Run (object [] arguments)
  71. {
  72. var interpreter = this.interpreter ?? new ExpressionInterpreter (lambda);
  73. return interpreter.Interpret (lambda, arguments);
  74. }
  75. MethodInfo GetActionRunner (params Type [] types)
  76. {
  77. return GetRunner ("ActionRunner", types);
  78. }
  79. MethodInfo GetFuncRunner (params Type [] types)
  80. {
  81. return GetRunner ("FuncRunner", types);
  82. }
  83. MethodInfo GetRunner (string name, Type [] type_arguments)
  84. {
  85. var method = GetMethod (name, type_arguments.Length);
  86. if (method == null)
  87. throw new InvalidOperationException ();
  88. if (type_arguments.Length == 0)
  89. return method;
  90. return method.MakeGenericMethod (type_arguments);
  91. }
  92. MethodInfo GetMethod (string name, int parameters)
  93. {
  94. foreach (var method in GetType ().GetMethods (method_flags)) {
  95. if (method.Name != name)
  96. continue;
  97. if (method.GetGenericArguments ().Length != parameters)
  98. continue;
  99. return method;
  100. }
  101. return null;
  102. }
  103. Delegate CreateDelegate (MethodInfo runner)
  104. {
  105. return Delegate.CreateDelegate (lambda.Type, this, runner);
  106. }
  107. // all methods below are called through reflection
  108. Delegate GetDelegate<TResult> ()
  109. {
  110. if (typeof (TResult) == VoidMarker)
  111. return CreateDelegate (GetActionRunner (Type.EmptyTypes));
  112. return CreateDelegate (GetFuncRunner (typeof (TResult)));
  113. }
  114. public TResult FuncRunner<TResult> ()
  115. {
  116. return (TResult) Run (new object [0]);
  117. }
  118. public void ActionRunner ()
  119. {
  120. Run (new object [0]);
  121. }
  122. Delegate GetDelegate<T, TResult> ()
  123. {
  124. if (typeof (TResult) == VoidMarker)
  125. return CreateDelegate (GetActionRunner (typeof (T)));
  126. return CreateDelegate (GetFuncRunner (typeof (T), typeof (TResult)));
  127. }
  128. public TResult FuncRunner<T, TResult> (T arg)
  129. {
  130. return (TResult) Run (new object [] { arg });
  131. }
  132. public void ActionRunner<T> (T arg)
  133. {
  134. Run (new object [] { arg });
  135. }
  136. public Delegate GetDelegate<T1, T2, TResult> ()
  137. {
  138. if (typeof (TResult) == VoidMarker)
  139. return CreateDelegate (GetActionRunner (typeof (T1), typeof (T2)));
  140. return CreateDelegate (GetFuncRunner (typeof (T1), typeof (T2), typeof (TResult)));
  141. }
  142. public TResult FuncRunner<T1, T2, TResult> (T1 arg1, T2 arg2)
  143. {
  144. return (TResult) Run (new object [] { arg1, arg2 });
  145. }
  146. public void ActionRunner<T1, T2> (T1 arg1, T2 arg2)
  147. {
  148. Run (new object [] { arg1, arg2 });
  149. }
  150. Delegate GetDelegate<T1, T2, T3, TResult> ()
  151. {
  152. if (typeof (TResult) == VoidMarker)
  153. return CreateDelegate (GetActionRunner (typeof (T1), typeof (T2), typeof (T3)));
  154. return CreateDelegate (GetFuncRunner (typeof (T1), typeof (T2), typeof (T3), typeof (TResult)));
  155. }
  156. public TResult FuncRunner<T1, T2, T3, TResult> (T1 arg1, T2 arg2, T3 arg3)
  157. {
  158. return (TResult) Run (new object [] { arg1, arg2, arg3 });
  159. }
  160. public void ActionRunner<T1, T2, T3> (T1 arg1, T2 arg2, T3 arg3)
  161. {
  162. Run (new object [] { arg1, arg2, arg3 });
  163. }
  164. Delegate GetDelegate<T1, T2, T3, T4, TResult> ()
  165. {
  166. if (typeof (TResult) == VoidMarker)
  167. return CreateDelegate (GetActionRunner (typeof (T1), typeof (T2), typeof (T3), typeof (T4)));
  168. return CreateDelegate (GetFuncRunner (typeof (T1), typeof (T2), typeof (T3), typeof (T4), typeof (TResult)));
  169. }
  170. public TResult FuncRunner<T1, T2, T3, T4, TResult> (T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  171. {
  172. return (TResult) Run (new object [] { arg1, arg2, arg3, arg4 });
  173. }
  174. public void ActionRunner<T1, T2, T3, T4> (T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  175. {
  176. Run (new object [] { arg1, arg2, arg3, arg4 });
  177. }
  178. }
  179. }