PageRenderTime 74ms CodeModel.GetById 55ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/interpreter/tags/at2dist091109/src/edu/vub/at/objects/mirrors/NativeAnonymousMethod.java

http://ambienttalk.googlecode.com/
Java | 109 lines | 48 code | 14 blank | 47 comment | 0 complexity | e4c782c5e4abeb93e8b583673cd5bdf8 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NativeAnonymousMethod.java created on 10-aug-2006 at 10:03:55
  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.mirrors;
 29
 30import edu.vub.at.eval.Evaluator;
 31import edu.vub.at.exceptions.InterpreterException;
 32import edu.vub.at.exceptions.XIllegalApplication;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATMethod;
 36import edu.vub.at.objects.ATObject;
 37import edu.vub.at.objects.ATTable;
 38import edu.vub.at.objects.coercion.NativeTypeTags;
 39import edu.vub.at.objects.grammar.ATBegin;
 40import edu.vub.at.objects.grammar.ATSymbol;
 41import edu.vub.at.objects.natives.NATByRef;
 42import edu.vub.at.objects.natives.NATClosure;
 43import edu.vub.at.objects.natives.NATTable;
 44import edu.vub.at.objects.natives.NATText;
 45import edu.vub.at.objects.natives.grammar.AGBegin;
 46import edu.vub.at.objects.natives.grammar.AGSymbol;
 47
 48/**
 49 * A NativeAnonymousMethod represents the meta_apply method of an anonymous NativeClosure subclass.
 50 * 
 51 * An anonymous native method has the following properties:
 52 *   - name = "nativelambda" (to distinguish it from 'ordinary' lambdas)
 53 *   - arguments = [ \@args ] (it takes an arbitrary number of arguments)
 54 *   - body = "Native implementation in {class}" (a string telling an inspector that
 55 *     this closure is natively implemented in the given Java class)
 56 *   - applying a nativelambda directly (without going through this NativeClosure)
 57 *     results in an error
 58 * 
 59 * @author tvcutsem
 60 */
 61public class NativeAnonymousMethod extends NATByRef implements ATMethod {
 62		
 63	private final Class creatorClass_;
 64	
 65	/**
 66	 * @param creatorClass class of the object that has created the NativeClosure that will wrap <tt>this</tt>
 67	 */
 68	public NativeAnonymousMethod(Class creatorClass) {
 69		creatorClass_ = creatorClass;
 70	}
 71
 72	/**
 73	 * It is an error to directly apply an anonymous method. The closure must be applied instead.
 74	 * @throws XIllegalApplication because an anonymous method must be applied through its wrapping closure.
 75	 */
 76	public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
 77		throw new XIllegalApplication("Cannot apply an anonymous native method. Apply the closure instead.", creatorClass_);
 78	}
 79
 80	public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException {
 81		return base_apply(arguments, ctx);
 82	}
 83
 84	public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException {
 85		return new NATClosure(this, lexicalScope, dynamicReceiver);
 86	}
 87	
 88	public ATSymbol base_name() { return Evaluator._ANON_MTH_NAM_; }
 89
 90	public ATTable base_parameters() { return Evaluator._ANON_MTH_ARGS_; }
 91
 92	public ATBegin base_bodyExpression() {
 93		return new AGBegin(NATTable.atValue(new ATObject[] {
 94				AGSymbol.jAlloc("Native anonymous implementation in " + creatorClass_.getName())}));
 95	}
 96	
 97	public ATTable base_annotations() throws InterpreterException {
 98		return NATTable.EMPTY;
 99	}
100	
101	public NATText meta_print() throws InterpreterException {
102		return NATText.atValue("<anonymous java method in "+creatorClass_.getName()+">");
103	}
104	
105    public ATTable meta_typeTags() throws InterpreterException {
106    	return NATTable.of(NativeTypeTags._METHOD_);
107    }
108
109}