PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Boo.Lang/Runtime/SliceDispatcherFactory.cs

https://github.com/boo/boo-lang
C# | 151 lines | 107 code | 16 blank | 28 comment | 20 complexity | 8992aa205f36f9db2257fd3e482cdf04 MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2003, 2004, 2005 Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. namespace Boo.Lang.Runtime
  32. {
  33. class SliceDispatcherFactory : AbstractDispatcherFactory
  34. {
  35. public SliceDispatcherFactory(ExtensionRegistry extensions, object target, Type type, string name, params object[] arguments) : base(extensions, target, type, name == "" ? RuntimeServices.GetDefaultMemberName(type) : name, arguments)
  36. {
  37. }
  38. public Dispatcher CreateGetter()
  39. {
  40. MemberInfo[] candidates = ResolveMember();
  41. if (candidates.Length == 1) return CreateGetter(candidates[0]);
  42. return EmitMethodDispatcher(Getters(candidates));
  43. }
  44. private IEnumerable<MethodInfo> Getters(MemberInfo[] candidates)
  45. {
  46. foreach (MemberInfo info in candidates)
  47. {
  48. PropertyInfo p = info as PropertyInfo;
  49. if (null == p) continue;
  50. MethodInfo getter = p.GetGetMethod(true);
  51. if (null == getter) continue;
  52. yield return getter;
  53. }
  54. }
  55. private Dispatcher CreateGetter(MemberInfo member)
  56. {
  57. switch (member.MemberType)
  58. {
  59. case MemberTypes.Field:
  60. {
  61. FieldInfo field = (FieldInfo)member;
  62. return
  63. delegate(object o, object[] arguments) { return RuntimeServices.GetSlice(field.GetValue(o), "", arguments); };
  64. }
  65. case MemberTypes.Property:
  66. {
  67. MethodInfo getter = ((PropertyInfo) member).GetGetMethod(true);
  68. if (null == getter) throw MissingField();
  69. if (getter.GetParameters().Length > 0) return EmitMethodDispatcher(getter);
  70. // TODO: remove the reflection invocation getter.Invoke from the path
  71. // otherwise its a simple property and the slice
  72. // should be applied to the return value
  73. return
  74. delegate(object o, object[] arguments) { return RuntimeServices.GetSlice(getter.Invoke(o, null), "", arguments); };
  75. }
  76. default:
  77. {
  78. throw MissingField();
  79. }
  80. }
  81. }
  82. private Dispatcher EmitMethodDispatcher(MethodInfo candidate)
  83. {
  84. return EmitMethodDispatcher(new MethodInfo[] { candidate });
  85. }
  86. private Dispatcher EmitMethodDispatcher(IEnumerable<MethodInfo> candidates)
  87. {
  88. CandidateMethod method = ResolveMethod(GetArgumentTypes(), candidates);
  89. if (null == method) throw MissingField();
  90. return new MethodDispatcherEmitter(_type, method, GetArgumentTypes()).Emit();
  91. }
  92. private MemberInfo[] ResolveMember()
  93. {
  94. MemberInfo[] candidates = _type.GetMember(_name, MemberTypes.Property | MemberTypes.Field, RuntimeServices.DefaultBindingFlags);
  95. if (candidates.Length == 0) throw MissingField();
  96. return candidates;
  97. }
  98. public Dispatcher CreateSetter()
  99. {
  100. MemberInfo[] candidates = ResolveMember();
  101. if (candidates.Length > 1) throw new AmbiguousMatchException(Builtins.join(candidates, ", "));
  102. return CreateSetter(candidates[0]);
  103. }
  104. private Dispatcher CreateSetter(MemberInfo member)
  105. {
  106. switch (member.MemberType)
  107. {
  108. case MemberTypes.Field:
  109. {
  110. FieldInfo field = (FieldInfo)member;
  111. return
  112. delegate(object o, object[] arguments) { return RuntimeServices.SetSlice(field.GetValue(o), "", arguments); };
  113. }
  114. case MemberTypes.Property:
  115. {
  116. PropertyInfo property = (PropertyInfo)member;
  117. if (property.GetIndexParameters().Length > 0)
  118. {
  119. MethodInfo setter = property.GetSetMethod(true);
  120. if (null == setter) throw MissingField();
  121. return EmitMethodDispatcher(setter);
  122. }
  123. return delegate(object o, object[] arguments)
  124. {
  125. return RuntimeServices.SetSlice(RuntimeServices.GetProperty(o, _name), "", arguments);
  126. };
  127. }
  128. default:
  129. {
  130. throw MissingField();
  131. }
  132. }
  133. }
  134. }
  135. }