/interpreter/tags/at2dist110511/src/edu/vub/at/objects/natives/NATDelegation.java
Java | 129 lines | 72 code | 12 blank | 45 comment | 2 complexity | fcb111120631cad12f49d5a78abe080d MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * NATDelegation.java created on 29-jan-2007 at 16:24:59 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.objects.ATMessage; 33import edu.vub.at.objects.ATMethodInvocation; 34import edu.vub.at.objects.ATObject; 35import edu.vub.at.objects.ATTypeTag; 36import edu.vub.at.objects.ATTable; 37import edu.vub.at.objects.coercion.NativeTypeTags; 38import edu.vub.at.objects.grammar.ATSymbol; 39import edu.vub.at.objects.natives.grammar.AGSymbol; 40import edu.vub.util.TempFieldGenerator; 41 42import java.util.LinkedList; 43import java.util.Set; 44import java.util.Vector; 45 46/** 47 * Instances of the class NATMethodInvocation represent first-class method invocations. 48 * They encapsulate a selector and arguments and may be turned into an actual invocation by invoking meta_sendTo. 49 * This method provides the invocation with a receiver to apply itself to. 50 * 51 * @author tvcutsem 52 */ 53public final class NATDelegation extends NATMessage implements ATMethodInvocation { 54 55 private final static AGSymbol _DELEGATOR_ = AGSymbol.jAlloc("delegator"); 56 57 public NATDelegation(ATObject delegator, ATSymbol sel, ATTable arg, ATTable annotations) throws InterpreterException { 58 super(sel, arg, annotations, NativeTypeTags._DELEGATION_); 59 super.meta_defineField(_DELEGATOR_, delegator); 60 } 61 62 /** 63 * Copy constructor. 64 */ 65 private NATDelegation(FieldMap map, 66 Vector state, 67 LinkedList originalCustomFields, 68 MethodDictionary methodDict, 69 ATObject dynamicParent, 70 ATObject lexicalParent, 71 byte flags, 72 ATTypeTag[] types, 73 Set freeVars) throws InterpreterException { 74 super(map, state, originalCustomFields, methodDict, dynamicParent, lexicalParent, flags, types, freeVars); 75 } 76 77 public ATMethodInvocation asMethodInvocation() { return this; } 78 79 /** 80 * To evaluate a delegating message send, invoke the method corresponding to the encapsulated 81 * selector with the encapsulated arguments. During execution of the method, 'self' should be 82 * bound to the object that initiated the delegating method invocation. 83 * 84 * @return the return value of the invoked method. 85 */ 86 public ATObject prim_sendTo(ATMessage self, ATObject receiver, ATObject sender) throws InterpreterException { 87 return receiver.meta_invoke(super.meta_invokeField(self, _DELEGATOR_), self.asMethodInvocation()); 88 } 89 90 public NATText meta_print() throws InterpreterException { 91 return NATText.atValue("<delegation:"+base_selector()+Evaluator.printAsList(base_arguments()).javaValue+">"); 92 } 93 94 public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException { 95 if (objectMap.contains(this)) { 96 return objectMap.getName(this); 97 } 98 //StringBuffer codeString = new StringBuffer("^" + base_selector().meta_print().javaValue + Evaluator.printAsList(base_arguments()).javaValue); 99 StringBuffer codeString = new StringBuffer("^" + base_selector().meta_print().javaValue + Evaluator.codeAsList(objectMap, base_arguments()).javaValue); 100 NATTable annotations = NATTable.atValue(this.typeTags_); 101 if(annotations.base_length().asNativeNumber().javaValue > 0) { 102 codeString.append("@"+annotations.impl_asCode(objectMap).javaValue); 103 } 104 NATText code = NATText.atValue(codeString.toString()); 105 NATText name = objectMap.put(this, code); 106 return name; 107 } 108 109 protected NATObject createClone(FieldMap map, 110 Vector state, 111 LinkedList originalCustomFields, 112 MethodDictionary methodDict, 113 ATObject dynamicParent, 114 ATObject lexicalParent, 115 byte flags, 116 ATTypeTag[] types, 117 Set freeVars) throws InterpreterException { 118 return new NATDelegation(map, 119 state, 120 originalCustomFields, 121 methodDict, 122 dynamicParent, 123 lexicalParent, 124 flags, 125 types, 126 freeVars); 127 } 128 129}