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

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