PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/Types/InstanceCreator.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 254 lines | 201 code | 34 blank | 19 comment | 21 complexity | d00a942bcc2fd9836e9bbc9407775d45 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Runtime.CompilerServices;
  17. using System.Threading;
  18. using IronPython.Runtime.Binding;
  19. using IronPython.Runtime.Operations;
  20. using Microsoft.Scripting.Actions;
  21. using Microsoft.Scripting.Runtime;
  22. using Microsoft.Scripting.Utils;
  23. namespace IronPython.Runtime.Types {
  24. /// <summary>
  25. /// Base class for helper which creates instances. We have two derived types: One for user
  26. /// defined types which prepends the type before calling, and one for .NET types which
  27. /// doesn't prepend the type.
  28. /// </summary>
  29. abstract class InstanceCreator {
  30. private readonly PythonType/*!*/ _type;
  31. protected InstanceCreator(PythonType type) {
  32. Assert.NotNull(type);
  33. _type = type;
  34. }
  35. public static InstanceCreator Make(PythonType type) {
  36. if (type.IsSystemType) {
  37. return new SystemInstanceCreator(type);
  38. }
  39. return new UserInstanceCreator(type);
  40. }
  41. protected PythonType Type {
  42. get {
  43. return _type;
  44. }
  45. }
  46. internal abstract object CreateInstance(CodeContext/*!*/ context);
  47. internal abstract object CreateInstance(CodeContext/*!*/ context, object arg0);
  48. internal abstract object CreateInstance(CodeContext/*!*/ context, object arg0, object arg1);
  49. internal abstract object CreateInstance(CodeContext/*!*/ context, object arg0, object arg1, object arg2);
  50. internal abstract object CreateInstance(CodeContext/*!*/ context, params object[] args);
  51. internal abstract object CreateInstance(CodeContext context, object[] args, string[] names);
  52. }
  53. class UserInstanceCreator : InstanceCreator {
  54. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object[], object>> _ctorSite;
  55. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object>> _ctorSite0;
  56. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object, object>> _ctorSite1;
  57. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object, object, object>> _ctorSite2;
  58. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object, object, object, object>> _ctorSite3;
  59. public UserInstanceCreator(PythonType/*!*/ type)
  60. : base(type) {
  61. }
  62. internal override object CreateInstance(CodeContext context) {
  63. if (_ctorSite0 == null) {
  64. Interlocked.CompareExchange(
  65. ref _ctorSite0,
  66. CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object>>.Create(
  67. PythonContext.GetContext(context).InvokeOne
  68. ),
  69. null
  70. );
  71. }
  72. return _ctorSite0.Target(_ctorSite0, context, Type.Ctor, Type);
  73. }
  74. internal override object CreateInstance(CodeContext context, object arg0) {
  75. if (_ctorSite1 == null) {
  76. Interlocked.CompareExchange(
  77. ref _ctorSite1,
  78. CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object, object>>.Create(
  79. PythonContext.GetContext(context).Invoke(
  80. new CallSignature(2)
  81. )
  82. ),
  83. null
  84. );
  85. }
  86. return _ctorSite1.Target(_ctorSite1, context, Type.Ctor, Type, arg0);
  87. }
  88. internal override object CreateInstance(CodeContext context, object arg0, object arg1) {
  89. if (_ctorSite2 == null) {
  90. Interlocked.CompareExchange(
  91. ref _ctorSite2,
  92. CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object, object, object>>.Create(
  93. PythonContext.GetContext(context).Invoke(
  94. new CallSignature(3)
  95. )
  96. ),
  97. null
  98. );
  99. }
  100. return _ctorSite2.Target(_ctorSite2, context, Type.Ctor, Type, arg0, arg1);
  101. }
  102. internal override object CreateInstance(CodeContext context, object arg0, object arg1, object arg2) {
  103. if (_ctorSite3 == null) {
  104. Interlocked.CompareExchange(
  105. ref _ctorSite3,
  106. CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object, object, object, object>>.Create(
  107. PythonContext.GetContext(context).Invoke(
  108. new CallSignature(4)
  109. )
  110. ),
  111. null
  112. );
  113. }
  114. return _ctorSite3.Target(_ctorSite3, context, Type.Ctor, Type, arg0, arg1, arg2);
  115. }
  116. internal override object CreateInstance(CodeContext context, params object[] args) {
  117. if (_ctorSite == null) {
  118. Interlocked.CompareExchange(
  119. ref _ctorSite,
  120. CallSite<Func<CallSite, CodeContext, BuiltinFunction, PythonType, object[], object>>.Create(
  121. PythonContext.GetContext(context).Invoke(
  122. new CallSignature(
  123. new Argument(ArgumentType.Simple),
  124. new Argument(ArgumentType.List)
  125. )
  126. )
  127. ),
  128. null
  129. );
  130. }
  131. return _ctorSite.Target(_ctorSite, context, Type.Ctor, Type, args);
  132. }
  133. internal override object CreateInstance(CodeContext context, object[] args, string[] names) {
  134. return PythonOps.CallWithKeywordArgs(context, Type.Ctor, ArrayUtils.Insert(Type, args), names);
  135. }
  136. }
  137. class SystemInstanceCreator : InstanceCreator {
  138. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, object[], object>> _ctorSite;
  139. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, object>> _ctorSite0;
  140. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object>> _ctorSite1;
  141. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object, object>> _ctorSite2;
  142. private CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object, object, object>> _ctorSite3;
  143. public SystemInstanceCreator(PythonType/*!*/ type)
  144. : base(type) {
  145. }
  146. internal override object CreateInstance(CodeContext context) {
  147. if (_ctorSite0 == null) {
  148. Interlocked.CompareExchange(
  149. ref _ctorSite0,
  150. CallSite<Func<CallSite, CodeContext, BuiltinFunction, object>>.Create(
  151. PythonContext.GetContext(context).InvokeNone
  152. ),
  153. null
  154. );
  155. }
  156. return _ctorSite0.Target(_ctorSite0, context, Type.Ctor);
  157. }
  158. internal override object CreateInstance(CodeContext context, object arg0) {
  159. if (_ctorSite1 == null) {
  160. Interlocked.CompareExchange(
  161. ref _ctorSite1,
  162. CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object>>.Create(
  163. PythonContext.GetContext(context).Invoke(
  164. new CallSignature(1)
  165. )
  166. ),
  167. null
  168. );
  169. }
  170. return _ctorSite1.Target(_ctorSite1, context, Type.Ctor, arg0);
  171. }
  172. internal override object CreateInstance(CodeContext context, object arg0, object arg1) {
  173. if (_ctorSite2 == null) {
  174. Interlocked.CompareExchange(
  175. ref _ctorSite2,
  176. CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object, object>>.Create(
  177. PythonContext.GetContext(context).Invoke(
  178. new CallSignature(2)
  179. )
  180. ),
  181. null
  182. );
  183. }
  184. return _ctorSite2.Target(_ctorSite2, context, Type.Ctor, arg0, arg1);
  185. }
  186. internal override object CreateInstance(CodeContext context, object arg0, object arg1, object arg2) {
  187. if (_ctorSite3 == null) {
  188. Interlocked.CompareExchange(
  189. ref _ctorSite3,
  190. CallSite<Func<CallSite, CodeContext, BuiltinFunction, object, object, object, object>>.Create(
  191. PythonContext.GetContext(context).Invoke(
  192. new CallSignature(3)
  193. )
  194. ),
  195. null
  196. );
  197. }
  198. return _ctorSite3.Target(_ctorSite3, context, Type.Ctor, arg0, arg1, arg2);
  199. }
  200. internal override object CreateInstance(CodeContext context, params object[] args) {
  201. if (_ctorSite == null) {
  202. Interlocked.CompareExchange(
  203. ref _ctorSite,
  204. CallSite<Func<CallSite, CodeContext, BuiltinFunction, object[], object>>.Create(
  205. PythonContext.GetContext(context).Invoke(
  206. new CallSignature(
  207. new Argument(ArgumentType.List)
  208. )
  209. )
  210. ),
  211. null
  212. );
  213. }
  214. return _ctorSite.Target(_ctorSite, context, Type.Ctor, args);
  215. }
  216. internal override object CreateInstance(CodeContext context, object[] args, string[] names) {
  217. return PythonOps.CallWithKeywordArgs(context, Type.Ctor, args, names);
  218. }
  219. }
  220. }