PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist090708/src/edu/vub/at/objects/natives/NATNil.java

http://ambienttalk.googlecode.com/
Java | 246 lines | 114 code | 32 blank | 100 comment | 3 complexity | 9c5c79a0604726d4bc7ac2cd9ffe8563 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATNil.java created on 15 jul 2007 at 18:33:28
  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.XArityMismatch;
 33import edu.vub.at.objects.ATBoolean;
 34import edu.vub.at.objects.ATClosure;
 35import edu.vub.at.objects.ATContext;
 36import edu.vub.at.objects.ATNil;
 37import edu.vub.at.objects.ATObject;
 38import edu.vub.at.objects.ATTable;
 39import edu.vub.at.objects.grammar.ATAssignmentSymbol;
 40import edu.vub.at.objects.grammar.ATSymbol;
 41import edu.vub.at.objects.mirrors.NATIntrospectiveMirror;
 42import edu.vub.at.objects.mirrors.PrimitiveMethod;
 43import edu.vub.at.objects.natives.grammar.AGSplice;
 44import edu.vub.at.objects.natives.grammar.AGSymbol;
 45
 46/**
 47 * This class encapsulates the behaviour of the native
 48 * <tt>nil</tt> AmbientTalk object.
 49 * 
 50 * def nil := object: {
 51 *   super := dynamicSentinel;
 52 *   def ==(other); // native implementation
 53 *   def !=(other) { self.==(other).not }
 54 *   def new(@args) { def c := (reflect: self).clone(); c.init(@args); c } 
 55 *   def init(@args) { }
 56 * }
 57 * 
 58 * @author tvcutsem
 59 */
 60public class NATNil extends NATObject implements ATNil {
 61	
 62	/**
 63	 * Nil has a special parent object which ends the recursion
 64	 * along the dynamic delegation chain. These methods cannot be implemented
 65	 * directly in this class because this class still implements useful
 66	 * <tt>base_</tt> Java methods which have to be invoked by means of the
 67	 * implementations defined in {@link NativeATObject}.
 68	 * 
 69	 * This object is shared by all actors, but it is constant so introduces
 70	 * no concurrency issues.
 71	 */
 72	private static final NativeATObject dynamicSentinel_ = new NATByCopy() {
 73
 74      private static final long serialVersionUID = -1307795172754062220L;
 75		// METHODS THAT END THE DYNAMIC DELEGATION CHAIN
 76		
 77		public ATBoolean meta_respondsTo(ATSymbol selector) throws InterpreterException {
 78			// no more delegation
 79			return NATBoolean._FALSE_;
 80		}
 81		
 82		/**
 83		 * When performing <tt>o.m()</tt> and <tt>m</tt> is not found, invoke
 84		 * <tt>doesNotUnderStand</tt> and apply the resulting closure to the given arguments.
 85		 */
 86		public ATObject impl_invokeAccessor(ATObject receiver, ATSymbol selector, ATTable arguments) throws InterpreterException {
 87			return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
 88		}
 89		
 90		public ATObject impl_invokeMutator(ATObject receiver, ATAssignmentSymbol selector, ATTable arguments) throws InterpreterException {
 91			return receiver.meta_doesNotUnderstand(selector).base_apply(arguments);
 92		}
 93
 94		/**
 95		 * When performing <tt>o.x</tt> and <tt>x</tt> is not found, invoke
 96		 * <tt>doesNotUnderStand</tt> and apply the corresponding closure with zero arguments.
 97		 */
 98		public ATObject meta_invokeField(ATObject receiver, ATSymbol selector) throws InterpreterException {
 99			return receiver.meta_doesNotUnderstand(selector).base_apply(NATTable.EMPTY);
100		}
101	    
102	    public ATClosure impl_selectAccessor(ATObject receiver, final ATSymbol selector) throws InterpreterException {
103	    	return receiver.meta_doesNotUnderstand(selector);
104	    }
105
106		public ATClosure impl_selectMutator(ATObject receiver, final ATAssignmentSymbol selector) throws InterpreterException {
107			return receiver.meta_doesNotUnderstand(selector);
108		}
109
110		public NATText meta_print() throws InterpreterException {
111			return NATText.atValue("dynamicsentinel");
112		}
113	};
114	
115	// The names of nil's primitive methods
116	public static final AGSymbol _EQL_NAME_ = AGSymbol.jAlloc("==");
117	public static final AGSymbol _NEW_NAME_ = AGSymbol.jAlloc("new");
118	public static final AGSymbol _INI_NAME_ = AGSymbol.jAlloc("init");
119	public static final ATSymbol _NEQ_NAME_ = AGSymbol.jAlloc("!=");
120
121	// The primitive methods themselves
122	
123	/** def ==(comparand) { comparand.impl_identityEquals(self) } */
124	private static final PrimitiveMethod _PRIM_EQL_ = new PrimitiveMethod(
125			_EQL_NAME_, NATTable.atValue(new ATObject[] { AGSymbol.jAlloc("comparand")})) {      
126		public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
127			if (!arguments.base_length().equals(NATNumber.ONE)) {
128				throw new XArityMismatch("==", 1, arguments.base_length().asNativeNumber().javaValue);
129			}
130			
131			ATObject comparand = arguments.base_at(NATNumber.ONE);
132			
133			// make other object perform the actual pointer equality
134			// if comparand is a proxy, it can delegate this request to its principal
135			return comparand.impl_identityEquals(ctx.base_receiver());
136		}
137	};
138	
139	/** def new(@initargs) { (reflect: self).newInstance(initargs) } */
140	private static final PrimitiveMethod _PRIM_NEW_ = new PrimitiveMethod(
141			_NEW_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
142		public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
143			return ctx.base_receiver().meta_newInstance(arguments);
144			// return ctx.base_receiver().base_new(arguments.asNativeTable().elements_);
145		}
146	};
147	
148	/** def init(@initargs) { self } */
149	private static final PrimitiveMethod _PRIM_INI_ = new PrimitiveMethod(
150			_INI_NAME_, NATTable.atValue(new ATObject[] { new AGSplice(AGSymbol.jAlloc("initargs")) })) {
151		public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
152			return ctx.base_receiver();
153		}
154	};
155	
156	/** def !=(other) { self.==(other).not } */
157	private static final PrimitiveMethod _PRIM_NEQ_ = new PrimitiveMethod(        
158			_NEQ_NAME_, NATTable.of(AGSymbol.jAlloc("other"))) {
159      public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
160			int arity = arguments.base_length().asNativeNumber().javaValue;
161			if (arity != 1) {
162				throw new XArityMismatch("!=", 1, arity);
163			}
164			ATObject other = arguments.base_at(NATNumber.ONE);
165			// return ctx.receiver == other
166			return ctx.base_receiver().meta_invoke(
167					ctx.base_receiver(), new NATMethodInvocation(_EQL_NAME_, NATTable.of(other), NATTable.EMPTY)).asBoolean().base_not();
168		}
169	};
170	
171	/**
172	 * Construct and initialize a new instance of <tt>nil</tt>. Only one instance
173	 * of <tt>nil</tt> should be created per actor.
174	 */
175	public NATNil() {
176		// super(dynamicParent, lexicalParent, parentType)
177
178		// super := dynamicSentinel
179		// the lexical root of nil is set to dynamicSentinel_ because,
180		// if we would make it refer to Evaluator.getGlobalLexicalRoot(),
181		// we get an infinite recursion: nil requires root, root requires nil, ...
182		super(dynamicSentinel_, dynamicSentinel_, NATObject._SHARES_A_);
183
184		// add ==, new, init and != to the method dictionary directly		
185		// cannot use meta_addMethod because this method returns nil,
186		// thus constructing nil requires already having a constructed nil...
187		// super.meta_addMethod(_PRIM_NEQ_);		
188		methodDictionary_.put(_EQL_NAME_, _PRIM_EQL_);
189		methodDictionary_.put(_NEW_NAME_, _PRIM_NEW_);
190		methodDictionary_.put(_INI_NAME_, _PRIM_INI_);
191		methodDictionary_.put(_NEQ_NAME_, _PRIM_NEQ_);
192	}
193	
194    /**
195     * The identity operator. In AmbientTalk, equality of objects
196     * is by default pointer-equality (i.e. objects are equal only
197     * if they are identical).
198     * 
199     * @return by default, true if the parameter object and this object are identical,
200     * false otherwise.
201     */
202    public ATBoolean base__opeql__opeql_(ATObject other) throws InterpreterException {
203    	return this.meta_invoke(this, new NATMethodInvocation(_EQL_NAME_, NATTable.of(other), NATTable.EMPTY)).asBoolean();
204    }
205    
206	public ATObject base_init(ATObject[] initargs) throws InterpreterException {
207    	return this.meta_invoke(this, new NATMethodInvocation(_INI_NAME_, NATTable.atValue(initargs), NATTable.EMPTY));
208	}
209
210	public ATObject base_new(ATObject[] initargs) throws InterpreterException {
211    	return this.meta_invoke(this, new NATMethodInvocation(_NEW_NAME_, NATTable.atValue(initargs), NATTable.EMPTY));
212	}
213	
214	public ATBoolean base__opnot__opeql_(ATObject other) throws InterpreterException {
215		// immediately hard-wired implementation, because we know 'self' is really
216		// bound to the Java 'this'.
217		// return this.meta_invoke(this, _NEQ_NAME_, NATTable.of(other));
218		return this.base__opeql__opeql_(other).base_not();
219	}
220
221	public ATObject base_super() {
222		return dynamicSentinel_;
223	}
224	
225	public ATObject meta_pass() throws InterpreterException {
226	    return this;
227	}
228	
229    /**
230     * After deserialization, ensure that nil becomes rebound to the nil
231     * object of the new actor.
232     */
233    public ATObject meta_resolve() throws InterpreterException {
234    	return Evaluator.getNil();
235    }
236    
237    /** nil is a singleton */
238    public ATObject meta_clone() throws InterpreterException {
239    	return this;
240    }
241
242	public NATText meta_print() throws InterpreterException {
243		return NATText.atValue("nil");
244	}
245	
246}