PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 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.ATTypeTag;
 37import edu.vub.at.objects.ATTable;
 38import edu.vub.at.objects.coercion.NativeTypeTags;
 39import edu.vub.at.objects.grammar.ATSymbol;
 40import edu.vub.at.objects.mirrors.PrimitiveMethod;
 41import edu.vub.at.objects.natives.grammar.AGSymbol;
 42
 43import java.util.LinkedList;
 44import java.util.Vector;
 45
 46/**
 47 * Instances of this class represent first-class messages.
 48 * This is an abstract class, as it can be either a synchronous method invocation or an asynchronous message send.
 49 * 
 50 * This class is a subclass of {@link NATObject}, such that its instances represent true AmbientTalk objects.
 51 * 
 52 * @author tvcutsem
 53 */
 54public abstract class NATMessage extends NATObject implements ATMessage {
 55	
 56	private final static AGSymbol _SELECTOR_ = AGSymbol.jAlloc("selector");
 57	private final static AGSymbol _ARGUMENTS_ = AGSymbol.jAlloc("arguments");
 58	
 59	/** def sendTo(receiver, sender) { nil } */
 60	private static final PrimitiveMethod _PRIM_SND_ = new PrimitiveMethod(
 61			AGSymbol.jAlloc("sendTo"), NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("receiver"), AGSymbol.jAlloc("sender") })) {
 62		public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
 63			int arity = arguments.base_getLength().asNativeNumber().javaValue;
 64			if (arity != 2) {
 65				throw new XArityMismatch("sendTo", 2, arity);
 66			}
 67			return ctx.base_getLexicalScope().asMessage().prim_sendTo(
 68					ctx.base_getSelf().asMessage(),
 69					arguments.base_at(NATNumber.ONE), arguments.base_at(NATNumber.atValue(2)));
 70		}
 71	};
 72	
 73	
 74    /**
 75     * Converts the given table of annotations into an ATTypeTag array.
 76     * Each element of the annotations table is converted into a type.
 77     * Moreover, because messages are isolates, the Isolate type is automatically appended to the resulting array.
 78     * Also, each type of message has its own type of type, so a subtype of Message is also added.
 79     */
 80    protected static ATTypeTag[] annotationsToTypes(ATTypeTag msgType, ATTable annotations) throws InterpreterException {
 81		if (annotations == NATTable.EMPTY) {
 82			return new ATTypeTag[] { NativeTypeTags._ISOLATE_, msgType };
 83		}
 84    	
 85        ATObject[] unwrapped = annotations.asNativeTable().elements_;
 86		ATTypeTag[] fulltypes = new ATTypeTag[unwrapped.length+2];
 87		for (int i = 0; i < unwrapped.length; i++) {
 88			fulltypes[i] = unwrapped[i].asTypeTag();
 89		}
 90		fulltypes[unwrapped.length] = NativeTypeTags._ISOLATE_;
 91		fulltypes[unwrapped.length+1] = msgType;
 92        return fulltypes;
 93    }
 94	
 95	/**
 96	 * Construct a new message from the given selector, arguments and annotations.
 97	 * The annotations become this message's types.
 98	 * 
 99	 * @param annotations a table of objects that should be convertible to types
100	 * @param msgType a subtype of the Message type, added by subclasses to mark which kind of native message is created
101	 */
102	protected NATMessage(ATSymbol sel, ATTable arg, ATTable annotations, ATTypeTag msgType) throws InterpreterException {
103		super(annotationsToTypes(msgType, annotations));
104		super.meta_defineField(_SELECTOR_, sel);
105		super.meta_defineField(_ARGUMENTS_, arg);
106		super.meta_addMethod(_PRIM_SND_);
107	}
108
109    /**
110     * Copy constructor.
111     */
112    protected NATMessage(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    	super(map, state, originalCustomFields, methodDict, dynamicParent, lexicalParent, flags, types);
121    }
122	
123	public ATSymbol base_getSelector() throws InterpreterException {
124		return super.meta_select(this, _SELECTOR_).asSymbol();
125	}
126
127	public ATTable base_getArguments() throws InterpreterException {
128		return super.meta_select(this, _ARGUMENTS_).asTable();
129	}
130	
131	public ATNil base_setArguments(ATTable arguments) throws InterpreterException {
132		super.meta_assignField(this, _ARGUMENTS_, arguments);
133		return NATNil._INSTANCE_;
134	}
135	
136	/**
137	 * If sendTo is invoked from the Java-level directly, use 'this' as the dynamic receiver.
138	 */
139	public ATObject base_sendTo(ATObject receiver, ATObject sender) throws InterpreterException {
140		return this.prim_sendTo(this, receiver, sender);
141	}
142
143	public ATMessage asMessage() {
144		return this;
145	}
146	
147}