PageRenderTime 44ms CodeModel.GetById 36ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist030708/src/edu/vub/at/objects/natives/NATField.java

http://ambienttalk.googlecode.com/
Java | 151 lines | 91 code | 17 blank | 43 comment | 3 complexity | 2300cbe450d01363cc09e9b6d091388d 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.eval.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XIllegalOperation;
 33import edu.vub.at.exceptions.XSelectorNotFound;
 34import edu.vub.at.exceptions.XTypeMismatch;
 35import edu.vub.at.objects.ATContext;
 36import edu.vub.at.objects.ATField;
 37import edu.vub.at.objects.ATMethod;
 38import edu.vub.at.objects.ATObject;
 39import edu.vub.at.objects.ATTable;
 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.grammar.AGSymbol;
 44
 45/**
 46 * NATField implements a causally connected field of an object. Rather than storing
 47 * these fields directly in every object, we choose to only make people pay when they
 48 * choose to reify them. 
 49 * 
 50 * @author smostinc, tvcutsem
 51 */
 52public class NATField extends NATByRef implements ATField {
 53
 54	private final ATSymbol name_;
 55	private final ATObject host_;
 56	
 57	/**
 58	 * Constructs a new native field object linked to the field of a base-level
 59	 * AmbientTalk object
 60	 */
 61	public NATField(ATSymbol name, ATObject host) {
 62		name_ = name;
 63		host_ = host;
 64	}
 65
 66	public ATSymbol base_name() {
 67		return name_;
 68	}
 69
 70	public ATObject base_readField() throws InterpreterException {
 71		try {
 72			return host_.meta_invokeField(host_, name_);
 73		} catch (XSelectorNotFound e) {
 74			e.catchOnlyIfSelectorEquals(name_);
 75			// Since the field was selected from the receiver, it should always be found
 76			throw new XIllegalOperation("Incorrectly initialised field accessed", e);
 77		}
 78	}
 79
 80	public ATObject base_writeField(ATObject newValue) throws InterpreterException {
 81		return host_.impl_invoke(host_, name_.asAssignmentSymbol(), NATTable.of(newValue));
 82	}
 83	
 84	public NATText meta_print() throws InterpreterException {
 85		return NATText.atValue("<field:"+name_.meta_print().javaValue+">");
 86	}
 87	
 88    public boolean isNativeField() {
 89        return true;
 90    }
 91
 92	public ATField asField() throws XTypeMismatch {
 93		return this;
 94	}
 95	
 96	/**
 97	 * Fields can be re-initialized when installed in an object that is being cloned.
 98	 * They expect the new owner of the field as the sole instance to their 'new' method
 99	 */
100	public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
101		if (initargs.base_length() != NATNumber.ONE) {
102			return super.meta_newInstance(initargs);
103		} else {
104			ATObject newhost = initargs.base_at(NATNumber.ONE);
105			return new NATField(name_, (NATCallframe) newhost);
106		}
107	}
108	
109    public ATTable meta_typeTags() throws InterpreterException {
110    	return NATTable.of(NativeTypeTags._FIELD_);
111    }
112    
113    public ATMethod base_accessor() throws InterpreterException {
114    	return accessorForField(this);
115    }
116    
117    public ATMethod base_mutator() throws InterpreterException {
118    	return mutatorForField(this);
119    }
120    
121    public static ATMethod accessorForField(final ATField f) throws InterpreterException {
122    	return new PrimitiveMethod(f.base_name(), NATTable.EMPTY, new PrimitiveMethod.PrimitiveBody() {
123    		public ATObject meta_eval(ATContext ctx) throws InterpreterException {
124    			return f.base_readField();
125    		}
126    		public NATText meta_print() throws InterpreterException {
127    			return NATText.atValue(f.base_readField().toString());
128    		}    		
129    	}) {
130    		public NATText meta_print() throws InterpreterException {
131    			return NATText.atValue("<accessor method for:"+f.base_name()+">");
132    		}   	
133    	};
134    }
135    
136    public static ATMethod mutatorForField(final ATField f) throws InterpreterException {
137    	return new PrimitiveMethod(f.base_name().asAssignmentSymbol(), NATTable.of(AGSymbol.jAlloc("v")), new PrimitiveMethod.PrimitiveBody() {
138    		public ATObject meta_eval(ATContext ctx) throws InterpreterException {
139    			return f.base_writeField(ctx.base_lexicalScope().impl_callField(AGSymbol.jAlloc("v")));
140    		}
141    		public NATText meta_print() throws InterpreterException {
142    			return NATText.atValue(""+f.base_name()+" := v");
143    		}
144    	}) {
145    		public NATText meta_print() throws InterpreterException {
146    			return NATText.atValue("<mutator method for:"+f.base_name()+">");
147    		}   	
148    	};
149    }
150
151}