/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/NATMessage.java

http://ambienttalk.googlecode.com/ · Java · 183 lines · 107 code · 20 blank · 56 comment · 9 complexity · 7a528818d0db16600a3e1869b289e018 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * NATMessage.java created on 31-jul-2006 at 12:31:31
  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.exceptions.XArityMismatch;
  32. import edu.vub.at.objects.ATClosure;
  33. import edu.vub.at.objects.ATContext;
  34. import edu.vub.at.objects.ATMessage;
  35. import edu.vub.at.objects.ATNil;
  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.NativeClosure;
  42. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  43. import edu.vub.at.objects.natives.grammar.AGSymbol;
  44. import java.util.LinkedList;
  45. import java.util.Set;
  46. import java.util.Vector;
  47. /**
  48. * Instances of this class represent first-class messages.
  49. * This is an abstract class, as it can be either a synchronous method invocation or an asynchronous message send.
  50. *
  51. * This class is a subclass of {@link NATObject}, such that its instances represent true AmbientTalk objects.
  52. *
  53. * @author tvcutsem
  54. */
  55. public abstract class NATMessage extends NATObject implements ATMessage {
  56. private final static AGSymbol _SELECTOR_ = AGSymbol.jAlloc("selector");
  57. private final static AGSymbol _ARGUMENTS_ = AGSymbol.jAlloc("arguments");
  58. /** def sendTo(receiver, sender) { nil } */
  59. private static final PrimitiveMethod _PRIM_SND_ = new PrimitiveMethod(
  60. AGSymbol.jAlloc("sendTo"), NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("receiver"), AGSymbol.jAlloc("sender") })) {
  61. private static final long serialVersionUID = -3475956316807558583L;
  62. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  63. int arity = arguments.base_length().asNativeNumber().javaValue;
  64. if (arity != 2) {
  65. throw new XArityMismatch("sendTo", 2, arity);
  66. }
  67. return ctx.base_lexicalScope().asMessage().prim_sendTo(
  68. ctx.base_receiver().asMessage(),
  69. arguments.base_at(NATNumber.ONE), arguments.base_at(NATNumber.atValue(2)));
  70. }
  71. };
  72. /** def from(sender) { nil } */
  73. private static final PrimitiveMethod _PRIM_FRM_ = new PrimitiveMethod(
  74. AGSymbol.jAlloc("from"), NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("sender") })) {
  75. private static final long serialVersionUID = -5721508425469755751L;
  76. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  77. int arity = arguments.base_length().asNativeNumber().javaValue;
  78. if (arity != 1) {
  79. throw new XArityMismatch("from", 1, arity);
  80. }
  81. return ctx.base_lexicalScope().asMessage().base_from(arguments.base_at(NATNumber.ONE));
  82. }
  83. };
  84. /**
  85. * Converts the given table of annotations into an ATTypeTag array.
  86. * Each element of the annotations table is converted into a type.
  87. * Moreover, because messages are isolates, the Isolate type is automatically appended to the resulting array.
  88. * Also, each type of message has its own type of type, so a subtype of Message is also added.
  89. */
  90. protected static ATTypeTag[] annotationsToTypes(ATTypeTag msgType, ATTable annotations) throws InterpreterException {
  91. if (annotations == NATTable.EMPTY) {
  92. return new ATTypeTag[] { NativeTypeTags._ISOLATE_, msgType };
  93. }
  94. ATObject[] unwrapped = annotations.asNativeTable().elements_;
  95. ATTypeTag[] fulltypes = new ATTypeTag[unwrapped.length+2];
  96. for (int i = 0; i < unwrapped.length; i++) {
  97. fulltypes[i] = unwrapped[i].asTypeTag();
  98. }
  99. fulltypes[unwrapped.length] = NativeTypeTags._ISOLATE_;
  100. fulltypes[unwrapped.length+1] = msgType;
  101. return fulltypes;
  102. }
  103. /**
  104. * Construct a new message from the given selector, arguments and annotations.
  105. * The annotations become this message's types.
  106. *
  107. * @param annotations a table of objects that should be convertible to types
  108. * @param msgType a subtype of the Message type, added by subclasses to mark which kind of native message is created
  109. */
  110. protected NATMessage(ATSymbol sel, ATTable arg, ATTable annotations, ATTypeTag msgType) throws InterpreterException {
  111. super(annotationsToTypes(msgType, annotations));
  112. super.meta_defineField(_SELECTOR_, sel);
  113. super.meta_defineField(_ARGUMENTS_, arg);
  114. super.meta_addMethod(_PRIM_SND_);
  115. super.meta_addMethod(_PRIM_FRM_);
  116. }
  117. /**
  118. * Copy constructor.
  119. */
  120. protected NATMessage(FieldMap map,
  121. Vector state,
  122. LinkedList originalCustomFields,
  123. MethodDictionary methodDict,
  124. ATObject dynamicParent,
  125. ATObject lexicalParent,
  126. byte flags,
  127. ATTypeTag[] types,
  128. Set freeVars) throws InterpreterException {
  129. super(map, state, originalCustomFields, methodDict, dynamicParent, lexicalParent, flags, types, freeVars);
  130. }
  131. public ATSymbol base_selector() throws InterpreterException {
  132. return super.meta_invokeField(this, _SELECTOR_).asSymbol();
  133. }
  134. public ATTable base_arguments() throws InterpreterException {
  135. return super.meta_invokeField(this, _ARGUMENTS_).asTable();
  136. }
  137. public ATNil base_arguments__opeql_(ATTable arguments) throws InterpreterException {
  138. super.impl_invokeMutator(this, _ARGUMENTS_.asAssignmentSymbol(), NATTable.of(arguments));
  139. return Evaluator.getNil();
  140. }
  141. public ATClosure base_from(final ATObject sender) {
  142. final NATMessage msg = this;
  143. return new NativeClosure(this) {
  144. private static final long serialVersionUID = -5978871207209804505L;
  145. public ATObject base_apply(ATTable args) throws InterpreterException {
  146. ATObject[] arguments = args.asNativeTable().elements_;
  147. if (arguments.length != 1) {
  148. throw new XArityMismatch(msg.toString(), 1, arguments.length);
  149. }
  150. return msg.base_sendTo(arguments[0], sender);
  151. }
  152. };
  153. }
  154. /**
  155. * If sendTo is invoked from the Java-level directly, use 'this' as the dynamic receiver.
  156. */
  157. public ATObject base_sendTo(ATObject receiver, ATObject sender) throws InterpreterException {
  158. return this.prim_sendTo(this, receiver, sender);
  159. }
  160. public ATMessage asMessage() {
  161. return this;
  162. }
  163. }