/interpreter/tags/at2dist030708/src/edu/vub/at/objects/natives/NATClosureMethod.java
Java | 141 lines | 54 code | 18 blank | 69 comment | 0 complexity | 868d5162d1e968e7abfc4bc3c5b3e945 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * NATClosureMethod.java created on 16-nov-2006 at 9:12:46 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.exceptions.InterpreterException; 31import edu.vub.at.exceptions.XTypeMismatch; 32import edu.vub.at.objects.ATClosure; 33import edu.vub.at.objects.ATContext; 34import edu.vub.at.objects.ATMethod; 35import edu.vub.at.objects.ATObject; 36import edu.vub.at.objects.ATTable; 37import edu.vub.at.objects.coercion.NativeTypeTags; 38import edu.vub.at.objects.grammar.ATBegin; 39import edu.vub.at.objects.grammar.ATDefExternalMethod; 40import edu.vub.at.objects.grammar.ATSymbol; 41 42/** 43 * A 'closure method' is literally a function that sits in between of a full closure and a method. 44 * It is a method that captures only its lexical scope of definition, but not the value of 'self' 45 * and 'super' which are active at that time. When invoked, the method is evaluated in its lexical 46 * scope, but with the values of 'self' and 'super' equal to those given to it at method invocation time. 47 * 48 * Closure methods are used to implement 'externally added' methods to objects. In such cases, 49 * the external methods can only access their own surrounding lexical scope (and not that of the actual 50 * object to which they are added), but their values for 'self' and 'super' will act as if the method 51 * was actually defined within the object itself. 52 * 53 * See the description of the {@link ATDefExternalMethod} interface for more information. 54 * 55 * @author tvcutsem 56 */ 57public class NATClosureMethod extends NATByRef implements ATMethod { 58 59 private final ATObject lexicalScope_; 60 private final ATMethod method_; 61 62 /** construct a new closure method */ 63 public NATClosureMethod(ATObject scope, ATMethod method) throws InterpreterException { 64 lexicalScope_ = scope; 65 method_ = method; 66 } 67 68 /** 69 * A closure method application acts exactly like a regular direct method application, except that 70 * the given lexical scope is disregarded and replaced by the lexical scope encapsulated by the 71 * closure method. The bindings for 'self' and 'super' remain intact. 72 * 73 * Closure methods are a result of external method definitions. 74 * 75 * @param arguments the evaluated actual arguments 76 * @param ctx the context whose 'self' binding will be used during the execution of the method. 77 * The lexical scope, however, will be replaced by the closure method's own lexical scope. A call frame will 78 * be inserted into this lexical scope before executing the method body. 79 * To ensure that 'super' points to the parent of the actual 'self' and not the parent of the object 80 * performing the external method definition, a 'super' field is added to the call frame shadowing 81 * the defining object's 'super' field. 82 * @return the value of evaluating the function body 83 */ 84 public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException { 85 // should be: method_.base_apply(arguments, ctx.base_withLexicalEnvironment(lexicalScope_), 86 // but this version is more efficient, as we immediately create the right context, rather than 87 // creating a temporary context which will in turn be modified by method_.base_apply 88 89 // hostObject = the object to which the external method was added (actually the object in which 90 // the external method was now found) 91 ATObject hostObject = ctx.base_lexicalScope(); 92 ATObject hostParent = hostObject.base_super(); 93 94 NATCallframe externalFrame = new NATCallframe(lexicalScope_); 95 // super = the parent of the object to which this method was added 96 externalFrame.meta_defineField(NATObject._SUPER_NAME_, hostParent); 97 98 return method_.base_applyInScope(arguments, ctx.base_withLexicalEnvironment(externalFrame)); 99 } 100 101 public ATObject base_applyInScope(ATTable arguments, ATContext ctx) throws InterpreterException { 102 return method_.base_applyInScope(arguments, ctx); 103 } 104 105 public ATBegin base_bodyExpression() throws InterpreterException { 106 return method_.base_bodyExpression(); 107 } 108 109 public ATSymbol base_name() throws InterpreterException { 110 return method_.base_name(); 111 } 112 113 public ATTable base_parameters() throws InterpreterException { 114 return method_.base_parameters(); 115 } 116 117 public ATTable base_annotations() throws InterpreterException { 118 return method_.base_annotations(); 119 } 120 121 /** 122 * When wrapping a closure method, return a closure that is bound to my own lexical scope. 123 * The lexical scope passed to wrap is ignored. 124 */ 125 public ATClosure base_wrap(ATObject lexicalScope, ATObject dynamicReceiver) throws InterpreterException { 126 return method_.base_wrap(lexicalScope_, dynamicReceiver); 127 } 128 129 public NATText meta_print() throws InterpreterException { 130 return NATText.atValue("<closure:"+method_.base_name()+">"); 131 } 132 133 public ATMethod asMethod() throws XTypeMismatch { 134 return this; 135 } 136 137 public ATTable meta_typeTags() throws InterpreterException { 138 return NATTable.of(NativeTypeTags._METHOD_); 139 } 140 141}