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

http://ambienttalk.googlecode.com/ · Java · 121 lines · 62 code · 13 blank · 46 comment · 2 complexity · cbe14cd3241a49b50cdb9550c1a684f3 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.ATStripe;
  36. import edu.vub.at.objects.ATTable;
  37. import edu.vub.at.objects.grammar.ATSymbol;
  38. import edu.vub.at.objects.mirrors.PrimitiveMethod;
  39. import edu.vub.at.objects.natives.grammar.AGSymbol;
  40. import java.util.LinkedList;
  41. import java.util.Vector;
  42. /**
  43. * Instances of the class NATMessage represent first-class messages.
  44. * A NATMessage is an abstract class, as it can be either a synchronous method invocation or an asynchronous message send.
  45. *
  46. * NATMessages subclass from NATIsolate, such that they represent true AmbientTalk objects.
  47. *
  48. * @author tvc
  49. */
  50. public abstract class NATMessage extends NATObject implements ATMessage {
  51. private final static AGSymbol _SELECTOR_ = AGSymbol.jAlloc("selector");
  52. private final static AGSymbol _ARGUMENTS_ = AGSymbol.jAlloc("arguments");
  53. /** def sendTo(receiver, sender) { nil } */
  54. private static final PrimitiveMethod _PRIM_SND_ = new PrimitiveMethod(
  55. AGSymbol.jAlloc("sendTo"), NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("receiver"), AGSymbol.jAlloc("sender") })) {
  56. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  57. int arity = arguments.base_getLength().asNativeNumber().javaValue;
  58. if (arity != 2) {
  59. throw new XArityMismatch("sendTo", 2, arity);
  60. }
  61. return ctx.base_getLexicalScope().asMessage().prim_sendTo(
  62. ctx.base_getSelf().asMessage(),
  63. arguments.base_at(NATNumber.ONE), arguments.base_at(NATNumber.atValue(2)));
  64. }
  65. };
  66. /**
  67. * @param stripes expects that the Isolate stripe is already present in the stripes array,
  68. * as well as the appropriate Message substripe!
  69. */
  70. protected NATMessage(ATSymbol sel, ATTable arg, ATStripe[] stripes) throws InterpreterException {
  71. super(stripes);
  72. super.meta_defineField(_SELECTOR_, sel);
  73. super.meta_defineField(_ARGUMENTS_, arg);
  74. super.meta_addMethod(_PRIM_SND_);
  75. }
  76. /**
  77. * Copy constructor.
  78. */
  79. protected NATMessage(FieldMap map,
  80. Vector state,
  81. LinkedList originalCustomFields,
  82. MethodDictionary methodDict,
  83. ATObject dynamicParent,
  84. ATObject lexicalParent,
  85. byte flags,
  86. ATStripe[] stripes) throws InterpreterException {
  87. super(map, state, originalCustomFields, methodDict, dynamicParent, lexicalParent, flags, stripes);
  88. }
  89. public ATSymbol base_getSelector() throws InterpreterException {
  90. return super.meta_select(this, _SELECTOR_).asSymbol();
  91. }
  92. public ATTable base_getArguments() throws InterpreterException {
  93. return super.meta_select(this, _ARGUMENTS_).asTable();
  94. }
  95. public ATNil base_setArguments(ATTable arguments) throws InterpreterException {
  96. super.meta_assignField(this, _ARGUMENTS_, arguments);
  97. return NATNil._INSTANCE_;
  98. }
  99. /**
  100. * If sendTo is invoked from the Java-level directly, use 'this' as the dynamic receiver.
  101. */
  102. public ATObject base_sendTo(ATObject receiver, ATObject sender) throws InterpreterException {
  103. return this.prim_sendTo(this, receiver, sender);
  104. }
  105. public ATMessage asMessage() {
  106. return this;
  107. }
  108. }