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