PageRenderTime 22ms CodeModel.GetById 9ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build060407/src/edu/vub/at/objects/natives/NATField.java

http://ambienttalk.googlecode.com/
Java | 103 lines | 51 code | 13 blank | 39 comment | 3 complexity | 4fe7080d9b0e615da09e2ae8f995facc MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATField.java created on Jul 27, 2006 at 2:27:53 AM
  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.XIllegalOperation;
 32import edu.vub.at.exceptions.XTypeMismatch;
 33import edu.vub.at.objects.ATField;
 34import edu.vub.at.objects.ATNil;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.ATTable;
 37import edu.vub.at.objects.coercion.NativeStripes;
 38import edu.vub.at.objects.grammar.ATSymbol;
 39
 40/**
 41 * NATField implements a causally connected field of an object. Rather than storing
 42 * these fields directly in every object, we choose to only make people pay when they
 43 * choose to reify them. 
 44 * 
 45 * @author smostinc
 46 */
 47public class NATField extends NATByRef implements ATField {
 48
 49	private final ATSymbol name_;
 50	private final NATCallframe frame_;
 51	
 52	public NATField(ATSymbol name, NATCallframe frame) {
 53		name_ = name;
 54		frame_ = frame;
 55	}
 56
 57	public ATSymbol base_getName() {
 58		return name_;
 59	}
 60
 61	public ATObject base_readField() throws InterpreterException {
 62		try {
 63			return frame_.getLocalField(name_);
 64		} catch (InterpreterException e) {
 65			// Since the field was selected from the receiver, it should always be found
 66			throw new XIllegalOperation("Incorrectly initialised field accessed", e);
 67		}
 68	}
 69
 70	public ATNil base_writeField(ATObject newValue) throws InterpreterException {
 71		return frame_.meta_assignField(frame_, name_, newValue);
 72	}
 73	
 74	public NATText meta_print() throws InterpreterException {
 75		return NATText.atValue("<field:"+name_.meta_print().javaValue+">");
 76	}
 77	
 78    public boolean isNativeField() {
 79        return true;
 80    }
 81
 82	public ATField asField() throws XTypeMismatch {
 83		return this;
 84	}
 85	
 86	/**
 87	 * Fields can be re-initialized when installed in an object that is being cloned.
 88	 * They expect the new owner of the field as the sole instance to their 'new' method
 89	 */
 90	public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
 91		if (initargs.base_getLength() != NATNumber.ONE) {
 92			return super.meta_newInstance(initargs);
 93		} else {
 94			ATObject newhost = initargs.base_at(NATNumber.ONE);
 95			return new NATField(name_, (NATCallframe) newhost);
 96		}
 97	}
 98	
 99    public ATTable meta_getStripes() throws InterpreterException {
100    	return NATTable.of(NativeStripes._FIELD_);
101    }
102
103}