/interpreter/tags/at2dist041108/src/edu/vub/at/actors/natives/NATAsyncMessage.java

http://ambienttalk.googlecode.com/ · Java · 165 lines · 86 code · 16 blank · 63 comment · 2 complexity · 3405e06de99f0b2a52f0b6a70d22af3d MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATAsyncMessage.java created on 31-jul-2006 at 12:34:20
  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.actors.natives;
  29. import edu.vub.at.actors.ATAsyncMessage;
  30. import edu.vub.at.eval.Evaluator;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.exceptions.XArityMismatch;
  33. import edu.vub.at.exceptions.XTypeMismatch;
  34. import edu.vub.at.objects.ATContext;
  35. import edu.vub.at.objects.ATMessage;
  36. import edu.vub.at.objects.ATObject;
  37. import edu.vub.at.objects.ATTable;
  38. import edu.vub.at.objects.ATTypeTag;
  39. import edu.vub.at.objects.coercion.NativeTypeTags;
  40. import edu.vub.at.objects.grammar.ATSymbol;
  41. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  42. import edu.vub.at.objects.natives.FieldMap;
  43. import edu.vub.at.objects.natives.MethodDictionary;
  44. import edu.vub.at.objects.natives.NATMessage;
  45. import edu.vub.at.objects.natives.NATMethodInvocation;
  46. import edu.vub.at.objects.natives.NATNumber;
  47. import edu.vub.at.objects.natives.NATObject;
  48. import edu.vub.at.objects.natives.NATTable;
  49. import edu.vub.at.objects.natives.NATText;
  50. import edu.vub.at.objects.natives.grammar.AGSymbol;
  51. import java.util.LinkedList;
  52. import java.util.Vector;
  53. /**
  54. * Instances of the class NATAsyncMessage represent first-class asynchronous messages.
  55. *
  56. * @author tvcutsem
  57. */
  58. public class NATAsyncMessage extends NATMessage implements ATAsyncMessage {
  59. // The primitive methods of a native asynchronous message
  60. /** def process(bhv) { nil } */
  61. private static final PrimitiveMethod _PRIM_PRO_ = new PrimitiveMethod(
  62. AGSymbol.jAlloc("process"), NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("bhv")})) {
  63. private static final long serialVersionUID = -1307795172754072220L;
  64. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  65. int arity = arguments.base_length().asNativeNumber().javaValue;
  66. if (arity != 1) {
  67. throw new XArityMismatch("process", 1, arity);
  68. }
  69. return ctx.base_lexicalScope().asAsyncMessage().prim_process(
  70. ctx.base_receiver().asAsyncMessage(),
  71. arguments.base_at(NATNumber.ONE));
  72. }
  73. };
  74. /**
  75. * Create a new asynchronous message.
  76. * @param sel the selector of the asynchronous message
  77. * @param arg the arguments of the asynchronous message
  78. * @param types the types for the message. Isolate and AsyncMessage types are automatically added.
  79. */
  80. public NATAsyncMessage(ATSymbol sel, ATTable arg, ATTable types) throws InterpreterException {
  81. super(sel, arg, types, NativeTypeTags._ASYNCMSG_);
  82. super.meta_addMethod(_PRIM_PRO_);
  83. }
  84. /**
  85. * Copy constructor.
  86. */
  87. private NATAsyncMessage(FieldMap map,
  88. Vector state,
  89. LinkedList originalCustomFields,
  90. MethodDictionary methodDict,
  91. ATObject dynamicParent,
  92. ATObject lexicalParent,
  93. byte flags,
  94. ATTypeTag[] types) throws InterpreterException {
  95. super(map, state, originalCustomFields, methodDict, dynamicParent, lexicalParent, flags, types);
  96. }
  97. /**
  98. * If cloning is not adapted for asynchronous messages, the result of cloning a
  99. * NATAsyncMessage is a NATObject, which is fine except that NATObject does not know
  100. * of prim_sendTo!
  101. */
  102. protected NATObject createClone(FieldMap map,
  103. Vector state,
  104. LinkedList originalCustomFields,
  105. MethodDictionary methodDict,
  106. ATObject dynamicParent,
  107. ATObject lexicalParent,
  108. byte flags,
  109. ATTypeTag[] types) throws InterpreterException {
  110. return new NATAsyncMessage(map,
  111. state,
  112. originalCustomFields,
  113. methodDict,
  114. dynamicParent,
  115. lexicalParent,
  116. flags,
  117. types);
  118. }
  119. /**
  120. * When process is invoked from the Java-level, invoke the primitive implementation
  121. * with self bound to 'this'.
  122. */
  123. public ATObject base_process(ATObject receiver) throws InterpreterException {
  124. return this.prim_process(this, receiver);
  125. }
  126. /**
  127. * The default implementation is to invoke the method corresponding to this message's selector.
  128. */
  129. public ATObject prim_process(ATAsyncMessage self, ATObject receiver) throws InterpreterException {
  130. // receiver is not necessarily equal to base_receiver() anymore
  131. return receiver.meta_invoke(receiver, new NATMethodInvocation(self.base_selector(), self.base_arguments(), self.meta_typeTags()));
  132. }
  133. /**
  134. * To evaluate an asynchronous message send, the asynchronous
  135. * message object is asked to be <i>sent</t> by the sender object.
  136. * I.e.: <tt>(reflect: sender).send(receiver, asyncmsg)</tt>
  137. *
  138. * @return NIL, by default. Overridable by the sender.
  139. */
  140. public ATObject prim_sendTo(ATMessage self, ATObject receiver, ATObject sender) throws InterpreterException {
  141. return sender.meta_send(receiver, self.asAsyncMessage());
  142. }
  143. public NATText meta_print() throws InterpreterException {
  144. return NATText.atValue("<async msg:"+base_selector()+Evaluator.printAsList(base_arguments()).javaValue+">");
  145. }
  146. public ATAsyncMessage asAsyncMessage() throws XTypeMismatch {
  147. return this;
  148. }
  149. }