/interpreter/tags/at2-build270707/src/edu/vub/at/objects/natives/NATDelegation.java

http://ambienttalk.googlecode.com/ · Java · 107 lines · 53 code · 10 blank · 44 comment · 0 complexity · ba86a6f1d16c4ec7cbf97b012908a44a MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATDelegation.java created on 29-jan-2007 at 16:24:59
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * 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
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.objects.natives;
  29. import edu.vub.at.eval.Evaluator;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.objects.ATMessage;
  32. import edu.vub.at.objects.ATMethodInvocation;
  33. import edu.vub.at.objects.ATObject;
  34. import edu.vub.at.objects.ATTypeTag;
  35. import edu.vub.at.objects.ATTable;
  36. import edu.vub.at.objects.coercion.NativeTypeTags;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.natives.grammar.AGSymbol;
  39. import java.util.LinkedList;
  40. import java.util.Vector;
  41. /**
  42. * Instances of the class NATMethodInvocation represent first-class method invocations.
  43. * They encapsulate a selector and arguments and may be turned into an actual invocation by invoking meta_sendTo.
  44. * This method provides the invocation with a receiver to apply itself to.
  45. *
  46. * @author tvcutsem
  47. */
  48. public final class NATDelegation extends NATMessage implements ATMethodInvocation {
  49. private final static AGSymbol _DELEGATOR_ = AGSymbol.jAlloc("delegator");
  50. public NATDelegation(ATObject delegator, ATSymbol sel, ATTable arg, ATTable annotations) throws InterpreterException {
  51. super(sel, arg, annotations, NativeTypeTags._DELEGATION_);
  52. super.meta_defineField(_DELEGATOR_, delegator);
  53. }
  54. /**
  55. * Copy constructor.
  56. */
  57. private NATDelegation(FieldMap map,
  58. Vector state,
  59. LinkedList originalCustomFields,
  60. MethodDictionary methodDict,
  61. ATObject dynamicParent,
  62. ATObject lexicalParent,
  63. byte flags,
  64. ATTypeTag[] types) throws InterpreterException {
  65. super(map, state, originalCustomFields, methodDict, dynamicParent, lexicalParent, flags, types);
  66. }
  67. /**
  68. * To evaluate a delegating message send, invoke the method corresponding to the encapsulated
  69. * selector with the encapsulated arguments. During execution of the method, 'self' should be
  70. * bound to the object that initiated the delegating method invocation.
  71. *
  72. * @return the return value of the invoked method.
  73. */
  74. public ATObject prim_sendTo(ATMessage self, ATObject receiver, ATObject sender) throws InterpreterException {
  75. return receiver.meta_invoke(super.meta_invokeField(self, _DELEGATOR_), self.base_selector(), self.base_arguments());
  76. }
  77. public NATText meta_print() throws InterpreterException {
  78. return NATText.atValue("<delegation:"+base_selector()+Evaluator.printAsList(base_arguments()).javaValue+">");
  79. }
  80. protected NATObject createClone(FieldMap map,
  81. Vector state,
  82. LinkedList originalCustomFields,
  83. MethodDictionary methodDict,
  84. ATObject dynamicParent,
  85. ATObject lexicalParent,
  86. byte flags,
  87. ATTypeTag[] types) throws InterpreterException {
  88. return new NATDelegation(map,
  89. state,
  90. originalCustomFields,
  91. methodDict,
  92. dynamicParent,
  93. lexicalParent,
  94. flags,
  95. types);
  96. }
  97. }