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

http://ambienttalk.googlecode.com/ · Java · 147 lines · 76 code · 16 blank · 55 comment · 5 complexity · 73baa7be46a008dbfd45bd773b0d4ebb 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. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  58. int arity = arguments.base_getLength().asNativeNumber().javaValue;
  59. if (arity != 2) {
  60. throw new XArityMismatch("sendTo", 2, arity);
  61. }
  62. return ctx.base_getLexicalScope().asMessage().prim_sendTo(
  63. ctx.base_getSelf().asMessage(),
  64. arguments.base_at(NATNumber.ONE), arguments.base_at(NATNumber.atValue(2)));
  65. }
  66. };
  67. /**
  68. * Converts the given table of annotations into an ATTypeTag array.
  69. * Each element of the annotations table is converted into a type.
  70. * Moreover, because messages are isolates, the Isolate type is automatically appended to the resulting array.
  71. * Also, each type of message has its own type of type, so a subtype of Message is also added.
  72. */
  73. protected static ATTypeTag[] annotationsToTypes(ATTypeTag msgType, ATTable annotations) throws InterpreterException {
  74. if (annotations == NATTable.EMPTY) {
  75. return new ATTypeTag[] { NativeTypeTags._ISOLATE_, msgType };
  76. }
  77. ATObject[] unwrapped = annotations.asNativeTable().elements_;
  78. ATTypeTag[] fulltypes = new ATTypeTag[unwrapped.length+2];
  79. for (int i = 0; i < unwrapped.length; i++) {
  80. fulltypes[i] = unwrapped[i].asTypeTag();
  81. }
  82. fulltypes[unwrapped.length] = NativeTypeTags._ISOLATE_;
  83. fulltypes[unwrapped.length+1] = msgType;
  84. return fulltypes;
  85. }
  86. /**
  87. * Construct a new message from the given selector, arguments and annotations.
  88. * The annotations become this message's types.
  89. *
  90. * @param annotations a table of objects that should be convertible to types
  91. * @param msgType a subtype of the Message type, added by subclasses to mark which kind of native message is created
  92. */
  93. protected NATMessage(ATSymbol sel, ATTable arg, ATTable annotations, ATTypeTag msgType) throws InterpreterException {
  94. super(annotationsToTypes(msgType, annotations));
  95. super.meta_defineField(_SELECTOR_, sel);
  96. super.meta_defineField(_ARGUMENTS_, arg);
  97. super.meta_addMethod(_PRIM_SND_);
  98. }
  99. /**
  100. * Copy constructor.
  101. */
  102. protected NATMessage(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. super(map, state, originalCustomFields, methodDict, dynamicParent, lexicalParent, flags, types);
  111. }
  112. public ATSymbol base_getSelector() throws InterpreterException {
  113. return super.meta_select(this, _SELECTOR_).asSymbol();
  114. }
  115. public ATTable base_getArguments() throws InterpreterException {
  116. return super.meta_select(this, _ARGUMENTS_).asTable();
  117. }
  118. public ATNil base_setArguments(ATTable arguments) throws InterpreterException {
  119. super.meta_assignField(this, _ARGUMENTS_, arguments);
  120. return NATNil._INSTANCE_;
  121. }
  122. /**
  123. * If sendTo is invoked from the Java-level directly, use 'this' as the dynamic receiver.
  124. */
  125. public ATObject base_sendTo(ATObject receiver, ATObject sender) throws InterpreterException {
  126. return this.prim_sendTo(this, receiver, sender);
  127. }
  128. public ATMessage asMessage() {
  129. return this;
  130. }
  131. }