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

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

http://ambienttalk.googlecode.com/
Java | 145 lines | 86 code | 17 blank | 42 comment | 3 complexity | 3b70d7b3ec3737f391028944ff0f16a5 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		return host_.meta_invokeField(host_, name_);
 72	}
 73
 74	public ATObject base_writeField(ATObject newValue) throws InterpreterException {
 75		return host_.impl_invoke(host_, name_.asAssignmentSymbol(), NATTable.of(newValue));
 76	}
 77	
 78	public NATText meta_print() throws InterpreterException {
 79		return NATText.atValue("<field:"+name_.meta_print().javaValue+">");
 80	}
 81	
 82    public boolean isNativeField() {
 83        return true;
 84    }
 85
 86	public ATField asField() throws XTypeMismatch {
 87		return this;
 88	}
 89	
 90	/**
 91	 * Fields can be re-initialized when installed in an object that is being cloned.
 92	 * They expect the new owner of the field as the sole instance to their 'new' method
 93	 */
 94	public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
 95		if (initargs.base_length() != NATNumber.ONE) {
 96			return super.meta_newInstance(initargs);
 97		} else {
 98			ATObject newhost = initargs.base_at(NATNumber.ONE);
 99			return new NATField(name_, (NATCallframe) newhost);
100		}
101	}
102	
103    public ATTable meta_typeTags() throws InterpreterException {
104    	return NATTable.of(NativeTypeTags._FIELD_);
105    }
106    
107    public ATMethod base_accessor() throws InterpreterException {
108    	return accessorForField(this);
109    }
110    
111    public ATMethod base_mutator() throws InterpreterException {
112    	return mutatorForField(this);
113    }
114    
115    public static ATMethod accessorForField(final ATField f) throws InterpreterException {
116    	return new PrimitiveMethod(f.base_name(), NATTable.EMPTY, new PrimitiveMethod.PrimitiveBody() {
117    		public ATObject meta_eval(ATContext ctx) throws InterpreterException {
118    			return f.base_readField();
119    		}
120    		public NATText meta_print() throws InterpreterException {
121    			return NATText.atValue(f.base_readField().toString());
122    		}    		
123    	}) {
124    		public NATText meta_print() throws InterpreterException {
125    			return NATText.atValue("<accessor method for:"+f.base_name()+">");
126    		}   	
127    	};
128    }
129    
130    public static ATMethod mutatorForField(final ATField f) throws InterpreterException {
131    	return new PrimitiveMethod(f.base_name().asAssignmentSymbol(), NATTable.of(AGSymbol.jAlloc("v")), new PrimitiveMethod.PrimitiveBody() {
132    		public ATObject meta_eval(ATContext ctx) throws InterpreterException {
133    			return f.base_writeField(ctx.base_lexicalScope().impl_callField(AGSymbol.jAlloc("v")));
134    		}
135    		public NATText meta_print() throws InterpreterException {
136    			return NATText.atValue(""+f.base_name()+" := v");
137    		}
138    	}) {
139    		public NATText meta_print() throws InterpreterException {
140    			return NATText.atValue("<mutator method for:"+f.base_name()+">");
141    		}   	
142    	};
143    }
144
145}