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

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