PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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