PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/actors/natives/NATAsyncMessage.java

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